1 defmodule Pleroma.User do
4 import Ecto.{Changeset, Query}
5 alias Pleroma.{Repo, User, Object, Web}
7 alias Pleroma.Web.{OStatus, Websub}
8 alias Pleroma.Web.ActivityPub.ActivityPub
14 field :nickname, :string
15 field :password_hash, :string
16 field :password, :string, virtual: true
17 field :password_confirmation, :string, virtual: true
18 field :following, {:array, :string}, default: []
21 field :local, :boolean, default: true
22 field :info, :map, default: %{}
27 def avatar_url(user) do
29 %{"url" => [%{"href" => href} | _]} -> href
30 _ -> "https://placehold.it/48x48"
34 def ap_id(%User{nickname: nickname}) do
35 "#{Web.base_url}/users/#{nickname}"
38 def ap_followers(%User{} = user) do
39 "#{ap_id(user)}/followers"
42 def follow_changeset(struct, params \\ %{}) do
44 |> cast(params, [:following])
45 |> validate_required([:following])
48 def user_info(%User{} = user) do
49 note_count_query = from a in Object,
50 where: fragment("? @> ?", a.data, ^%{actor: user.ap_id, type: "Note"}),
53 follower_count_query = from u in User,
54 where: fragment("? @> ?", u.following, ^User.ap_followers(user)),
58 following_count: length(user.following),
59 note_count: Repo.one(note_count_query),
60 follower_count: Repo.one(follower_count_query)
64 def register_changeset(struct, params \\ %{}) do
66 |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
67 |> validate_required([:bio, :email, :name, :nickname, :password, :password_confirmation])
68 |> validate_confirmation(:password)
69 |> unique_constraint(:email)
70 |> unique_constraint(:nickname)
71 |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
73 if changeset.valid? do
74 hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
75 ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]})
76 followers = User.ap_followers(%User{nickname: changeset.changes[:nickname]})
78 |> put_change(:password_hash, hashed)
79 |> put_change(:ap_id, ap_id)
80 |> put_change(:following, [followers])
86 def follow(%User{} = follower, %User{} = followed) do
87 ap_followers = User.ap_followers(followed)
88 if following?(follower, followed) do
90 "Could not follow user: #{followed.nickname} is already on your list."}
93 Websub.subscribe(follower, followed)
96 following = [ap_followers | follower.following]
100 |> follow_changeset(%{following: following})
105 def unfollow(%User{} = follower, %User{} = followed) do
106 ap_followers = User.ap_followers(followed)
107 if following?(follower, followed) do
108 following = follower.following
109 |> List.delete(ap_followers)
111 { :ok, follower } = follower
112 |> follow_changeset(%{following: following})
114 { :ok, follower, ActivityPub.fetch_latest_follow(follower, followed)}
116 {:error, "Not subscribed!"}
120 def following?(%User{} = follower, %User{} = followed) do
121 Enum.member?(follower.following, User.ap_followers(followed))
124 def get_cached_by_ap_id(ap_id) do
125 key = "ap_id:#{ap_id}"
126 Cachex.get!(:user_cache, key, fallback: fn(_) -> Repo.get_by(User, ap_id: ap_id) end)
129 def get_cached_by_nickname(nickname) do
130 key = "nickname:#{nickname}"
131 Cachex.get!(:user_cache, key, fallback: fn(_) -> get_or_fetch_by_nickname(nickname) end)
134 def get_by_nickname(nickname) do
135 Repo.get_by(User, nickname: nickname)
138 def get_cached_user_info(user) do
139 key = "user_info:#{user.id}"
140 Cachex.get!(:user_cache, key, fallback: fn(_) -> user_info(user) end)
143 def get_or_fetch_by_nickname(nickname) do
144 with %User{} = user <- get_by_nickname(nickname) do
147 with [nick, domain] <- String.split(nickname, "@"),
148 {:ok, user} <- OStatus.make_user(nickname) do