From 1a1f4520cd711d46a53ffa0ec657f8a7e46896e7 Mon Sep 17 00:00:00 2001 From: eugenijm Date: Sat, 2 Mar 2019 22:18:51 +0300 Subject: [PATCH] Use sql query in User.get_follow_requests/1 for filtering logic --- lib/pleroma/user.ex | 15 +++++++-------- test/user_test.exs | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index d58274508..06b430ccd 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -648,15 +648,14 @@ defmodule Pleroma.User do 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.uniq() - |> Enum.map(fn ap_id -> get_by_ap_id(ap_id) end) - |> Enum.filter(fn u -> !is_nil(u) end) - |> Enum.filter(fn u -> !following?(u, user) end) + user + |> User.get_follow_requests_query() + |> join(:inner, [a], u in User, a.actor == u.ap_id) + |> where([a, u], not fragment("? @> ?", u.following, ^[user.follower_address])) + |> group_by([a, u], u.id) + |> select([a, u], u) + |> Repo.all() {:ok, users} end diff --git a/test/user_test.exs b/test/user_test.exs index cbe4693fc..b8d41ecfd 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -64,6 +64,20 @@ defmodule Pleroma.UserTest do assert activity end + test "doesn't return already accepted or duplicate follow requests" do + locked = insert(:user, %{info: %{locked: true}}) + pending_follower = insert(:user) + accepted_follower = insert(:user) + + Pleroma.Web.TwitterAPI.TwitterAPI.follow(pending_follower, %{"user_id" => locked.id}) + Pleroma.Web.TwitterAPI.TwitterAPI.follow(pending_follower, %{"user_id" => locked.id}) + Pleroma.Web.TwitterAPI.TwitterAPI.follow(accepted_follower, %{"user_id" => locked.id}) + User.maybe_follow(accepted_follower, locked) + + assert {:ok, [activity]} = User.get_follow_requests(locked) + assert activity + end + test "follow_all follows mutliple users" do user = insert(:user) followed_zero = insert(:user)