Fix MRF policies to also work with Update
[akkoma] / lib / pleroma / user_relationship.ex
index 235ad427ce8887df7903f31c8d7cabef3810332d..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)
@@ -138,11 +155,16 @@ defmodule Pleroma.UserRelationship do
 
   def view_relationships_option(%User{} = reading_user, actors, opts) do
     {source_to_target_rel_types, target_to_source_rel_types} =
-      if opts[:source_mutes_only] do
-        # This option is used for rendering statuses (FE needs `muted` flag for each one anyways)
-        {[:mute], []}
-      else
-        {[:block, :mute, :notification_mute, :reblog_mute], [:block, :inverse_subscription]}
+      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 =
@@ -153,7 +175,17 @@ defmodule Pleroma.UserRelationship do
         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