X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fuser.ex;h=7b0edee75d3728d2283dff4a5f02a9e9eeda2abc;hb=756764266cb5319a775845635b91b04aacf16aae;hp=64c69b209cddc9ae339f4e1a13b2aad6d6361d87;hpb=2e2f4587050da652fb0c4b8822d912301a8be273;p=akkoma diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 64c69b209..7b0edee75 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -4,7 +4,7 @@ defmodule Pleroma.User do import Ecto.{Changeset, Query} alias Pleroma.{Repo, User, Object, Web, Activity, Notification} alias Comeonin.Pbkdf2 - alias Pleroma.Web.{OStatus, Websub} + alias Pleroma.Web.{OStatus, Websub, OAuth} alias Pleroma.Web.ActivityPub.{Utils, ActivityPub} schema "users" do @@ -19,10 +19,11 @@ defmodule Pleroma.User do field(:ap_id, :string) field(:avatar, :map) field(:local, :boolean, default: true) - field(:info, :map, default: %{}) field(:follower_address, :string) field(:search_distance, :float, virtual: true) + field(:last_refreshed_at, :naive_datetime) has_many(:notifications, Notification) + embeds_one(:info, Pleroma.User.Info) timestamps() end @@ -41,6 +42,10 @@ defmodule Pleroma.User do end end + def profile_url(%User{info: %{"source_data" => %{"url" => url}}}), do: url + def profile_url(%User{ap_id: ap_id}), do: ap_id + def profile_url(_), do: nil + def ap_id(%User{nickname: nickname}) do "#{Web.base_url()}/users/#{nickname}" end @@ -66,10 +71,10 @@ defmodule Pleroma.User do %{ following_count: length(user.following) - oneself, - note_count: user.info["note_count"] || 0, - follower_count: user.info["follower_count"] || 0, - locked: user.info["locked"] || false, - default_scope: user.info["default_scope"] || "public" + note_count: user.info.note_count, + follower_count: user.info.follower_count, + locked: user.info.locked, + default_scope: user.info.default_scope } end @@ -112,8 +117,12 @@ defmodule Pleroma.User do end def upgrade_changeset(struct, params \\ %{}) do + params = + params + |> Map.put(:last_refreshed_at, NaiveDateTime.utc_now()) + struct - |> cast(params, [:bio, :name, :info, :follower_address, :avatar]) + |> cast(params, [:bio, :name, :info, :follower_address, :avatar, :last_refreshed_at]) |> unique_constraint(:nickname) |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/) |> validate_length(:bio, max: 5000) @@ -127,6 +136,9 @@ defmodule Pleroma.User do |> validate_required([:password, :password_confirmation]) |> validate_confirmation(:password) + OAuth.Token.delete_user_tokens(struct) + OAuth.Authorization.delete_user_authorizations(struct) + if changeset.valid? do hashed = Pbkdf2.hashpwsalt(changeset.changes[:password]) @@ -169,33 +181,26 @@ defmodule Pleroma.User do end end - def maybe_direct_follow(%User{} = follower, %User{info: info} = followed) do - user_config = Application.get_env(:pleroma, :user) - deny_follow_blocked = Keyword.get(user_config, :deny_follow_blocked) + def needs_update?(%User{local: true}), do: false - user_info = user_info(followed) + def needs_update?(%User{local: false, last_refreshed_at: nil}), do: true - should_direct_follow = - cond do - # if the account is locked, don't pre-create the relationship - user_info[:locked] == true -> - false + def needs_update?(%User{local: false} = user) do + NaiveDateTime.diff(NaiveDateTime.utc_now(), user.last_refreshed_at) >= 86400 + end - # if the users are blocking each other, we shouldn't even be here, but check for it anyway - deny_follow_blocked and - (User.blocks?(follower, followed) or User.blocks?(followed, follower)) -> - false + def needs_update?(_), do: true - # if OStatus, then there is no three-way handshake to follow - User.ap_enabled?(followed) != true -> - true + def maybe_direct_follow(%User{} = follower, %User{local: true, info: %{"locked" => true}}) do + {:ok, follower} + end - # if there are no other reasons not to, just pre-create the relationship - true -> - true - end + def maybe_direct_follow(%User{} = follower, %User{local: true} = followed) do + follow(follower, followed) + end - if should_direct_follow do + def maybe_direct_follow(%User{} = follower, %User{} = followed) do + if !User.ap_enabled?(followed) do follow(follower, followed) else {:ok, follower} @@ -290,6 +295,7 @@ defmodule Pleroma.User do def invalidate_cache(user) do Cachex.del(:user_cache, "ap_id:#{user.ap_id}") Cachex.del(:user_cache, "nickname:#{user.nickname}") + Cachex.del(:user_cache, "user_info:#{user.id}") end def get_cached_by_ap_id(ap_id) do @@ -405,22 +411,23 @@ defmodule Pleroma.User do end def increase_note_count(%User{} = user) do - note_count = (user.info["note_count"] || 0) + 1 - new_info = Map.put(user.info, "note_count", note_count) + info_cng = User.Info.add_to_note_count(user.info, 1) - cs = info_changeset(user, %{info: new_info}) + cng = + change(user) + |> put_embed(:info, info_cng) - update_and_set_cache(cs) + update_and_set_cache(cng) end def decrease_note_count(%User{} = user) do - note_count = user.info["note_count"] || 0 - note_count = if note_count <= 0, do: 0, else: note_count - 1 - new_info = Map.put(user.info, "note_count", note_count) + info_cng = User.Info.add_to_note_count(user.info, -1) - cs = info_changeset(user, %{info: new_info}) + cng = + change(user) + |> put_embed(:info, info_cng) - update_and_set_cache(cs) + update_and_set_cache(cng) end def update_note_count(%User{} = user) do @@ -451,43 +458,36 @@ defmodule Pleroma.User do follower_count = Repo.one(follower_count_query) - new_info = Map.put(user.info, "follower_count", follower_count) + info_cng = + user.info + |> User.Info.set_follower_count(follower_count) - cs = info_changeset(user, %{info: new_info}) + cng = + change(user) + |> put_embed(:info, info_cng) - update_and_set_cache(cs) + update_and_set_cache(cng) end - def get_notified_from_activity_query(to) do + def get_users_from_set_query(ap_ids, false) do from( u in User, - where: u.ap_id in ^to, - where: u.local == true + where: u.ap_id in ^ap_ids ) end - def get_notified_from_activity(%Activity{recipients: to, data: %{"type" => "Announce"} = data}) do - object = Object.normalize(data["object"]) - actor = User.get_cached_by_ap_id(data["actor"]) + def get_users_from_set_query(ap_ids, true) do + query = get_users_from_set_query(ap_ids, false) - # ensure that the actor who published the announced object appears only once - to = - if actor.nickname != nil do - to ++ [object.data["actor"]] - else - to - end - |> Enum.uniq() - - query = get_notified_from_activity_query(to) - - Repo.all(query) + from( + u in query, + where: u.local == true + ) end - def get_notified_from_activity(%Activity{recipients: to}) do - query = get_notified_from_activity_query(to) - - Repo.all(query) + def get_users_from_set(ap_ids, local_only \\ true) do + get_users_from_set_query(ap_ids, local_only) + |> Repo.all() end def get_recipients_from_activity(%Activity{recipients: to}) do @@ -503,7 +503,7 @@ defmodule Pleroma.User do Repo.all(query) end - def search(query, resolve) do + def search(query, resolve \\ false) do # strip the beginning @ off if there is a query query = String.trim_leading(query, "@") @@ -609,10 +609,22 @@ defmodule Pleroma.User do ) end - def deactivate(%User{} = user) do - new_info = Map.put(user.info, "deactivated", true) - cs = User.info_changeset(user, %{info: new_info}) - update_and_set_cache(cs) + def moderator_user_query() do + from( + u in User, + where: u.local == true, + where: fragment("?->'is_moderator' @> 'true'", u.info) + ) + end + + def deactivate(%User{} = user, status \\ true) do + info_cng = User.Info.set_activation_status(user.info, status) + + cng = + change(user) + |> put_embed(:info, info_cng) + + update_and_set_cache(cng) end def delete(%User{} = user) do @@ -643,11 +655,19 @@ defmodule Pleroma.User do end end) - :ok + {:ok, user} + end + + def html_filter_policy(%User{info: %{"no_rich_text" => true}}) do + Pleroma.HTML.Scrubber.TwitterText end + def html_filter_policy(_), do: nil + def get_or_fetch_by_ap_id(ap_id) do - if user = get_by_ap_id(ap_id) do + user = get_by_ap_id(ap_id) + + if !is_nil(user) and !User.needs_update?(user) do user else ap_try = ActivityPub.make_user_from_ap_id(ap_id) @@ -722,6 +742,7 @@ defmodule Pleroma.User do Repo.insert(cs, on_conflict: :replace_all, conflict_target: :nickname) end + def ap_enabled?(%User{local: true}), do: true def ap_enabled?(%User{info: info}), do: info["ap_enabled"] def ap_enabled?(_), do: false @@ -732,4 +753,28 @@ defmodule Pleroma.User do get_or_fetch_by_nickname(uri_or_nickname) end end + + # wait a period of time and return newest version of the User structs + # this is because we have synchronous follow APIs and need to simulate them + # with an async handshake + def wait_and_refresh(_, %User{local: true} = a, %User{local: true} = b) do + with %User{} = a <- Repo.get(User, a.id), + %User{} = b <- Repo.get(User, b.id) do + {:ok, a, b} + else + _e -> + :error + end + end + + def wait_and_refresh(timeout, %User{} = a, %User{} = b) do + with :ok <- :timer.sleep(timeout), + %User{} = a <- Repo.get(User, a.id), + %User{} = b <- Repo.get(User, b.id) do + {:ok, a, b} + else + _e -> + :error + end + end end