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
15 alias Pleroma.Activity
16 alias Pleroma.Notification
18 alias Pleroma.Formatter
19 alias Pleroma.Web.CommonAPI.Utils, as: CommonUtils
20 alias Pleroma.Web.OStatus
21 alias Pleroma.Web.Websub
22 alias Pleroma.Web.OAuth
23 alias Pleroma.Web.ActivityPub.Utils
24 alias Pleroma.Web.ActivityPub.ActivityPub
25 alias Pleroma.Web.RelMe
29 @type t :: %__MODULE__{}
31 @primary_key {:id, Pleroma.FlakeId, autogenerate: true}
33 @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])?)*$/
35 @strict_local_nickname_regex ~r/^[a-zA-Z\d]+$/
36 @extended_local_nickname_regex ~r/^[a-zA-Z\d_-]+$/
40 field(:email, :string)
42 field(:nickname, :string)
43 field(:password_hash, :string)
44 field(:password, :string, virtual: true)
45 field(:password_confirmation, :string, virtual: true)
46 field(:following, {:array, :string}, default: [])
47 field(:ap_id, :string)
49 field(:local, :boolean, default: true)
50 field(:follower_address, :string)
51 field(:search_rank, :float, virtual: true)
52 field(:tags, {:array, :string}, default: [])
53 field(:bookmarks, {:array, :string}, default: [])
54 field(:last_refreshed_at, :naive_datetime)
55 has_many(:notifications, Notification)
56 embeds_one(:info, Pleroma.User.Info)
61 def auth_active?(%User{local: false}), do: true
63 def auth_active?(%User{info: %User.Info{confirmation_pending: false}}), do: true
65 def auth_active?(%User{info: %User.Info{confirmation_pending: true}}),
66 do: !Pleroma.Config.get([:instance, :account_activation_required])
68 def auth_active?(_), do: false
70 def visible_for?(user, for_user \\ nil)
72 def visible_for?(%User{id: user_id}, %User{id: for_id}) when user_id == for_id, do: true
74 def visible_for?(%User{} = user, for_user) do
75 auth_active?(user) || superuser?(for_user)
78 def visible_for?(_, _), do: false
80 def superuser?(%User{local: true, info: %User.Info{is_admin: true}}), do: true
81 def superuser?(%User{local: true, info: %User.Info{is_moderator: true}}), do: true
82 def superuser?(_), do: false
84 def avatar_url(user) do
86 %{"url" => [%{"href" => href} | _]} -> href
87 _ -> "#{Web.base_url()}/images/avi.png"
91 def banner_url(user) do
92 case user.info.banner do
93 %{"url" => [%{"href" => href} | _]} -> href
94 _ -> "#{Web.base_url()}/images/banner.png"
98 def profile_url(%User{info: %{source_data: %{"url" => url}}}), do: url
99 def profile_url(%User{ap_id: ap_id}), do: ap_id
100 def profile_url(_), do: nil
102 def ap_id(%User{nickname: nickname}) do
103 "#{Web.base_url()}/users/#{nickname}"
106 def ap_followers(%User{} = user) do
107 "#{ap_id(user)}/followers"
110 def user_info(%User{} = user) do
112 following_count: following_count(user),
113 note_count: user.info.note_count,
114 follower_count: user.info.follower_count,
115 locked: user.info.locked,
116 confirmation_pending: user.info.confirmation_pending,
117 default_scope: user.info.default_scope
121 defp restrict_disabled(query) do
123 where: not fragment("? \\? 'disabled' AND ?->'disabled' @> 'true'", u.info, u.info)
127 def following_count(%User{following: []}), do: 0
129 def following_count(%User{following: following, id: id}) do
131 where: u.follower_address in ^following,
134 |> restrict_disabled()
135 |> Repo.aggregate(:count, :id)
138 def remote_user_creation(params) do
141 |> Map.put(:info, params[:info] || %{})
143 info_cng = User.Info.remote_user_creation(%User.Info{}, params[:info])
147 |> cast(params, [:bio, :name, :ap_id, :nickname, :avatar])
148 |> validate_required([:name, :ap_id])
149 |> unique_constraint(:nickname)
150 |> validate_format(:nickname, @email_regex)
151 |> validate_length(:bio, max: 5000)
152 |> validate_length(:name, max: 100)
153 |> put_change(:local, false)
154 |> put_embed(:info, info_cng)
157 case info_cng.changes[:source_data] do
158 %{"followers" => followers} ->
160 |> put_change(:follower_address, followers)
163 followers = User.ap_followers(%User{nickname: changes.changes[:nickname]})
166 |> put_change(:follower_address, followers)
173 def update_changeset(struct, params \\ %{}) do
175 |> cast(params, [:bio, :name, :avatar])
176 |> unique_constraint(:nickname)
177 |> validate_format(:nickname, local_nickname_regex())
178 |> validate_length(:bio, max: 5000)
179 |> validate_length(:name, min: 1, max: 100)
182 def upgrade_changeset(struct, params \\ %{}) do
185 |> Map.put(:last_refreshed_at, NaiveDateTime.utc_now())
189 |> User.Info.user_upgrade(params[:info])
192 |> cast(params, [:bio, :name, :follower_address, :avatar, :last_refreshed_at])
193 |> unique_constraint(:nickname)
194 |> validate_format(:nickname, local_nickname_regex())
195 |> validate_length(:bio, max: 5000)
196 |> validate_length(:name, max: 100)
197 |> put_embed(:info, info_cng)
200 def password_update_changeset(struct, params) do
203 |> cast(params, [:password, :password_confirmation])
204 |> validate_required([:password, :password_confirmation])
205 |> validate_confirmation(:password)
207 OAuth.Token.delete_user_tokens(struct)
208 OAuth.Authorization.delete_user_authorizations(struct)
210 if changeset.valid? do
211 hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
214 |> put_change(:password_hash, hashed)
220 def reset_password(user, data) do
221 update_and_set_cache(password_update_changeset(user, data))
224 def register_changeset(struct, params \\ %{}, opts \\ []) do
225 confirmation_status =
226 if opts[:confirmed] || !Pleroma.Config.get([:instance, :account_activation_required]) do
232 info_change = User.Info.confirmation_changeset(%User.Info{}, confirmation_status)
236 |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
237 |> validate_required([:email, :name, :nickname, :password, :password_confirmation])
238 |> validate_confirmation(:password)
239 |> unique_constraint(:email)
240 |> unique_constraint(:nickname)
241 |> validate_exclusion(:nickname, Pleroma.Config.get([Pleroma.User, :restricted_nicknames]))
242 |> validate_format(:nickname, local_nickname_regex())
243 |> validate_format(:email, @email_regex)
244 |> validate_length(:bio, max: 1000)
245 |> validate_length(:name, min: 1, max: 100)
246 |> put_change(:info, info_change)
248 if changeset.valid? do
249 hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
250 ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]})
251 followers = User.ap_followers(%User{nickname: changeset.changes[:nickname]})
254 |> put_change(:password_hash, hashed)
255 |> put_change(:ap_id, ap_id)
256 |> unique_constraint(:ap_id)
257 |> put_change(:following, [followers])
258 |> put_change(:follower_address, followers)
264 defp autofollow_users(user) do
265 candidates = Pleroma.Config.get([:instance, :autofollowed_nicknames])
269 where: u.local == true,
270 where: u.nickname in ^candidates
274 follow_all(user, autofollowed_users)
277 @doc "Inserts provided changeset, performs post-registration actions (confirmation email sending etc.)"
278 def register(%Ecto.Changeset{} = changeset) do
279 with {:ok, user} <- Repo.insert(changeset),
280 {:ok, user} <- autofollow_users(user),
281 {:ok, _} <- Pleroma.User.WelcomeMessage.post_welcome_message_to_user(user),
282 {:ok, _} <- try_send_confirmation_email(user) do
287 def try_send_confirmation_email(%User{} = user) do
288 if user.info.confirmation_pending &&
289 Pleroma.Config.get([:instance, :account_activation_required]) do
291 |> Pleroma.UserEmail.account_confirmation_email()
292 |> Pleroma.Mailer.deliver_async()
298 def needs_update?(%User{local: true}), do: false
300 def needs_update?(%User{local: false, last_refreshed_at: nil}), do: true
302 def needs_update?(%User{local: false} = user) do
303 NaiveDateTime.diff(NaiveDateTime.utc_now(), user.last_refreshed_at) >= 86400
306 def needs_update?(_), do: true
308 def maybe_direct_follow(%User{} = follower, %User{local: true, info: %{locked: true}}) do
312 def maybe_direct_follow(%User{} = follower, %User{local: true} = followed) do
313 follow(follower, followed)
316 def maybe_direct_follow(%User{} = follower, %User{} = followed) do
317 if not User.ap_enabled?(followed) do
318 follow(follower, followed)
324 def maybe_follow(%User{} = follower, %User{info: _info} = followed) do
325 if not following?(follower, followed) do
326 follow(follower, followed)
332 @doc "A mass follow for local users. Respects blocks in both directions but does not create activities."
333 @spec follow_all(User.t(), list(User.t())) :: {atom(), User.t()}
334 def follow_all(follower, followeds) do
337 |> Enum.reject(fn followed -> blocks?(follower, followed) || blocks?(followed, follower) end)
338 |> Enum.map(fn %{follower_address: fa} -> fa end)
342 where: u.id == ^follower.id,
347 "array(select distinct unnest (array_cat(?, ?)))",
355 {1, [follower]} = Repo.update_all(q, [], returning: true)
357 Enum.each(followeds, fn followed ->
358 update_follower_count(followed)
364 def follow(%User{} = follower, %User{info: info} = followed) do
365 user_config = Application.get_env(:pleroma, :user)
366 deny_follow_blocked = Keyword.get(user_config, :deny_follow_blocked)
368 ap_followers = followed.follower_address
371 following?(follower, followed) or info.deactivated ->
372 {:error, "Could not follow user: #{followed.nickname} is already on your list."}
374 deny_follow_blocked and blocks?(followed, follower) ->
375 {:error, "Could not follow user: #{followed.nickname} blocked you."}
378 if !followed.local && follower.local && !ap_enabled?(followed) do
379 Websub.subscribe(follower, followed)
384 where: u.id == ^follower.id,
385 update: [push: [following: ^ap_followers]]
388 {1, [follower]} = Repo.update_all(q, [], returning: true)
390 {:ok, _} = update_follower_count(followed)
396 def unfollow(%User{} = follower, %User{} = followed) do
397 ap_followers = followed.follower_address
399 if following?(follower, followed) and follower.ap_id != followed.ap_id do
402 where: u.id == ^follower.id,
403 update: [pull: [following: ^ap_followers]]
406 {1, [follower]} = Repo.update_all(q, [], returning: true)
408 {:ok, followed} = update_follower_count(followed)
412 {:ok, follower, Utils.fetch_latest_follow(follower, followed)}
414 {:error, "Not subscribed!"}
418 @spec following?(User.t(), User.t()) :: boolean
419 def following?(%User{} = follower, %User{} = followed) do
420 Enum.member?(follower.following, followed.follower_address)
423 def follow_import(%User{} = follower, followed_identifiers)
424 when is_list(followed_identifiers) do
426 followed_identifiers,
427 fn followed_identifier ->
428 with %User{} = followed <- get_or_fetch(followed_identifier),
429 {:ok, follower} <- maybe_direct_follow(follower, followed),
430 {:ok, _} <- ActivityPub.follow(follower, followed) do
434 Logger.debug("follow_import failed for #{followed_identifier} with: #{inspect(err)}")
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 # This is mostly an SPC migration fix. This guesses the user nickname (by taking the last part of the ap_id and the domain) and tries to get that user
454 def get_by_guessed_nickname(ap_id) do
455 domain = URI.parse(ap_id).host
456 name = List.last(String.split(ap_id, "/"))
457 nickname = "#{name}@#{domain}"
459 get_by_nickname(nickname)
462 def set_cache(user) do
463 Cachex.put(:user_cache, "ap_id:#{user.ap_id}", user)
464 Cachex.put(:user_cache, "nickname:#{user.nickname}", user)
465 Cachex.put(:user_cache, "user_info:#{user.id}", user_info(user))
469 def update_and_set_cache(changeset) do
470 with {:ok, user} <- Repo.update(changeset) do
477 def invalidate_cache(user) do
478 Cachex.del(:user_cache, "ap_id:#{user.ap_id}")
479 Cachex.del(:user_cache, "nickname:#{user.nickname}")
480 Cachex.del(:user_cache, "user_info:#{user.id}")
483 def get_cached_by_ap_id(ap_id) do
484 key = "ap_id:#{ap_id}"
485 Cachex.fetch!(:user_cache, key, fn _ -> get_by_ap_id(ap_id) end)
488 def get_cached_by_id(id) do
492 Cachex.fetch!(:user_cache, key, fn _ ->
496 Cachex.put(:user_cache, "ap_id:#{user.ap_id}", user)
497 {:commit, user.ap_id}
503 get_cached_by_ap_id(ap_id)
506 def get_cached_by_nickname(nickname) do
507 key = "nickname:#{nickname}"
508 Cachex.fetch!(:user_cache, key, fn _ -> get_or_fetch_by_nickname(nickname) end)
511 def get_cached_by_nickname_or_id(nickname_or_id) do
512 get_cached_by_id(nickname_or_id) || get_cached_by_nickname(nickname_or_id)
515 def get_by_nickname(nickname) do
516 Repo.get_by(User, nickname: nickname) ||
517 if Regex.match?(~r(@#{Pleroma.Web.Endpoint.host()})i, nickname) do
518 Repo.get_by(User, nickname: local_nickname(nickname))
522 def get_by_nickname_or_email(nickname_or_email) do
523 case user = Repo.get_by(User, nickname: nickname_or_email) do
525 nil -> Repo.get_by(User, email: nickname_or_email)
529 def get_cached_user_info(user) do
530 key = "user_info:#{user.id}"
531 Cachex.fetch!(:user_cache, key, fn _ -> user_info(user) end)
534 def fetch_by_nickname(nickname) do
535 ap_try = ActivityPub.make_user_from_nickname(nickname)
538 {:ok, user} -> {:ok, user}
539 _ -> OStatus.make_user(nickname)
543 def get_or_fetch_by_nickname(nickname) do
544 with %User{} = user <- get_by_nickname(nickname) do
548 with [_nick, _domain] <- String.split(nickname, "@"),
549 {:ok, user} <- fetch_by_nickname(nickname) do
557 def get_followers_query(%User{id: id, follower_address: follower_address}, nil) do
560 where: fragment("? <@ ?", ^[follower_address], u.following),
563 |> restrict_disabled()
566 def get_followers_query(user, page) do
567 from(u in get_followers_query(user, nil))
568 |> paginate(page, 20)
571 def get_followers_query(user), do: get_followers_query(user, nil)
573 def get_followers(user, page \\ nil) do
574 q = get_followers_query(user, page)
579 def get_followers_ids(user, page \\ nil) do
580 q = get_followers_query(user, page)
582 Repo.all(from(u in q, select: u.id))
585 def get_friends_query(%User{id: id, following: following}, nil) do
588 where: u.follower_address in ^following,
591 |> restrict_disabled()
594 def get_friends_query(user, page) do
595 from(u in get_friends_query(user, nil))
596 |> paginate(page, 20)
599 def get_friends_query(user), do: get_friends_query(user, nil)
601 def get_friends(user, page \\ nil) do
602 q = get_friends_query(user, page)
607 def get_friends_ids(user, page \\ nil) do
608 q = get_friends_query(user, page)
610 Repo.all(from(u in q, select: u.id))
613 def get_follow_requests_query(%User{} = user) do
618 "? ->> 'type' = 'Follow'",
623 "? ->> 'state' = 'pending'",
628 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
636 def get_follow_requests(%User{} = user) do
639 |> User.get_follow_requests_query()
640 |> join(:inner, [a], u in User, a.actor == u.ap_id)
641 |> where([a, u], not fragment("? @> ?", u.following, ^[user.follower_address]))
642 |> group_by([a, u], u.id)
649 def increase_note_count(%User{} = user) do
651 |> where(id: ^user.id)
656 "jsonb_set(?, '{note_count}', ((?->>'note_count')::int + 1)::varchar::jsonb, true)",
662 |> Repo.update_all([], returning: true)
664 {1, [user]} -> set_cache(user)
669 def decrease_note_count(%User{} = user) do
671 |> where(id: ^user.id)
676 "jsonb_set(?, '{note_count}', (greatest(0, (?->>'note_count')::int - 1))::varchar::jsonb, true)",
682 |> Repo.update_all([], returning: true)
684 {1, [user]} -> set_cache(user)
689 def update_note_count(%User{} = user) do
693 where: fragment("?->>'actor' = ? and ?->>'type' = 'Note'", a.data, ^user.ap_id, a.data),
697 note_count = Repo.one(note_count_query)
699 info_cng = User.Info.set_note_count(user.info, note_count)
703 |> put_embed(:info, info_cng)
704 |> update_and_set_cache()
707 def update_follower_count(%User{} = user) do
708 follower_count_query =
710 |> where([u], ^user.follower_address in u.following)
711 |> where([u], u.id != ^user.id)
712 |> select([u], %{count: count(u.id)})
713 |> restrict_disabled()
716 |> where(id: ^user.id)
717 |> join(:inner, [u], s in subquery(follower_count_query))
722 "jsonb_set(?, '{follower_count}', ?::varchar::jsonb, true)",
728 |> Repo.update_all([], returning: true)
730 {1, [user]} -> set_cache(user)
735 def get_users_from_set_query(ap_ids, false) do
738 where: u.ap_id in ^ap_ids
742 def get_users_from_set_query(ap_ids, true) do
743 query = get_users_from_set_query(ap_ids, false)
747 where: u.local == true
751 def get_users_from_set(ap_ids, local_only \\ true) do
752 get_users_from_set_query(ap_ids, local_only)
756 def get_recipients_from_activity(%Activity{recipients: to}) do
760 where: u.ap_id in ^to,
761 or_where: fragment("? && ?", u.following, ^to)
764 query = from(u in query, where: u.local == true)
769 @spec search_for_admin(binary(), %{
770 admin: Pleroma.User.t(),
774 }) :: {:ok, [Pleroma.User.t()], number()}
775 def search_for_admin(term, %{admin: admin, local: local, page: page, page_size: page_size}) do
776 term = String.trim_leading(term, "@")
778 local_paginated_query =
780 |> maybe_local_user_query(local)
781 |> paginate(page, page_size)
783 search_query = fts_search_subquery(term, local_paginated_query)
787 |> fts_search_subquery()
788 |> maybe_local_user_query(local)
789 |> Repo.aggregate(:count, :id)
791 {:ok, do_search(search_query, admin), count}
794 @spec all_for_admin(number(), number()) :: {:ok, [Pleroma.User.t()], number()}
795 def all_for_admin(page, page_size) do
796 query = from(u in User, order_by: u.id)
800 |> paginate(page, page_size)
804 |> Repo.aggregate(:count, :id)
806 {:ok, Repo.all(paginated_query), count}
809 def search(query, resolve \\ false, for_user \\ nil) do
810 # Strip the beginning @ off if there is a query
811 query = String.trim_leading(query, "@")
813 if resolve, do: get_or_fetch(query)
815 fts_results = do_search(fts_search_subquery(query), for_user)
817 {:ok, trigram_results} =
818 Repo.transaction(fn ->
819 Ecto.Adapters.SQL.query(Repo, "select set_limit(0.25)", [])
820 do_search(trigram_search_subquery(query), for_user)
823 Enum.uniq_by(fts_results ++ trigram_results, & &1.id)
826 defp do_search(subquery, for_user, options \\ []) do
829 s in subquery(subquery),
830 order_by: [desc: s.search_rank],
831 limit: ^(options[:limit] || 20)
837 |> Enum.filter(&(&1.search_rank > 0))
839 boost_search_results(results, for_user)
842 defp fts_search_subquery(term, query \\ User) do
845 |> String.replace(~r/\W+/, " ")
848 |> Enum.map(&(&1 <> ":*"))
858 setweight(to_tsvector('simple', regexp_replace(?, '\\W', ' ', 'g')), 'A') ||
859 setweight(to_tsvector('simple', regexp_replace(coalesce(?, ''), '\\W', ' ', 'g')), 'B'),
860 to_tsquery('simple', ?),
872 (setweight(to_tsvector('simple', regexp_replace(?, '\\W', ' ', 'g')), 'A') ||
873 setweight(to_tsvector('simple', regexp_replace(coalesce(?, ''), '\\W', ' ', 'g')), 'B')) @@ to_tsquery('simple', ?)
880 |> restrict_disabled()
883 defp trigram_search_subquery(term) do
889 "similarity(?, trim(? || ' ' || coalesce(?, '')))",
895 where: fragment("trim(? || ' ' || coalesce(?, '')) % ?", u.nickname, u.name, ^term)
897 |> restrict_disabled()
900 defp boost_search_results(results, nil), do: results
902 defp boost_search_results(results, for_user) do
903 friends_ids = get_friends_ids(for_user)
904 followers_ids = get_followers_ids(for_user)
911 u.id in friends_ids ->
914 u.id in followers_ids ->
921 Map.put(u, :search_rank, u.search_rank * search_rank_coef)
924 |> Enum.sort_by(&(-&1.search_rank))
927 def blocks_import(%User{} = blocker, blocked_identifiers) when is_list(blocked_identifiers) do
930 fn blocked_identifier ->
931 with %User{} = blocked <- get_or_fetch(blocked_identifier),
932 {:ok, blocker} <- block(blocker, blocked),
933 {:ok, _} <- ActivityPub.block(blocker, blocked) do
937 Logger.debug("blocks_import failed for #{blocked_identifier} with: #{inspect(err)}")
944 def mute(muter, %User{ap_id: ap_id}) do
947 |> User.Info.add_to_mutes(ap_id)
951 |> put_embed(:info, info_cng)
953 update_and_set_cache(cng)
956 def unmute(muter, %{ap_id: ap_id}) do
959 |> User.Info.remove_from_mutes(ap_id)
963 |> put_embed(:info, info_cng)
965 update_and_set_cache(cng)
968 def block(blocker, %User{ap_id: ap_id} = blocked) do
969 # sever any follow relationships to prevent leaks per activitypub (Pleroma issue #213)
971 if following?(blocker, blocked) do
972 {:ok, blocker, _} = unfollow(blocker, blocked)
978 if following?(blocked, blocker) do
979 unfollow(blocked, blocker)
984 |> User.Info.add_to_block(ap_id)
988 |> put_embed(:info, info_cng)
990 update_and_set_cache(cng)
993 # helper to handle the block given only an actor's AP id
994 def block(blocker, %{ap_id: ap_id}) do
995 block(blocker, User.get_by_ap_id(ap_id))
998 def unblock(blocker, %{ap_id: ap_id}) do
1001 |> User.Info.remove_from_block(ap_id)
1005 |> put_embed(:info, info_cng)
1007 update_and_set_cache(cng)
1010 def mutes?(nil, _), do: false
1011 def mutes?(user, %{ap_id: ap_id}), do: Enum.member?(user.info.mutes, ap_id)
1013 def blocks?(user, %{ap_id: ap_id}) do
1014 blocks = user.info.blocks
1015 domain_blocks = user.info.domain_blocks
1016 %{host: host} = URI.parse(ap_id)
1018 Enum.member?(blocks, ap_id) ||
1019 Enum.any?(domain_blocks, fn domain ->
1024 def muted_users(user),
1025 do: Repo.all(from(u in User, where: u.ap_id in ^user.info.mutes))
1027 def blocked_users(user),
1028 do: Repo.all(from(u in User, where: u.ap_id in ^user.info.blocks))
1030 def block_domain(user, domain) do
1033 |> User.Info.add_to_domain_block(domain)
1037 |> put_embed(:info, info_cng)
1039 update_and_set_cache(cng)
1042 def unblock_domain(user, domain) do
1045 |> User.Info.remove_from_domain_block(domain)
1049 |> put_embed(:info, info_cng)
1051 update_and_set_cache(cng)
1054 def maybe_local_user_query(query, local) do
1055 if local, do: local_user_query(query), else: query
1058 def local_user_query(query \\ User) do
1061 where: u.local == true,
1062 where: not is_nil(u.nickname)
1066 def active_local_user_query do
1068 u in local_user_query(),
1069 where: fragment("not (?->'deactivated' @> 'true')", u.info)
1073 def moderator_user_query do
1076 where: u.local == true,
1077 where: fragment("?->'is_moderator' @> 'true'", u.info)
1081 def deactivate(%User{} = user, status \\ true) do
1082 info_cng = User.Info.set_activation_status(user.info, status)
1086 |> put_embed(:info, info_cng)
1087 |> update_and_set_cache()
1090 def delete(%User{} = user) do
1091 {:ok, user} = User.deactivate(user)
1093 # Remove all relationships
1094 {:ok, followers} = User.get_followers(user)
1097 |> Enum.each(fn follower -> User.unfollow(follower, user) end)
1099 {:ok, friends} = User.get_friends(user)
1102 |> Enum.each(fn followed -> User.unfollow(user, followed) end)
1104 query = from(a in Activity, where: a.actor == ^user.ap_id)
1107 |> Enum.each(fn activity ->
1108 case activity.data["type"] do
1110 ActivityPub.delete(Object.normalize(activity.data["object"]))
1112 # TODO: Do something with likes, follows, repeats.
1121 def disable_async(user, status \\ true) do
1122 Pleroma.Jobs.enqueue(:user, __MODULE__, [:disable_async, user, status])
1125 def disable(%User{} = user, status \\ true) do
1126 with {:ok, user} <- User.deactivate(user, status),
1127 info_cng <- User.Info.set_disabled_status(user.info, status),
1131 |> put_embed(:info, info_cng)
1132 |> update_and_set_cache(),
1133 {:ok, friends} <- User.get_friends(user) do
1134 Enum.each(friends, &update_follower_count(&1))
1139 def perform(:disable_async, user, status), do: disable(user, status)
1141 def html_filter_policy(%User{info: %{no_rich_text: true}}) do
1142 Pleroma.HTML.Scrubber.TwitterText
1145 @default_scrubbers Pleroma.Config.get([:markup, :scrub_policy])
1147 def html_filter_policy(_), do: @default_scrubbers
1149 def get_or_fetch_by_ap_id(ap_id) do
1150 user = get_by_ap_id(ap_id)
1152 if !is_nil(user) and !User.needs_update?(user) do
1155 ap_try = ActivityPub.make_user_from_ap_id(ap_id)
1162 case OStatus.make_user(ap_id) do
1164 _ -> {:error, "Could not fetch by AP id"}
1170 def get_or_create_instance_user do
1171 relay_uri = "#{Pleroma.Web.Endpoint.url()}/relay"
1173 if user = get_by_ap_id(relay_uri) do
1177 %User{info: %User.Info{}}
1178 |> cast(%{}, [:ap_id, :nickname, :local])
1179 |> put_change(:ap_id, relay_uri)
1180 |> put_change(:nickname, nil)
1181 |> put_change(:local, true)
1182 |> put_change(:follower_address, relay_uri <> "/followers")
1184 {:ok, user} = Repo.insert(changes)
1190 def public_key_from_info(%{
1191 source_data: %{"publicKey" => %{"publicKeyPem" => public_key_pem}}
1195 |> :public_key.pem_decode()
1197 |> :public_key.pem_entry_decode()
1203 def public_key_from_info(%{magic_key: magic_key}) do
1204 {:ok, Pleroma.Web.Salmon.decode_key(magic_key)}
1207 def get_public_key_for_ap_id(ap_id) do
1208 with %User{} = user <- get_or_fetch_by_ap_id(ap_id),
1209 {:ok, public_key} <- public_key_from_info(user.info) do
1216 defp blank?(""), do: nil
1217 defp blank?(n), do: n
1219 def insert_or_update_user(data) do
1222 |> Map.put(:name, blank?(data[:name]) || data[:nickname])
1224 cs = User.remote_user_creation(data)
1226 Repo.insert(cs, on_conflict: :replace_all, conflict_target: :nickname)
1229 def ap_enabled?(%User{local: true}), do: true
1230 def ap_enabled?(%User{info: info}), do: info.ap_enabled
1231 def ap_enabled?(_), do: false
1233 @doc "Gets or fetch a user by uri or nickname."
1234 @spec get_or_fetch(String.t()) :: User.t()
1235 def get_or_fetch("http" <> _host = uri), do: get_or_fetch_by_ap_id(uri)
1236 def get_or_fetch(nickname), do: get_or_fetch_by_nickname(nickname)
1238 # wait a period of time and return newest version of the User structs
1239 # this is because we have synchronous follow APIs and need to simulate them
1240 # with an async handshake
1241 def wait_and_refresh(_, %User{local: true} = a, %User{local: true} = b) do
1242 with %User{} = a <- Repo.get(User, a.id),
1243 %User{} = b <- Repo.get(User, b.id) do
1251 def wait_and_refresh(timeout, %User{} = a, %User{} = b) do
1252 with :ok <- :timer.sleep(timeout),
1253 %User{} = a <- Repo.get(User, a.id),
1254 %User{} = b <- Repo.get(User, b.id) do
1262 def parse_bio(bio, user \\ %User{info: %{source_data: %{}}})
1263 def parse_bio(nil, _user), do: ""
1264 def parse_bio(bio, _user) when bio == "", do: bio
1266 def parse_bio(bio, user) do
1268 (user.info.source_data["tag"] || [])
1269 |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
1270 |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} ->
1271 {String.trim(name, ":"), url}
1274 # TODO: get profile URLs other than user.ap_id
1275 profile_urls = [user.ap_id]
1278 |> CommonUtils.format_input("text/plain",
1279 mentions_format: :full,
1280 rel: &RelMe.maybe_put_rel_me(&1, profile_urls)
1283 |> Formatter.emojify(emoji)
1286 def tag(user_identifiers, tags) when is_list(user_identifiers) do
1287 Repo.transaction(fn ->
1288 for user_identifier <- user_identifiers, do: tag(user_identifier, tags)
1292 def tag(nickname, tags) when is_binary(nickname),
1293 do: tag(User.get_by_nickname(nickname), tags)
1295 def tag(%User{} = user, tags),
1296 do: update_tags(user, Enum.uniq((user.tags || []) ++ normalize_tags(tags)))
1298 def untag(user_identifiers, tags) when is_list(user_identifiers) do
1299 Repo.transaction(fn ->
1300 for user_identifier <- user_identifiers, do: untag(user_identifier, tags)
1304 def untag(nickname, tags) when is_binary(nickname),
1305 do: untag(User.get_by_nickname(nickname), tags)
1307 def untag(%User{} = user, tags),
1308 do: update_tags(user, (user.tags || []) -- normalize_tags(tags))
1310 defp update_tags(%User{} = user, new_tags) do
1311 {:ok, updated_user} =
1313 |> change(%{tags: new_tags})
1314 |> update_and_set_cache()
1319 def bookmark(%User{} = user, status_id) do
1320 bookmarks = Enum.uniq(user.bookmarks ++ [status_id])
1321 update_bookmarks(user, bookmarks)
1324 def unbookmark(%User{} = user, status_id) do
1325 bookmarks = Enum.uniq(user.bookmarks -- [status_id])
1326 update_bookmarks(user, bookmarks)
1329 def update_bookmarks(%User{} = user, bookmarks) do
1331 |> change(%{bookmarks: bookmarks})
1332 |> update_and_set_cache
1335 defp normalize_tags(tags) do
1338 |> Enum.map(&String.downcase(&1))
1341 defp local_nickname_regex() do
1342 if Pleroma.Config.get([:instance, :extended_nickname_format]) do
1343 @extended_local_nickname_regex
1345 @strict_local_nickname_regex
1349 def local_nickname(nickname_or_mention) do
1352 |> String.split("@")
1356 def full_nickname(nickname_or_mention),
1357 do: String.trim_leading(nickname_or_mention, "@")
1359 def error_user(ap_id) do
1364 nickname: "erroruser@example.com",
1365 inserted_at: NaiveDateTime.utc_now()
1369 def all_superusers do
1372 where: u.local == true,
1373 where: fragment("?->'is_admin' @> 'true' OR ?->'is_moderator' @> 'true'", u.info, u.info)
1378 defp paginate(query, page, page_size) do
1381 offset: ^((page - 1) * page_size)