From 9c889334220487c4e370333a13908639b984bc19 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sat, 26 May 2018 16:03:32 +0000 Subject: [PATCH] implement tracking of follow requests --- lib/pleroma/user.ex | 30 +++++++++++++++++++ lib/pleroma/web/activity_pub/utils.ex | 3 +- .../mastodon_api/mastodon_api_controller.ex | 6 ++++ lib/pleroma/web/router.ex | 4 ++- 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index c7b7b4f0a..35f3371ba 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -353,6 +353,36 @@ def get_friends(user) do {:ok, Repo.all(q)} end + def get_follow_requests_query(%User{} = user) do + from( + a in Activity, + where: fragment( + "? ->> 'type' = 'Follow'", + a.data + ), + where: fragment( + "? ->> 'state' = 'pending'", + a.data + ), + where: fragment( + "? @> ?", + a.data, + ^%{"object" => user.ap_id} + ) + ) + end + + def get_follow_requests(%User{} = user) do + q = get_follow_requests_query(user) + reqs = Repo.all(q) + + users = + Enum.map(reqs, fn (req) -> req.actor end) + |> Enum.map(fn (ap_id) -> get_by_ap_id(ap_id) end) + + {:ok, users} + end + def increase_note_count(%User{} = user) do note_count = (user.info["note_count"] || 0) + 1 new_info = Map.put(user.info, "note_count", note_count) diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index 56b80a8db..3229949c0 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -219,7 +219,7 @@ def remove_like_from_object(%Activity{data: %{"actor" => actor}}, object) do @doc """ Makes a follow activity data for the given follower and followed """ - def make_follow_data(%User{ap_id: follower_id}, %User{ap_id: followed_id}, activity_id) do + def make_follow_data(%User{ap_id: follower_id}, %User{ap_id: followed_id} = followed, activity_id) do data = %{ "type" => "Follow", "actor" => follower_id, @@ -229,6 +229,7 @@ def make_follow_data(%User{ap_id: follower_id}, %User{ap_id: followed_id}, activ } if activity_id, do: Map.put(data, "id", activity_id), else: data + if User.locked?(followed), do: Map.put(data, "state", "pending"), else: data end def fetch_latest_follow(%User{ap_id: follower_id}, %User{ap_id: followed_id}) do diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 0f7d4bb6d..e92c6277b 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -476,6 +476,12 @@ def following(conn, %{"id" => id}) do end end + def follow_requests(%{assigns: %{user: followed}} = conn, _params) do + with {:ok, follow_requests} <- User.get_follow_requests(followed) do + render(conn, AccountView, "accounts.json", %{users: follow_requests, as: :user}) + end + end + def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do with %User{} = followed <- Repo.get(User, id), {:ok, follower} <- User.maybe_direct_follow(follower, followed), diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 57b10bff1..e517510b8 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -97,11 +97,13 @@ def user_fetcher(username) do post("/accounts/:id/mute", MastodonAPIController, :relationship_noop) post("/accounts/:id/unmute", MastodonAPIController, :relationship_noop) + get("/follow_requests", MastodonAPIController, :follow_requests) + post("/follows", MastodonAPIController, :follow) get("/blocks", MastodonAPIController, :blocks) - get("/follow_requests", MastodonAPIController, :empty_array) + get("/domain_blocks", MastodonAPIController, :empty_array) get("/mutes", MastodonAPIController, :empty_array) get("/timelines/home", MastodonAPIController, :home_timeline)