Fix tagpolicy to also work with Update
[akkoma] / lib / pleroma / user_relationship.ex
index ad0d303b1c2973ce8f62ca8d9936cc83cbc05e80..2f77697d4db18b4e1b6ae9b78177f7286e4fbe6f 100644 (file)
@@ -1,5 +1,5 @@
 # Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.UserRelationship do
@@ -67,8 +67,9 @@ defmodule Pleroma.UserRelationship do
       target_id: target.id
     })
     |> Repo.insert(
-      on_conflict: {:replace_all_except, [:id]},
-      conflict_target: [:source_id, :relationship_type, :target_id]
+      on_conflict: {:replace_all_except, [:id, :inserted_at]},
+      conflict_target: [:source_id, :relationship_type, :target_id],
+      returning: true
     )
   end
 
@@ -87,6 +88,22 @@ defmodule Pleroma.UserRelationship do
         source_to_target_rel_types \\ nil,
         target_to_source_rel_types \\ nil
       )
+
+  def dictionary(
+        _source_users,
+        _target_users,
+        [] = _source_to_target_rel_types,
+        [] = _target_to_source_rel_types
+      ) do
+    []
+  end
+
+  def dictionary(
+        source_users,
+        target_users,
+        source_to_target_rel_types,
+        target_to_source_rel_types
+      )
       when is_list(source_users) and is_list(target_users) do
     source_user_ids = User.binary_id(source_users)
     target_user_ids = User.binary_id(target_users)
@@ -130,20 +147,45 @@ defmodule Pleroma.UserRelationship do
   end
 
   @doc ":relationships option for StatusView / AccountView / NotificationView"
-  def view_relationships_option(nil = _reading_user, _actors) do
+  def view_relationships_option(reading_user, actors, opts \\ [])
+
+  def view_relationships_option(nil = _reading_user, _actors, _opts) do
     %{user_relationships: [], following_relationships: []}
   end
 
-  def view_relationships_option(%User{} = reading_user, actors) do
+  def view_relationships_option(%User{} = reading_user, actors, opts) do
+    {source_to_target_rel_types, target_to_source_rel_types} =
+      case opts[:subset] do
+        :source_mutes ->
+          # Used for statuses rendering (FE needs `muted` flag for each status when statuses load)
+          {[:mute], []}
+
+        nil ->
+          {[:block, :mute, :notification_mute, :reblog_mute], [:block, :inverse_subscription]}
+
+        unknown ->
+          raise "Unsupported :subset option value: #{inspect(unknown)}"
+      end
+
     user_relationships =
       UserRelationship.dictionary(
         [reading_user],
         actors,
-        [:block, :mute, :notification_mute, :reblog_mute],
-        [:block, :inverse_subscription]
+        source_to_target_rel_types,
+        target_to_source_rel_types
       )
 
-    following_relationships = FollowingRelationship.all_between_user_sets([reading_user], actors)
+    following_relationships =
+      case opts[:subset] do
+        :source_mutes ->
+          []
+
+        nil ->
+          FollowingRelationship.all_between_user_sets([reading_user], actors)
+
+        unknown ->
+          raise "Unsupported :subset option value: #{inspect(unknown)}"
+      end
 
     %{user_relationships: user_relationships, following_relationships: following_relationships}
   end