Implement mastodon's reblog hiding feature

This commit is contained in:
Karen Konou 2019-03-09 14:08:41 +01:00
parent 41fc67aa49
commit c8f31e0bc2
6 changed files with 56 additions and 2 deletions
lib/pleroma

View file

@ -1385,4 +1385,8 @@ defp paginate(query, page, page_size) do
offset: ^((page - 1) * page_size) offset: ^((page - 1) * page_size)
) )
end end
def showing_reblogs?(%User{} = user, %User{} = target) do
target.id not in user.info.muted_reblogs
end
end end

View file

@ -21,6 +21,7 @@ defmodule Pleroma.User.Info do
field(:blocks, {:array, :string}, default: []) field(:blocks, {:array, :string}, default: [])
field(:domain_blocks, {:array, :string}, default: []) field(:domain_blocks, {:array, :string}, default: [])
field(:mutes, {:array, :string}, default: []) field(:mutes, {:array, :string}, default: [])
field(:muted_reblogs, {:array, :string}, default: [])
field(:deactivated, :boolean, default: false) field(:deactivated, :boolean, default: false)
field(:no_rich_text, :boolean, default: false) field(:no_rich_text, :boolean, default: false)
field(:ap_enabled, :boolean, default: false) field(:ap_enabled, :boolean, default: false)
@ -259,4 +260,16 @@ def roles(%Info{is_moderator: is_moderator, is_admin: is_admin}) do
moderator: is_moderator moderator: is_moderator
} }
end end
def add_reblog_mute(info, id) do
params = %{muted_reblogs: info.muted_reblogs ++ [id]}
cast(info, params, [:muted_reblogs])
end
def remove_reblog_mute(info, id) do
params = %{muted_reblogs: List.delete(info.muted_reblogs, id)}
cast(info, params, [:muted_reblogs])
end
end end

View file

@ -951,9 +951,17 @@ def contain_broken_threads(%Activity{} = activity, %User{} = user) do
entire_thread_visible_for_user?(activity, user) entire_thread_visible_for_user?(activity, user)
end end
# filter out muted threads
def contain_muted_boosts(%Activity{data: %{"type" => "Announce"}} = activity, %User{} = user) do
id = User.get_by_ap_id(activity.actor).id
id not in user.info.muted_reblogs
end
def contain_muted_boosts(%Activity{} = _activity, %User{} = _user), do: true
# do post-processing on a specific activity # do post-processing on a specific activity
def contain_activity(%Activity{} = activity, %User{} = user) do def contain_activity(%Activity{} = activity, %User{} = user) do
contain_broken_threads(activity, user) contain_broken_threads(activity, user) and contain_muted_boosts(activity, user)
end end
# do post-processing on a timeline # do post-processing on a timeline

View file

@ -299,4 +299,20 @@ def report(user, data) do
{:account, nil} -> {:error, "Account not found"} {:account, nil} -> {:error, "Account not found"}
end end
end end
def hide_reblogs(user, id) do
if id not in user.info.muted_reblogs do
info_changeset = User.Info.add_reblog_mute(user.info, id)
changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset)
User.update_and_set_cache(changeset)
end
end
def show_reblogs(user, id) do
if id in user.info.muted_reblogs do
info_changeset = User.Info.remove_reblog_mute(user.info, id)
changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset)
User.update_and_set_cache(changeset)
end
end
end end

View file

@ -723,11 +723,24 @@ def reject_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) d
def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
with %User{} = followed <- Repo.get(User, id), with %User{} = followed <- Repo.get(User, id),
false <- User.following?(follower, followed),
{:ok, follower, followed, _} <- CommonAPI.follow(follower, followed) do {:ok, follower, followed, _} <- CommonAPI.follow(follower, followed) do
conn conn
|> put_view(AccountView) |> put_view(AccountView)
|> render("relationship.json", %{user: follower, target: followed}) |> render("relationship.json", %{user: follower, target: followed})
else else
true ->
case conn.params["reblogs"] do
true -> CommonAPI.show_reblogs(follower, id)
false -> CommonAPI.hide_reblogs(follower, id)
end
followed = Repo.get(User, id)
conn
|> put_view(AccountView)
|> render("relationship.json", %{user: follower, target: followed})
{:error, message} -> {:error, message} ->
conn conn
|> put_resp_content_type("application/json") |> put_resp_content_type("application/json")

View file

@ -55,7 +55,7 @@ def render("relationship.json", %{user: %User{} = user, target: %User{} = target
muting_notifications: false, muting_notifications: false,
requested: requested, requested: requested,
domain_blocking: false, domain_blocking: false,
showing_reblogs: false, showing_reblogs: User.showing_reblogs?(user, target),
endorsed: false endorsed: false
} }
end end