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, 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: %{}
23 field :follower_address, :string
24 has_many :notifications, Notification
29 def avatar_url(user) do
31 %{"url" => [%{"href" => href} | _]} -> href
32 _ -> "#{Web.base_url()}/images/avi.png"
36 def banner_url(user) do
37 case user.info["banner"] do
38 %{"url" => [%{"href" => href} | _]} -> href
39 _ -> "#{Web.base_url()}/images/banner.png"
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 case changes.changes[:info]["source_data"] do
84 %{"followers" => followers} ->
86 |> put_change(:follower_address, followers)
88 followers = User.ap_followers(%User{nickname: changes.changes[:nickname]})
90 |> put_change(:follower_address, followers)
97 def update_changeset(struct, params \\ %{}) do
99 |> cast(params, [:bio, :name])
100 |> unique_constraint(:nickname)
101 |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
102 |> validate_length(:bio, min: 1, max: 1000)
103 |> validate_length(:name, min: 1, max: 100)
106 def password_update_changeset(struct, params) do
108 |> cast(params, [:password, :password_confirmation])
109 |> validate_required([:password, :password_confirmation])
110 |> validate_confirmation(:password)
112 if changeset.valid? do
113 hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
115 |> put_change(:password_hash, hashed)
121 def reset_password(user, data) do
122 update_and_set_cache(password_update_changeset(user, data))
125 def register_changeset(struct, params \\ %{}) do
127 |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
128 |> validate_required([:bio, :email, :name, :nickname, :password, :password_confirmation])
129 |> validate_confirmation(:password)
130 |> unique_constraint(:email)
131 |> unique_constraint(:nickname)
132 |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
133 |> validate_format(:email, @email_regex)
134 |> validate_length(:bio, min: 1, max: 1000)
135 |> validate_length(:name, min: 1, max: 100)
137 if changeset.valid? do
138 hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
139 ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]})
140 followers = User.ap_followers(%User{nickname: changeset.changes[:nickname]})
142 |> put_change(:password_hash, hashed)
143 |> put_change(:ap_id, ap_id)
144 |> put_change(:following, [followers])
145 |> put_change(:follower_address, followers)
151 def follow(%User{} = follower, %User{info: info} = followed) do
152 ap_followers = followed.follower_address
154 if following?(follower, followed) or info["deactivated"] do
156 "Could not follow user: #{followed.nickname} is already on your list."}
158 if !followed.local && follower.local && !ap_enabled?(followed) do
159 Websub.subscribe(follower, followed)
162 following = [ap_followers | follower.following]
166 |> follow_changeset(%{following: following})
167 |> update_and_set_cache
169 {:ok, _} = update_follower_count(followed)
175 def unfollow(%User{} = follower, %User{} = followed) do
176 ap_followers = followed.follower_address
177 if following?(follower, followed) and follower.ap_id != followed.ap_id do
178 following = follower.following
179 |> List.delete(ap_followers)
181 { :ok, follower } = follower
182 |> follow_changeset(%{following: following})
183 |> update_and_set_cache
185 {:ok, followed} = update_follower_count(followed)
187 {:ok, follower, Utils.fetch_latest_follow(follower, followed)}
189 {:error, "Not subscribed!"}
193 def following?(%User{} = follower, %User{} = followed) do
194 Enum.member?(follower.following, followed.follower_address)
197 def get_by_ap_id(ap_id) do
198 Repo.get_by(User, ap_id: ap_id)
201 def update_and_set_cache(changeset) do
202 with {:ok, user} <- Repo.update(changeset) do
203 Cachex.set(:user_cache, "ap_id:#{user.ap_id}", user)
204 Cachex.set(:user_cache, "nickname:#{user.nickname}", user)
205 Cachex.set(:user_cache, "user_info:#{user.id}", user_info(user))
212 def get_cached_by_ap_id(ap_id) do
213 key = "ap_id:#{ap_id}"
214 Cachex.get!(:user_cache, key, fallback: fn(_) -> get_by_ap_id(ap_id) end)
217 def get_cached_by_nickname(nickname) do
218 key = "nickname:#{nickname}"
219 Cachex.get!(:user_cache, key, fallback: fn(_) -> get_or_fetch_by_nickname(nickname) end)
222 def get_by_nickname(nickname) do
223 Repo.get_by(User, nickname: nickname)
226 def get_cached_user_info(user) do
227 key = "user_info:#{user.id}"
228 Cachex.get!(:user_cache, key, fallback: fn(_) -> user_info(user) end)
231 def fetch_by_nickname(nickname) do
232 ap_try = ActivityPub.make_user_from_nickname(nickname)
235 {:ok, user} -> {:ok, user}
236 _ -> OStatus.make_user(nickname)
240 def get_or_fetch_by_nickname(nickname) do
241 with %User{} = user <- get_by_nickname(nickname) do
244 with [_nick, _domain] <- String.split(nickname, "@"),
245 {:ok, user} <- fetch_by_nickname(nickname) do
252 def get_followers(%User{id: id, follower_address: follower_address}) do
254 where: ^follower_address in u.following,
260 def get_friends(%User{id: id, following: following}) do
262 where: u.follower_address in ^following,
268 def increase_note_count(%User{} = user) do
269 note_count = (user.info["note_count"] || 0) + 1
270 new_info = Map.put(user.info, "note_count", note_count)
272 cs = info_changeset(user, %{info: new_info})
274 update_and_set_cache(cs)
277 def update_note_count(%User{} = user) do
278 note_count_query = from a in Object,
279 where: fragment("?->>'actor' = ? and ?->>'type' = 'Note'", a.data, ^user.ap_id, a.data),
282 note_count = Repo.one(note_count_query)
284 new_info = Map.put(user.info, "note_count", note_count)
286 cs = info_changeset(user, %{info: new_info})
288 update_and_set_cache(cs)
291 def update_follower_count(%User{} = user) do
292 follower_count_query = from u in User,
293 where: ^user.follower_address in u.following,
294 where: u.id != ^user.id,
297 follower_count = Repo.one(follower_count_query)
299 new_info = Map.put(user.info, "follower_count", follower_count)
301 cs = info_changeset(user, %{info: new_info})
303 update_and_set_cache(cs)
306 def get_notified_from_activity(%Activity{recipients: to}) do
307 query = from u in User,
308 where: u.ap_id in ^to,
309 where: u.local == true
314 def get_recipients_from_activity(%Activity{recipients: to}) do
315 query = from u in User,
316 where: u.ap_id in ^to,
317 or_where: fragment("? \\\?| ?", u.following, ^to)
319 query = from u in query,
320 where: u.local == true
325 def search(query, resolve) do
327 User.get_or_fetch_by_nickname(query)
330 where: fragment("(to_tsvector('english', ?) || to_tsvector('english', ?)) @@ plainto_tsquery('english', ?)", u.nickname, u.name, ^query),
335 def block(user, %{ap_id: ap_id}) do
336 blocks = user.info["blocks"] || []
337 new_blocks = Enum.uniq([ap_id | blocks])
338 new_info = Map.put(user.info, "blocks", new_blocks)
340 cs = User.info_changeset(user, %{info: new_info})
341 update_and_set_cache(cs)
344 def unblock(user, %{ap_id: ap_id}) do
345 blocks = user.info["blocks"] || []
346 new_blocks = List.delete(blocks, ap_id)
347 new_info = Map.put(user.info, "blocks", new_blocks)
349 cs = User.info_changeset(user, %{info: new_info})
350 update_and_set_cache(cs)
353 def blocks?(user, %{ap_id: ap_id}) do
354 blocks = user.info["blocks"] || []
355 Enum.member?(blocks, ap_id)
358 def local_user_query() do
360 where: u.local == true
363 def deactivate (%User{} = user) do
364 new_info = Map.put(user.info, "deactivated", true)
365 cs = User.info_changeset(user, %{info: new_info})
366 update_and_set_cache(cs)
369 def delete (%User{} = user) do
370 {:ok, user} = User.deactivate(user)
372 # Remove all relationships
373 {:ok, followers } = User.get_followers(user)
375 |> Enum.each(fn (follower) -> User.unfollow(follower, user) end)
377 {:ok, friends} = User.get_friends(user)
379 |> Enum.each(fn (followed) -> User.unfollow(user, followed) end)
381 query = from a in Activity,
382 where: a.actor == ^user.ap_id
385 |> Enum.each(fn (activity) ->
386 case activity.data["type"] do
387 "Create" -> ActivityPub.delete(Object.get_by_ap_id(activity.data["object"]["id"]))
388 _ -> "Doing nothing" # TODO: Do something with likes, follows, repeats.
395 def get_or_fetch_by_ap_id(ap_id) do
396 if user = get_by_ap_id(ap_id) do
399 with {:ok, user} <- ActivityPub.make_user_from_ap_id(ap_id) do
406 def public_key_from_info(%{"source_data" => %{"publicKey" => %{"publicKeyPem" => public_key_pem}}}) do
407 key = :public_key.pem_decode(public_key_pem)
409 |> :public_key.pem_entry_decode()
415 def public_key_from_info(%{"magic_key" => magic_key}) do
416 {:ok, Pleroma.Web.Salmon.decode_key(magic_key)}
419 def get_public_key_for_ap_id(ap_id) do
420 with %User{} = user <- get_or_fetch_by_ap_id(ap_id),
421 {:ok, public_key} <- public_key_from_info(user.info) do
428 defp blank?(""), do: nil
429 defp blank?(n), do: n
431 def insert_or_update_user(data) do
433 |> Map.put(:name, blank?(data[:name]) || data[:nickname])
434 cs = User.remote_user_creation(data)
435 Repo.insert(cs, on_conflict: :replace_all, conflict_target: :nickname)
438 def ap_enabled?(%User{info: info}), do: info["ap_enabled"]