Do not return tuple when unneeded

This commit is contained in:
Egor Kislitsyn 2019-09-24 14:16:52 +07:00
parent 29dd8ab9c0
commit 79b25be4e1
5 changed files with 53 additions and 63 deletions

View file

@ -228,9 +228,9 @@ def run(["unsubscribe", nickname]) do
shell_info("Deactivating #{user.nickname}") shell_info("Deactivating #{user.nickname}")
User.deactivate(user) User.deactivate(user)
{:ok, friends} = User.get_friends(user) user
|> User.get_friends()
Enum.each(friends, fn friend -> |> Enum.each(fn friend ->
user = User.get_cached_by_id(user.id) user = User.get_cached_by_id(user.id)
shell_info("Unsubscribing #{friend.nickname} from #{user.nickname}") shell_info("Unsubscribing #{friend.nickname} from #{user.nickname}")
@ -405,7 +405,7 @@ def run(["delete_activities", nickname]) do
start_pleroma() start_pleroma()
with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
{:ok, _} = User.delete_user_activities(user) User.delete_user_activities(user)
shell_info("User #{nickname} statuses deleted.") shell_info("User #{nickname} statuses deleted.")
else else
_ -> _ ->

View file

@ -685,9 +685,9 @@ def get_followers_query(user), do: get_followers_query(user, nil)
@spec get_followers(User.t(), pos_integer()) :: {:ok, list(User.t())} @spec get_followers(User.t(), pos_integer()) :: {:ok, list(User.t())}
def get_followers(user, page \\ nil) do def get_followers(user, page \\ nil) do
q = get_followers_query(user, page) user
|> get_followers_query(page)
{:ok, Repo.all(q)} |> Repo.all()
end end
@spec get_external_followers(User.t(), pos_integer()) :: {:ok, list(User.t())} @spec get_external_followers(User.t(), pos_integer()) :: {:ok, list(User.t())}
@ -720,9 +720,9 @@ def get_friends_query(user, page) do
def get_friends_query(user), do: get_friends_query(user, nil) def get_friends_query(user), do: get_friends_query(user, nil)
def get_friends(user, page \\ nil) do def get_friends(user, page \\ nil) do
q = get_friends_query(user, page) user
|> get_friends_query(page)
{:ok, Repo.all(q)} |> Repo.all()
end end
def get_friends_ids(user, page \\ nil) do def get_friends_ids(user, page \\ nil) do
@ -733,15 +733,13 @@ def get_friends_ids(user, page \\ nil) do
@spec get_follow_requests(User.t()) :: {:ok, [User.t()]} @spec get_follow_requests(User.t()) :: {:ok, [User.t()]}
def get_follow_requests(%User{} = user) do def get_follow_requests(%User{} = user) do
users = user
Activity.follow_requests_for_actor(user) |> Activity.follow_requests_for_actor()
|> join(:inner, [a], u in User, on: a.actor == u.ap_id) |> join(:inner, [a], u in User, on: a.actor == u.ap_id)
|> where([a, u], not fragment("? @> ?", u.following, ^[user.follower_address])) |> where([a, u], not fragment("? @> ?", u.following, ^[user.follower_address]))
|> group_by([a, u], u.id) |> group_by([a, u], u.id)
|> select([a, u], u) |> select([a, u], u)
|> Repo.all() |> Repo.all()
{:ok, users}
end end
def increase_note_count(%User{} = user) do def increase_note_count(%User{} = user) do
@ -1104,15 +1102,13 @@ def deactivate_async(user, status \\ true) do
def deactivate(%User{} = user, status \\ true) do def deactivate(%User{} = user, status \\ true) do
info_cng = User.Info.set_activation_status(user.info, status) info_cng = User.Info.set_activation_status(user.info, status)
with {:ok, friends} <- User.get_friends(user), with {:ok, user} <-
{:ok, followers} <- User.get_followers(user),
{:ok, user} <-
user user
|> change() |> change()
|> put_embed(:info, info_cng) |> put_embed(:info, info_cng)
|> update_and_set_cache() do |> update_and_set_cache() do
Enum.each(followers, &invalidate_cache(&1)) Enum.each(get_followers(user), &invalidate_cache/1)
Enum.each(friends, &update_follower_count(&1)) Enum.each(get_friends(user), &update_follower_count/1)
{:ok, user} {:ok, user}
end end
@ -1137,18 +1133,18 @@ def perform(:delete, %User{} = user) do
{:ok, _user} = ActivityPub.delete(user) {:ok, _user} = ActivityPub.delete(user)
# Remove all relationships # Remove all relationships
{:ok, followers} = User.get_followers(user) user
|> get_followers()
Enum.each(followers, fn follower -> |> Enum.each(fn follower ->
ActivityPub.unfollow(follower, user) ActivityPub.unfollow(follower, user)
User.unfollow(follower, user) unfollow(follower, user)
end) end)
{:ok, friends} = User.get_friends(user) user
|> get_friends()
Enum.each(friends, fn followed -> |> Enum.each(fn followed ->
ActivityPub.unfollow(user, followed) ActivityPub.unfollow(user, followed)
User.unfollow(user, followed) unfollow(user, followed)
end) end)
delete_user_activities(user) delete_user_activities(user)
@ -1160,13 +1156,11 @@ def perform(:delete, %User{} = user) do
def perform(:fetch_initial_posts, %User{} = user) do def perform(:fetch_initial_posts, %User{} = user) do
pages = Pleroma.Config.get!([:fetch_initial_posts, :pages]) pages = Pleroma.Config.get!([:fetch_initial_posts, :pages])
Enum.each(
# Insert all the posts in reverse order, so they're in the right order on the timeline # Insert all the posts in reverse order, so they're in the right order on the timeline
Enum.reverse(Utils.fetch_ordered_collection(user.info.source_data["outbox"], pages)), user.info.source_data["outbox"]
&Pleroma.Web.Federator.incoming_ap_doc/1 |> Utils.fetch_ordered_collection(pages)
) |> Enum.reverse()
|> Enum.each(&Pleroma.Web.Federator.incoming_ap_doc/1)
{:ok, user}
end end
def perform(:deactivate_async, user, status), do: deactivate(user, status) def perform(:deactivate_async, user, status), do: deactivate(user, status)
@ -1252,16 +1246,12 @@ def follow_import(%User{} = follower, followed_identifiers)
}) })
end end
def delete_user_activities(%User{ap_id: ap_id} = user) do def delete_user_activities(%User{ap_id: ap_id}) do
ap_id ap_id
|> Activity.Queries.by_actor() |> Activity.Queries.by_actor()
|> RepoStreamer.chunk_stream(50) |> RepoStreamer.chunk_stream(50)
|> Stream.each(fn activities -> |> Stream.each(fn activities -> Enum.each(activities, &delete_activity/1) end)
Enum.each(activities, &delete_activity(&1))
end)
|> Stream.run() |> Stream.run()
{:ok, user}
end end
defp delete_activity(%{data: %{"type" => "Create"}} = activity) do defp delete_activity(%{data: %{"type" => "Create"}} = activity) do

