X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;ds=inline;f=lib%2Fpleroma%2Fuser_relationship.ex;h=a467e9b65ad23c6366286387028341af4a59bd53;hb=85d71d4f1d433a168eb136ad88b651b6c1d1a4fc;hp=011cf68227070a35597ee1e2e44c6298f8dc36bb;hpb=460e41585c2cd3f137c0f80173da60167fb318bf;p=akkoma
diff --git a/lib/pleroma/user_relationship.ex b/lib/pleroma/user_relationship.ex
index 011cf6822..a467e9b65 100644
--- a/lib/pleroma/user_relationship.ex
+++ b/lib/pleroma/user_relationship.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors
+# Copyright © 2017-2021 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.UserRelationship do
@@ -8,6 +8,7 @@ defmodule Pleroma.UserRelationship do
import Ecto.Changeset
import Ecto.Query
+ alias Ecto.Changeset
alias Pleroma.FollowingRelationship
alias Pleroma.Repo
alias Pleroma.User
@@ -16,28 +17,31 @@ defmodule Pleroma.UserRelationship do
schema "user_relationships" do
belongs_to(:source, User, type: FlakeId.Ecto.CompatType)
belongs_to(:target, User, type: FlakeId.Ecto.CompatType)
- field(:relationship_type, UserRelationshipTypeEnum)
+ field(:relationship_type, Pleroma.UserRelationship.Type)
timestamps(updated_at: false)
end
- for relationship_type <- Keyword.keys(UserRelationshipTypeEnum.__enum_map__()) do
- # Definitions of `create_block/2`, `create_mute/2` etc.
+ for relationship_type <- Keyword.keys(Pleroma.UserRelationship.Type.__enum_map__()) do
+ # `def create_block/2`, `def create_mute/2`, `def create_reblog_mute/2`,
+ # `def create_notification_mute/2`, `def create_inverse_subscription/2`
def unquote(:"create_#{relationship_type}")(source, target),
do: create(unquote(relationship_type), source, target)
- # Definitions of `delete_block/2`, `delete_mute/2` etc.
+ # `def delete_block/2`, `def delete_mute/2`, `def delete_reblog_mute/2`,
+ # `def delete_notification_mute/2`, `def delete_inverse_subscription/2`
def unquote(:"delete_#{relationship_type}")(source, target),
do: delete(unquote(relationship_type), source, target)
- # Definitions of `block_exists?/2`, `mute_exists?/2` etc.
+ # `def block_exists?/2`, `def mute_exists?/2`, `def reblog_mute_exists?/2`,
+ # `def notification_mute_exists?/2`, `def inverse_subscription_exists?/2`
def unquote(:"#{relationship_type}_exists?")(source, target),
do: exists?(unquote(relationship_type), source, target)
end
def user_relationship_types, do: Keyword.keys(user_relationship_mappings())
- def user_relationship_mappings, do: UserRelationshipTypeEnum.__enum_map__()
+ def user_relationship_mappings, do: Pleroma.UserRelationship.Type.__enum_map__()
def changeset(%UserRelationship{} = user_relationship, params \\ %{}) do
user_relationship
@@ -83,6 +87,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)
@@ -126,36 +146,69 @@ 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
- defp validate_not_self_relationship(%Ecto.Changeset{} = changeset) do
+ defp validate_not_self_relationship(%Changeset{} = changeset) do
changeset
- |> validate_change(:target_id, fn _, target_id ->
- if target_id == get_field(changeset, :source_id) do
- [target_id: "can't be equal to source_id"]
+ |> validate_source_id_target_id_inequality()
+ |> validate_target_id_source_id_inequality()
+ end
+
+ defp validate_source_id_target_id_inequality(%Changeset{} = changeset) do
+ validate_change(changeset, :source_id, fn _, source_id ->
+ if source_id == get_field(changeset, :target_id) do
+ [source_id: "can't be equal to target_id"]
else
[]
end
end)
- |> validate_change(:source_id, fn _, source_id ->
- if source_id == get_field(changeset, :target_id) do
- [source_id: "can't be equal to target_id"]
+ end
+
+ defp validate_target_id_source_id_inequality(%Changeset{} = changeset) do
+ validate_change(changeset, :target_id, fn _, target_id ->
+ if target_id == get_field(changeset, :source_id) do
+ [target_id: "can't be equal to source_id"]
else
[]
end