1 defmodule Pleroma.User do
4 import Ecto.{Changeset, Query}
5 alias Pleroma.{Repo, User, Object, Web, Activity, Notification}
7 alias Pleroma.Web.{OStatus, Websub}
8 alias Pleroma.Web.ActivityPub.Utils
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: %{}
23 field :follower_address, :string
24 has_many :notifications, Notification
29 def avatar_url(user) do
31 %{"url" => [%{"href" => href} | _]} -> href
32 _ -> "https://placehold.it/48x48"
36 def banner_url(user) do
37 case user.info["banner"] do
38 %{"url" => [%{"href" => href} | _]} -> href
43 def ap_id(%User{nickname: nickname}) do
44 "#{Web.base_url}/users/#{nickname}"
47 def ap_followers(%User{} = user) do
48 "#{ap_id(user)}/followers"
51 def follow_changeset(struct, params \\ %{}) do
53 |> cast(params, [:following])
54 |> validate_required([:following])
57 def info_changeset(struct, params \\ %{}) do
59 |> cast(params, [:info])
60 |> validate_required([:info])
63 def user_info(%User{} = user) do
64 oneself = if user.local, do: 1, else: 0
66 following_count: length(user.following) - oneself,
67 note_count: user.info["note_count"] || 0,
68 follower_count: user.info["follower_count"] || 0
72 @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])?)*$/
73 def remote_user_creation(params) do
75 |> cast(params, [:bio, :name, :ap_id, :nickname, :info, :avatar])
76 |> validate_required([:name, :ap_id, :nickname])
77 |> unique_constraint(:nickname)
78 |> validate_format(:nickname, @email_regex)
79 |> validate_length(:bio, max: 5000)
80 |> validate_length(:name, max: 100)
81 |> put_change(:local, false)
83 followers = User.ap_followers(%User{nickname: changes.changes[:nickname]})
85 |> put_change(:follower_address, followers)
91 def update_changeset(struct, params \\ %{}) do
93 |> cast(params, [:bio, :name])
94 |> unique_constraint(:nickname)
95 |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
96 |> validate_length(:bio, min: 1, max: 1000)
97 |> validate_length(:name, min: 1, max: 100)
100 def password_update_changeset(struct, params) do
102 |> cast(params, [:password, :password_confirmation])
103 |> validate_required([:password, :password_confirmation])
104 |> validate_confirmation(:password)
106 if changeset.valid? do
107 hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
109 |> put_change(:password_hash, hashed)
115 def reset_password(user, data) do
116 Repo.update(password_update_changeset(user, data))
119 def register_changeset(struct, params \\ %{}) do
121 |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
122 |> validate_required([:bio, :email, :name, :nickname, :password, :password_confirmation])
123 |> validate_confirmation(:password)
124 |> unique_constraint(:email)
125 |> unique_constraint(:nickname)
126 |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
127 |> validate_format(:email, @email_regex)
128 |> validate_length(:bio, min: 1, max: 1000)
129 |> validate_length(:name, min: 1, max: 100)
131 if changeset.valid? do
132 hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
133 ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]})
134 followers = User.ap_followers(%User{nickname: changeset.changes[:nickname]})
136 |> put_change(:password_hash, hashed)
137 |> put_change(:ap_id, ap_id)
138 |> put_change(:following, [followers])
139 |> put_change(:follower_address, followers)
145 def follow(%User{} = follower, %User{} = followed) do
146 ap_followers = followed.follower_address
147 if following?(follower, followed) do
149 "Could not follow user: #{followed.nickname} is already on your list."}
151 if !followed.local && follower.local do
152 Websub.subscribe(follower, followed)
155 following = [ap_followers | follower.following]
159 |> follow_changeset(%{following: following})
162 {:ok, _} = update_follower_count(followed)
168 def unfollow(%User{} = follower, %User{} = followed) do
169 ap_followers = followed.follower_address
170 if following?(follower, followed) and follower.ap_id != followed.ap_id do
171 following = follower.following
172 |> List.delete(ap_followers)
174 { :ok, follower } = follower
175 |> follow_changeset(%{following: following})
178 {:ok, followed} = update_follower_count(followed)
180 {:ok, follower, Utils.fetch_latest_follow(follower, followed)}
182 {:error, "Not subscribed!"}
186 def following?(%User{} = follower, %User{} = followed) do
187 Enum.member?(follower.following, followed.follower_address)
190 def get_by_ap_id(ap_id) do
191 Repo.get_by(User, ap_id: ap_id)
194 def get_cached_by_ap_id(ap_id) do
195 key = "ap_id:#{ap_id}"
196 Cachex.get!(:user_cache, key, fallback: fn(_) -> get_by_ap_id(ap_id) end)
199 def get_cached_by_nickname(nickname) do
200 key = "nickname:#{nickname}"
201 Cachex.get!(:user_cache, key, fallback: fn(_) -> get_or_fetch_by_nickname(nickname) end)
204 def get_by_nickname(nickname) do
205 Repo.get_by(User, nickname: nickname)
208 def get_cached_user_info(user) do
209 key = "user_info:#{user.id}"
210 Cachex.get!(:user_cache, key, fallback: fn(_) -> user_info(user) end)
213 def get_or_fetch_by_nickname(nickname) do
214 with %User{} = user <- get_by_nickname(nickname) do
217 with [_nick, _domain] <- String.split(nickname, "@"),
218 {:ok, user} <- OStatus.make_user(nickname) do
225 # TODO: these queries could be more efficient if the type in postgresql wasn't map, but array.
226 def get_followers(%User{id: id, follower_address: follower_address}) do
228 where: fragment("? @> ?", u.following, ^follower_address ),
234 def get_friends(%User{id: id, following: following}) do
236 where: u.follower_address in ^following,
242 def increase_note_count(%User{} = user) do
243 note_count = (user.info["note_count"] || 0) + 1
244 new_info = Map.put(user.info, "note_count", note_count)
246 cs = info_changeset(user, %{info: new_info})
251 def update_note_count(%User{} = user) do
252 note_count_query = from a in Object,
253 where: fragment("?->>'actor' = ? and ?->>'type' = 'Note'", a.data, ^user.ap_id, a.data),
256 note_count = Repo.one(note_count_query)
258 new_info = Map.put(user.info, "note_count", note_count)
260 cs = info_changeset(user, %{info: new_info})
265 def update_follower_count(%User{} = user) do
266 follower_count_query = from u in User,
267 where: fragment("? @> ?", u.following, ^user.follower_address),
268 where: u.id != ^user.id,
271 follower_count = Repo.one(follower_count_query)
273 new_info = Map.put(user.info, "follower_count", follower_count)
275 cs = info_changeset(user, %{info: new_info})
280 def get_notified_from_activity(%Activity{data: %{"to" => to}}) do
281 query = from u in User,
282 where: u.ap_id in ^to,
283 where: u.local == true
288 def get_recipients_from_activity(%Activity{data: %{"to" => to}}) do
289 query = from u in User,
290 where: u.ap_id in ^to,
291 or_where: fragment("? \\\?| ?", u.following, ^to)
293 query = from u in query,
294 where: u.local == true
299 def search(query, resolve) do
301 User.get_or_fetch_by_nickname(query)
304 where: fragment("(to_tsvector('english', ?) || to_tsvector('english', ?)) @@ plainto_tsquery('english', ?)", u.nickname, u.name, ^query),
309 def block(user, %{ap_id: ap_id}) do
310 blocks = user.info["blocks"] || []
311 new_blocks = Enum.uniq([ap_id | blocks])
312 new_info = Map.put(user.info, "blocks", new_blocks)
314 cs = User.info_changeset(user, %{info: new_info})
318 def unblock(user, %{ap_id: ap_id}) do
319 blocks = user.info["blocks"] || []
320 new_blocks = List.delete(blocks, ap_id)
321 new_info = Map.put(user.info, "blocks", new_blocks)
323 cs = User.info_changeset(user, %{info: new_info})
327 def blocks?(user, %{ap_id: ap_id}) do
328 blocks = user.info["blocks"] || []
329 Enum.member?(blocks, ap_id)
332 def local_user_query() do
334 where: u.local == true