Implement mastodon's reblog hiding feature
authorKaren Konou <konoukaren@gmail.com>
Sat, 9 Mar 2019 13:08:41 +0000 (14:08 +0100)
committerKaren Konou <konoukaren@gmail.com>
Fri, 15 Mar 2019 13:18:21 +0000 (14:18 +0100)
lib/pleroma/user.ex
lib/pleroma/user/info.ex
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/common_api/common_api.ex
lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
lib/pleroma/web/mastodon_api/views/account_view.ex

index 1ce9882f63b6125c82c947dd16a47f3e55342f34..467cb910b65692421bbef1bfe446fd8b3c13d9df 100644 (file)
@@ -1385,4 +1385,8 @@ defmodule Pleroma.User do
       offset: ^((page - 1) * page_size)
     )
   end
+
+  def showing_reblogs?(%User{} = user, %User{} = target) do
+    target.id not in user.info.muted_reblogs
+  end
 end
index e3fd65a6ed8dcecd8989796cb6d226de68e06993..35586e212de7df93d3f47c206b19ec1c71190a44 100644 (file)
@@ -21,6 +21,7 @@ defmodule Pleroma.User.Info do
     field(:blocks, {:array, :string}, default: [])
     field(:domain_blocks, {:array, :string}, default: [])
     field(:mutes, {:array, :string}, default: [])
+    field(:muted_reblogs, {:array, :string}, default: [])
     field(:deactivated, :boolean, default: false)
     field(:no_rich_text, :boolean, default: false)
     field(:ap_enabled, :boolean, default: false)
@@ -259,4 +260,16 @@ defmodule Pleroma.User.Info do
       moderator: is_moderator
     }
   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
index 70db419cad6cf400b92efb3d2fc1ea2a69e05634..779ee139b0984bc71d75cd8e91a0c89fb1f81e54 100644 (file)
@@ -951,9 +951,17 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     entire_thread_visible_for_user?(activity, user)
   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
   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
 
   # do post-processing on a timeline
index de0759fb09279995aa4f0c051586409cb8a716d9..035c59387a2d97a890f9edb321c8c312f901415a 100644 (file)
@@ -299,4 +299,20 @@ defmodule Pleroma.Web.CommonAPI do
       {:account, nil} -> {:error, "Account not found"}
     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
index 265bf837efd6fff2a3fb351a31a700434ae4c6c1..570bf0c0f92dc23fdbeccbe6fef6ffd071675e17 100644 (file)
@@ -723,11 +723,24 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
 
   def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
     with %User{} = followed <- Repo.get(User, id),
+         false <- User.following?(follower, followed),
          {:ok, follower, followed, _} <- CommonAPI.follow(follower, followed) do
       conn
       |> put_view(AccountView)
       |> render("relationship.json", %{user: follower, target: followed})
     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} ->
         conn
         |> put_resp_content_type("application/json")
index c32f27be2afd75a8cc0f8c64c083230c4b2ca297..b5f3bbb9d0c4a291d67a306430232139faed9f13 100644 (file)
@@ -55,7 +55,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
       muting_notifications: false,
       requested: requested,
       domain_blocking: false,
-      showing_reblogs: false,
+      showing_reblogs: User.showing_reblogs?(user, target),
       endorsed: false
     }
   end