1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.User do
13 alias Pleroma.Activity
15 alias Pleroma.Notification
17 alias Pleroma.Registration
19 alias Pleroma.RepoStreamer
22 alias Pleroma.Web.ActivityPub.ActivityPub
23 alias Pleroma.Web.ActivityPub.Utils
24 alias Pleroma.Web.CommonAPI.Utils, as: CommonUtils
25 alias Pleroma.Web.OAuth
26 alias Pleroma.Web.OStatus
27 alias Pleroma.Web.RelMe
28 alias Pleroma.Web.Websub
32 @type t :: %__MODULE__{}
34 @primary_key {:id, Pleroma.FlakeId, autogenerate: true}
36 # credo:disable-for-next-line Credo.Check.Readability.MaxLineLength
37 @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])?)*$/
39 @strict_local_nickname_regex ~r/^[a-zA-Z\d]+$/
40 @extended_local_nickname_regex ~r/^[a-zA-Z\d_-]+$/
44 field(:email, :string)
46 field(:nickname, :string)
47 field(:password_hash, :string)
48 field(:password, :string, virtual: true)
49 field(:password_confirmation, :string, virtual: true)
50 field(:following, {:array, :string}, default: [])
51 field(:ap_id, :string)
53 field(:local, :boolean, default: true)
54 field(:follower_address, :string)
55 field(:following_address, :string)
56 field(:search_rank, :float, virtual: true)
57 field(:search_type, :integer, virtual: true)
58 field(:tags, {:array, :string}, default: [])
59 field(:last_refreshed_at, :naive_datetime_usec)
60 has_many(:notifications, Notification)
61 has_many(:registrations, Registration)
62 embeds_one(:info, User.Info)
67 def auth_active?(%User{info: %User.Info{confirmation_pending: true}}),
68 do: !Pleroma.Config.get([:instance, :account_activation_required])
70 def auth_active?(%User{}), do: true
72 def visible_for?(user, for_user \\ nil)
74 def visible_for?(%User{id: user_id}, %User{id: for_id}) when user_id == for_id, do: true
76 def visible_for?(%User{} = user, for_user) do
77 auth_active?(user) || superuser?(for_user)
80 def visible_for?(_, _), do: false
82 def superuser?(%User{local: true, info: %User.Info{is_admin: true}}), do: true
83 def superuser?(%User{local: true, info: %User.Info{is_moderator: true}}), do: true
84 def superuser?(_), do: false
86 def avatar_url(user, options \\ []) do
88 %{"url" => [%{"href" => href} | _]} -> href
89 _ -> !options[:no_default] && "#{Web.base_url()}/images/avi.png"
93 def banner_url(user, options \\ []) do
94 case user.info.banner do
95 %{"url" => [%{"href" => href} | _]} -> href
96 _ -> !options[:no_default] && "#{Web.base_url()}/images/banner.png"
100 def profile_url(%User{info: %{source_data: %{"url" => url}}}), do: url
101 def profile_url(%User{ap_id: ap_id}), do: ap_id
102 def profile_url(_), do: nil
104 def ap_id(%User{nickname: nickname}) do
105 "#{Web.base_url()}/users/#{nickname}"
108 def ap_followers(%User{follower_address: fa}) when is_binary(fa), do: fa
109 def ap_followers(%User{} = user), do: "#{ap_id(user)}/followers"
111 @spec ap_following(User.t()) :: Sring.t()
112 def ap_following(%User{following_address: fa}) when is_binary(fa), do: fa
113 def ap_following(%User{} = user), do: "#{ap_id(user)}/following"
115 def user_info(%User{} = user, args \\ %{}) do
117 if args[:following_count], do: args[:following_count], else: following_count(user)
120 if args[:follower_count], do: args[:follower_count], else: user.info.follower_count
123 note_count: user.info.note_count,
124 locked: user.info.locked,
125 confirmation_pending: user.info.confirmation_pending,
126 default_scope: user.info.default_scope
128 |> Map.put(:following_count, following_count)
129 |> Map.put(:follower_count, follower_count)
132 def set_info_cache(user, args) do
133 Cachex.put(:user_cache, "user_info:#{user.id}", user_info(user, args))
136 @spec restrict_deactivated(Ecto.Query.t()) :: Ecto.Query.t()
137 def restrict_deactivated(query) do
139 where: not fragment("? \\? 'deactivated' AND ?->'deactivated' @> 'true'", u.info, u.info)
143 def following_count(%User{following: []}), do: 0
145 def following_count(%User{} = user) do
147 |> get_friends_query()
148 |> Repo.aggregate(:count, :id)
151 def remote_user_creation(params) do
154 |> Map.put(:info, params[:info] || %{})
156 info_cng = User.Info.remote_user_creation(%User.Info{}, params[:info])
160 |> cast(params, [:bio, :name, :ap_id, :nickname, :avatar])
161 |> validate_required([:name, :ap_id])
162 |> unique_constraint(:nickname)
163 |> validate_format(:nickname, @email_regex)
164 |> validate_length(:bio, max: 5000)
165 |> validate_length(:name, max: 100)
166 |> put_change(:local, false)
167 |> put_embed(:info, info_cng)
170 case info_cng.changes[:source_data] do
171 %{"followers" => followers, "following" => following} ->
173 |> put_change(:follower_address, followers)
174 |> put_change(:following_address, following)
177 followers = User.ap_followers(%User{nickname: changes.changes[:nickname]})
180 |> put_change(:follower_address, followers)
187 def update_changeset(struct, params \\ %{}) do
189 |> cast(params, [:bio, :name, :avatar, :following])
190 |> unique_constraint(:nickname)
191 |> validate_format(:nickname, local_nickname_regex())
192 |> validate_length(:bio, max: 5000)
193 |> validate_length(:name, min: 1, max: 100)
196 def upgrade_changeset(struct, params \\ %{}) do
199 |> Map.put(:last_refreshed_at, NaiveDateTime.utc_now())
203 |> User.Info.user_upgrade(params[:info])
214 |> unique_constraint(:nickname)
215 |> validate_format(:nickname, local_nickname_regex())
216 |> validate_length(:bio, max: 5000)
217 |> validate_length(:name, max: 100)
218 |> put_embed(:info, info_cng)
221 def password_update_changeset(struct, params) do
223 |> cast(params, [:password, :password_confirmation])
224 |> validate_required([:password, :password_confirmation])
225 |> validate_confirmation(:password)
229 def reset_password(%User{id: user_id} = user, data) do
232 |> Multi.update(:user, password_update_changeset(user, data))
233 |> Multi.delete_all(:tokens, OAuth.Token.Query.get_by_user(user_id))
234 |> Multi.delete_all(:auth, OAuth.Authorization.delete_by_user_query(user))
236 case Repo.transaction(multi) do
237 {:ok, %{user: user} = _} -> set_cache(user)
238 {:error, _, changeset, _} -> {:error, changeset}
242 def register_changeset(struct, params \\ %{}, opts \\ []) do
244 if is_nil(opts[:need_confirmation]) do
245 Pleroma.Config.get([:instance, :account_activation_required])
247 opts[:need_confirmation]
251 User.Info.confirmation_changeset(%User.Info{}, need_confirmation: need_confirmation?)
255 |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
256 |> validate_required([:name, :nickname, :password, :password_confirmation])
257 |> validate_confirmation(:password)
258 |> unique_constraint(:email)
259 |> unique_constraint(:nickname)
260 |> validate_exclusion(:nickname, Pleroma.Config.get([User, :restricted_nicknames]))
261 |> validate_format(:nickname, local_nickname_regex())
262 |> validate_format(:email, @email_regex)
263 |> validate_length(:bio, max: 1000)
264 |> validate_length(:name, min: 1, max: 100)
265 |> put_change(:info, info_change)
268 if opts[:external] do
271 validate_required(changeset, [:email])
274 if changeset.valid? do
275 ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]})
276 followers = User.ap_followers(%User{nickname: changeset.changes[:nickname]})
280 |> put_change(:ap_id, ap_id)
281 |> unique_constraint(:ap_id)
282 |> put_change(:following, [followers])
283 |> put_change(:follower_address, followers)
289 defp autofollow_users(user) do
290 candidates = Pleroma.Config.get([:instance, :autofollowed_nicknames])
293 User.Query.build(%{nickname: candidates, local: true, deactivated: false})
296 follow_all(user, autofollowed_users)
299 @doc "Inserts provided changeset, performs post-registration actions (confirmation email sending etc.)"
300 def register(%Ecto.Changeset{} = changeset) do
301 with {:ok, user} <- Repo.insert(changeset),
302 {:ok, user} <- autofollow_users(user),
303 {:ok, user} <- set_cache(user),
304 {:ok, _} <- User.WelcomeMessage.post_welcome_message_to_user(user),
305 {:ok, _} <- try_send_confirmation_email(user) do
310 def try_send_confirmation_email(%User{} = user) do
311 if user.info.confirmation_pending &&
312 Pleroma.Config.get([:instance, :account_activation_required]) do
314 |> Pleroma.Emails.UserEmail.account_confirmation_email()
315 |> Pleroma.Emails.Mailer.deliver_async()
323 def needs_update?(%User{local: true}), do: false
325 def needs_update?(%User{local: false, last_refreshed_at: nil}), do: true
327 def needs_update?(%User{local: false} = user) do
328 NaiveDateTime.diff(NaiveDateTime.utc_now(), user.last_refreshed_at) >= 86_400
331 def needs_update?(_), do: true
333 def maybe_direct_follow(%User{} = follower, %User{local: true, info: %{locked: true}}) do
337 def maybe_direct_follow(%User{} = follower, %User{local: true} = followed) do
338 follow(follower, followed)
341 def maybe_direct_follow(%User{} = follower, %User{} = followed) do
342 if not User.ap_enabled?(followed) do
343 follow(follower, followed)
349 @doc "A mass follow for local users. Respects blocks in both directions but does not create activities."
350 @spec follow_all(User.t(), list(User.t())) :: {atom(), User.t()}
351 def follow_all(follower, followeds) do
354 |> Enum.reject(fn followed -> blocks?(follower, followed) || blocks?(followed, follower) end)
355 |> Enum.map(fn %{follower_address: fa} -> fa end)
359 where: u.id == ^follower.id,
364 "array(select distinct unnest (array_cat(?, ?)))",
373 {1, [follower]} = Repo.update_all(q, [])
375 Enum.each(followeds, fn followed ->
376 update_follower_count(followed)
382 def follow(%User{} = follower, %User{info: info} = followed) do
383 deny_follow_blocked = Pleroma.Config.get([:user, :deny_follow_blocked])
384 ap_followers = followed.follower_address
388 {:error, "Could not follow user: You are deactivated."}
390 deny_follow_blocked and blocks?(followed, follower) ->
391 {:error, "Could not follow user: #{followed.nickname} blocked you."}
394 if !followed.local && follower.local && !ap_enabled?(followed) do
395 Websub.subscribe(follower, followed)
400 where: u.id == ^follower.id,
401 update: [push: [following: ^ap_followers]],
405 {1, [follower]} = Repo.update_all(q, [])
407 {:ok, _} = update_follower_count(followed)
413 def unfollow(%User{} = follower, %User{} = followed) do
414 ap_followers = followed.follower_address
416 if following?(follower, followed) and follower.ap_id != followed.ap_id do
419 where: u.id == ^follower.id,
420 update: [pull: [following: ^ap_followers]],
424 {1, [follower]} = Repo.update_all(q, [])
426 {:ok, followed} = update_follower_count(followed)
430 {:ok, follower, Utils.fetch_latest_follow(follower, followed)}
432 {:error, "Not subscribed!"}
436 @spec following?(User.t(), User.t()) :: boolean
437 def following?(%User{} = follower, %User{} = followed) do
438 Enum.member?(follower.following, followed.follower_address)
441 def locked?(%User{} = user) do
442 user.info.locked || false
446 Repo.get_by(User, id: id)
449 def get_by_ap_id(ap_id) do
450 Repo.get_by(User, ap_id: ap_id)
453 def get_all_by_ap_id(ap_ids) do
454 from(u in __MODULE__,
455 where: u.ap_id in ^ap_ids
460 # This is mostly an SPC migration fix. This guesses the user nickname by taking the last part
461 # of the ap_id and the domain and tries to get that user
462 def get_by_guessed_nickname(ap_id) do
463 domain = URI.parse(ap_id).host
464 name = List.last(String.split(ap_id, "/"))
465 nickname = "#{name}@#{domain}"
467 get_cached_by_nickname(nickname)
470 def set_cache({:ok, user}), do: set_cache(user)
471 def set_cache({:error, err}), do: {:error, err}
473 def set_cache(%User{} = user) do
474 Cachex.put(:user_cache, "ap_id:#{user.ap_id}", user)
475 Cachex.put(:user_cache, "nickname:#{user.nickname}", user)
476 Cachex.put(:user_cache, "user_info:#{user.id}", user_info(user))
480 def update_and_set_cache(changeset) do
481 with {:ok, user} <- Repo.update(changeset, stale_error_field: :id) do
488 def invalidate_cache(user) do
489 Cachex.del(:user_cache, "ap_id:#{user.ap_id}")
490 Cachex.del(:user_cache, "nickname:#{user.nickname}")
491 Cachex.del(:user_cache, "user_info:#{user.id}")
494 def get_cached_by_ap_id(ap_id) do
495 key = "ap_id:#{ap_id}"
496 Cachex.fetch!(:user_cache, key, fn _ -> get_by_ap_id(ap_id) end)
499 def get_cached_by_id(id) do
503 Cachex.fetch!(:user_cache, key, fn _ ->
507 Cachex.put(:user_cache, "ap_id:#{user.ap_id}", user)
508 {:commit, user.ap_id}
514 get_cached_by_ap_id(ap_id)
517 def get_cached_by_nickname(nickname) do
518 key = "nickname:#{nickname}"
520 Cachex.fetch!(:user_cache, key, fn ->
521 user_result = get_or_fetch_by_nickname(nickname)
524 {:ok, user} -> {:commit, user}
525 {:error, _error} -> {:ignore, nil}
530 def get_cached_by_nickname_or_id(nickname_or_id) do
531 get_cached_by_id(nickname_or_id) || get_cached_by_nickname(nickname_or_id)
534 def get_by_nickname(nickname) do
535 Repo.get_by(User, nickname: nickname) ||
536 if Regex.match?(~r(@#{Pleroma.Web.Endpoint.host()})i, nickname) do
537 Repo.get_by(User, nickname: local_nickname(nickname))
541 def get_by_email(email), do: Repo.get_by(User, email: email)
543 def get_by_nickname_or_email(nickname_or_email) do
544 get_by_nickname(nickname_or_email) || get_by_email(nickname_or_email)
547 def get_cached_user_info(user) do
548 key = "user_info:#{user.id}"
549 Cachex.fetch!(:user_cache, key, fn _ -> user_info(user) end)
552 def fetch_by_nickname(nickname) do
553 ap_try = ActivityPub.make_user_from_nickname(nickname)
556 {:ok, user} -> {:ok, user}
557 _ -> OStatus.make_user(nickname)
561 def get_or_fetch_by_nickname(nickname) do
562 with %User{} = user <- get_by_nickname(nickname) do
566 with [_nick, _domain] <- String.split(nickname, "@"),
567 {:ok, user} <- fetch_by_nickname(nickname) do
568 if Pleroma.Config.get([:fetch_initial_posts, :enabled]) do
569 fetch_initial_posts(user)
574 _e -> {:error, "not found " <> nickname}
579 @doc "Fetch some posts when the user has just been federated with"
580 def fetch_initial_posts(user),
581 do: PleromaJobQueue.enqueue(:background, __MODULE__, [:fetch_initial_posts, user])
583 @spec get_followers_query(User.t(), pos_integer() | nil) :: Ecto.Query.t()
584 def get_followers_query(%User{} = user, nil) do
585 User.Query.build(%{followers: user, deactivated: false})
588 def get_followers_query(user, page) do
589 from(u in get_followers_query(user, nil))
590 |> User.Query.paginate(page, 20)
593 @spec get_followers_query(User.t()) :: Ecto.Query.t()
594 def get_followers_query(user), do: get_followers_query(user, nil)
596 @spec get_followers(User.t(), pos_integer()) :: {:ok, list(User.t())}
597 def get_followers(user, page \\ nil) do
598 q = get_followers_query(user, page)
603 @spec get_external_followers(User.t(), pos_integer()) :: {:ok, list(User.t())}
604 def get_external_followers(user, page \\ nil) do
607 |> get_followers_query(page)
608 |> User.Query.build(%{external: true})
613 def get_followers_ids(user, page \\ nil) do
614 q = get_followers_query(user, page)
616 Repo.all(from(u in q, select: u.id))
619 @spec get_friends_query(User.t(), pos_integer() | nil) :: Ecto.Query.t()
620 def get_friends_query(%User{} = user, nil) do
621 User.Query.build(%{friends: user, deactivated: false})
624 def get_friends_query(user, page) do
625 from(u in get_friends_query(user, nil))
626 |> User.Query.paginate(page, 20)
629 @spec get_friends_query(User.t()) :: Ecto.Query.t()
630 def get_friends_query(user), do: get_friends_query(user, nil)
632 def get_friends(user, page \\ nil) do
633 q = get_friends_query(user, page)
638 def get_friends_ids(user, page \\ nil) do
639 q = get_friends_query(user, page)
641 Repo.all(from(u in q, select: u.id))
644 @spec get_follow_requests(User.t()) :: {:ok, [User.t()]}
645 def get_follow_requests(%User{} = user) do
647 Activity.follow_requests_for_actor(user)
648 |> join(:inner, [a], u in User, on: a.actor == u.ap_id)
649 |> where([a, u], not fragment("? @> ?", u.following, ^[user.follower_address]))
650 |> group_by([a, u], u.id)
657 def increase_note_count(%User{} = user) do
659 |> where(id: ^user.id)
664 "jsonb_set(?, '{note_count}', ((?->>'note_count')::int + 1)::varchar::jsonb, true)",
671 |> Repo.update_all([])
673 {1, [user]} -> set_cache(user)
678 def decrease_note_count(%User{} = user) do
680 |> where(id: ^user.id)
685 "jsonb_set(?, '{note_count}', (greatest(0, (?->>'note_count')::int - 1))::varchar::jsonb, true)",
692 |> Repo.update_all([])
694 {1, [user]} -> set_cache(user)
699 def update_note_count(%User{} = user) do
703 where: fragment("?->>'actor' = ? and ?->>'type' = 'Note'", a.data, ^user.ap_id, a.data),
707 note_count = Repo.one(note_count_query)
709 info_cng = User.Info.set_note_count(user.info, note_count)
713 |> put_embed(:info, info_cng)
714 |> update_and_set_cache()
717 def update_follower_count(%User{} = user) do
718 follower_count_query =
719 User.Query.build(%{followers: user, deactivated: false})
720 |> select([u], %{count: count(u.id)})
723 |> where(id: ^user.id)
724 |> join(:inner, [u], s in subquery(follower_count_query))
729 "jsonb_set(?, '{follower_count}', ?::varchar::jsonb, true)",
736 |> Repo.update_all([])
738 {1, [user]} -> set_cache(user)
743 def remove_duplicated_following(%User{following: following} = user) do
744 uniq_following = Enum.uniq(following)
746 if length(following) == length(uniq_following) do
750 |> update_changeset(%{following: uniq_following})
751 |> update_and_set_cache()
755 @spec get_users_from_set([String.t()], boolean()) :: [User.t()]
756 def get_users_from_set(ap_ids, local_only \\ true) do
757 criteria = %{ap_id: ap_ids, deactivated: false}
758 criteria = if local_only, do: Map.put(criteria, :local, true), else: criteria
760 User.Query.build(criteria)
764 @spec get_recipients_from_activity(Activity.t()) :: [User.t()]
765 def get_recipients_from_activity(%Activity{recipients: to}) do
766 User.Query.build(%{recipients_from_activity: to, local: true, deactivated: false})
770 @spec mute(User.t(), User.t(), boolean()) :: {:ok, User.t()} | {:error, String.t()}
771 def mute(muter, %User{ap_id: ap_id}, notifications? \\ true) do
775 User.Info.add_to_mutes(info, ap_id)
776 |> User.Info.add_to_muted_notifications(info, ap_id, notifications?)
780 |> put_embed(:info, info_cng)
782 update_and_set_cache(cng)
785 def unmute(muter, %{ap_id: ap_id}) do
789 User.Info.remove_from_mutes(info, ap_id)
790 |> User.Info.remove_from_muted_notifications(info, ap_id)
794 |> put_embed(:info, info_cng)
796 update_and_set_cache(cng)
799 def subscribe(subscriber, %{ap_id: ap_id}) do
800 deny_follow_blocked = Pleroma.Config.get([:user, :deny_follow_blocked])
802 with %User{} = subscribed <- get_cached_by_ap_id(ap_id) do
803 blocked = blocks?(subscribed, subscriber) and deny_follow_blocked
806 {:error, "Could not subscribe: #{subscribed.nickname} is blocking you"}
810 |> User.Info.add_to_subscribers(subscriber.ap_id)
813 |> put_embed(:info, info_cng)
814 |> update_and_set_cache()
819 def unsubscribe(unsubscriber, %{ap_id: ap_id}) do
820 with %User{} = user <- get_cached_by_ap_id(ap_id) do
823 |> User.Info.remove_from_subscribers(unsubscriber.ap_id)
826 |> put_embed(:info, info_cng)
827 |> update_and_set_cache()
831 def block(blocker, %User{ap_id: ap_id} = blocked) do
832 # sever any follow relationships to prevent leaks per activitypub (Pleroma issue #213)
834 if following?(blocker, blocked) do
835 {:ok, blocker, _} = unfollow(blocker, blocked)
842 if subscribed_to?(blocked, blocker) do
843 {:ok, blocker} = unsubscribe(blocked, blocker)
849 if following?(blocked, blocker) do
850 unfollow(blocked, blocker)
853 {:ok, blocker} = update_follower_count(blocker)
857 |> User.Info.add_to_block(ap_id)
861 |> put_embed(:info, info_cng)
863 update_and_set_cache(cng)
866 # helper to handle the block given only an actor's AP id
867 def block(blocker, %{ap_id: ap_id}) do
868 block(blocker, get_cached_by_ap_id(ap_id))
871 def unblock(blocker, %{ap_id: ap_id}) do
874 |> User.Info.remove_from_block(ap_id)
878 |> put_embed(:info, info_cng)
880 update_and_set_cache(cng)
883 def mutes?(nil, _), do: false
884 def mutes?(user, %{ap_id: ap_id}), do: Enum.member?(user.info.mutes, ap_id)
886 @spec muted_notifications?(User.t() | nil, User.t() | map()) :: boolean()
887 def muted_notifications?(nil, _), do: false
889 def muted_notifications?(user, %{ap_id: ap_id}),
890 do: Enum.member?(user.info.muted_notifications, ap_id)
892 def blocks?(%User{} = user, %User{} = target) do
893 blocks_ap_id?(user, target) || blocks_domain?(user, target)
896 def blocks?(nil, _), do: false
898 def blocks_ap_id?(%User{} = user, %User{} = target) do
899 Enum.member?(user.info.blocks, target.ap_id)
902 def blocks_ap_id?(_, _), do: false
904 def blocks_domain?(%User{} = user, %User{} = target) do
905 domain_blocks = Pleroma.Web.ActivityPub.MRF.subdomains_regex(user.info.domain_blocks)
906 %{host: host} = URI.parse(target.ap_id)
907 Pleroma.Web.ActivityPub.MRF.subdomain_match?(domain_blocks, host)
910 def blocks_domain?(_, _), do: false
912 def subscribed_to?(user, %{ap_id: ap_id}) do
913 with %User{} = target <- get_cached_by_ap_id(ap_id) do
914 Enum.member?(target.info.subscribers, user.ap_id)
918 @spec muted_users(User.t()) :: [User.t()]
919 def muted_users(user) do
920 User.Query.build(%{ap_id: user.info.mutes, deactivated: false})
924 @spec blocked_users(User.t()) :: [User.t()]
925 def blocked_users(user) do
926 User.Query.build(%{ap_id: user.info.blocks, deactivated: false})
930 @spec subscribers(User.t()) :: [User.t()]
931 def subscribers(user) do
932 User.Query.build(%{ap_id: user.info.subscribers, deactivated: false})
936 def block_domain(user, domain) do
939 |> User.Info.add_to_domain_block(domain)
943 |> put_embed(:info, info_cng)
945 update_and_set_cache(cng)
948 def unblock_domain(user, domain) do
951 |> User.Info.remove_from_domain_block(domain)
955 |> put_embed(:info, info_cng)
957 update_and_set_cache(cng)
960 def deactivate_async(user, status \\ true) do
961 PleromaJobQueue.enqueue(:background, __MODULE__, [:deactivate_async, user, status])
964 def deactivate(%User{} = user, status \\ true) do
965 info_cng = User.Info.set_activation_status(user.info, status)
967 with {:ok, friends} <- User.get_friends(user),
968 {:ok, followers} <- User.get_followers(user),
972 |> put_embed(:info, info_cng)
973 |> update_and_set_cache() do
974 Enum.each(followers, &invalidate_cache(&1))
975 Enum.each(friends, &update_follower_count(&1))
981 def update_notification_settings(%User{} = user, settings \\ %{}) do
982 info_changeset = User.Info.update_notification_settings(user.info, settings)
985 |> put_embed(:info, info_changeset)
986 |> update_and_set_cache()
989 @spec delete(User.t()) :: :ok
990 def delete(%User{} = user),
991 do: PleromaJobQueue.enqueue(:background, __MODULE__, [:delete, user])
993 @spec perform(atom(), User.t()) :: {:ok, User.t()}
994 def perform(:delete, %User{} = user) do
995 {:ok, _user} = ActivityPub.delete(user)
997 # Remove all relationships
998 {:ok, followers} = User.get_followers(user)
1000 Enum.each(followers, fn follower ->
1001 ActivityPub.unfollow(follower, user)
1002 User.unfollow(follower, user)
1005 {:ok, friends} = User.get_friends(user)
1007 Enum.each(friends, fn followed ->
1008 ActivityPub.unfollow(user, followed)
1009 User.unfollow(user, followed)
1012 delete_user_activities(user)
1013 invalidate_cache(user)
1017 @spec perform(atom(), User.t()) :: {:ok, User.t()}
1018 def perform(:fetch_initial_posts, %User{} = user) do
1019 pages = Pleroma.Config.get!([:fetch_initial_posts, :pages])
1022 # Insert all the posts in reverse order, so they're in the right order on the timeline
1023 Enum.reverse(Utils.fetch_ordered_collection(user.info.source_data["outbox"], pages)),
1024 &Pleroma.Web.Federator.incoming_ap_doc/1
1030 def perform(:deactivate_async, user, status), do: deactivate(user, status)
1032 @spec perform(atom(), User.t(), list()) :: list() | {:error, any()}
1033 def perform(:blocks_import, %User{} = blocker, blocked_identifiers)
1034 when is_list(blocked_identifiers) do
1036 blocked_identifiers,
1037 fn blocked_identifier ->
1038 with {:ok, %User{} = blocked} <- get_or_fetch(blocked_identifier),
1039 {:ok, blocker} <- block(blocker, blocked),
1040 {:ok, _} <- ActivityPub.block(blocker, blocked) do
1044 Logger.debug("blocks_import failed for #{blocked_identifier} with: #{inspect(err)}")
1051 @spec perform(atom(), User.t(), list()) :: list() | {:error, any()}
1052 def perform(:follow_import, %User{} = follower, followed_identifiers)
1053 when is_list(followed_identifiers) do
1055 followed_identifiers,
1056 fn followed_identifier ->
1057 with {:ok, %User{} = followed} <- get_or_fetch(followed_identifier),
1058 {:ok, follower} <- maybe_direct_follow(follower, followed),
1059 {:ok, _} <- ActivityPub.follow(follower, followed) do
1063 Logger.debug("follow_import failed for #{followed_identifier} with: #{inspect(err)}")
1070 @spec external_users_query() :: Ecto.Query.t()
1071 def external_users_query do
1079 @spec external_users(keyword()) :: [User.t()]
1080 def external_users(opts \\ []) do
1082 external_users_query()
1083 |> select([u], struct(u, [:id, :ap_id, :info]))
1087 do: where(query, [u], u.id > ^opts[:max_id]),
1092 do: limit(query, ^opts[:limit]),
1098 def blocks_import(%User{} = blocker, blocked_identifiers) when is_list(blocked_identifiers),
1100 PleromaJobQueue.enqueue(:background, __MODULE__, [
1106 def follow_import(%User{} = follower, followed_identifiers) when is_list(followed_identifiers),
1108 PleromaJobQueue.enqueue(:background, __MODULE__, [
1111 followed_identifiers
1114 def delete_user_activities(%User{ap_id: ap_id} = user) do
1116 |> Activity.query_by_actor()
1117 |> RepoStreamer.chunk_stream(50)
1118 |> Stream.each(fn activities ->
1119 Enum.each(activities, &delete_activity(&1))
1126 defp delete_activity(%{data: %{"type" => "Create"}} = activity) do
1128 |> Object.normalize()
1129 |> ActivityPub.delete()
1132 defp delete_activity(%{data: %{"type" => "Like"}} = activity) do
1133 user = get_cached_by_ap_id(activity.actor)
1134 object = Object.normalize(activity)
1136 ActivityPub.unlike(user, object)
1139 defp delete_activity(%{data: %{"type" => "Announce"}} = activity) do
1140 user = get_cached_by_ap_id(activity.actor)
1141 object = Object.normalize(activity)
1143 ActivityPub.unannounce(user, object)
1146 defp delete_activity(_activity), do: "Doing nothing"
1148 def html_filter_policy(%User{info: %{no_rich_text: true}}) do
1149 Pleroma.HTML.Scrubber.TwitterText
1152 def html_filter_policy(_), do: Pleroma.Config.get([:markup, :scrub_policy])
1154 def fetch_by_ap_id(ap_id) do
1155 ap_try = ActivityPub.make_user_from_ap_id(ap_id)
1162 case OStatus.make_user(ap_id) do
1163 {:ok, user} -> {:ok, user}
1164 _ -> {:error, "Could not fetch by AP id"}
1169 def get_or_fetch_by_ap_id(ap_id) do
1170 user = get_cached_by_ap_id(ap_id)
1172 if !is_nil(user) and !User.needs_update?(user) do
1175 # Whether to fetch initial posts for the user (if it's a new user & the fetching is enabled)
1176 should_fetch_initial = is_nil(user) and Pleroma.Config.get([:fetch_initial_posts, :enabled])
1178 resp = fetch_by_ap_id(ap_id)
1180 if should_fetch_initial do
1181 with {:ok, %User{} = user} <- resp do
1182 fetch_initial_posts(user)
1190 @doc "Creates an internal service actor by URI if missing. Optionally takes nickname for addressing."
1191 def get_or_create_service_actor_by_ap_id(uri, nickname \\ nil) do
1192 if user = get_cached_by_ap_id(uri) do
1196 %User{info: %User.Info{}}
1197 |> cast(%{}, [:ap_id, :nickname, :local])
1198 |> put_change(:ap_id, uri)
1199 |> put_change(:nickname, nickname)
1200 |> put_change(:local, true)
1201 |> put_change(:follower_address, uri <> "/followers")
1203 {:ok, user} = Repo.insert(changes)
1209 def public_key_from_info(%{
1210 source_data: %{"publicKey" => %{"publicKeyPem" => public_key_pem}}
1214 |> :public_key.pem_decode()
1216 |> :public_key.pem_entry_decode()
1222 def public_key_from_info(%{magic_key: magic_key}) when not is_nil(magic_key) do
1223 {:ok, Pleroma.Web.Salmon.decode_key(magic_key)}
1226 def public_key_from_info(_), do: {:error, "not found key"}
1228 def get_public_key_for_ap_id(ap_id) do
1229 with {:ok, %User{} = user} <- get_or_fetch_by_ap_id(ap_id),
1230 {:ok, public_key} <- public_key_from_info(user.info) do
1237 defp blank?(""), do: nil
1238 defp blank?(n), do: n
1240 def insert_or_update_user(data) do
1242 |> Map.put(:name, blank?(data[:name]) || data[:nickname])
1243 |> remote_user_creation()
1244 |> Repo.insert(on_conflict: :replace_all_except_primary_key, conflict_target: :nickname)
1248 def ap_enabled?(%User{local: true}), do: true
1249 def ap_enabled?(%User{info: info}), do: info.ap_enabled
1250 def ap_enabled?(_), do: false
1252 @doc "Gets or fetch a user by uri or nickname."
1253 @spec get_or_fetch(String.t()) :: {:ok, User.t()} | {:error, String.t()}
1254 def get_or_fetch("http" <> _host = uri), do: get_or_fetch_by_ap_id(uri)
1255 def get_or_fetch(nickname), do: get_or_fetch_by_nickname(nickname)
1257 # wait a period of time and return newest version of the User structs
1258 # this is because we have synchronous follow APIs and need to simulate them
1259 # with an async handshake
1260 def wait_and_refresh(_, %User{local: true} = a, %User{local: true} = b) do
1261 with %User{} = a <- User.get_cached_by_id(a.id),
1262 %User{} = b <- User.get_cached_by_id(b.id) do
1270 def wait_and_refresh(timeout, %User{} = a, %User{} = b) do
1271 with :ok <- :timer.sleep(timeout),
1272 %User{} = a <- User.get_cached_by_id(a.id),
1273 %User{} = b <- User.get_cached_by_id(b.id) do
1281 def parse_bio(bio) when is_binary(bio) and bio != "" do
1283 |> CommonUtils.format_input("text/plain", mentions_format: :full)
1287 def parse_bio(_), do: ""
1289 def parse_bio(bio, user) when is_binary(bio) and bio != "" do
1290 # TODO: get profile URLs other than user.ap_id
1291 profile_urls = [user.ap_id]
1294 |> CommonUtils.format_input("text/plain",
1295 mentions_format: :full,
1296 rel: &RelMe.maybe_put_rel_me(&1, profile_urls)
1301 def parse_bio(_, _), do: ""
1303 def tag(user_identifiers, tags) when is_list(user_identifiers) do
1304 Repo.transaction(fn ->
1305 for user_identifier <- user_identifiers, do: tag(user_identifier, tags)
1309 def tag(nickname, tags) when is_binary(nickname),
1310 do: tag(get_by_nickname(nickname), tags)
1312 def tag(%User{} = user, tags),
1313 do: update_tags(user, Enum.uniq((user.tags || []) ++ normalize_tags(tags)))
1315 def untag(user_identifiers, tags) when is_list(user_identifiers) do
1316 Repo.transaction(fn ->
1317 for user_identifier <- user_identifiers, do: untag(user_identifier, tags)
1321 def untag(nickname, tags) when is_binary(nickname),
1322 do: untag(get_by_nickname(nickname), tags)
1324 def untag(%User{} = user, tags),
1325 do: update_tags(user, (user.tags || []) -- normalize_tags(tags))
1327 defp update_tags(%User{} = user, new_tags) do
1328 {:ok, updated_user} =
1330 |> change(%{tags: new_tags})
1331 |> update_and_set_cache()
1336 defp normalize_tags(tags) do
1339 |> Enum.map(&String.downcase(&1))
1342 defp local_nickname_regex do
1343 if Pleroma.Config.get([:instance, :extended_nickname_format]) do
1344 @extended_local_nickname_regex
1346 @strict_local_nickname_regex
1350 def local_nickname(nickname_or_mention) do
1353 |> String.split("@")
1357 def full_nickname(nickname_or_mention),
1358 do: String.trim_leading(nickname_or_mention, "@")
1360 def error_user(ap_id) do
1365 nickname: "erroruser@example.com",
1366 inserted_at: NaiveDateTime.utc_now()
1370 @spec all_superusers() :: [User.t()]
1371 def all_superusers do
1372 User.Query.build(%{super_users: true, local: true, deactivated: false})
1376 def showing_reblogs?(%User{} = user, %User{} = target) do
1377 target.ap_id not in user.info.muted_reblogs
1380 @spec toggle_confirmation(User.t()) :: {:ok, User.t()} | {:error, Changeset.t()}
1381 def toggle_confirmation(%User{} = user) do
1382 need_confirmation? = !user.info.confirmation_pending
1385 User.Info.confirmation_changeset(user.info, need_confirmation: need_confirmation?)
1389 |> put_embed(:info, info_changeset)
1390 |> update_and_set_cache()
1393 def get_mascot(%{info: %{mascot: %{} = mascot}}) when not is_nil(mascot) do
1397 def get_mascot(%{info: %{mascot: mascot}}) when is_nil(mascot) do
1398 # use instance-default
1399 config = Pleroma.Config.get([:assets, :mascots])
1400 default_mascot = Pleroma.Config.get([:assets, :default_mascot])
1401 mascot = Keyword.get(config, default_mascot)
1404 "id" => "default-mascot",
1405 "url" => mascot[:url],
1406 "preview_url" => mascot[:url],
1408 "mime_type" => mascot[:mime_type]
1413 def ensure_keys_present(%User{info: info} = user) do
1417 {:ok, pem} = Keys.generate_rsa_pem()
1420 |> Ecto.Changeset.change()
1421 |> Ecto.Changeset.put_embed(:info, User.Info.set_keys(info, pem))
1422 |> update_and_set_cache()
1426 def get_ap_ids_by_nicknames(nicknames) do
1428 where: u.nickname in ^nicknames,
1434 defdelegate search(query, opts \\ []), to: User.Search
1436 defp put_password_hash(
1437 %Ecto.Changeset{valid?: true, changes: %{password: password}} = changeset
1439 change(changeset, password_hash: Pbkdf2.hashpwsalt(password))
1442 defp put_password_hash(changeset), do: changeset
1444 def is_internal_user?(%User{nickname: nil}), do: true
1445 def is_internal_user?(%User{local: true, nickname: "internal." <> _}), do: true
1446 def is_internal_user?(_), do: false