View file

@ -111,11 +111,11 @@ defp should_federate?(inbox, public) do
@spec recipients(User.t(), Activity.t()) :: list(User.t()) | [] @spec recipients(User.t(), Activity.t()) :: list(User.t()) | []
defp recipients(actor, activity) do defp recipients(actor, activity) do
{:ok, followers} = followers =
if actor.follower_address in activity.recipients do if actor.follower_address in activity.recipients do
User.get_external_followers(actor) User.get_external_followers(actor)
else else
{:ok, []} []
end end
fetchers = fetchers =

View file

@ -958,12 +958,12 @@ def following(%{assigns: %{user: for_user}} = conn, %{"id" => id} = params) do
end end
def follow_requests(%{assigns: %{user: followed}} = conn, _params) do def follow_requests(%{assigns: %{user: followed}} = conn, _params) do
with {:ok, follow_requests} <- User.get_follow_requests(followed) do follow_requests = User.get_follow_requests(followed)
conn conn
|> put_view(AccountView) |> put_view(AccountView)
|> render("accounts.json", %{for: followed, users: follow_requests, as: :user}) |> render("accounts.json", %{for: followed, users: follow_requests, as: :user})
end end
end
def authorize_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do def authorize_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
with %User{} = follower <- User.get_cached_by_id(id), with %User{} = follower <- User.get_cached_by_id(id),

View file

