Cleanup Pleroma.User

This commit is contained in:
Egor Kislitsyn 2019-09-24 14:14:34 +07:00
parent 209395c7e6
commit 1bea67cb5e
3 changed files with 108 additions and 155 deletions

View File

@ -106,9 +106,7 @@ defmodule Pleroma.User do
def profile_url(%User{ap_id: ap_id}), do: ap_id def profile_url(%User{ap_id: ap_id}), do: ap_id
def profile_url(_), do: nil def profile_url(_), do: nil
def ap_id(%User{nickname: nickname}) do def ap_id(%User{nickname: nickname}), do: "#{Web.base_url()}/users/#{nickname}"
"#{Web.base_url()}/users/#{nickname}"
end
def ap_followers(%User{follower_address: fa}) when is_binary(fa), do: fa def ap_followers(%User{follower_address: fa}) when is_binary(fa), do: fa
def ap_followers(%User{} = user), do: "#{ap_id(user)}/followers" def ap_followers(%User{} = user), do: "#{ap_id(user)}/followers"
@ -119,12 +117,9 @@ defmodule Pleroma.User do
def user_info(%User{} = user, args \\ %{}) do def user_info(%User{} = user, args \\ %{}) do
following_count = following_count =
if args[:following_count], Map.get(args, :following_count, user.info.following_count || following_count(user))
do: args[:following_count],
else: user.info.following_count || following_count(user)
follower_count = follower_count = Map.get(args, :follower_count, user.info.follower_count)
if args[:follower_count], do: args[:follower_count], else: user.info.follower_count
%{ %{
note_count: user.info.note_count, note_count: user.info.note_count,
@ -137,12 +132,11 @@ defmodule Pleroma.User do
end end
def follow_state(%User{} = user, %User{} = target) do def follow_state(%User{} = user, %User{} = target) do
follow_activity = Utils.fetch_latest_follow(user, target) case Utils.fetch_latest_follow(user, target) do
%{data: %{"state" => state}} -> state
if follow_activity,
do: follow_activity.data["state"],
# Ideally this would be nil, but then Cachex does not commit the value # Ideally this would be nil, but then Cachex does not commit the value
else: false _ -> false
end
end end
def get_cached_follow_state(user, target) do def get_cached_follow_state(user, target) do
@ -152,11 +146,7 @@ defmodule Pleroma.User do
@spec set_follow_state_cache(String.t(), String.t(), String.t()) :: {:ok | :error, boolean()} @spec set_follow_state_cache(String.t(), String.t(), String.t()) :: {:ok | :error, boolean()}
def set_follow_state_cache(user_ap_id, target_ap_id, state) do def set_follow_state_cache(user_ap_id, target_ap_id, state) do
Cachex.put( Cachex.put(:user_cache, "follow_state:#{user_ap_id}|#{target_ap_id}", state)
:user_cache,
"follow_state:#{user_ap_id}|#{target_ap_id}",
state
)
end end
def set_info_cache(user, args) do def set_info_cache(user, args) do
@ -197,32 +187,25 @@ defmodule Pleroma.User do
|> truncate_if_exists(:name, name_limit) |> truncate_if_exists(:name, name_limit)
|> truncate_if_exists(:bio, bio_limit) |> truncate_if_exists(:bio, bio_limit)
changes = changeset =
%User{} %User{local: false}
|> cast(params, [:bio, :name, :ap_id, :nickname, :avatar]) |> cast(params, [:bio, :name, :ap_id, :nickname, :avatar])
|> validate_required([:name, :ap_id]) |> validate_required([:name, :ap_id])
|> unique_constraint(:nickname) |> unique_constraint(:nickname)
|> validate_format(:nickname, @email_regex) |> validate_format(:nickname, @email_regex)
|> validate_length(:bio, max: bio_limit) |> validate_length(:bio, max: bio_limit)
|> validate_length(:name, max: name_limit) |> validate_length(:name, max: name_limit)
|> put_change(:local, false)
|> change_info(&User.Info.remote_user_creation(&1, params[:info])) |> change_info(&User.Info.remote_user_creation(&1, params[:info]))
if changes.valid? do case params[:info][:source_data] do
case info_cng.changes[:source_data] do
%{"followers" => followers, "following" => following} -> %{"followers" => followers, "following" => following} ->
changes changeset
|> put_change(:follower_address, followers) |> put_change(:follower_address, followers)
|> put_change(:following_address, following) |> put_change(:following_address, following)
_ -> _ ->
followers = User.ap_followers(%User{nickname: changes.changes[:nickname]}) followers = ap_followers(%User{nickname: get_field(changeset, :nickname)})
put_change(changeset, :follower_address, followers)
changes
|> put_change(:follower_address, followers)
end
else
changes
end end
end end
@ -308,10 +291,6 @@ defmodule Pleroma.User do
opts[:need_confirmation] opts[:need_confirmation]
end end
info_change =
User.Info.confirmation_changeset(%User.Info{}, need_confirmation: need_confirmation?)
changeset =
struct struct
|> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation]) |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
|> validate_required([:name, :nickname, :password, :password_confirmation]) |> validate_required([:name, :nickname, :password, :password_confirmation])
@ -323,28 +302,28 @@ defmodule Pleroma.User do
|> validate_format(:email, @email_regex) |> validate_format(:email, @email_regex)
|> validate_length(:bio, max: bio_limit) |> validate_length(:bio, max: bio_limit)
|> validate_length(:name, min: 1, max: name_limit) |> validate_length(:name, min: 1, max: name_limit)
|> put_change(:info, info_change) |> change_info(&User.Info.confirmation_changeset(&1, need_confirmation: need_confirmation?))
|> maybe_validate_required_email(opts[:external])
changeset = |> put_password_hash
if opts[:external] do |> put_ap_id()
changeset |> unique_constraint(:ap_id)
else |> put_following_and_follower_address()
validate_required(changeset, [:email])
end end
if changeset.valid? do def maybe_validate_required_email(changeset, true), do: changeset
ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]}) def maybe_validate_required_email(changeset, _), do: validate_required(changeset, [:email])
followers = User.ap_followers(%User{nickname: changeset.changes[:nickname]})
defp put_ap_id(changeset) do
ap_id = ap_id(%User{nickname: get_field(changeset, :nickname)})
put_change(changeset, :ap_id, ap_id)
end
defp put_following_and_follower_address(changeset) do
followers = ap_followers(%User{nickname: get_field(changeset, :nickname)})
changeset changeset
|> put_password_hash
|> put_change(:ap_id, ap_id)
|> unique_constraint(:ap_id)
|> put_change(:following, [followers]) |> put_change(:following, [followers])
|> put_change(:follower_address, followers) |> put_change(:follower_address, followers)
else
changeset
end
end end
defp autofollow_users(user) do defp autofollow_users(user) do
@ -359,9 +338,8 @@ defmodule Pleroma.User do
@doc "Inserts provided changeset, performs post-registration actions (confirmation email sending etc.)" @doc "Inserts provided changeset, performs post-registration actions (confirmation email sending etc.)"
def register(%Ecto.Changeset{} = changeset) do def register(%Ecto.Changeset{} = changeset) do
with {:ok, user} <- Repo.insert(changeset), with {:ok, user} <- Repo.insert(changeset) do
{:ok, user} <- post_register_action(user) do post_register_action(user)
{:ok, user}
end end
end end
@ -407,7 +385,7 @@ defmodule Pleroma.User do
end end
def maybe_direct_follow(%User{} = follower, %User{} = followed) do def maybe_direct_follow(%User{} = follower, %User{} = followed) do
if not User.ap_enabled?(followed) do if not ap_enabled?(followed) do
follow(follower, followed) follow(follower, followed)
else else
{:ok, follower} {:ok, follower}
@ -440,9 +418,7 @@ defmodule Pleroma.User do
{1, [follower]} = Repo.update_all(q, []) {1, [follower]} = Repo.update_all(q, [])
Enum.each(followeds, fn followed -> Enum.each(followeds, &update_follower_count/1)
update_follower_count(followed)
end)
set_cache(follower) set_cache(follower)
end end
@ -552,8 +528,6 @@ defmodule Pleroma.User do
def update_and_set_cache(changeset) do def update_and_set_cache(changeset) do
with {:ok, user} <- Repo.update(changeset, stale_error_field: :id) do with {:ok, user} <- Repo.update(changeset, stale_error_field: :id) do
set_cache(user) set_cache(user)
else
e -> e
end end
end end
@ -590,9 +564,7 @@ defmodule Pleroma.User do
key = "nickname:#{nickname}" key = "nickname:#{nickname}"
Cachex.fetch!(:user_cache, key, fn -> Cachex.fetch!(:user_cache, key, fn ->
user_result = get_or_fetch_by_nickname(nickname) case get_or_fetch_by_nickname(nickname) do
case user_result do
{:ok, user} -> {:commit, user} {:ok, user} -> {:commit, user}
{:error, _error} -> {:ignore, nil} {:error, _error} -> {:ignore, nil}
end end
@ -632,13 +604,11 @@ defmodule Pleroma.User do
def get_cached_user_info(user) do def get_cached_user_info(user) do
key = "user_info:#{user.id}" key = "user_info:#{user.id}"
Cachex.fetch!(:user_cache, key, fn _ -> user_info(user) end) Cachex.fetch!(:user_cache, key, fn -> user_info(user) end)
end end
def fetch_by_nickname(nickname) do def fetch_by_nickname(nickname) do
ap_try = ActivityPub.make_user_from_nickname(nickname) case ActivityPub.make_user_from_nickname(nickname) do
case ap_try do
{:ok, user} -> {:ok, user} {:ok, user} -> {:ok, user}
_ -> OStatus.make_user(nickname) _ -> OStatus.make_user(nickname)
end end
@ -673,7 +643,8 @@ defmodule Pleroma.User do
end end
def get_followers_query(user, page) do def get_followers_query(user, page) do
from(u in get_followers_query(user, nil)) user
|> get_followers_query(nil)
|> User.Query.paginate(page, 20) |> User.Query.paginate(page, 20)
end end
@ -689,18 +660,17 @@ defmodule Pleroma.User do
@spec get_external_followers(User.t(), pos_integer()) :: {:ok, list(User.t())} @spec get_external_followers(User.t(), pos_integer()) :: {:ok, list(User.t())}
def get_external_followers(user, page \\ nil) do def get_external_followers(user, page \\ nil) do
q =
user user
|> get_followers_query(page) |> get_followers_query(page)
|> User.Query.build(%{external: true}) |> User.Query.build(%{external: true})
|> Repo.all()
{:ok, Repo.all(q)}
end end
def get_followers_ids(user, page \\ nil) do def get_followers_ids(user, page \\ nil) do
q = get_followers_query(user, page) user
|> get_followers_query(page)
Repo.all(from(u in q, select: u.id)) |> select([u], u.id)
|> Repo.all()
end end
@spec get_friends_query(User.t(), pos_integer() | nil) :: Ecto.Query.t() @spec get_friends_query(User.t(), pos_integer() | nil) :: Ecto.Query.t()
@ -709,7 +679,8 @@ defmodule Pleroma.User do
end end
def get_friends_query(user, page) do def get_friends_query(user, page) do
from(u in get_friends_query(user, nil)) user
|> get_friends_query(nil)
|> User.Query.paginate(page, 20) |> User.Query.paginate(page, 20)
end end
@ -723,9 +694,10 @@ defmodule Pleroma.User do
end end
def get_friends_ids(user, page \\ nil) do def get_friends_ids(user, page \\ nil) do
q = get_friends_query(user, page) user
|> get_friends_query(page)
Repo.all(from(u in q, select: u.id)) |> select([u], u.id)
|> Repo.all()
end end
@spec get_follow_requests(User.t()) :: {:ok, [User.t()]} @spec get_follow_requests(User.t()) :: {:ok, [User.t()]}
@ -889,12 +861,10 @@ defmodule Pleroma.User do
end end
def subscribe(subscriber, %{ap_id: ap_id}) do def subscribe(subscriber, %{ap_id: ap_id}) do
with %User{} = subscribed <- get_cached_by_ap_id(ap_id) do
deny_follow_blocked = Pleroma.Config.get([:user, :deny_follow_blocked]) deny_follow_blocked = Pleroma.Config.get([:user, :deny_follow_blocked])
with %User{} = subscribed <- get_cached_by_ap_id(ap_id) do if blocks?(subscribed, subscriber) and deny_follow_blocked do
blocked = blocks?(subscribed, subscriber) and deny_follow_blocked
if blocked do
{:error, "Could not subscribe: #{subscribed.nickname} is blocking you"} {:error, "Could not subscribe: #{subscribed.nickname} is blocking you"}
else else
update_info(subscribed, &User.Info.add_to_subscribers(&1, subscriber.ap_id)) update_info(subscribed, &User.Info.add_to_subscribers(&1, subscriber.ap_id))
@ -933,9 +903,7 @@ defmodule Pleroma.User do
blocker blocker
end end
if following?(blocked, blocker) do if following?(blocked, blocker), do: unfollow(blocked, blocker)
unfollow(blocked, blocker)
end
{:ok, blocker} = update_follower_count(blocker) {:ok, blocker} = update_follower_count(blocker)
@ -1168,17 +1136,19 @@ defmodule Pleroma.User do
end end
defp delete_activity(%{data: %{"type" => "Like"}} = activity) do defp delete_activity(%{data: %{"type" => "Like"}} = activity) do
user = get_cached_by_ap_id(activity.actor)
object = Object.normalize(activity) object = Object.normalize(activity)
ActivityPub.unlike(user, object) activity.actor
|> get_cached_by_ap_id()
|> ActivityPub.unlike(object)
end end
defp delete_activity(%{data: %{"type" => "Announce"}} = activity) do defp delete_activity(%{data: %{"type" => "Announce"}} = activity) do
user = get_cached_by_ap_id(activity.actor)
object = Object.normalize(activity) object = Object.normalize(activity)
ActivityPub.unannounce(user, object) activity.actor
|> get_cached_by_ap_id()
|> ActivityPub.unannounce(object)
end end
defp delete_activity(_activity), do: "Doing nothing" defp delete_activity(_activity), do: "Doing nothing"
@ -1190,9 +1160,7 @@ defmodule Pleroma.User do
def html_filter_policy(_), do: Pleroma.Config.get([:markup, :scrub_policy]) def html_filter_policy(_), do: Pleroma.Config.get([:markup, :scrub_policy])
def fetch_by_ap_id(ap_id) do def fetch_by_ap_id(ap_id) do
ap_try = ActivityPub.make_user_from_ap_id(ap_id) case ActivityPub.make_user_from_ap_id(ap_id) do
case ap_try do
{:ok, user} -> {:ok, user} ->
{:ok, user} {:ok, user}
@ -1207,7 +1175,7 @@ defmodule Pleroma.User do
def get_or_fetch_by_ap_id(ap_id) do def get_or_fetch_by_ap_id(ap_id) do
user = get_cached_by_ap_id(ap_id) user = get_cached_by_ap_id(ap_id)
if !is_nil(user) and !User.needs_update?(user) do if !is_nil(user) and !needs_update?(user) do
{:ok, user} {:ok, user}
else else
# Whether to fetch initial posts for the user (if it's a new user & the fetching is enabled) # Whether to fetch initial posts for the user (if it's a new user & the fetching is enabled)
@ -1227,18 +1195,19 @@ defmodule Pleroma.User do
@doc "Creates an internal service actor by URI if missing. Optionally takes nickname for addressing." @doc "Creates an internal service actor by URI if missing. Optionally takes nickname for addressing."
def get_or_create_service_actor_by_ap_id(uri, nickname \\ nil) do def get_or_create_service_actor_by_ap_id(uri, nickname \\ nil) do
if user = get_cached_by_ap_id(uri) do with %User{} = user <- get_cached_by_ap_id(uri) do
user user
else else
changes = _ ->
{:ok, user} =
%User{info: %User.Info{}} %User{info: %User.Info{}}
|> cast(%{}, [:ap_id, :nickname, :local]) |> cast(%{}, [:ap_id, :nickname, :local])
|> put_change(:ap_id, uri) |> put_change(:ap_id, uri)
|> put_change(:nickname, nickname) |> put_change(:nickname, nickname)
|> put_change(:local, true) |> put_change(:local, true)
|> put_change(:follower_address, uri <> "/followers") |> put_change(:follower_address, uri <> "/followers")
|> Repo.insert()
{:ok, user} = Repo.insert(changes)
user user
end end
end end
@ -1296,23 +1265,21 @@ defmodule Pleroma.User do
# this is because we have synchronous follow APIs and need to simulate them # this is because we have synchronous follow APIs and need to simulate them
# with an async handshake # with an async handshake
def wait_and_refresh(_, %User{local: true} = a, %User{local: true} = b) do def wait_and_refresh(_, %User{local: true} = a, %User{local: true} = b) do
with %User{} = a <- User.get_cached_by_id(a.id), with %User{} = a <- get_cached_by_id(a.id),
%User{} = b <- User.get_cached_by_id(b.id) do %User{} = b <- get_cached_by_id(b.id) do
{:ok, a, b} {:ok, a, b}
else else
_e -> nil -> :error
:error
end end
end end
def wait_and_refresh(timeout, %User{} = a, %User{} = b) do def wait_and_refresh(timeout, %User{} = a, %User{} = b) do
with :ok <- :timer.sleep(timeout), with :ok <- :timer.sleep(timeout),
%User{} = a <- User.get_cached_by_id(a.id), %User{} = a <- get_cached_by_id(a.id),
%User{} = b <- User.get_cached_by_id(b.id) do %User{} = b <- get_cached_by_id(b.id) do
{:ok, a, b} {:ok, a, b}
else else
_e -> nil -> :error
:error
end end
end end
@ -1374,7 +1341,7 @@ defmodule Pleroma.User do
defp normalize_tags(tags) do defp normalize_tags(tags) do
[tags] [tags]
|> List.flatten() |> List.flatten()
|> Enum.map(&String.downcase(&1)) |> Enum.map(&String.downcase/1)
end end
defp local_nickname_regex do defp local_nickname_regex do

View File

@ -309,8 +309,7 @@ defmodule Pleroma.Web.CommonAPI do
with {:ok, user} <- User.update_info(user, &User.Info.set_source_data(&1, source_data)) do with {:ok, user} <- User.update_info(user, &User.Info.set_source_data(&1, source_data)) do
user user
else else
_e -> _e -> user
user
end end
ActivityPub.update(%{ ActivityPub.update(%{
@ -338,11 +337,8 @@ defmodule Pleroma.Web.CommonAPI do
{:ok, _user} <- User.update_info(user, &User.Info.add_pinnned_activity(&1, activity)) do {:ok, _user} <- User.update_info(user, &User.Info.add_pinnned_activity(&1, activity)) do
{:ok, activity} {:ok, activity}
else else
%{errors: [pinned_activities: {err, _}]} -> {:error, %{changes: %{info: %{errors: [pinned_activities: {err, _}]}}}} -> {:error, err}
{:error, err} _ -> {:error, dgettext("errors", "Could not pin")}
_ ->
{:error, dgettext("errors", "Could not pin")}
end end
end end
@ -351,11 +347,8 @@ defmodule Pleroma.Web.CommonAPI do
{:ok, _user} <- User.update_info(user, &User.Info.remove_pinnned_activity(&1, activity)) do {:ok, _user} <- User.update_info(user, &User.Info.remove_pinnned_activity(&1, activity)) do
{:ok, activity} {:ok, activity}
else else
%{errors: [pinned_activities: {err, _}]} -> %{errors: [pinned_activities: {err, _}]} -> {:error, err}
{:error, err} _ -> {:error, dgettext("errors", "Could not unpin")}
_ ->
{:error, dgettext("errors", "Could not unpin")}
end end
end end
@ -450,17 +443,13 @@ defmodule Pleroma.Web.CommonAPI do
defp set_visibility(activity, _), do: {:ok, activity} defp set_visibility(activity, _), do: {:ok, activity}
def hide_reblogs(user, muted) do def hide_reblogs(user, %{ap_id: ap_id} = _muted) do
ap_id = muted.ap_id
if ap_id not in user.info.muted_reblogs do if ap_id not in user.info.muted_reblogs do
User.update_info(user, &User.Info.add_reblog_mute(&1, ap_id)) User.update_info(user, &User.Info.add_reblog_mute(&1, ap_id))
end end
end end
def show_reblogs(user, muted) do def show_reblogs(user, %{ap_id: ap_id} = _muted) do
ap_id = muted.ap_id
if ap_id in user.info.muted_reblogs do if ap_id in user.info.muted_reblogs do
User.update_info(user, &User.Info.remove_reblog_mute(&1, ap_id)) User.update_info(user, &User.Info.remove_reblog_mute(&1, ap_id))
end end

View File

@ -15,14 +15,11 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
action_fallback(:errors) action_fallback(:errors)
def confirm_email(conn, %{"user_id" => uid, "token" => token}) do def confirm_email(conn, %{"user_id" => uid, "token" => token}) do
true <- user.local,
true <- user.info.confirmation_pending,
true <- user.info.confirmation_token == token,
conn
|> redirect(to: "/")
with %User{info: info} = user <- User.get_cached_by_id(uid), with %User{info: info} = user <- User.get_cached_by_id(uid),
true <- user.local and info.confirmation_pending and info.confirmation_token == token,
{:ok, _} <- {:ok, _} <-
User.update_info(user, &User.Info.confirmation_changeset(&1, need_confirmation: false)) do User.update_info(user, &User.Info.confirmation_changeset(&1, need_confirmation: false)) do
redirect(conn, to: "/")
end end
end end