From 72ca58c540ee03de50631ea08a419f2589a0fe8e Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sat, 22 Jul 2017 17:42:15 +0200 Subject: [PATCH] Save follower count and note count in user. --- lib/pleroma/user.ex | 51 ++++++++++++++----- .../web/ostatus/handlers/note_handler.ex | 4 +- lib/pleroma/web/twitter_api/twitter_api.ex | 4 +- test/support/factory.ex | 2 +- test/user_test.exs | 32 ++++++++++++ test/web/ostatus/ostatus_test.exs | 2 + test/web/twitter_api/twitter_api_test.exs | 14 +++-- test/web/twitter_api/views/user_view_test.exs | 2 + 8 files changed, 92 insertions(+), 19 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index d5befa67b..f28f0deb0 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -54,18 +54,10 @@ def info_changeset(struct, params \\ %{}) do end 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), - note_count: Repo.one(note_count_query), - follower_count: Repo.one(follower_count_query) + note_count: user.info["note_count"] || 0, + follower_count: user.info["follower_count"] || 0 } end @@ -127,9 +119,13 @@ def follow(%User{} = follower, %User{} = followed) do following = [ap_followers | follower.following] |> Enum.uniq - follower + follower = follower |> follow_changeset(%{following: following}) |> Repo.update + + {:ok, followed} = update_follower_count(followed) + + follower end end @@ -142,7 +138,10 @@ def unfollow(%User{} = follower, %User{} = followed) do { :ok, follower } = follower |> follow_changeset(%{following: following}) |> 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 {:error, "Not subscribed!"} end @@ -203,4 +202,32 @@ def get_friends(%User{id: id, following: following}) do {:ok, Repo.all(q)} 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 diff --git a/lib/pleroma/web/ostatus/handlers/note_handler.ex b/lib/pleroma/web/ostatus/handlers/note_handler.ex index f675901b2..e55f972b2 100644 --- a/lib/pleroma/web/ostatus/handlers/note_handler.ex +++ b/lib/pleroma/web/ostatus/handlers/note_handler.ex @@ -92,7 +92,9 @@ def handle_note(entry, doc \\ nil) do # TODO: Handle this case in make_note_data note <- (if inReplyTo && !inReplyToActivity, do: note |> Map.put("inReplyTo", inReplyTo), else: note) 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 %Activity{} = activity -> {:ok, activity} e -> {:error, e} diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 04c1d914c..dc66e27ad 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -39,7 +39,9 @@ def create_status(%User{} = user, %{"status" => status} = data) do context <- make_context(inReplyTo), tags <- Formatter.parse_tags(status), 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 diff --git a/test/support/factory.ex b/test/support/factory.ex index 1356ebde9..eca73725d 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -7,7 +7,7 @@ def user_factory do email: sequence(:email, &"user#{&1}@example.com"), nickname: sequence(:nickname, &"nick#{&1}"), 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) } end diff --git a/test/user_test.exs b/test/user_test.exs index 81827afa3..097d7d98e 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -37,6 +37,9 @@ test "follow takes a user and another user" do 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)] end @@ -224,8 +227,37 @@ test "gets all friends (followed users) for a given user" do {: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] 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 diff --git a/test/web/ostatus/ostatus_test.exs b/test/web/ostatus/ostatus_test.exs index 34bfaa005..959b744af 100644 --- a/test/web/ostatus/ostatus_test.exs +++ b/test/web/ostatus/ostatus_test.exs @@ -15,6 +15,8 @@ test "handle incoming note - GS, Salmon" do incoming = File.read!("test/fixtures/incoming_note_activity.xml") {: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["object"]["type"] == "Note" assert activity.data["object"]["id"] == "tag:gs.example.org:4040,2017-04-23:noticeId=29:objectType=note" diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index 48d48eb2b..bbb261eff 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -9,8 +9,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do import Pleroma.Factory test "create a status" do - # user = UserBuilder.build(%{ap_id: "142344"}) - user = insert(:user, %{ap_id: "142344"}) + user = insert(:user) _mentioned_user = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"}) object_data = %{ @@ -53,10 +52,14 @@ test "create a status" do assert is_list(activity.data["object"]["attachment"]) 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 test "create a status that is a reply" do - user = UserBuilder.build(%{ap_id: "some_cool_id"}) + user = insert(:user) input = %{ "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", "inReplyTo"]) == get_in(activity.data, ["object", "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 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}) 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}) assert msg == "Could not follow user: #{followed.nickname} is already on your list." end diff --git a/test/web/twitter_api/views/user_view_test.exs b/test/web/twitter_api/views/user_view_test.exs index de2cd3d30..b81d3d64d 100644 --- a/test/web/twitter_api/views/user_view_test.exs +++ b/test/web/twitter_api/views/user_view_test.exs @@ -22,6 +22,7 @@ test "A user with an avatar object", %{user: user} do test "A user" do note_activity = insert(:note_activity) user = User.get_cached_by_ap_id(note_activity.data["actor"]) + {:ok, user} = User.update_note_count(user) 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 {:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]}) + {:ok, user} = User.update_follower_count(user) image = "https://placehold.it/48x48" represented = %{ "id" => user.id,