@ -74,8 +74,8 @@ test "returns all pending follow requests" do
CommonAPI.follow(follower, unlocked) CommonAPI.follow(follower, unlocked)
CommonAPI.follow(follower, locked) CommonAPI.follow(follower, locked)
assert {:ok, []} = User.get_follow_requests(unlocked) assert [] = User.get_follow_requests(unlocked)
assert {:ok, [activity]} = User.get_follow_requests(locked) assert [activity] = User.get_follow_requests(locked)
assert activity assert activity
end end
@ -90,7 +90,7 @@ test "doesn't return already accepted or duplicate follow requests" do
CommonAPI.follow(accepted_follower, locked) CommonAPI.follow(accepted_follower, locked)
User.follow(accepted_follower, locked) User.follow(accepted_follower, locked)
assert {:ok, [activity]} = User.get_follow_requests(locked) assert [activity] = User.get_follow_requests(locked)
assert activity assert activity
end end
@ -99,10 +99,10 @@ test "clears follow requests when requester is blocked" do
follower = insert(:user) follower = insert(:user)
CommonAPI.follow(follower, followed) CommonAPI.follow(follower, followed)
assert {:ok, [_activity]} = User.get_follow_requests(followed) assert [_activity] = User.get_follow_requests(followed)
{:ok, _follower} = User.block(followed, follower) {:ok, _follower} = User.block(followed, follower)
assert {:ok, []} = User.get_follow_requests(followed) assert [] = User.get_follow_requests(followed)
end end
test "follow_all follows mutliple users" do test "follow_all follows mutliple users" do
@ -560,7 +560,7 @@ test "it sets the follower_adress" do
test "it enforces the fqn format for nicknames" do test "it enforces the fqn format for nicknames" do
cs = User.remote_user_creation(%{@valid_remote | nickname: "bla"}) cs = User.remote_user_creation(%{@valid_remote | nickname: "bla"})
assert cs.changes.local == false assert Ecto.Changeset.get_field(cs, :local) == false
assert cs.changes.avatar assert cs.changes.avatar
refute cs.valid? refute cs.valid?
end end
@ -584,7 +584,7 @@ test "gets all followers for a given user" do
{:ok, follower_one} = User.follow(follower_one, user) {:ok, follower_one} = User.follow(follower_one, user)
{:ok, follower_two} = User.follow(follower_two, user) {:ok, follower_two} = User.follow(follower_two, user)
{:ok, res} = User.get_followers(user) res = User.get_followers(user)
assert Enum.member?(res, follower_one) assert Enum.member?(res, follower_one)
assert Enum.member?(res, follower_two) assert Enum.member?(res, follower_two)
@ -600,7 +600,7 @@ test "gets all friends (followed users) for a given user" do
{:ok, user} = User.follow(user, followed_one) {:ok, user} = User.follow(user, followed_one)
{:ok, user} = User.follow(user, followed_two) {:ok, user} = User.follow(user, followed_two)
{:ok, res} = User.get_friends(user) res = User.get_friends(user)
followed_one = User.get_cached_by_ap_id(followed_one.ap_id) followed_one = User.get_cached_by_ap_id(followed_one.ap_id)
followed_two = User.get_cached_by_ap_id(followed_two.ap_id) followed_two = User.get_cached_by_ap_id(followed_two.ap_id)
@ -975,7 +975,7 @@ test "hide a user from followers " do
info = User.get_cached_user_info(user2) info = User.get_cached_user_info(user2)
assert info.follower_count == 0 assert info.follower_count == 0
assert {:ok, []} = User.get_followers(user2) assert [] = User.get_followers(user2)
end end
test "hide a user from friends" do test "hide a user from friends" do
@ -991,7 +991,7 @@ test "hide a user from friends" do
assert info.following_count == 0 assert info.following_count == 0
assert User.following_count(user2) == 0 assert User.following_count(user2) == 0
assert {:ok, []} = User.get_friends(user2) assert [] = User.get_friends(user2)
end end
test "hide a user's statuses from timelines and notifications" do test "hide a user's statuses from timelines and notifications" do
@ -1034,7 +1034,7 @@ test "hide a user's statuses from timelines and notifications" do
test ".delete_user_activities deletes all create activities", %{user: user} do test ".delete_user_activities deletes all create activities", %{user: user} do
{:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"}) {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
{:ok, _} = User.delete_user_activities(user) User.delete_user_activities(user)
# TODO: Remove favorites, repeats, delete activities. # TODO: Remove favorites, repeats, delete activities.
refute Activity.get_by_id(activity.id) refute Activity.get_by_id(activity.id)