forked from AkkomaGang/akkoma
Save follower count and note count in user.
This commit is contained in:
parent
a027b0027c
commit
72ca58c540
8 changed files with 92 additions and 19 deletions
|
@ -54,18 +54,10 @@ def info_changeset(struct, params \\ %{}) do
|
||||||
end
|
end
|
||||||
|
|
||||||
def user_info(%User{} = user) do
|
def user_info(%User{} = user) do
|
||||||
note_count_query = from a in Object,
|
|
||||||
where: fragment("? @> ?", a.data, ^%{actor: user.ap_id, type: "Note"}),
|
|
||||||
select: count(a.id)
|
|
||||||
|
|
||||||
follower_count_query = from u in User,
|
|
||||||
where: fragment("? @> ?", u.following, ^user.follower_address),
|
|
||||||
select: count(u.id)
|
|
||||||
|
|
||||||
%{
|
%{
|
||||||
following_count: length(user.following),
|
following_count: length(user.following),
|
||||||
note_count: Repo.one(note_count_query),
|
note_count: user.info["note_count"] || 0,
|
||||||
follower_count: Repo.one(follower_count_query)
|
follower_count: user.info["follower_count"] || 0
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -127,9 +119,13 @@ def follow(%User{} = follower, %User{} = followed) do
|
||||||
following = [ap_followers | follower.following]
|
following = [ap_followers | follower.following]
|
||||||
|> Enum.uniq
|
|> Enum.uniq
|
||||||
|
|
||||||
follower
|
follower = follower
|
||||||
|> follow_changeset(%{following: following})
|
|> follow_changeset(%{following: following})
|
||||||
|> Repo.update
|
|> Repo.update
|
||||||
|
|
||||||
|
{:ok, followed} = update_follower_count(followed)
|
||||||
|
|
||||||
|
follower
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -142,7 +138,10 @@ def unfollow(%User{} = follower, %User{} = followed) do
|
||||||
{ :ok, follower } = follower
|
{ :ok, follower } = follower
|
||||||
|> follow_changeset(%{following: following})
|
|> follow_changeset(%{following: following})
|
||||||
|> Repo.update
|
|> Repo.update
|
||||||
{ :ok, follower, Utils.fetch_latest_follow(follower, followed)}
|
|
||||||
|
{:ok, followed} = update_follower_count(followed)
|
||||||
|
|
||||||
|
{:ok, follower, Utils.fetch_latest_follow(follower, followed)}
|
||||||
else
|
else
|
||||||
{:error, "Not subscribed!"}
|
{:error, "Not subscribed!"}
|
||||||
end
|
end
|
||||||
|
@ -203,4 +202,32 @@ def get_friends(%User{id: id, following: following}) do
|
||||||
|
|
||||||
{:ok, Repo.all(q)}
|
{:ok, Repo.all(q)}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def update_note_count(%User{} = user) do
|
||||||
|
note_count_query = from a in Object,
|
||||||
|
where: fragment("? @> ?", a.data, ^%{actor: user.ap_id, type: "Note"}),
|
||||||
|
select: count(a.id)
|
||||||
|
|
||||||
|
note_count = Repo.one(note_count_query)
|
||||||
|
|
||||||
|
new_info = Map.put(user.info, "note_count", note_count)
|
||||||
|
|
||||||
|
cs = info_changeset(user, %{info: new_info})
|
||||||
|
|
||||||
|
Repo.update(cs)
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_follower_count(%User{} = user) do
|
||||||
|
follower_count_query = from u in User,
|
||||||
|
where: fragment("? @> ?", u.following, ^user.follower_address),
|
||||||
|
select: count(u.id)
|
||||||
|
|
||||||
|
follower_count = Repo.one(follower_count_query)
|
||||||
|
|
||||||
|
new_info = Map.put(user.info, "follower_count", follower_count)
|
||||||
|
|
||||||
|
cs = info_changeset(user, %{info: new_info})
|
||||||
|
|
||||||
|
Repo.update(cs)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -92,7 +92,9 @@ def handle_note(entry, doc \\ nil) do
|
||||||
# TODO: Handle this case in make_note_data
|
# TODO: Handle this case in make_note_data
|
||||||
note <- (if inReplyTo && !inReplyToActivity, do: note |> Map.put("inReplyTo", inReplyTo), else: note)
|
note <- (if inReplyTo && !inReplyToActivity, do: note |> Map.put("inReplyTo", inReplyTo), else: note)
|
||||||
do
|
do
|
||||||
ActivityPub.create(to, actor, context, note, %{}, date, false)
|
res = ActivityPub.create(to, actor, context, note, %{}, date, false)
|
||||||
|
User.update_note_count(actor)
|
||||||
|
res
|
||||||
else
|
else
|
||||||
%Activity{} = activity -> {:ok, activity}
|
%Activity{} = activity -> {:ok, activity}
|
||||||
e -> {:error, e}
|
e -> {:error, e}
|
||||||
|
|
|
@ -39,7 +39,9 @@ def create_status(%User{} = user, %{"status" => status} = data) do
|
||||||
context <- make_context(inReplyTo),
|
context <- make_context(inReplyTo),
|
||||||
tags <- Formatter.parse_tags(status),
|
tags <- Formatter.parse_tags(status),
|
||||||
object <- make_note_data(user.ap_id, to, context, content_html, attachments, inReplyTo, tags) do
|
object <- make_note_data(user.ap_id, to, context, content_html, attachments, inReplyTo, tags) do
|
||||||
ActivityPub.create(to, user, context, object)
|
res = ActivityPub.create(to, user, context, object)
|
||||||
|
User.update_note_count(user)
|
||||||
|
res
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ def user_factory do
|
||||||
email: sequence(:email, &"user#{&1}@example.com"),
|
email: sequence(:email, &"user#{&1}@example.com"),
|
||||||
nickname: sequence(:nickname, &"nick#{&1}"),
|
nickname: sequence(:nickname, &"nick#{&1}"),
|
||||||
password_hash: Comeonin.Pbkdf2.hashpwsalt("test"),
|
password_hash: Comeonin.Pbkdf2.hashpwsalt("test"),
|
||||||
bio: sequence(:bio, &"Tester Number #{&1}"),
|
bio: sequence(:bio, &"Tester Number #{&1}")
|
||||||
}
|
}
|
||||||
%{ user | ap_id: Pleroma.User.ap_id(user), follower_address: Pleroma.User.ap_followers(user) }
|
%{ user | ap_id: Pleroma.User.ap_id(user), follower_address: Pleroma.User.ap_followers(user) }
|
||||||
end
|
end
|
||||||
|
|
|
@ -37,6 +37,9 @@ test "follow takes a user and another user" do
|
||||||
|
|
||||||
user = Repo.get(User, user.id)
|
user = Repo.get(User, user.id)
|
||||||
|
|
||||||
|
followed = User.get_by_ap_id(followed.ap_id)
|
||||||
|
assert followed.info["follower_count"] == 1
|
||||||
|
|
||||||
assert user.following == [User.ap_followers(followed)]
|
assert user.following == [User.ap_followers(followed)]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -224,8 +227,37 @@ test "gets all friends (followed users) for a given user" do
|
||||||
|
|
||||||
{:ok, res} = User.get_friends(user)
|
{:ok, res} = User.get_friends(user)
|
||||||
|
|
||||||
|
followed_one = User.get_by_ap_id(followed_one.ap_id)
|
||||||
|
followed_two = User.get_by_ap_id(followed_two.ap_id)
|
||||||
assert res == [followed_one, followed_two]
|
assert res == [followed_one, followed_two]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "updating note and follower count" do
|
||||||
|
test "it sets the info->note_count property" do
|
||||||
|
note = insert(:note)
|
||||||
|
|
||||||
|
user = User.get_by_ap_id(note.data["actor"])
|
||||||
|
|
||||||
|
assert user.info["note_count"] == nil
|
||||||
|
|
||||||
|
{:ok, user} = User.update_note_count(user)
|
||||||
|
|
||||||
|
assert user.info["note_count"] == 1
|
||||||
|
end
|
||||||
|
|
||||||
|
test "it sets the info->follower_count property" do
|
||||||
|
user = insert(:user)
|
||||||
|
follower = insert(:user)
|
||||||
|
|
||||||
|
User.follow(follower, user)
|
||||||
|
|
||||||
|
assert user.info["follower_count"] == nil
|
||||||
|
|
||||||
|
{:ok, user} = User.update_follower_count(user)
|
||||||
|
|
||||||
|
assert user.info["follower_count"] == 1
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,8 @@ test "handle incoming note - GS, Salmon" do
|
||||||
incoming = File.read!("test/fixtures/incoming_note_activity.xml")
|
incoming = File.read!("test/fixtures/incoming_note_activity.xml")
|
||||||
{:ok, [activity]} = OStatus.handle_incoming(incoming)
|
{:ok, [activity]} = OStatus.handle_incoming(incoming)
|
||||||
|
|
||||||
|
user = User.get_by_ap_id(activity.data["actor"])
|
||||||
|
assert user.info["note_count"] == 1
|
||||||
assert activity.data["type"] == "Create"
|
assert activity.data["type"] == "Create"
|
||||||
assert activity.data["object"]["type"] == "Note"
|
assert activity.data["object"]["type"] == "Note"
|
||||||
assert activity.data["object"]["id"] == "tag:gs.example.org:4040,2017-04-23:noticeId=29:objectType=note"
|
assert activity.data["object"]["id"] == "tag:gs.example.org:4040,2017-04-23:noticeId=29:objectType=note"
|
||||||
|
|
|
@ -9,8 +9,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
|
||||||
import Pleroma.Factory
|
import Pleroma.Factory
|
||||||
|
|
||||||
test "create a status" do
|
test "create a status" do
|
||||||
# user = UserBuilder.build(%{ap_id: "142344"})
|
user = insert(:user)
|
||||||
user = insert(:user, %{ap_id: "142344"})
|
|
||||||
_mentioned_user = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"})
|
_mentioned_user = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"})
|
||||||
|
|
||||||
object_data = %{
|
object_data = %{
|
||||||
|
@ -53,10 +52,14 @@ test "create a status" do
|
||||||
assert is_list(activity.data["object"]["attachment"])
|
assert is_list(activity.data["object"]["attachment"])
|
||||||
|
|
||||||
assert activity.data["object"] == Object.get_by_ap_id(activity.data["object"]["id"]).data
|
assert activity.data["object"] == Object.get_by_ap_id(activity.data["object"]["id"]).data
|
||||||
|
|
||||||
|
user = User.get_by_ap_id(user.ap_id)
|
||||||
|
|
||||||
|
assert user.info["note_count"] == 1
|
||||||
end
|
end
|
||||||
|
|
||||||
test "create a status that is a reply" do
|
test "create a status that is a reply" do
|
||||||
user = UserBuilder.build(%{ap_id: "some_cool_id"})
|
user = insert(:user)
|
||||||
input = %{
|
input = %{
|
||||||
"status" => "Hello again."
|
"status" => "Hello again."
|
||||||
}
|
}
|
||||||
|
@ -74,7 +77,7 @@ test "create a status that is a reply" do
|
||||||
assert get_in(reply.data, ["object", "context"]) == get_in(activity.data, ["object", "context"])
|
assert get_in(reply.data, ["object", "context"]) == get_in(activity.data, ["object", "context"])
|
||||||
assert get_in(reply.data, ["object", "inReplyTo"]) == get_in(activity.data, ["object", "id"])
|
assert get_in(reply.data, ["object", "inReplyTo"]) == get_in(activity.data, ["object", "id"])
|
||||||
assert get_in(reply.data, ["object", "inReplyToStatusId"]) == activity.id
|
assert get_in(reply.data, ["object", "inReplyToStatusId"]) == activity.id
|
||||||
assert Enum.member?(get_in(reply.data, ["to"]), "some_cool_id")
|
assert Enum.member?(get_in(reply.data, ["to"]), user.ap_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "fetch public statuses, excluding remote ones." do
|
test "fetch public statuses, excluding remote ones." do
|
||||||
|
@ -188,6 +191,9 @@ test "Follow another user using screen_name" do
|
||||||
{:ok, user, followed, _activity } = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
|
{:ok, user, followed, _activity } = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
|
||||||
assert user.following == [User.ap_followers(followed)]
|
assert user.following == [User.ap_followers(followed)]
|
||||||
|
|
||||||
|
followed = User.get_by_ap_id(followed.ap_id)
|
||||||
|
assert followed.info["follower_count"] == 1
|
||||||
|
|
||||||
{ :error, msg } = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
|
{ :error, msg } = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
|
||||||
assert msg == "Could not follow user: #{followed.nickname} is already on your list."
|
assert msg == "Could not follow user: #{followed.nickname} is already on your list."
|
||||||
end
|
end
|
||||||
|
|
|
@ -22,6 +22,7 @@ test "A user with an avatar object", %{user: user} do
|
||||||
test "A user" do
|
test "A user" do
|
||||||
note_activity = insert(:note_activity)
|
note_activity = insert(:note_activity)
|
||||||
user = User.get_cached_by_ap_id(note_activity.data["actor"])
|
user = User.get_cached_by_ap_id(note_activity.data["actor"])
|
||||||
|
{:ok, user} = User.update_note_count(user)
|
||||||
follower = insert(:user)
|
follower = insert(:user)
|
||||||
second_follower = insert(:user)
|
second_follower = insert(:user)
|
||||||
|
|
||||||
|
@ -57,6 +58,7 @@ test "A user" do
|
||||||
|
|
||||||
test "A user for a given other follower", %{user: user} do
|
test "A user for a given other follower", %{user: user} do
|
||||||
{:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]})
|
{:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]})
|
||||||
|
{:ok, user} = User.update_follower_count(user)
|
||||||
image = "https://placehold.it/48x48"
|
image = "https://placehold.it/48x48"
|
||||||
represented = %{
|
represented = %{
|
||||||
"id" => user.id,
|
"id" => user.id,
|
||||||
|
|
Loading…
Reference in a new issue