1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.User do
10 import Ecto, only: [assoc: 2]
13 alias Pleroma.Activity
15 alias Pleroma.Conversation.Participation
16 alias Pleroma.Delivery
17 alias Pleroma.EctoType.ActivityPub.ObjectValidators
19 alias Pleroma.FollowingRelationship
20 alias Pleroma.Formatter
24 alias Pleroma.Notification
26 alias Pleroma.Registration
28 alias Pleroma.RepoStreamer
30 alias Pleroma.UserRelationship
32 alias Pleroma.Web.ActivityPub.ActivityPub
33 alias Pleroma.Web.ActivityPub.Builder
34 alias Pleroma.Web.ActivityPub.Pipeline
35 alias Pleroma.Web.ActivityPub.Utils
36 alias Pleroma.Web.CommonAPI
37 alias Pleroma.Web.CommonAPI.Utils, as: CommonUtils
38 alias Pleroma.Web.OAuth
39 alias Pleroma.Web.RelMe
40 alias Pleroma.Workers.BackgroundWorker
44 @type t :: %__MODULE__{}
45 @type account_status :: :active | :deactivated | :password_reset_pending | :confirmation_pending
46 @primary_key {:id, FlakeId.Ecto.CompatType, autogenerate: true}
48 # credo:disable-for-next-line Credo.Check.Readability.MaxLineLength
49 @email_regex ~r/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
51 @strict_local_nickname_regex ~r/^[a-zA-Z\d]+$/
52 @extended_local_nickname_regex ~r/^[a-zA-Z\d_-]+$/
54 # AP ID user relationships (blocks, mutes etc.)
55 # Format: [rel_type: [outgoing_rel: :outgoing_rel_target, incoming_rel: :incoming_rel_source]]
56 @user_relationships_config [
58 blocker_blocks: :blocked_users,
59 blockee_blocks: :blocker_users
62 muter_mutes: :muted_users,
63 mutee_mutes: :muter_users
66 reblog_muter_mutes: :reblog_muted_users,
67 reblog_mutee_mutes: :reblog_muter_users
70 notification_muter_mutes: :notification_muted_users,
71 notification_mutee_mutes: :notification_muter_users
73 # Note: `inverse_subscription` relationship is inverse: subscriber acts as relationship target
74 inverse_subscription: [
75 subscribee_subscriptions: :subscriber_users,
76 subscriber_subscriptions: :subscribee_users
82 field(:raw_bio, :string)
83 field(:email, :string)
85 field(:nickname, :string)
86 field(:password_hash, :string)
87 field(:password, :string, virtual: true)
88 field(:password_confirmation, :string, virtual: true)
90 field(:public_key, :string)
91 field(:ap_id, :string)
92 field(:avatar, :map, default: %{})
93 field(:local, :boolean, default: true)
94 field(:follower_address, :string)
95 field(:following_address, :string)
96 field(:search_rank, :float, virtual: true)
97 field(:search_type, :integer, virtual: true)
98 field(:tags, {:array, :string}, default: [])
99 field(:last_refreshed_at, :naive_datetime_usec)
100 field(:last_digest_emailed_at, :naive_datetime)
101 field(:banner, :map, default: %{})
102 field(:background, :map, default: %{})
103 field(:note_count, :integer, default: 0)
104 field(:follower_count, :integer, default: 0)
105 field(:following_count, :integer, default: 0)
106 field(:locked, :boolean, default: false)
107 field(:confirmation_pending, :boolean, default: false)
108 field(:password_reset_pending, :boolean, default: false)
109 field(:approval_pending, :boolean, default: false)
110 field(:registration_reason, :string, default: nil)
111 field(:confirmation_token, :string, default: nil)
112 field(:default_scope, :string, default: "public")
113 field(:domain_blocks, {:array, :string}, default: [])
114 field(:deactivated, :boolean, default: false)
115 field(:no_rich_text, :boolean, default: false)
116 field(:ap_enabled, :boolean, default: false)
117 field(:is_moderator, :boolean, default: false)
118 field(:is_admin, :boolean, default: false)
119 field(:show_role, :boolean, default: true)
120 field(:mastofe_settings, :map, default: nil)
121 field(:uri, ObjectValidators.Uri, default: nil)
122 field(:hide_followers_count, :boolean, default: false)
123 field(:hide_follows_count, :boolean, default: false)
124 field(:hide_followers, :boolean, default: false)
125 field(:hide_follows, :boolean, default: false)
126 field(:hide_favorites, :boolean, default: true)
127 field(:unread_conversation_count, :integer, default: 0)
128 field(:pinned_activities, {:array, :string}, default: [])
129 field(:email_notifications, :map, default: %{"digest" => false})
130 field(:mascot, :map, default: nil)
131 field(:emoji, :map, default: %{})
132 field(:pleroma_settings_store, :map, default: %{})
133 field(:fields, {:array, :map}, default: [])
134 field(:raw_fields, {:array, :map}, default: [])
135 field(:discoverable, :boolean, default: false)
136 field(:invisible, :boolean, default: false)
137 field(:allow_following_move, :boolean, default: true)
138 field(:skip_thread_containment, :boolean, default: false)
139 field(:actor_type, :string, default: "Person")
140 field(:also_known_as, {:array, :string}, default: [])
141 field(:inbox, :string)
142 field(:shared_inbox, :string)
143 field(:accepts_chat_messages, :boolean, default: nil)
146 :notification_settings,
147 Pleroma.User.NotificationSetting,
151 has_many(:notifications, Notification)
152 has_many(:registrations, Registration)
153 has_many(:deliveries, Delivery)
155 has_many(:outgoing_relationships, UserRelationship, foreign_key: :source_id)
156 has_many(:incoming_relationships, UserRelationship, foreign_key: :target_id)
158 for {relationship_type,
160 {outgoing_relation, outgoing_relation_target},
161 {incoming_relation, incoming_relation_source}
162 ]} <- @user_relationships_config do
163 # Definitions of `has_many` relations: :blocker_blocks, :muter_mutes, :reblog_muter_mutes,
164 # :notification_muter_mutes, :subscribee_subscriptions
165 has_many(outgoing_relation, UserRelationship,
166 foreign_key: :source_id,
167 where: [relationship_type: relationship_type]
170 # Definitions of `has_many` relations: :blockee_blocks, :mutee_mutes, :reblog_mutee_mutes,
171 # :notification_mutee_mutes, :subscriber_subscriptions
172 has_many(incoming_relation, UserRelationship,
173 foreign_key: :target_id,
174 where: [relationship_type: relationship_type]
177 # Definitions of `has_many` relations: :blocked_users, :muted_users, :reblog_muted_users,
178 # :notification_muted_users, :subscriber_users
179 has_many(outgoing_relation_target, through: [outgoing_relation, :target])
181 # Definitions of `has_many` relations: :blocker_users, :muter_users, :reblog_muter_users,
182 # :notification_muter_users, :subscribee_users
183 has_many(incoming_relation_source, through: [incoming_relation, :source])
186 # `:blocks` is deprecated (replaced with `blocked_users` relation)
187 field(:blocks, {:array, :string}, default: [])
188 # `:mutes` is deprecated (replaced with `muted_users` relation)
189 field(:mutes, {:array, :string}, default: [])
190 # `:muted_reblogs` is deprecated (replaced with `reblog_muted_users` relation)
191 field(:muted_reblogs, {:array, :string}, default: [])
192 # `:muted_notifications` is deprecated (replaced with `notification_muted_users` relation)
193 field(:muted_notifications, {:array, :string}, default: [])
194 # `:subscribers` is deprecated (replaced with `subscriber_users` relation)
195 field(:subscribers, {:array, :string}, default: [])
198 :multi_factor_authentication_settings,
206 for {_relationship_type, [{_outgoing_relation, outgoing_relation_target}, _]} <-
207 @user_relationships_config do
208 # `def blocked_users_relation/2`, `def muted_users_relation/2`,
209 # `def reblog_muted_users_relation/2`, `def notification_muted_users/2`,
210 # `def subscriber_users/2`
211 def unquote(:"#{outgoing_relation_target}_relation")(user, restrict_deactivated? \\ false) do
212 target_users_query = assoc(user, unquote(outgoing_relation_target))
214 if restrict_deactivated? do
215 restrict_deactivated(target_users_query)
221 # `def blocked_users/2`, `def muted_users/2`, `def reblog_muted_users/2`,
222 # `def notification_muted_users/2`, `def subscriber_users/2`
223 def unquote(outgoing_relation_target)(user, restrict_deactivated? \\ false) do
225 |> apply(unquote(:"#{outgoing_relation_target}_relation"), [
227 restrict_deactivated?
232 # `def blocked_users_ap_ids/2`, `def muted_users_ap_ids/2`, `def reblog_muted_users_ap_ids/2`,
233 # `def notification_muted_users_ap_ids/2`, `def subscriber_users_ap_ids/2`
234 def unquote(:"#{outgoing_relation_target}_ap_ids")(user, restrict_deactivated? \\ false) do
236 |> apply(unquote(:"#{outgoing_relation_target}_relation"), [
238 restrict_deactivated?
240 |> select([u], u.ap_id)
246 Dumps Flake Id to SQL-compatible format (16-byte UUID).
247 E.g. "9pQtDGXuq4p3VlcJEm" -> <<0, 0, 1, 110, 179, 218, 42, 92, 213, 41, 44, 227, 95, 213, 0, 0>>
249 def binary_id(source_id) when is_binary(source_id) do
250 with {:ok, dumped_id} <- FlakeId.Ecto.CompatType.dump(source_id) do
257 def binary_id(source_ids) when is_list(source_ids) do
258 Enum.map(source_ids, &binary_id/1)
261 def binary_id(%User{} = user), do: binary_id(user.id)
263 @doc "Returns status account"
264 @spec account_status(User.t()) :: account_status()
265 def account_status(%User{deactivated: true}), do: :deactivated
266 def account_status(%User{password_reset_pending: true}), do: :password_reset_pending
267 def account_status(%User{approval_pending: true}), do: :approval_pending
269 def account_status(%User{confirmation_pending: true}) do
270 if Config.get([:instance, :account_activation_required]) do
271 :confirmation_pending
277 def account_status(%User{}), do: :active
279 @spec visible_for(User.t(), User.t() | nil) ::
282 | :restricted_unauthenticated
284 | :confirmation_pending
285 def visible_for(user, for_user \\ nil)
287 def visible_for(%User{invisible: true}, _), do: :invisible
289 def visible_for(%User{id: user_id}, %User{id: user_id}), do: :visible
291 def visible_for(%User{} = user, nil) do
292 if restrict_unauthenticated?(user) do
293 :restrict_unauthenticated
295 visible_account_status(user)
299 def visible_for(%User{} = user, for_user) do
300 if superuser?(for_user) do
303 visible_account_status(user)
307 def visible_for(_, _), do: :invisible
309 defp restrict_unauthenticated?(%User{local: local}) do
310 config_key = if local, do: :local, else: :remote
312 Config.get([:restrict_unauthenticated, :profiles, config_key], false)
315 defp visible_account_status(user) do
316 status = account_status(user)
318 if status in [:active, :password_reset_pending] do
325 @spec superuser?(User.t()) :: boolean()
326 def superuser?(%User{local: true, is_admin: true}), do: true
327 def superuser?(%User{local: true, is_moderator: true}), do: true
328 def superuser?(_), do: false
330 @spec invisible?(User.t()) :: boolean()
331 def invisible?(%User{invisible: true}), do: true
332 def invisible?(_), do: false
334 def avatar_url(user, options \\ []) do
336 %{"url" => [%{"href" => href} | _]} ->
340 unless options[:no_default] do
341 Config.get([:assets, :default_user_avatar], "#{Web.base_url()}/images/avi.png")
346 def banner_url(user, options \\ []) do
348 %{"url" => [%{"href" => href} | _]} -> href
349 _ -> !options[:no_default] && "#{Web.base_url()}/images/banner.png"
353 # Should probably be renamed or removed
354 def ap_id(%User{nickname: nickname}), do: "#{Web.base_url()}/users/#{nickname}"
356 def ap_followers(%User{follower_address: fa}) when is_binary(fa), do: fa
357 def ap_followers(%User{} = user), do: "#{ap_id(user)}/followers"
359 @spec ap_following(User.t()) :: String.t()
360 def ap_following(%User{following_address: fa}) when is_binary(fa), do: fa
361 def ap_following(%User{} = user), do: "#{ap_id(user)}/following"
363 @spec restrict_deactivated(Ecto.Query.t()) :: Ecto.Query.t()
364 def restrict_deactivated(query) do
365 from(u in query, where: u.deactivated != ^true)
368 defdelegate following_count(user), to: FollowingRelationship
370 defp truncate_fields_param(params) do
371 if Map.has_key?(params, :fields) do
372 Map.put(params, :fields, Enum.map(params[:fields], &truncate_field/1))
378 defp truncate_if_exists(params, key, max_length) do
379 if Map.has_key?(params, key) and is_binary(params[key]) do
380 {value, _chopped} = String.split_at(params[key], max_length)
381 Map.put(params, key, value)
387 defp fix_follower_address(%{follower_address: _, following_address: _} = params), do: params
389 defp fix_follower_address(%{nickname: nickname} = params),
390 do: Map.put(params, :follower_address, ap_followers(%User{nickname: nickname}))
392 defp fix_follower_address(params), do: params
394 def remote_user_changeset(struct \\ %User{local: false}, params) do
395 bio_limit = Config.get([:instance, :user_bio_length], 5000)
396 name_limit = Config.get([:instance, :user_name_length], 100)
399 case params[:name] do
400 name when is_binary(name) and byte_size(name) > 0 -> name
401 _ -> params[:nickname]
406 |> Map.put(:name, name)
407 |> Map.put_new(:last_refreshed_at, NaiveDateTime.utc_now())
408 |> truncate_if_exists(:name, name_limit)
409 |> truncate_if_exists(:bio, bio_limit)
410 |> truncate_fields_param()
411 |> fix_follower_address()
435 :hide_followers_count,
444 :accepts_chat_messages
447 |> validate_required([:name, :ap_id])
448 |> unique_constraint(:nickname)
449 |> validate_format(:nickname, @email_regex)
450 |> validate_length(:bio, max: bio_limit)
451 |> validate_length(:name, max: name_limit)
452 |> validate_fields(true)
455 def update_changeset(struct, params \\ %{}) do
456 bio_limit = Config.get([:instance, :user_bio_length], 5000)
457 name_limit = Config.get([:instance, :user_name_length], 100)
477 :hide_followers_count,
480 :allow_following_move,
483 :skip_thread_containment,
486 :pleroma_settings_store,
490 :accepts_chat_messages
493 |> unique_constraint(:nickname)
494 |> validate_format(:nickname, local_nickname_regex())
495 |> validate_length(:bio, max: bio_limit)
496 |> validate_length(:name, min: 1, max: name_limit)
497 |> validate_inclusion(:actor_type, ["Person", "Service"])
500 |> put_change_if_present(:bio, &{:ok, parse_bio(&1, struct)})
501 |> put_change_if_present(:avatar, &put_upload(&1, :avatar))
502 |> put_change_if_present(:banner, &put_upload(&1, :banner))
503 |> put_change_if_present(:background, &put_upload(&1, :background))
504 |> put_change_if_present(
505 :pleroma_settings_store,
506 &{:ok, Map.merge(struct.pleroma_settings_store, &1)}
508 |> validate_fields(false)
511 defp put_fields(changeset) do
512 if raw_fields = get_change(changeset, :raw_fields) do
515 |> Enum.filter(fn %{"name" => n} -> n != "" end)
519 |> Enum.map(fn f -> Map.update!(f, "value", &parse_fields(&1)) end)
522 |> put_change(:raw_fields, raw_fields)
523 |> put_change(:fields, fields)
529 defp parse_fields(value) do
531 |> Formatter.linkify(mentions_format: :full)
535 defp put_emoji(changeset) do
536 bio = get_change(changeset, :bio)
537 name = get_change(changeset, :name)
540 emoji = Map.merge(Emoji.Formatter.get_emoji_map(bio), Emoji.Formatter.get_emoji_map(name))
541 put_change(changeset, :emoji, emoji)
547 defp put_change_if_present(changeset, map_field, value_function) do
548 with {:ok, value} <- fetch_change(changeset, map_field),
549 {:ok, new_value} <- value_function.(value) do
550 put_change(changeset, map_field, new_value)
556 defp put_upload(value, type) do
557 with %Plug.Upload{} <- value,
558 {:ok, object} <- ActivityPub.upload(value, type: type) do
563 def update_as_admin_changeset(struct, params) do
565 |> update_changeset(params)
566 |> cast(params, [:email])
567 |> delete_change(:also_known_as)
568 |> unique_constraint(:email)
569 |> validate_format(:email, @email_regex)
570 |> validate_inclusion(:actor_type, ["Person", "Service"])
573 @spec update_as_admin(User.t(), map()) :: {:ok, User.t()} | {:error, Changeset.t()}
574 def update_as_admin(user, params) do
575 params = Map.put(params, "password_confirmation", params["password"])
576 changeset = update_as_admin_changeset(user, params)
578 if params["password"] do
579 reset_password(user, changeset, params)
581 User.update_and_set_cache(changeset)
585 def password_update_changeset(struct, params) do
587 |> cast(params, [:password, :password_confirmation])
588 |> validate_required([:password, :password_confirmation])
589 |> validate_confirmation(:password)
590 |> put_password_hash()
591 |> put_change(:password_reset_pending, false)
594 @spec reset_password(User.t(), map()) :: {:ok, User.t()} | {:error, Changeset.t()}
595 def reset_password(%User{} = user, params) do
596 reset_password(user, user, params)
599 def reset_password(%User{id: user_id} = user, struct, params) do
602 |> Multi.update(:user, password_update_changeset(struct, params))
603 |> Multi.delete_all(:tokens, OAuth.Token.Query.get_by_user(user_id))
604 |> Multi.delete_all(:auth, OAuth.Authorization.delete_by_user_query(user))
606 case Repo.transaction(multi) do
607 {:ok, %{user: user} = _} -> set_cache(user)
608 {:error, _, changeset, _} -> {:error, changeset}
612 def update_password_reset_pending(user, value) do
615 |> put_change(:password_reset_pending, value)
616 |> update_and_set_cache()
619 def force_password_reset_async(user) do
620 BackgroundWorker.enqueue("force_password_reset", %{"user_id" => user.id})
623 @spec force_password_reset(User.t()) :: {:ok, User.t()} | {:error, Ecto.Changeset.t()}
624 def force_password_reset(user), do: update_password_reset_pending(user, true)
626 def register_changeset(struct, params \\ %{}, opts \\ []) do
627 bio_limit = Config.get([:instance, :user_bio_length], 5000)
628 name_limit = Config.get([:instance, :user_name_length], 100)
629 params = Map.put_new(params, :accepts_chat_messages, true)
632 if is_nil(opts[:need_confirmation]) do
633 Config.get([:instance, :account_activation_required])
635 opts[:need_confirmation]
639 if is_nil(opts[:need_approval]) do
640 Config.get([:instance, :account_approval_required])
646 |> confirmation_changeset(need_confirmation: need_confirmation?)
647 |> approval_changeset(need_approval: need_approval?)
655 :password_confirmation,
657 :accepts_chat_messages,
660 |> validate_required([:name, :nickname, :password, :password_confirmation])
661 |> validate_confirmation(:password)
662 |> unique_constraint(:email)
663 |> unique_constraint(:nickname)
664 |> validate_exclusion(:nickname, Config.get([User, :restricted_nicknames]))
665 |> validate_format(:nickname, local_nickname_regex())
666 |> validate_format(:email, @email_regex)
667 |> validate_length(:bio, max: bio_limit)
668 |> validate_length(:name, min: 1, max: name_limit)
669 |> maybe_validate_required_email(opts[:external])
672 |> unique_constraint(:ap_id)
673 |> put_following_and_follower_address()
676 def maybe_validate_required_email(changeset, true), do: changeset
678 def maybe_validate_required_email(changeset, _) do
679 if Config.get([:instance, :account_activation_required]) do
680 validate_required(changeset, [:email])
686 defp put_ap_id(changeset) do
687 ap_id = ap_id(%User{nickname: get_field(changeset, :nickname)})
688 put_change(changeset, :ap_id, ap_id)
691 defp put_following_and_follower_address(changeset) do
692 followers = ap_followers(%User{nickname: get_field(changeset, :nickname)})
695 |> put_change(:follower_address, followers)
698 defp autofollow_users(user) do
699 candidates = Config.get([:instance, :autofollowed_nicknames])
702 User.Query.build(%{nickname: candidates, local: true, deactivated: false})
705 follow_all(user, autofollowed_users)
708 @doc "Inserts provided changeset, performs post-registration actions (confirmation email sending etc.)"
709 def register(%Ecto.Changeset{} = changeset) do
710 with {:ok, user} <- Repo.insert(changeset) do
711 post_register_action(user)
715 def post_register_action(%User{} = user) do
716 with {:ok, user} <- autofollow_users(user),
717 {:ok, user} <- set_cache(user),
718 {:ok, _} <- User.WelcomeMessage.post_welcome_message_to_user(user),
719 {:ok, _} <- try_send_confirmation_email(user) do
724 def try_send_confirmation_email(%User{} = user) do
725 if user.confirmation_pending &&
726 Config.get([:instance, :account_activation_required]) do
728 |> Pleroma.Emails.UserEmail.account_confirmation_email()
729 |> Pleroma.Emails.Mailer.deliver_async()
737 def try_send_confirmation_email(users) do
738 Enum.each(users, &try_send_confirmation_email/1)
741 def needs_update?(%User{local: true}), do: false
743 def needs_update?(%User{local: false, last_refreshed_at: nil}), do: true
745 def needs_update?(%User{local: false} = user) do
746 NaiveDateTime.diff(NaiveDateTime.utc_now(), user.last_refreshed_at) >= 86_400
749 def needs_update?(_), do: true
751 @spec maybe_direct_follow(User.t(), User.t()) :: {:ok, User.t()} | {:error, String.t()}
753 # "Locked" (self-locked) users demand explicit authorization of follow requests
754 def maybe_direct_follow(%User{} = follower, %User{local: true, locked: true} = followed) do
755 follow(follower, followed, :follow_pending)
758 def maybe_direct_follow(%User{} = follower, %User{local: true} = followed) do
759 follow(follower, followed)
762 def maybe_direct_follow(%User{} = follower, %User{} = followed) do
763 if not ap_enabled?(followed) do
764 follow(follower, followed)
770 @doc "A mass follow for local users. Respects blocks in both directions but does not create activities."
771 @spec follow_all(User.t(), list(User.t())) :: {atom(), User.t()}
772 def follow_all(follower, followeds) do
774 |> Enum.reject(fn followed -> blocks?(follower, followed) || blocks?(followed, follower) end)
775 |> Enum.each(&follow(follower, &1, :follow_accept))
780 defdelegate following(user), to: FollowingRelationship
782 def follow(%User{} = follower, %User{} = followed, state \\ :follow_accept) do
783 deny_follow_blocked = Config.get([:user, :deny_follow_blocked])
786 followed.deactivated ->
787 {:error, "Could not follow user: #{followed.nickname} is deactivated."}
789 deny_follow_blocked and blocks?(followed, follower) ->
790 {:error, "Could not follow user: #{followed.nickname} blocked you."}
793 FollowingRelationship.follow(follower, followed, state)
795 {:ok, _} = update_follower_count(followed)
798 |> update_following_count()
802 def unfollow(%User{ap_id: ap_id}, %User{ap_id: ap_id}) do
803 {:error, "Not subscribed!"}
806 @spec unfollow(User.t(), User.t()) :: {:ok, User.t(), Activity.t()} | {:error, String.t()}
807 def unfollow(%User{} = follower, %User{} = followed) do
808 case do_unfollow(follower, followed) do
809 {:ok, follower, followed} ->
810 {:ok, follower, Utils.fetch_latest_follow(follower, followed)}
817 @spec do_unfollow(User.t(), User.t()) :: {:ok, User.t(), User.t()} | {:error, String.t()}
818 defp do_unfollow(%User{} = follower, %User{} = followed) do
819 case get_follow_state(follower, followed) do
820 state when state in [:follow_pending, :follow_accept] ->
821 FollowingRelationship.unfollow(follower, followed)
822 {:ok, followed} = update_follower_count(followed)
826 |> update_following_count()
828 {:ok, follower, followed}
831 {:error, "Not subscribed!"}
835 defdelegate following?(follower, followed), to: FollowingRelationship
837 @doc "Returns follow state as Pleroma.FollowingRelationship.State value"
838 def get_follow_state(%User{} = follower, %User{} = following) do
839 following_relationship = FollowingRelationship.get(follower, following)
840 get_follow_state(follower, following, following_relationship)
843 def get_follow_state(
846 following_relationship
848 case {following_relationship, following.local} do
850 case Utils.fetch_latest_follow(follower, following) do
851 %Activity{data: %{"state" => state}} when state in ["pending", "accept"] ->
852 FollowingRelationship.state_to_enum(state)
858 {%{state: state}, _} ->
866 def locked?(%User{} = user) do
871 Repo.get_by(User, id: id)
874 def get_by_ap_id(ap_id) do
875 Repo.get_by(User, ap_id: ap_id)
878 def get_all_by_ap_id(ap_ids) do
879 from(u in __MODULE__,
880 where: u.ap_id in ^ap_ids
885 def get_all_by_ids(ids) do
886 from(u in __MODULE__, where: u.id in ^ids)
890 # This is mostly an SPC migration fix. This guesses the user nickname by taking the last part
891 # of the ap_id and the domain and tries to get that user
892 def get_by_guessed_nickname(ap_id) do
893 domain = URI.parse(ap_id).host
894 name = List.last(String.split(ap_id, "/"))
895 nickname = "#{name}@#{domain}"
897 get_cached_by_nickname(nickname)
900 def set_cache({:ok, user}), do: set_cache(user)
901 def set_cache({:error, err}), do: {:error, err}
903 def set_cache(%User{} = user) do
904 Cachex.put(:user_cache, "ap_id:#{user.ap_id}", user)
905 Cachex.put(:user_cache, "nickname:#{user.nickname}", user)
906 Cachex.put(:user_cache, "friends_ap_ids:#{user.nickname}", get_user_friends_ap_ids(user))
910 def update_and_set_cache(struct, params) do
912 |> update_changeset(params)
913 |> update_and_set_cache()
916 def update_and_set_cache(changeset) do
917 with {:ok, user} <- Repo.update(changeset, stale_error_field: :id) do
922 def get_user_friends_ap_ids(user) do
923 from(u in User.get_friends_query(user), select: u.ap_id)
927 @spec get_cached_user_friends_ap_ids(User.t()) :: [String.t()]
928 def get_cached_user_friends_ap_ids(user) do
929 Cachex.fetch!(:user_cache, "friends_ap_ids:#{user.ap_id}", fn _ ->
930 get_user_friends_ap_ids(user)
934 def invalidate_cache(user) do
935 Cachex.del(:user_cache, "ap_id:#{user.ap_id}")
936 Cachex.del(:user_cache, "nickname:#{user.nickname}")
937 Cachex.del(:user_cache, "friends_ap_ids:#{user.ap_id}")
940 @spec get_cached_by_ap_id(String.t()) :: User.t() | nil
941 def get_cached_by_ap_id(ap_id) do
942 key = "ap_id:#{ap_id}"
944 with {:ok, nil} <- Cachex.get(:user_cache, key),
945 user when not is_nil(user) <- get_by_ap_id(ap_id),
946 {:ok, true} <- Cachex.put(:user_cache, key, user) do
954 def get_cached_by_id(id) do
958 Cachex.fetch!(:user_cache, key, fn _ ->
962 Cachex.put(:user_cache, "ap_id:#{user.ap_id}", user)
963 {:commit, user.ap_id}
969 get_cached_by_ap_id(ap_id)
972 def get_cached_by_nickname(nickname) do
973 key = "nickname:#{nickname}"
975 Cachex.fetch!(:user_cache, key, fn ->
976 case get_or_fetch_by_nickname(nickname) do
977 {:ok, user} -> {:commit, user}
978 {:error, _error} -> {:ignore, nil}
983 def get_cached_by_nickname_or_id(nickname_or_id, opts \\ []) do
984 restrict_to_local = Config.get([:instance, :limit_to_local_content])
987 is_integer(nickname_or_id) or FlakeId.flake_id?(nickname_or_id) ->
988 get_cached_by_id(nickname_or_id) || get_cached_by_nickname(nickname_or_id)
990 restrict_to_local == false or not String.contains?(nickname_or_id, "@") ->
991 get_cached_by_nickname(nickname_or_id)
993 restrict_to_local == :unauthenticated and match?(%User{}, opts[:for]) ->
994 get_cached_by_nickname(nickname_or_id)
1001 @spec get_by_nickname(String.t()) :: User.t() | nil
1002 def get_by_nickname(nickname) do
1003 Repo.get_by(User, nickname: nickname) ||
1004 if Regex.match?(~r(@#{Pleroma.Web.Endpoint.host()})i, nickname) do
1005 Repo.get_by(User, nickname: local_nickname(nickname))
1009 def get_by_email(email), do: Repo.get_by(User, email: email)
1011 def get_by_nickname_or_email(nickname_or_email) do
1012 get_by_nickname(nickname_or_email) || get_by_email(nickname_or_email)
1015 def fetch_by_nickname(nickname), do: ActivityPub.make_user_from_nickname(nickname)
1017 def get_or_fetch_by_nickname(nickname) do
1018 with %User{} = user <- get_by_nickname(nickname) do
1022 with [_nick, _domain] <- String.split(nickname, "@"),
1023 {:ok, user} <- fetch_by_nickname(nickname) do
1026 _e -> {:error, "not found " <> nickname}
1031 @spec get_followers_query(User.t(), pos_integer() | nil) :: Ecto.Query.t()
1032 def get_followers_query(%User{} = user, nil) do
1033 User.Query.build(%{followers: user, deactivated: false})
1036 def get_followers_query(user, page) do
1038 |> get_followers_query(nil)
1039 |> User.Query.paginate(page, 20)
1042 @spec get_followers_query(User.t()) :: Ecto.Query.t()
1043 def get_followers_query(user), do: get_followers_query(user, nil)
1045 @spec get_followers(User.t(), pos_integer() | nil) :: {:ok, list(User.t())}
1046 def get_followers(user, page \\ nil) do
1048 |> get_followers_query(page)
1052 @spec get_external_followers(User.t(), pos_integer() | nil) :: {:ok, list(User.t())}
1053 def get_external_followers(user, page \\ nil) do
1055 |> get_followers_query(page)
1056 |> User.Query.build(%{external: true})
1060 def get_followers_ids(user, page \\ nil) do
1062 |> get_followers_query(page)
1063 |> select([u], u.id)
1067 @spec get_friends_query(User.t(), pos_integer() | nil) :: Ecto.Query.t()
1068 def get_friends_query(%User{} = user, nil) do
1069 User.Query.build(%{friends: user, deactivated: false})
1072 def get_friends_query(user, page) do
1074 |> get_friends_query(nil)
1075 |> User.Query.paginate(page, 20)
1078 @spec get_friends_query(User.t()) :: Ecto.Query.t()
1079 def get_friends_query(user), do: get_friends_query(user, nil)
1081 def get_friends(user, page \\ nil) do
1083 |> get_friends_query(page)
1087 def get_friends_ap_ids(user) do
1089 |> get_friends_query(nil)
1090 |> select([u], u.ap_id)
1094 def get_friends_ids(user, page \\ nil) do
1096 |> get_friends_query(page)
1097 |> select([u], u.id)
1101 defdelegate get_follow_requests(user), to: FollowingRelationship
1103 def increase_note_count(%User{} = user) do
1105 |> where(id: ^user.id)
1106 |> update([u], inc: [note_count: 1])
1108 |> Repo.update_all([])
1110 {1, [user]} -> set_cache(user)
1115 def decrease_note_count(%User{} = user) do
1117 |> where(id: ^user.id)
1120 note_count: fragment("greatest(0, note_count - 1)")
1124 |> Repo.update_all([])
1126 {1, [user]} -> set_cache(user)
1131 def update_note_count(%User{} = user, note_count \\ nil) do
1136 where: fragment("?->>'actor' = ? and ?->>'type' = 'Note'", a.data, ^user.ap_id, a.data),
1142 |> cast(%{note_count: note_count}, [:note_count])
1143 |> update_and_set_cache()
1146 @spec maybe_fetch_follow_information(User.t()) :: User.t()
1147 def maybe_fetch_follow_information(user) do
1148 with {:ok, user} <- fetch_follow_information(user) do
1152 Logger.error("Follower/Following counter update for #{user.ap_id} failed.\n#{inspect(e)}")
1158 def fetch_follow_information(user) do
1159 with {:ok, info} <- ActivityPub.fetch_follow_information_for_user(user) do
1161 |> follow_information_changeset(info)
1162 |> update_and_set_cache()
1166 defp follow_information_changeset(user, params) do
1173 :hide_followers_count,
1178 @spec update_follower_count(User.t()) :: {:ok, User.t()}
1179 def update_follower_count(%User{} = user) do
1180 if user.local or !Config.get([:instance, :external_user_synchronization]) do
1181 follower_count = FollowingRelationship.follower_count(user)
1184 |> follow_information_changeset(%{follower_count: follower_count})
1185 |> update_and_set_cache
1187 {:ok, maybe_fetch_follow_information(user)}
1191 @spec update_following_count(User.t()) :: {:ok, User.t()}
1192 def update_following_count(%User{local: false} = user) do
1193 if Config.get([:instance, :external_user_synchronization]) do
1194 {:ok, maybe_fetch_follow_information(user)}
1200 def update_following_count(%User{local: true} = user) do
1201 following_count = FollowingRelationship.following_count(user)
1204 |> follow_information_changeset(%{following_count: following_count})
1205 |> update_and_set_cache()
1208 def set_unread_conversation_count(%User{local: true} = user) do
1209 unread_query = Participation.unread_conversation_count_for_user(user)
1212 |> join(:inner, [u], p in subquery(unread_query))
1214 set: [unread_conversation_count: p.count]
1216 |> where([u], u.id == ^user.id)
1218 |> Repo.update_all([])
1220 {1, [user]} -> set_cache(user)
1225 def set_unread_conversation_count(user), do: {:ok, user}
1227 def increment_unread_conversation_count(conversation, %User{local: true} = user) do
1229 Participation.unread_conversation_count_for_user(user)
1230 |> where([p], p.conversation_id == ^conversation.id)
1233 |> join(:inner, [u], p in subquery(unread_query))
1235 inc: [unread_conversation_count: 1]
1237 |> where([u], u.id == ^user.id)
1238 |> where([u, p], p.count == 0)
1240 |> Repo.update_all([])
1242 {1, [user]} -> set_cache(user)
1247 def increment_unread_conversation_count(_, user), do: {:ok, user}
1249 @spec get_users_from_set([String.t()], keyword()) :: [User.t()]
1250 def get_users_from_set(ap_ids, opts \\ []) do
1251 local_only = Keyword.get(opts, :local_only, true)
1252 criteria = %{ap_id: ap_ids, deactivated: false}
1253 criteria = if local_only, do: Map.put(criteria, :local, true), else: criteria
1255 User.Query.build(criteria)
1259 @spec get_recipients_from_activity(Activity.t()) :: [User.t()]
1260 def get_recipients_from_activity(%Activity{recipients: to, actor: actor}) do
1263 query = User.Query.build(%{recipients_from_activity: to, local: true, deactivated: false})
1269 @spec mute(User.t(), User.t(), boolean()) ::
1270 {:ok, list(UserRelationship.t())} | {:error, String.t()}
1271 def mute(%User{} = muter, %User{} = mutee, notifications? \\ true) do
1272 add_to_mutes(muter, mutee, notifications?)
1275 def unmute(%User{} = muter, %User{} = mutee) do
1276 remove_from_mutes(muter, mutee)
1279 def subscribe(%User{} = subscriber, %User{} = target) do
1280 deny_follow_blocked = Config.get([:user, :deny_follow_blocked])
1282 if blocks?(target, subscriber) and deny_follow_blocked do
1283 {:error, "Could not subscribe: #{target.nickname} is blocking you"}
1285 # Note: the relationship is inverse: subscriber acts as relationship target
1286 UserRelationship.create_inverse_subscription(target, subscriber)
1290 def subscribe(%User{} = subscriber, %{ap_id: ap_id}) do
1291 with %User{} = subscribee <- get_cached_by_ap_id(ap_id) do
1292 subscribe(subscriber, subscribee)
1296 def unsubscribe(%User{} = unsubscriber, %User{} = target) do
1297 # Note: the relationship is inverse: subscriber acts as relationship target
1298 UserRelationship.delete_inverse_subscription(target, unsubscriber)
1301 def unsubscribe(%User{} = unsubscriber, %{ap_id: ap_id}) do
1302 with %User{} = user <- get_cached_by_ap_id(ap_id) do
1303 unsubscribe(unsubscriber, user)
1307 def block(%User{} = blocker, %User{} = blocked) do
1308 # sever any follow relationships to prevent leaks per activitypub (Pleroma issue #213)
1310 if following?(blocker, blocked) do
1311 {:ok, blocker, _} = unfollow(blocker, blocked)
1317 # clear any requested follows as well
1319 case CommonAPI.reject_follow_request(blocked, blocker) do
1320 {:ok, %User{} = updated_blocked} -> updated_blocked
1324 unsubscribe(blocked, blocker)
1326 unfollowing_blocked = Config.get([:activitypub, :unfollow_blocked], true)
1327 if unfollowing_blocked && following?(blocked, blocker), do: unfollow(blocked, blocker)
1329 {:ok, blocker} = update_follower_count(blocker)
1330 {:ok, blocker, _} = Participation.mark_all_as_read(blocker, blocked)
1331 add_to_block(blocker, blocked)
1334 # helper to handle the block given only an actor's AP id
1335 def block(%User{} = blocker, %{ap_id: ap_id}) do
1336 block(blocker, get_cached_by_ap_id(ap_id))
1339 def unblock(%User{} = blocker, %User{} = blocked) do
1340 remove_from_block(blocker, blocked)
1343 # helper to handle the block given only an actor's AP id
1344 def unblock(%User{} = blocker, %{ap_id: ap_id}) do
1345 unblock(blocker, get_cached_by_ap_id(ap_id))
1348 def mutes?(nil, _), do: false
1349 def mutes?(%User{} = user, %User{} = target), do: mutes_user?(user, target)
1351 def mutes_user?(%User{} = user, %User{} = target) do
1352 UserRelationship.mute_exists?(user, target)
1355 @spec muted_notifications?(User.t() | nil, User.t() | map()) :: boolean()
1356 def muted_notifications?(nil, _), do: false
1358 def muted_notifications?(%User{} = user, %User{} = target),
1359 do: UserRelationship.notification_mute_exists?(user, target)
1361 def blocks?(nil, _), do: false
1363 def blocks?(%User{} = user, %User{} = target) do
1364 blocks_user?(user, target) ||
1365 (blocks_domain?(user, target) and not User.following?(user, target))
1368 def blocks_user?(%User{} = user, %User{} = target) do
1369 UserRelationship.block_exists?(user, target)
1372 def blocks_user?(_, _), do: false
1374 def blocks_domain?(%User{} = user, %User{} = target) do
1375 domain_blocks = Pleroma.Web.ActivityPub.MRF.subdomains_regex(user.domain_blocks)
1376 %{host: host} = URI.parse(target.ap_id)
1377 Pleroma.Web.ActivityPub.MRF.subdomain_match?(domain_blocks, host)
1380 def blocks_domain?(_, _), do: false
1382 def subscribed_to?(%User{} = user, %User{} = target) do
1383 # Note: the relationship is inverse: subscriber acts as relationship target
1384 UserRelationship.inverse_subscription_exists?(target, user)
1387 def subscribed_to?(%User{} = user, %{ap_id: ap_id}) do
1388 with %User{} = target <- get_cached_by_ap_id(ap_id) do
1389 subscribed_to?(user, target)
1394 Returns map of outgoing (blocked, muted etc.) relationships' user AP IDs by relation type.
1395 E.g. `outgoing_relationships_ap_ids(user, [:block])` -> `%{block: ["https://some.site/users/userapid"]}`
1397 @spec outgoing_relationships_ap_ids(User.t(), list(atom())) :: %{atom() => list(String.t())}
1398 def outgoing_relationships_ap_ids(_user, []), do: %{}
1400 def outgoing_relationships_ap_ids(nil, _relationship_types), do: %{}
1402 def outgoing_relationships_ap_ids(%User{} = user, relationship_types)
1403 when is_list(relationship_types) do
1406 |> assoc(:outgoing_relationships)
1407 |> join(:inner, [user_rel], u in assoc(user_rel, :target))
1408 |> where([user_rel, u], user_rel.relationship_type in ^relationship_types)
1409 |> select([user_rel, u], [user_rel.relationship_type, fragment("array_agg(?)", u.ap_id)])
1410 |> group_by([user_rel, u], user_rel.relationship_type)
1412 |> Enum.into(%{}, fn [k, v] -> {k, v} end)
1417 fn rel_type -> {rel_type, db_result[rel_type] || []} end
1421 def incoming_relationships_ungrouped_ap_ids(user, relationship_types, ap_ids \\ nil)
1423 def incoming_relationships_ungrouped_ap_ids(_user, [], _ap_ids), do: []
1425 def incoming_relationships_ungrouped_ap_ids(nil, _relationship_types, _ap_ids), do: []
1427 def incoming_relationships_ungrouped_ap_ids(%User{} = user, relationship_types, ap_ids)
1428 when is_list(relationship_types) do
1430 |> assoc(:incoming_relationships)
1431 |> join(:inner, [user_rel], u in assoc(user_rel, :source))
1432 |> where([user_rel, u], user_rel.relationship_type in ^relationship_types)
1433 |> maybe_filter_on_ap_id(ap_ids)
1434 |> select([user_rel, u], u.ap_id)
1439 defp maybe_filter_on_ap_id(query, ap_ids) when is_list(ap_ids) do
1440 where(query, [user_rel, u], u.ap_id in ^ap_ids)
1443 defp maybe_filter_on_ap_id(query, _ap_ids), do: query
1445 def deactivate_async(user, status \\ true) do
1446 BackgroundWorker.enqueue("deactivate_user", %{"user_id" => user.id, "status" => status})
1449 def deactivate(user, status \\ true)
1451 def deactivate(users, status) when is_list(users) do
1452 Repo.transaction(fn ->
1453 for user <- users, do: deactivate(user, status)
1457 def deactivate(%User{} = user, status) do
1458 with {:ok, user} <- set_activation_status(user, status) do
1461 |> Enum.filter(& &1.local)
1462 |> Enum.each(&set_cache(update_following_count(&1)))
1464 # Only update local user counts, remote will be update during the next pull.
1467 |> Enum.filter(& &1.local)
1468 |> Enum.each(&do_unfollow(user, &1))
1474 def update_notification_settings(%User{} = user, settings) do
1476 |> cast(%{notification_settings: settings}, [])
1477 |> cast_embed(:notification_settings)
1478 |> validate_required([:notification_settings])
1479 |> update_and_set_cache()
1482 def delete(users) when is_list(users) do
1483 for user <- users, do: delete(user)
1486 def delete(%User{} = user) do
1487 BackgroundWorker.enqueue("delete_user", %{"user_id" => user.id})
1490 defp delete_and_invalidate_cache(%User{} = user) do
1491 invalidate_cache(user)
1495 defp delete_or_deactivate(%User{local: false} = user), do: delete_and_invalidate_cache(user)
1497 defp delete_or_deactivate(%User{local: true} = user) do
1498 status = account_status(user)
1500 if status == :confirmation_pending do
1501 delete_and_invalidate_cache(user)
1504 |> change(%{deactivated: true, email: nil})
1505 |> update_and_set_cache()
1509 def perform(:force_password_reset, user), do: force_password_reset(user)
1511 @spec perform(atom(), User.t()) :: {:ok, User.t()}
1512 def perform(:delete, %User{} = user) do
1513 # Remove all relationships
1516 |> Enum.each(fn follower ->
1517 ActivityPub.unfollow(follower, user)
1518 unfollow(follower, user)
1523 |> Enum.each(fn followed ->
1524 ActivityPub.unfollow(user, followed)
1525 unfollow(user, followed)
1528 delete_user_activities(user)
1529 delete_notifications_from_user_activities(user)
1531 delete_outgoing_pending_follow_requests(user)
1533 delete_or_deactivate(user)
1536 def perform(:deactivate_async, user, status), do: deactivate(user, status)
1538 @spec perform(atom(), User.t(), list()) :: list() | {:error, any()}
1539 def perform(:blocks_import, %User{} = blocker, blocked_identifiers)
1540 when is_list(blocked_identifiers) do
1542 blocked_identifiers,
1543 fn blocked_identifier ->
1544 with {:ok, %User{} = blocked} <- get_or_fetch(blocked_identifier),
1545 {:ok, _block} <- CommonAPI.block(blocker, blocked) do
1549 Logger.debug("blocks_import failed for #{blocked_identifier} with: #{inspect(err)}")
1556 def perform(:follow_import, %User{} = follower, followed_identifiers)
1557 when is_list(followed_identifiers) do
1559 followed_identifiers,
1560 fn followed_identifier ->
1561 with {:ok, %User{} = followed} <- get_or_fetch(followed_identifier),
1562 {:ok, follower} <- maybe_direct_follow(follower, followed),
1563 {:ok, _, _, _} <- CommonAPI.follow(follower, followed) do
1567 Logger.debug("follow_import failed for #{followed_identifier} with: #{inspect(err)}")
1574 @spec external_users_query() :: Ecto.Query.t()
1575 def external_users_query do
1583 @spec external_users(keyword()) :: [User.t()]
1584 def external_users(opts \\ []) do
1586 external_users_query()
1587 |> select([u], struct(u, [:id, :ap_id]))
1591 do: where(query, [u], u.id > ^opts[:max_id]),
1596 do: limit(query, ^opts[:limit]),
1602 def blocks_import(%User{} = blocker, blocked_identifiers) when is_list(blocked_identifiers) do
1603 BackgroundWorker.enqueue("blocks_import", %{
1604 "blocker_id" => blocker.id,
1605 "blocked_identifiers" => blocked_identifiers
1609 def follow_import(%User{} = follower, followed_identifiers)
1610 when is_list(followed_identifiers) do
1611 BackgroundWorker.enqueue("follow_import", %{
1612 "follower_id" => follower.id,
1613 "followed_identifiers" => followed_identifiers
1617 def delete_notifications_from_user_activities(%User{ap_id: ap_id}) do
1619 |> join(:inner, [n], activity in assoc(n, :activity))
1620 |> where([n, a], fragment("? = ?", a.actor, ^ap_id))
1621 |> Repo.delete_all()
1624 def delete_user_activities(%User{ap_id: ap_id} = user) do
1626 |> Activity.Queries.by_actor()
1627 |> RepoStreamer.chunk_stream(50)
1628 |> Stream.each(fn activities ->
1629 Enum.each(activities, fn activity -> delete_activity(activity, user) end)
1634 defp delete_activity(%{data: %{"type" => "Create", "object" => object}} = activity, user) do
1635 with {_, %Object{}} <- {:find_object, Object.get_by_ap_id(object)},
1636 {:ok, delete_data, _} <- Builder.delete(user, object) do
1637 Pipeline.common_pipeline(delete_data, local: user.local)
1639 {:find_object, nil} ->
1640 # We have the create activity, but not the object, it was probably pruned.
1641 # Insert a tombstone and try again
1642 with {:ok, tombstone_data, _} <- Builder.tombstone(user.ap_id, object),
1643 {:ok, _tombstone} <- Object.create(tombstone_data) do
1644 delete_activity(activity, user)
1648 Logger.error("Could not delete #{object} created by #{activity.data["ap_id"]}")
1649 Logger.error("Error: #{inspect(e)}")
1653 defp delete_activity(%{data: %{"type" => type}} = activity, user)
1654 when type in ["Like", "Announce"] do
1655 {:ok, undo, _} = Builder.undo(user, activity)
1656 Pipeline.common_pipeline(undo, local: user.local)
1659 defp delete_activity(_activity, _user), do: "Doing nothing"
1661 defp delete_outgoing_pending_follow_requests(user) do
1663 |> FollowingRelationship.outgoing_pending_follow_requests_query()
1664 |> Repo.delete_all()
1667 def html_filter_policy(%User{no_rich_text: true}) do
1668 Pleroma.HTML.Scrubber.TwitterText
1671 def html_filter_policy(_), do: Config.get([:markup, :scrub_policy])
1673 def fetch_by_ap_id(ap_id), do: ActivityPub.make_user_from_ap_id(ap_id)
1675 def get_or_fetch_by_ap_id(ap_id) do
1676 cached_user = get_cached_by_ap_id(ap_id)
1678 maybe_fetched_user = needs_update?(cached_user) && fetch_by_ap_id(ap_id)
1680 case {cached_user, maybe_fetched_user} do
1681 {_, {:ok, %User{} = user}} ->
1684 {%User{} = user, _} ->
1688 {:error, :not_found}
1693 Creates an internal service actor by URI if missing.
1694 Optionally takes nickname for addressing.
1696 @spec get_or_create_service_actor_by_ap_id(String.t(), String.t()) :: User.t() | nil
1697 def get_or_create_service_actor_by_ap_id(uri, nickname) do
1699 case get_cached_by_ap_id(uri) do
1701 with {:error, %{errors: errors}} <- create_service_actor(uri, nickname) do
1702 Logger.error("Cannot create service actor: #{uri}/.\n#{inspect(errors)}")
1706 %User{invisible: false} = user ->
1716 @spec set_invisible(User.t()) :: {:ok, User.t()}
1717 defp set_invisible(user) do
1719 |> change(%{invisible: true})
1720 |> update_and_set_cache()
1723 @spec create_service_actor(String.t(), String.t()) ::
1724 {:ok, User.t()} | {:error, Ecto.Changeset.t()}
1725 defp create_service_actor(uri, nickname) do
1731 follower_address: uri <> "/followers"
1734 |> unique_constraint(:nickname)
1739 def public_key(%{public_key: public_key_pem}) when is_binary(public_key_pem) do
1742 |> :public_key.pem_decode()
1744 |> :public_key.pem_entry_decode()
1749 def public_key(_), do: {:error, "key not found"}
1751 def get_public_key_for_ap_id(ap_id) do
1752 with {:ok, %User{} = user} <- get_or_fetch_by_ap_id(ap_id),
1753 {:ok, public_key} <- public_key(user) do
1760 def ap_enabled?(%User{local: true}), do: true
1761 def ap_enabled?(%User{ap_enabled: ap_enabled}), do: ap_enabled
1762 def ap_enabled?(_), do: false
1764 @doc "Gets or fetch a user by uri or nickname."
1765 @spec get_or_fetch(String.t()) :: {:ok, User.t()} | {:error, String.t()}
1766 def get_or_fetch("http" <> _host = uri), do: get_or_fetch_by_ap_id(uri)
1767 def get_or_fetch(nickname), do: get_or_fetch_by_nickname(nickname)
1769 # wait a period of time and return newest version of the User structs
1770 # this is because we have synchronous follow APIs and need to simulate them
1771 # with an async handshake
1772 def wait_and_refresh(_, %User{local: true} = a, %User{local: true} = b) do
1773 with %User{} = a <- get_cached_by_id(a.id),
1774 %User{} = b <- get_cached_by_id(b.id) do
1781 def wait_and_refresh(timeout, %User{} = a, %User{} = b) do
1782 with :ok <- :timer.sleep(timeout),
1783 %User{} = a <- get_cached_by_id(a.id),
1784 %User{} = b <- get_cached_by_id(b.id) do
1791 def parse_bio(bio) when is_binary(bio) and bio != "" do
1793 |> CommonUtils.format_input("text/plain", mentions_format: :full)
1797 def parse_bio(_), do: ""
1799 def parse_bio(bio, user) when is_binary(bio) and bio != "" do
1800 # TODO: get profile URLs other than user.ap_id
1801 profile_urls = [user.ap_id]
1804 |> CommonUtils.format_input("text/plain",
1805 mentions_format: :full,
1806 rel: &RelMe.maybe_put_rel_me(&1, profile_urls)
1811 def parse_bio(_, _), do: ""
1813 def tag(user_identifiers, tags) when is_list(user_identifiers) do
1814 Repo.transaction(fn ->
1815 for user_identifier <- user_identifiers, do: tag(user_identifier, tags)
1819 def tag(nickname, tags) when is_binary(nickname),
1820 do: tag(get_by_nickname(nickname), tags)
1822 def tag(%User{} = user, tags),
1823 do: update_tags(user, Enum.uniq((user.tags || []) ++ normalize_tags(tags)))
1825 def untag(user_identifiers, tags) when is_list(user_identifiers) do
1826 Repo.transaction(fn ->
1827 for user_identifier <- user_identifiers, do: untag(user_identifier, tags)
1831 def untag(nickname, tags) when is_binary(nickname),
1832 do: untag(get_by_nickname(nickname), tags)
1834 def untag(%User{} = user, tags),
1835 do: update_tags(user, (user.tags || []) -- normalize_tags(tags))
1837 defp update_tags(%User{} = user, new_tags) do
1838 {:ok, updated_user} =
1840 |> change(%{tags: new_tags})
1841 |> update_and_set_cache()
1846 defp normalize_tags(tags) do
1849 |> Enum.map(&String.downcase/1)
1852 defp local_nickname_regex do
1853 if Config.get([:instance, :extended_nickname_format]) do
1854 @extended_local_nickname_regex
1856 @strict_local_nickname_regex
1860 def local_nickname(nickname_or_mention) do
1863 |> String.split("@")
1867 def full_nickname(nickname_or_mention),
1868 do: String.trim_leading(nickname_or_mention, "@")
1870 def error_user(ap_id) do
1874 nickname: "erroruser@example.com",
1875 inserted_at: NaiveDateTime.utc_now()
1879 @spec all_superusers() :: [User.t()]
1880 def all_superusers do
1881 User.Query.build(%{super_users: true, local: true, deactivated: false})
1885 def muting_reblogs?(%User{} = user, %User{} = target) do
1886 UserRelationship.reblog_mute_exists?(user, target)
1889 def showing_reblogs?(%User{} = user, %User{} = target) do
1890 not muting_reblogs?(user, target)
1894 The function returns a query to get users with no activity for given interval of days.
1895 Inactive users are those who didn't read any notification, or had any activity where
1896 the user is the activity's actor, during `inactivity_threshold` days.
1897 Deactivated users will not appear in this list.
1901 iex> Pleroma.User.list_inactive_users()
1904 @spec list_inactive_users_query(integer()) :: Ecto.Query.t()
1905 def list_inactive_users_query(inactivity_threshold \\ 7) do
1906 negative_inactivity_threshold = -inactivity_threshold
1907 now = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
1908 # Subqueries are not supported in `where` clauses, join gets too complicated.
1909 has_read_notifications =
1910 from(n in Pleroma.Notification,
1911 where: n.seen == true,
1913 having: max(n.updated_at) > datetime_add(^now, ^negative_inactivity_threshold, "day"),
1916 |> Pleroma.Repo.all()
1918 from(u in Pleroma.User,
1919 left_join: a in Pleroma.Activity,
1920 on: u.ap_id == a.actor,
1921 where: not is_nil(u.nickname),
1922 where: u.deactivated != ^true,
1923 where: u.id not in ^has_read_notifications,
1926 max(a.inserted_at) < datetime_add(^now, ^negative_inactivity_threshold, "day") or
1927 is_nil(max(a.inserted_at))
1932 Enable or disable email notifications for user
1936 iex> Pleroma.User.switch_email_notifications(Pleroma.User{email_notifications: %{"digest" => false}}, "digest", true)
1937 Pleroma.User{email_notifications: %{"digest" => true}}
1939 iex> Pleroma.User.switch_email_notifications(Pleroma.User{email_notifications: %{"digest" => true}}, "digest", false)
1940 Pleroma.User{email_notifications: %{"digest" => false}}
1942 @spec switch_email_notifications(t(), String.t(), boolean()) ::
1943 {:ok, t()} | {:error, Ecto.Changeset.t()}
1944 def switch_email_notifications(user, type, status) do
1945 User.update_email_notifications(user, %{type => status})
1949 Set `last_digest_emailed_at` value for the user to current time
1951 @spec touch_last_digest_emailed_at(t()) :: t()
1952 def touch_last_digest_emailed_at(user) do
1953 now = NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
1955 {:ok, updated_user} =
1957 |> change(%{last_digest_emailed_at: now})
1958 |> update_and_set_cache()
1963 @spec toggle_confirmation(User.t()) :: {:ok, User.t()} | {:error, Changeset.t()}
1964 def toggle_confirmation(%User{} = user) do
1966 |> confirmation_changeset(need_confirmation: !user.confirmation_pending)
1967 |> update_and_set_cache()
1970 @spec toggle_confirmation([User.t()]) :: [{:ok, User.t()} | {:error, Changeset.t()}]
1971 def toggle_confirmation(users) do
1972 Enum.map(users, &toggle_confirmation/1)
1975 def get_mascot(%{mascot: %{} = mascot}) when not is_nil(mascot) do
1979 def get_mascot(%{mascot: mascot}) when is_nil(mascot) do
1980 # use instance-default
1981 config = Config.get([:assets, :mascots])
1982 default_mascot = Config.get([:assets, :default_mascot])
1983 mascot = Keyword.get(config, default_mascot)
1986 "id" => "default-mascot",
1987 "url" => mascot[:url],
1988 "preview_url" => mascot[:url],
1990 "mime_type" => mascot[:mime_type]
1995 def ensure_keys_present(%{keys: keys} = user) when not is_nil(keys), do: {:ok, user}
1997 def ensure_keys_present(%User{} = user) do
1998 with {:ok, pem} <- Keys.generate_rsa_pem() do
2000 |> cast(%{keys: pem}, [:keys])
2001 |> validate_required([:keys])
2002 |> update_and_set_cache()
2006 def get_ap_ids_by_nicknames(nicknames) do
2008 where: u.nickname in ^nicknames,
2014 defdelegate search(query, opts \\ []), to: User.Search
2016 defp put_password_hash(
2017 %Ecto.Changeset{valid?: true, changes: %{password: password}} = changeset
2019 change(changeset, password_hash: Pbkdf2.hash_pwd_salt(password))
2022 defp put_password_hash(changeset), do: changeset
2024 def is_internal_user?(%User{nickname: nil}), do: true
2025 def is_internal_user?(%User{local: true, nickname: "internal." <> _}), do: true
2026 def is_internal_user?(_), do: false
2028 # A hack because user delete activities have a fake id for whatever reason
2029 # TODO: Get rid of this
2030 def get_delivered_users_by_object_id("pleroma:fake_object_id"), do: []
2032 def get_delivered_users_by_object_id(object_id) do
2034 inner_join: delivery in assoc(u, :deliveries),
2035 where: delivery.object_id == ^object_id
2040 def change_email(user, email) do
2042 |> cast(%{email: email}, [:email])
2043 |> validate_required([:email])
2044 |> unique_constraint(:email)
2045 |> validate_format(:email, @email_regex)
2046 |> update_and_set_cache()
2049 # Internal function; public one is `deactivate/2`
2050 defp set_activation_status(user, deactivated) do
2052 |> cast(%{deactivated: deactivated}, [:deactivated])
2053 |> update_and_set_cache()
2056 def update_banner(user, banner) do
2058 |> cast(%{banner: banner}, [:banner])
2059 |> update_and_set_cache()
2062 def update_background(user, background) do
2064 |> cast(%{background: background}, [:background])
2065 |> update_and_set_cache()
2068 def roles(%{is_moderator: is_moderator, is_admin: is_admin}) do
2071 moderator: is_moderator
2075 def validate_fields(changeset, remote? \\ false) do
2076 limit_name = if remote?, do: :max_remote_account_fields, else: :max_account_fields
2077 limit = Config.get([:instance, limit_name], 0)
2080 |> validate_length(:fields, max: limit)
2081 |> validate_change(:fields, fn :fields, fields ->
2082 if Enum.all?(fields, &valid_field?/1) do
2090 defp valid_field?(%{"name" => name, "value" => value}) do
2091 name_limit = Config.get([:instance, :account_field_name_length], 255)
2092 value_limit = Config.get([:instance, :account_field_value_length], 255)
2094 is_binary(name) && is_binary(value) && String.length(name) <= name_limit &&
2095 String.length(value) <= value_limit
2098 defp valid_field?(_), do: false
2100 defp truncate_field(%{"name" => name, "value" => value}) do
2102 String.split_at(name, Config.get([:instance, :account_field_name_length], 255))
2105 String.split_at(value, Config.get([:instance, :account_field_value_length], 255))
2107 %{"name" => name, "value" => value}
2110 def admin_api_update(user, params) do
2117 |> update_and_set_cache()
2120 @doc "Signs user out of all applications"
2121 def global_sign_out(user) do
2122 OAuth.Authorization.delete_user_authorizations(user)
2123 OAuth.Token.delete_user_tokens(user)
2126 def mascot_update(user, url) do
2128 |> cast(%{mascot: url}, [:mascot])
2129 |> validate_required([:mascot])
2130 |> update_and_set_cache()
2133 def mastodon_settings_update(user, settings) do
2135 |> cast(%{mastofe_settings: settings}, [:mastofe_settings])
2136 |> validate_required([:mastofe_settings])
2137 |> update_and_set_cache()
2140 @spec confirmation_changeset(User.t(), keyword()) :: Changeset.t()
2141 def confirmation_changeset(user, need_confirmation: need_confirmation?) do
2143 if need_confirmation? do
2145 confirmation_pending: true,
2146 confirmation_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64()
2150 confirmation_pending: false,
2151 confirmation_token: nil
2155 cast(user, params, [:confirmation_pending, :confirmation_token])
2158 @spec approval_changeset(User.t(), keyword()) :: Changeset.t()
2159 def approval_changeset(user, need_approval: need_approval?) do
2160 params = if need_approval?, do: %{approval_pending: true}, else: %{approval_pending: false}
2161 cast(user, params, [:approval_pending])
2164 def add_pinnned_activity(user, %Pleroma.Activity{id: id}) do
2165 if id not in user.pinned_activities do
2166 max_pinned_statuses = Config.get([:instance, :max_pinned_statuses], 0)
2167 params = %{pinned_activities: user.pinned_activities ++ [id]}
2170 |> cast(params, [:pinned_activities])
2171 |> validate_length(:pinned_activities,
2172 max: max_pinned_statuses,
2173 message: "You have already pinned the maximum number of statuses"
2178 |> update_and_set_cache()
2181 def remove_pinnned_activity(user, %Pleroma.Activity{id: id}) do
2182 params = %{pinned_activities: List.delete(user.pinned_activities, id)}
2185 |> cast(params, [:pinned_activities])
2186 |> update_and_set_cache()
2189 def update_email_notifications(user, settings) do
2190 email_notifications =
2191 user.email_notifications
2192 |> Map.merge(settings)
2193 |> Map.take(["digest"])
2195 params = %{email_notifications: email_notifications}
2196 fields = [:email_notifications]
2199 |> cast(params, fields)
2200 |> validate_required(fields)
2201 |> update_and_set_cache()
2204 defp set_domain_blocks(user, domain_blocks) do
2205 params = %{domain_blocks: domain_blocks}
2208 |> cast(params, [:domain_blocks])
2209 |> validate_required([:domain_blocks])
2210 |> update_and_set_cache()
2213 def block_domain(user, domain_blocked) do
2214 set_domain_blocks(user, Enum.uniq([domain_blocked | user.domain_blocks]))
2217 def unblock_domain(user, domain_blocked) do
2218 set_domain_blocks(user, List.delete(user.domain_blocks, domain_blocked))
2221 @spec add_to_block(User.t(), User.t()) ::
2222 {:ok, UserRelationship.t()} | {:error, Ecto.Changeset.t()}
2223 defp add_to_block(%User{} = user, %User{} = blocked) do
2224 UserRelationship.create_block(user, blocked)
2227 @spec add_to_block(User.t(), User.t()) ::
2228 {:ok, UserRelationship.t()} | {:ok, nil} | {:error, Ecto.Changeset.t()}
2229 defp remove_from_block(%User{} = user, %User{} = blocked) do
2230 UserRelationship.delete_block(user, blocked)
2233 defp add_to_mutes(%User{} = user, %User{} = muted_user, notifications?) do
2234 with {:ok, user_mute} <- UserRelationship.create_mute(user, muted_user),
2235 {:ok, user_notification_mute} <-
2236 (notifications? && UserRelationship.create_notification_mute(user, muted_user)) ||
2238 {:ok, Enum.filter([user_mute, user_notification_mute], & &1)}
2242 defp remove_from_mutes(user, %User{} = muted_user) do
2243 with {:ok, user_mute} <- UserRelationship.delete_mute(user, muted_user),
2244 {:ok, user_notification_mute} <-
2245 UserRelationship.delete_notification_mute(user, muted_user) do
2246 {:ok, [user_mute, user_notification_mute]}
2250 def set_invisible(user, invisible) do
2251 params = %{invisible: invisible}
2254 |> cast(params, [:invisible])
2255 |> validate_required([:invisible])
2256 |> update_and_set_cache()
2259 def sanitize_html(%User{} = user) do
2260 sanitize_html(user, nil)
2263 # User data that mastodon isn't filtering (treated as plaintext):
2266 def sanitize_html(%User{} = user, filter) do
2268 Enum.map(user.fields, fn %{"name" => name, "value" => value} ->
2271 "value" => HTML.filter_tags(value, Pleroma.HTML.Scrubber.LinksOnly)
2276 |> Map.put(:bio, HTML.filter_tags(user.bio, filter))
2277 |> Map.put(:fields, fields)