@strict_local_nickname_regex ~r/^[a-zA-Z\d]+$/
@extended_local_nickname_regex ~r/^[a-zA-Z\d_-]+$/
+ # AP ID user relationships (blocks, mutes etc.)
+ # Format: [rel_type: [outgoing_rel: :outgoing_rel_target, incoming_rel: :incoming_rel_source]]
+ @user_relationships_config [
+ block: [
+ blocker_blocks: :blocked_users,
+ blockee_blocks: :blocker_users
+ ],
+ mute: [
+ muter_mutes: :muted_users,
+ mutee_mutes: :muter_users
+ ],
+ reblog_mute: [
+ reblog_muter_mutes: :reblog_muted_users,
+ reblog_mutee_mutes: :reblog_muter_users
+ ],
+ notification_mute: [
+ notification_muter_mutes: :notification_muted_users,
+ notification_mutee_mutes: :notification_muter_users
+ ],
+ # Note: `inverse_subscription` relationship is inverse: subscriber acts as relationship target
+ inverse_subscription: [
+ subscribee_subscriptions: :subscriber_users,
+ subscriber_subscriptions: :subscribee_users
+ ]
+ ]
+
schema "users" do
field(:bio, :string)
field(:email, :string)
field(:tags, {:array, :string}, default: [])
field(:last_refreshed_at, :naive_datetime_usec)
field(:last_digest_emailed_at, :naive_datetime)
-
field(:banner, :map, default: %{})
field(:background, :map, default: %{})
field(:source_data, :map, default: %{})
field(:note_count, :integer, default: 0)
field(:follower_count, :integer, default: 0)
- # Should be filled in only for remote users
- field(:following_count, :integer, default: nil)
+ field(:following_count, :integer, default: 0)
field(:locked, :boolean, default: false)
field(:confirmation_pending, :boolean, default: false)
field(:password_reset_pending, :boolean, default: false)
field(:confirmation_token, :string, default: nil)
field(:default_scope, :string, default: "public")
field(:domain_blocks, {:array, :string}, default: [])
- field(:muted_reblogs, {:array, :string}, default: [])
- field(:muted_notifications, {:array, :string}, default: [])
- field(:subscribers, {:array, :string}, default: [])
field(:deactivated, :boolean, default: false)
field(:no_rich_text, :boolean, default: false)
field(:ap_enabled, :boolean, default: false)
field(:raw_fields, {:array, :map}, default: [])
field(:discoverable, :boolean, default: false)
field(:invisible, :boolean, default: false)
+ field(:allow_following_move, :boolean, default: true)
field(:skip_thread_containment, :boolean, default: false)
+ field(:actor_type, :string, default: "Person")
+ field(:also_known_as, {:array, :string}, default: [])
- field(:notification_settings, :map,
- default: %{
- "followers" => true,
- "follows" => true,
- "non_follows" => true,
- "non_followers" => true
- }
+ embeds_one(
+ :notification_settings,
+ Pleroma.User.NotificationSetting,
+ on_replace: :update
)
has_many(:notifications, Notification)
has_many(:outgoing_relationships, UserRelationship, foreign_key: :source_id)
has_many(:incoming_relationships, UserRelationship, foreign_key: :target_id)
- has_many(:blocker_blocks, UserRelationship,
- foreign_key: :source_id,
- where: [relationship_type: :block]
- )
-
- has_many(:blockee_blocks, UserRelationship,
- foreign_key: :target_id,
- where: [relationship_type: :block]
- )
-
- has_many(:blocked_users, through: [:blocker_blocks, :target])
- has_many(:blocker_users, through: [:blockee_blocks, :source])
-
- has_many(:muter_mutes, UserRelationship,
- foreign_key: :source_id,
- where: [relationship_type: :mute]
- )
+ for {relationship_type,
+ [
+ {outgoing_relation, outgoing_relation_target},
+ {incoming_relation, incoming_relation_source}
+ ]} <- @user_relationships_config do
+ # Definitions of `has_many :blocker_blocks`, `has_many :muter_mutes` etc.
+ has_many(outgoing_relation, UserRelationship,
+ foreign_key: :source_id,
+ where: [relationship_type: relationship_type]
+ )
- has_many(:mutee_mutes, UserRelationship,
- foreign_key: :target_id,
- where: [relationship_type: :mute]
- )
+ # Definitions of `has_many :blockee_blocks`, `has_many :mutee_mutes` etc.
+ has_many(incoming_relation, UserRelationship,
+ foreign_key: :target_id,
+ where: [relationship_type: relationship_type]
+ )
- has_many(:muted_users, through: [:muter_mutes, :target])
- has_many(:muter_users, through: [:mutee_mutes, :source])
+ # Definitions of `has_many :blocked_users`, `has_many :muted_users` etc.
+ has_many(outgoing_relation_target, through: [outgoing_relation, :target])
- field(:info, :map, default: %{})
+ # Definitions of `has_many :blocker_users`, `has_many :muter_users` etc.
+ has_many(incoming_relation_source, through: [incoming_relation, :source])
+ end
# `:blocks` is deprecated (replaced with `blocked_users` relation)
field(:blocks, {:array, :string}, default: [])
# `:mutes` is deprecated (replaced with `muted_users` relation)
field(:mutes, {:array, :string}, default: [])
+ # `:muted_reblogs` is deprecated (replaced with `reblog_muted_users` relation)
+ field(:muted_reblogs, {:array, :string}, default: [])
+ # `:muted_notifications` is deprecated (replaced with `notification_muted_users` relation)
+ field(:muted_notifications, {:array, :string}, default: [])
+ # `:subscribers` is deprecated (replaced with `subscriber_users` relation)
+ field(:subscribers, {:array, :string}, default: [])
timestamps()
end
+ for {_relationship_type, [{_outgoing_relation, outgoing_relation_target}, _]} <-
+ @user_relationships_config do
+ # Definitions of `blocked_users_relation/1`, `muted_users_relation/1`, etc.
+ def unquote(:"#{outgoing_relation_target}_relation")(user, restrict_deactivated? \\ false) do
+ target_users_query = assoc(user, unquote(outgoing_relation_target))
+
+ if restrict_deactivated? do
+ restrict_deactivated(target_users_query)
+ else
+ target_users_query
+ end
+ end
+
+ # Definitions of `blocked_users/1`, `muted_users/1`, etc.
+ def unquote(outgoing_relation_target)(user, restrict_deactivated? \\ false) do
+ __MODULE__
+ |> apply(unquote(:"#{outgoing_relation_target}_relation"), [
+ user,
+ restrict_deactivated?
+ ])
+ |> Repo.all()
+ end
+
+ # Definitions of `blocked_users_ap_ids/1`, `muted_users_ap_ids/1`, etc.
+ def unquote(:"#{outgoing_relation_target}_ap_ids")(user, restrict_deactivated? \\ false) do
+ __MODULE__
+ |> apply(unquote(:"#{outgoing_relation_target}_relation"), [
+ user,
+ restrict_deactivated?
+ ])
+ |> select([u], u.ap_id)
+ |> Repo.all()
+ end
+ end
+
@doc "Returns if the user should be allowed to authenticate"
def auth_active?(%User{deactivated: true}), do: false
def visible_for?(user, for_user \\ nil)
+ def visible_for?(%User{invisible: true}, _), do: false
+
def visible_for?(%User{id: user_id}, %User{id: for_id}) when user_id == for_id, do: true
def visible_for?(%User{} = user, for_user) do
def ap_following(%User{following_address: fa}) when is_binary(fa), do: fa
def ap_following(%User{} = user), do: "#{ap_id(user)}/following"
- def user_info(%User{} = user, args \\ %{}) do
- following_count =
- Map.get(args, :following_count, user.following_count || following_count(user))
-
- follower_count = Map.get(args, :follower_count, user.follower_count)
-
- %{
- note_count: user.note_count,
- locked: user.locked,
- confirmation_pending: user.confirmation_pending,
- default_scope: user.default_scope
- }
- |> Map.put(:following_count, following_count)
- |> Map.put(:follower_count, follower_count)
- end
-
def follow_state(%User{} = user, %User{} = target) do
case Utils.fetch_latest_follow(user, target) do
%{data: %{"state" => state}} -> state
Cachex.put(:user_cache, "follow_state:#{user_ap_id}|#{target_ap_id}", state)
end
- def set_info_cache(user, args) do
- Cachex.put(:user_cache, "user_info:#{user.id}", user_info(user, args))
- end
-
@spec restrict_deactivated(Ecto.Query.t()) :: Ecto.Query.t()
def restrict_deactivated(query) do
from(u in query, where: u.deactivated != ^true)
params =
params
- |> Map.put(:info, params[:info] || %{})
|> truncate_if_exists(:name, name_limit)
|> truncate_if_exists(:bio, bio_limit)
|> truncate_fields_param()
:fields,
:following_count,
:discoverable,
- :invisible
+ :invisible,
+ :actor_type,
+ :also_known_as
]
)
|> validate_required([:name, :ap_id])
:hide_followers_count,
:hide_follows_count,
:hide_favorites,
+ :allow_following_move,
:background,
:show_role,
:skip_thread_containment,
:fields,
:raw_fields,
:pleroma_settings_store,
- :discoverable
+ :discoverable,
+ :actor_type,
+ :also_known_as
]
)
|> unique_constraint(:nickname)
:hide_follows,
:fields,
:hide_followers,
+ :allow_following_move,
:discoverable,
:hide_followers_count,
- :hide_follows_count
+ :hide_follows_count,
+ :actor_type,
+ :also_known_as
]
)
|> unique_constraint(:nickname)
end
end
+ def try_send_confirmation_email(users) do
+ Enum.each(users, &try_send_confirmation_email/1)
+ end
+
def needs_update?(%User{local: true}), do: false
def needs_update?(%User{local: false, last_refreshed_at: nil}), do: true
@doc "A mass follow for local users. Respects blocks in both directions but does not create activities."
@spec follow_all(User.t(), list(User.t())) :: {atom(), User.t()}
def follow_all(follower, followeds) do
- followeds =
- Enum.reject(followeds, fn followed ->
- blocks?(follower, followed) || blocks?(followed, follower)
- end)
-
- Enum.each(followeds, &follow(follower, &1, "accept"))
-
- Enum.each(followeds, &update_follower_count/1)
+ followeds
+ |> Enum.reject(fn followed -> blocks?(follower, followed) || blocks?(followed, follower) end)
+ |> Enum.each(&follow(follower, &1, "accept"))
set_cache(follower)
end
true ->
FollowingRelationship.follow(follower, followed, state)
- follower = maybe_update_following_count(follower)
-
{:ok, _} = update_follower_count(followed)
- set_cache(follower)
+ follower
+ |> update_following_count()
+ |> set_cache()
end
end
if following?(follower, followed) and follower.ap_id != followed.ap_id do
FollowingRelationship.unfollow(follower, followed)
- follower = maybe_update_following_count(follower)
-
{:ok, followed} = update_follower_count(followed)
- set_cache(follower)
+ {:ok, follower} =
+ follower
+ |> update_following_count()
+ |> set_cache()
{:ok, follower, Utils.fetch_latest_follow(follower, followed)}
else
def set_cache(%User{} = user) do
Cachex.put(:user_cache, "ap_id:#{user.ap_id}", user)
Cachex.put(:user_cache, "nickname:#{user.nickname}", user)
- Cachex.put(:user_cache, "user_info:#{user.id}", user_info(user))
{:ok, user}
end
def invalidate_cache(user) do
Cachex.del(:user_cache, "ap_id:#{user.ap_id}")
Cachex.del(:user_cache, "nickname:#{user.nickname}")
- Cachex.del(:user_cache, "user_info:#{user.id}")
end
def get_cached_by_ap_id(ap_id) do
get_by_nickname(nickname_or_email) || get_by_email(nickname_or_email)
end
- def get_cached_user_info(user) do
- key = "user_info:#{user.id}"
- Cachex.fetch!(:user_cache, key, fn -> user_info(user) end)
- end
-
def fetch_by_nickname(nickname), do: ActivityPub.make_user_from_nickname(nickname)
def get_or_fetch_by_nickname(nickname) do
|> Repo.all()
end
+ def get_friends_ap_ids(user) do
+ user
+ |> get_friends_query(nil)
+ |> select([u], u.ap_id)
+ |> Repo.all()
+ end
+
def get_friends_ids(user, page \\ nil) do
user
|> get_friends_query(page)
end
end
- @spec maybe_update_following_count(User.t()) :: User.t()
- def maybe_update_following_count(%User{local: false} = user) do
+ @spec update_following_count(User.t()) :: User.t()
+ def update_following_count(%User{local: false} = user) do
if Pleroma.Config.get([:instance, :external_user_synchronization]) do
maybe_fetch_follow_information(user)
else
end
end
- def maybe_update_following_count(user), do: user
+ def update_following_count(%User{local: true} = user) do
+ following_count = FollowingRelationship.following_count(user)
+
+ user
+ |> follow_information_changeset(%{following_count: following_count})
+ |> Repo.update!()
+ end
def set_unread_conversation_count(%User{local: true} = user) do
unread_query = Participation.unread_conversation_count_for_user(user)
|> Repo.all()
end
- @spec mute(User.t(), User.t(), boolean()) :: {:ok, User.t()} | {:error, String.t()}
- def mute(muter, %User{} = mutee, notifications? \\ true) do
+ @spec mute(User.t(), User.t(), boolean()) ::
+ {:ok, list(UserRelationship.t())} | {:error, String.t()}
+ def mute(%User{} = muter, %User{} = mutee, notifications? \\ true) do
add_to_mutes(muter, mutee, notifications?)
end
- def unmute(muter, %User{} = mutee) do
+ def unmute(%User{} = muter, %User{} = mutee) do
remove_from_mutes(muter, mutee)
end
- def subscribe(subscriber, %{ap_id: ap_id}) do
- with %User{} = subscribed <- get_cached_by_ap_id(ap_id) do
- deny_follow_blocked = Pleroma.Config.get([:user, :deny_follow_blocked])
+ def subscribe(%User{} = subscriber, %User{} = target) do
+ deny_follow_blocked = Pleroma.Config.get([:user, :deny_follow_blocked])
- if blocks?(subscribed, subscriber) and deny_follow_blocked do
- {:error, "Could not subscribe: #{subscribed.nickname} is blocking you"}
- else
- User.add_to_subscribers(subscribed, subscriber.ap_id)
- end
+ if blocks?(target, subscriber) and deny_follow_blocked do
+ {:error, "Could not subscribe: #{target.nickname} is blocking you"}
+ else
+ # Note: the relationship is inverse: subscriber acts as relationship target
+ UserRelationship.create_inverse_subscription(target, subscriber)
end
end
- def unsubscribe(unsubscriber, %{ap_id: ap_id}) do
+ def subscribe(%User{} = subscriber, %{ap_id: ap_id}) do
+ with %User{} = subscribee <- get_cached_by_ap_id(ap_id) do
+ subscribe(subscriber, subscribee)
+ end
+ end
+
+ def unsubscribe(%User{} = unsubscriber, %User{} = target) do
+ # Note: the relationship is inverse: subscriber acts as relationship target
+ UserRelationship.delete_inverse_subscription(target, unsubscriber)
+ end
+
+ def unsubscribe(%User{} = unsubscriber, %{ap_id: ap_id}) do
with %User{} = user <- get_cached_by_ap_id(ap_id) do
- User.remove_from_subscribers(user, unsubscriber.ap_id)
+ unsubscribe(unsubscriber, user)
end
end
- def block(blocker, %User{} = blocked) do
+ def block(%User{} = blocker, %User{} = blocked) do
# sever any follow relationships to prevent leaks per activitypub (Pleroma issue #213)
blocker =
if following?(blocker, blocked) do
nil -> blocked
end
- blocker =
- if subscribed_to?(blocked, blocker) do
- {:ok, blocker} = unsubscribe(blocked, blocker)
- blocker
- else
- blocker
- end
+ unsubscribe(blocked, blocker)
if following?(blocked, blocker), do: unfollow(blocked, blocker)
end
# helper to handle the block given only an actor's AP id
- def block(blocker, %{ap_id: ap_id}) do
+ def block(%User{} = blocker, %{ap_id: ap_id}) do
block(blocker, get_cached_by_ap_id(ap_id))
end
- def unblock(blocker, %User{} = blocked) do
+ def unblock(%User{} = blocker, %User{} = blocked) do
remove_from_block(blocker, blocked)
end
# helper to handle the block given only an actor's AP id
- def unblock(blocker, %{ap_id: ap_id}) do
+ def unblock(%User{} = blocker, %{ap_id: ap_id}) do
unblock(blocker, get_cached_by_ap_id(ap_id))
end
@spec muted_notifications?(User.t() | nil, User.t() | map()) :: boolean()
def muted_notifications?(nil, _), do: false
- def muted_notifications?(user, %{ap_id: ap_id}),
- do: Enum.member?(user.muted_notifications, ap_id)
+ def muted_notifications?(%User{} = user, %User{} = target),
+ do: UserRelationship.notification_mute_exists?(user, target)
def blocks?(nil, _), do: false
def blocks?(%User{} = user, %User{} = target) do
- blocks_user?(user, target) || blocks_domain?(user, target)
+ blocks_user?(user, target) ||
+ (!User.following?(user, target) && blocks_domain?(user, target))
end
def blocks_user?(%User{} = user, %User{} = target) do
def blocks_domain?(_, _), do: false
- def subscribed_to?(user, %{ap_id: ap_id}) do
- with %User{} = target <- get_cached_by_ap_id(ap_id) do
- Enum.member?(target.subscribers, user.ap_id)
- end
+ def subscribed_to?(%User{} = user, %User{} = target) do
+ # Note: the relationship is inverse: subscriber acts as relationship target
+ UserRelationship.inverse_subscription_exists?(target, user)
end
- @spec muted_users(User.t()) :: [User.t()]
- def muted_users(user) do
- user
- |> assoc(:muted_users)
- |> restrict_deactivated()
- |> Repo.all()
- end
-
- def muted_ap_ids(user) do
- user
- |> assoc(:muted_users)
- |> select([u], u.ap_id)
- |> Repo.all()
- end
-
- @spec blocked_users(User.t()) :: [User.t()]
- def blocked_users(user) do
- user
- |> assoc(:blocked_users)
- |> restrict_deactivated()
- |> Repo.all()
- end
-
- def blocked_ap_ids(user) do
- user
- |> assoc(:blocked_users)
- |> select([u], u.ap_id)
- |> Repo.all()
+ def subscribed_to?(%User{} = user, %{ap_id: ap_id}) do
+ with %User{} = target <- get_cached_by_ap_id(ap_id) do
+ subscribed_to?(user, target)
+ end
end
@doc """
- Returns map of related AP IDs list by relation type.
- E.g. `related_ap_ids(user, [:blocks])` -> `%{blocks: ["https://some.site/users/userapid"]}`
+ Returns map of outgoing (blocked, muted etc.) relations' user AP IDs by relation type.
+ E.g. `outgoing_relations_ap_ids(user, [:block])` -> `%{block: ["https://some.site/users/userapid"]}`
"""
- @spec related_ap_ids(User.t(), list(atom())) :: %{atom() => list(String.t())}
- def related_ap_ids(%User{} = user, relationship_types) when is_list(relationship_types) do
- user
- |> assoc(:outgoing_relationships)
- |> join(:inner, [user_rel], u in assoc(user_rel, :target))
- |> where([user_rel, u], user_rel.relationship_type in ^relationship_types)
- |> select([user_rel, u], [user_rel.relationship_type, fragment("array_agg(?)", u.ap_id)])
- |> group_by([user_rel, u], user_rel.relationship_type)
- |> Repo.all()
- |> Enum.into(%{}, fn [k, v] -> {k, v} end)
- end
+ @spec outgoing_relations_ap_ids(User.t(), list(atom())) :: %{atom() => list(String.t())}
+ def outgoing_relations_ap_ids(_, []), do: %{}
- @spec subscribers(User.t()) :: [User.t()]
- def subscribers(user) do
- User.Query.build(%{ap_id: user.subscribers, deactivated: false})
- |> Repo.all()
+ def outgoing_relations_ap_ids(%User{} = user, relationship_types)
+ when is_list(relationship_types) do
+ db_result =
+ user
+ |> assoc(:outgoing_relationships)
+ |> join(:inner, [user_rel], u in assoc(user_rel, :target))
+ |> where([user_rel, u], user_rel.relationship_type in ^relationship_types)
+ |> select([user_rel, u], [user_rel.relationship_type, fragment("array_agg(?)", u.ap_id)])
+ |> group_by([user_rel, u], user_rel.relationship_type)
+ |> Repo.all()
+ |> Enum.into(%{}, fn [k, v] -> {k, v} end)
+
+ Enum.into(
+ relationship_types,
+ %{},
+ fn rel_type -> {rel_type, db_result[rel_type] || []} end
+ )
end
def deactivate_async(user, status \\ true) do
def deactivate(%User{} = user, status) do
with {:ok, user} <- set_activation_status(user, status) do
- Enum.each(get_followers(user), &invalidate_cache/1)
+ user
+ |> get_followers()
+ |> Enum.filter(& &1.local)
+ |> Enum.each(fn follower ->
+ follower |> update_following_count() |> set_cache()
+ end)
# Only update local user counts, remote will be update during the next pull.
user
end
def update_notification_settings(%User{} = user, settings) do
- settings =
- settings
- |> Enum.map(fn {k, v} -> {k, v in [true, "true", "True", "1"]} end)
- |> Map.new()
-
- notification_settings =
- user.notification_settings
- |> Map.merge(settings)
- |> Map.take(["followers", "follows", "non_follows", "non_followers"])
-
- params = %{notification_settings: notification_settings}
-
user
- |> cast(params, [:notification_settings])
+ |> cast(%{notification_settings: settings}, [])
+ |> cast_embed(:notification_settings)
|> validate_required([:notification_settings])
|> update_and_set_cache()
end
def external_users(opts \\ []) do
query =
external_users_query()
- |> select([u], struct(u, [:id, :ap_id, :info]))
+ |> select([u], struct(u, [:id, :ap_id]))
query =
if opts[:max_id],
end
end
- @doc "Creates an internal service actor by URI if missing. Optionally takes nickname for addressing."
- def get_or_create_service_actor_by_ap_id(uri, nickname \\ nil) do
- with %User{} = user <- get_cached_by_ap_id(uri) do
- user
- else
- _ ->
- {:ok, user} =
- %User{}
- |> cast(%{}, [:ap_id, :nickname, :local])
- |> put_change(:ap_id, uri)
- |> put_change(:nickname, nickname)
- |> put_change(:local, true)
- |> put_change(:follower_address, uri <> "/followers")
- |> Repo.insert()
+ @doc """
+ Creates an internal service actor by URI if missing.
+ Optionally takes nickname for addressing.
+ """
+ @spec get_or_create_service_actor_by_ap_id(String.t(), String.t()) :: User.t() | nil
+ def get_or_create_service_actor_by_ap_id(uri, nickname) do
+ {_, user} =
+ case get_cached_by_ap_id(uri) do
+ nil ->
+ with {:error, %{errors: errors}} <- create_service_actor(uri, nickname) do
+ Logger.error("Cannot create service actor: #{uri}/.\n#{inspect(errors)}")
+ {:error, nil}
+ end
- user
- end
+ %User{invisible: false} = user ->
+ set_invisible(user)
+
+ user ->
+ {:ok, user}
+ end
+
+ user
+ end
+
+ @spec set_invisible(User.t()) :: {:ok, User.t()}
+ defp set_invisible(user) do
+ user
+ |> change(%{invisible: true})
+ |> update_and_set_cache()
+ end
+
+ @spec create_service_actor(String.t(), String.t()) ::
+ {:ok, User.t()} | {:error, Ecto.Changeset.t()}
+ defp create_service_actor(uri, nickname) do
+ %User{
+ invisible: true,
+ local: true,
+ ap_id: uri,
+ nickname: nickname,
+ follower_address: uri <> "/followers"
+ }
+ |> change
+ |> unique_constraint(:nickname)
+ |> Repo.insert()
+ |> set_cache()
end
# AP style
end
def showing_reblogs?(%User{} = user, %User{} = target) do
- target.ap_id not in user.muted_reblogs
+ not UserRelationship.reblog_mute_exists?(user, target)
end
@doc """
|> update_and_set_cache()
end
+ @spec toggle_confirmation([User.t()]) :: [{:ok, User.t()} | {:error, Changeset.t()}]
+ def toggle_confirmation(users) do
+ Enum.map(users, &toggle_confirmation/1)
+ end
+
def get_mascot(%{mascot: %{} = mascot}) when not is_nil(mascot) do
mascot
end
end
def admin_api_update(user, params) do
- user
- |> cast(params, [
- :is_moderator,
- :is_admin,
- :show_role
- ])
- |> update_and_set_cache()
+ changeset =
+ cast(user, params, [
+ :is_moderator,
+ :is_admin,
+ :show_role
+ ])
+
+ with {:ok, updated_user} <- update_and_set_cache(changeset) do
+ if user.is_admin != updated_user.is_admin do
+ # Admin status change results in change of accessible OAuth scopes, and instead of changing
+ # already issued tokens we revoke them, requiring user to sign in again
+ global_sign_out(user)
+ end
+
+ {:ok, updated_user}
+ end
+ end
+
+ @doc "Signs user out of all applications"
+ def global_sign_out(user) do
+ OAuth.Authorization.delete_user_authorizations(user)
+ OAuth.Token.delete_user_tokens(user)
end
def mascot_update(user, url) do
|> update_and_set_cache()
end
- defp set_subscribers(user, subscribers) do
- params = %{subscribers: subscribers}
-
- user
- |> cast(params, [:subscribers])
- |> validate_required([:subscribers])
- |> update_and_set_cache()
- end
-
- def add_to_subscribers(user, subscribed) do
- set_subscribers(user, Enum.uniq([subscribed | user.subscribers]))
- end
-
- def remove_from_subscribers(user, subscribed) do
- set_subscribers(user, List.delete(user.subscribers, subscribed))
- end
-
defp set_domain_blocks(user, domain_blocks) do
params = %{domain_blocks: domain_blocks}
UserRelationship.delete_block(user, blocked)
end
- defp add_to_mutes(%User{} = user, %User{ap_id: ap_id} = muted_user, notifications?) do
+ defp add_to_mutes(%User{} = user, %User{} = muted_user, notifications?) do
with {:ok, user_mute} <- UserRelationship.create_mute(user, muted_user),
- {:ok, _user} <-
- set_notification_mutes(
- user,
- Enum.uniq([ap_id | user.muted_notifications]),
- notifications?
- ) do
- {:ok, user_mute}
+ {:ok, user_notification_mute} <-
+ (notifications? && UserRelationship.create_notification_mute(user, muted_user)) ||
+ {:ok, nil} do
+ {:ok, Enum.filter([user_mute, user_notification_mute], & &1)}
end
end
- defp remove_from_mutes(user, %User{ap_id: ap_id} = muted_user) do
+ defp remove_from_mutes(user, %User{} = muted_user) do
with {:ok, user_mute} <- UserRelationship.delete_mute(user, muted_user),
- {:ok, _user} <-
- set_notification_mutes(
- user,
- List.delete(user.muted_notifications, ap_id),
- true
- ) do
- {:ok, user_mute}
+ {:ok, user_notification_mute} <-
+ UserRelationship.delete_notification_mute(user, muted_user) do
+ {:ok, [user_mute, user_notification_mute]}
end
end
- defp set_notification_mutes(user, _muted_notifications, false = _notifications?) do
- {:ok, user}
- end
-
- defp set_notification_mutes(user, muted_notifications, true = _notifications?) do
- params = %{muted_notifications: muted_notifications}
-
- user
- |> cast(params, [:muted_notifications])
- |> validate_required([:muted_notifications])
- |> update_and_set_cache()
- end
-
- def add_reblog_mute(user, ap_id) do
- params = %{muted_reblogs: user.muted_reblogs ++ [ap_id]}
-
- user
- |> cast(params, [:muted_reblogs])
- |> update_and_set_cache()
- end
-
- def remove_reblog_mute(user, ap_id) do
- params = %{muted_reblogs: List.delete(user.muted_reblogs, ap_id)}
-
- user
- |> cast(params, [:muted_reblogs])
- |> update_and_set_cache()
- end
-
def set_invisible(user, invisible) do
params = %{invisible: invisible}