v2 Suggestions: also filter out users you follow

This commit is contained in:
Alex Gleason 2021-11-26 21:42:28 -06:00
parent 437c1a5a52
commit e5a7547fbe
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 43 additions and 10 deletions

View file

@ -5,6 +5,7 @@
defmodule Pleroma.Web.MastodonAPI.SuggestionController do defmodule Pleroma.Web.MastodonAPI.SuggestionController do
use Pleroma.Web, :controller use Pleroma.Web, :controller
import Ecto.Query import Ecto.Query
alias Pleroma.FollowingRelationship
alias Pleroma.User alias Pleroma.User
alias Pleroma.UserRelationship alias Pleroma.UserRelationship
@ -66,20 +67,15 @@ defmodule Pleroma.Web.MastodonAPI.SuggestionController do
do: Pleroma.Web.MastodonAPI.MastodonAPIController.empty_array(conn, params) do: Pleroma.Web.MastodonAPI.MastodonAPIController.empty_array(conn, params)
@doc "GET /api/v2/suggestions" @doc "GET /api/v2/suggestions"
def index2(%{assigns: %{user: %{id: user_id} = user}} = conn, params) do def index2(%{assigns: %{user: user}} = conn, params) do
limit = Map.get(params, :limit, 40) |> min(80) limit = Map.get(params, :limit, 40) |> min(80)
users = users =
%{is_suggested: true, invisible: false, limit: limit} %{is_suggested: true, invisible: false, limit: limit}
|> User.Query.build() |> User.Query.build()
|> where([u], u.id != ^user_id) |> exclude_user(user)
|> join(:left, [u], r in UserRelationship, |> exclude_relationships(user, [:block, :mute, :suggestion_dismiss])
as: :relationships, |> exclude_following(user)
on:
r.target_id == u.id and r.source_id == ^user_id and
r.relationship_type in [:block, :mute, :suggestion_dismiss]
)
|> where([relationships: r], is_nil(r.target_id))
|> Pleroma.Repo.all() |> Pleroma.Repo.all()
render(conn, "index.json", %{ render(conn, "index.json", %{
@ -90,6 +86,30 @@ defmodule Pleroma.Web.MastodonAPI.SuggestionController do
}) })
end end
defp exclude_user(query, %User{id: user_id}) do
where(query, [u], u.id != ^user_id)
end
defp exclude_relationships(query, %User{id: user_id}, relationship_types) do
query
|> join(:left, [u], r in UserRelationship,
as: :user_relationships,
on:
r.target_id == u.id and r.source_id == ^user_id and
r.relationship_type in ^relationship_types
)
|> where([user_relationships: r], is_nil(r.target_id))
end
defp exclude_following(query, %User{id: user_id}) do
query
|> join(:left, [u], r in FollowingRelationship,
as: :following_relationships,
on: r.following_id == u.id and r.follower_id == ^user_id and r.state == :follow_accept
)
|> where([following_relationships: r], is_nil(r.following_id))
end
@doc "DELETE /api/v1/suggestions/:account_id" @doc "DELETE /api/v1/suggestions/:account_id"
def dismiss(%{assigns: %{user: source}} = conn, %{account_id: user_id}) do def dismiss(%{assigns: %{user: source}} = conn, %{account_id: user_id}) do
with %User{} = target <- User.get_cached_by_id(user_id), with %User{} = target <- User.get_cached_by_id(user_id),

View file

@ -5,6 +5,7 @@
defmodule Pleroma.Web.MastodonAPI.SuggestionControllerTest do defmodule Pleroma.Web.MastodonAPI.SuggestionControllerTest do
use Pleroma.Web.ConnCase, async: true use Pleroma.Web.ConnCase, async: true
alias Pleroma.UserRelationship alias Pleroma.UserRelationship
alias Pleroma.Web.CommonAPI
import Pleroma.Factory import Pleroma.Factory
setup do: oauth_access(["read", "write"]) setup do: oauth_access(["read", "write"])
@ -46,7 +47,19 @@ defmodule Pleroma.Web.MastodonAPI.SuggestionControllerTest do
test "returns v2 suggestions excluding blocked accounts", %{conn: conn, user: blocker} do test "returns v2 suggestions excluding blocked accounts", %{conn: conn, user: blocker} do
blocked = insert(:user, is_suggested: true) blocked = insert(:user, is_suggested: true)
{:ok, _} = Pleroma.Web.CommonAPI.block(blocker, blocked) {:ok, _} = CommonAPI.block(blocker, blocked)
res =
conn
|> get("/api/v2/suggestions")
|> json_response_and_validate_schema(200)
assert [] = res
end
test "returns v2 suggestions excluding followed accounts", %{conn: conn, user: follower} do
followed = insert(:user, is_suggested: true)
{:ok, _, _, _} = CommonAPI.follow(follower, followed)
res = res =
conn conn