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
65 following_count: length(user.following),
66 note_count: user.info["note_count"] || 0,
67 follower_count: user.info["follower_count"] || 0
71 @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])?)*$/
72 def remote_user_creation(params) do
74 |> cast(params, [:bio, :name, :ap_id, :nickname, :info, :avatar])
75 |> validate_required([:name, :ap_id, :nickname])
76 |> unique_constraint(:nickname)
77 |> validate_format(:nickname, @email_regex)
78 |> validate_length(:bio, max: 5000)
79 |> validate_length(:name, max: 100)
80 |> put_change(:local, false)
82 followers = User.ap_followers(%User{nickname: changes.changes[:nickname]})
84 |> put_change(:follower_address, followers)
90 def update_changeset(struct, params \\ %{}) do
92 |> cast(params, [:bio, :name])
93 |> unique_constraint(:nickname)
94 |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
95 |> validate_length(:bio, min: 1, max: 1000)
96 |> validate_length(:name, min: 1, max: 100)
99 def password_update_changeset(struct, params) do
101 |> cast(params, [:password, :password_confirmation])
102 |> validate_required([:password, :password_confirmation])
103 |> validate_confirmation(:password)
105 if changeset.valid? do
106 hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
108 |> put_change(:password_hash, hashed)
114 def reset_password(user, data) do
115 Repo.update(password_update_changeset(user, data))
118 def register_changeset(struct, params \\ %{}) do
120 |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
121 |> validate_required([:bio, :email, :name, :nickname, :password, :password_confirmation])
122 |> validate_confirmation(:password)
123 |> unique_constraint(:email)
124 |> unique_constraint(:nickname)
125 |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
126 |> validate_format(:email, @email_regex)
127 |> validate_length(:bio, min: 1, max: 1000)
128 |> validate_length(:name, min: 1, max: 100)
130 if changeset.valid? do
131 hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
132 ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]})
133 followers = User.ap_followers(%User{nickname: changeset.changes[:nickname]})
135 |> put_change(:password_hash, hashed)
136 |> put_change(:ap_id, ap_id)
137 |> put_change(:following, [followers])
138 |> put_change(:follower_address, followers)
144 def follow(%User{} = follower, %User{} = followed) do
145 ap_followers = followed.follower_address
146 if following?(follower, followed) do
148 "Could not follow user: #{followed.nickname} is already on your list."}
150 if !followed.local && follower.local do
151 Websub.subscribe(follower, followed)
154 following = [ap_followers | follower.following]
158 |> follow_changeset(%{following: following})
161 {:ok, _} = update_follower_count(followed)
167 def unfollow(%User{} = follower, %User{} = followed) do
168 ap_followers = followed.follower_address
169 if following?(follower, followed) do
170 following = follower.following
171 |> List.delete(ap_followers)
173 { :ok, follower } = follower
174 |> follow_changeset(%{following: following})
177 {:ok, followed} = update_follower_count(followed)
179 {:ok, follower, Utils.fetch_latest_follow(follower, followed)}
181 {:error, "Not subscribed!"}
185 def following?(%User{} = follower, %User{} = followed) do
186 Enum.member?(follower.following, followed.follower_address)
189 def get_by_ap_id(ap_id) do
190 Repo.get_by(User, ap_id: ap_id)
193 def get_cached_by_ap_id(ap_id) do
194 key = "ap_id:#{ap_id}"
195 Cachex.get!(:user_cache, key, fallback: fn(_) -> get_by_ap_id(ap_id) end)
198 def get_cached_by_nickname(nickname) do
199 key = "nickname:#{nickname}"
200 Cachex.get!(:user_cache, key, fallback: fn(_) -> get_or_fetch_by_nickname(nickname) end)
203 def get_by_nickname(nickname) do
204 Repo.get_by(User, nickname: nickname)
207 def get_cached_user_info(user) do
208 key = "user_info:#{user.id}"
209 Cachex.get!(:user_cache, key, fallback: fn(_) -> user_info(user) end)
212 def get_or_fetch_by_nickname(nickname) do
213 with %User{} = user <- get_by_nickname(nickname) do
216 with [_nick, _domain] <- String.split(nickname, "@"),
217 {:ok, user} <- OStatus.make_user(nickname) do
224 # TODO: these queries could be more efficient if the type in postgresql wasn't map, but array.
225 def get_followers(%User{id: id, follower_address: follower_address}) do
227 where: fragment("? @> ?", u.following, ^follower_address ),
233 def get_friends(%User{id: id, following: following}) do
235 where: u.follower_address in ^following,
241 def increase_note_count(%User{} = user) do
242 note_count = (user.info["note_count"] || 0) + 1
243 new_info = Map.put(user.info, "note_count", note_count)
245 cs = info_changeset(user, %{info: new_info})
250 def update_note_count(%User{} = user) do
251 note_count_query = from a in Object,
252 where: fragment("?->>'actor' = ? and ?->>'type' = 'Note'", a.data, ^user.ap_id, a.data),
255 note_count = Repo.one(note_count_query)
257 new_info = Map.put(user.info, "note_count", note_count)
259 cs = info_changeset(user, %{info: new_info})
264 def update_follower_count(%User{} = user) do
265 follower_count_query = from u in User,
266 where: fragment("? @> ?", u.following, ^user.follower_address),
269 follower_count = Repo.one(follower_count_query)
271 new_info = Map.put(user.info, "follower_count", follower_count)
273 cs = info_changeset(user, %{info: new_info})
278 def get_notified_from_activity(%Activity{data: %{"to" => to}}) do
279 query = from u in User,
280 where: u.ap_id in ^to,
281 where: u.local == true
286 def get_recipients_from_activity(%Activity{data: %{"to" => to}}) do
287 query = from u in User,
288 where: u.local == true
290 query = from u in query,
291 where: u.ap_id in ^to,
292 or_where: fragment("? \\\?| ?", u.following, ^to)
297 def search(query, resolve) do
299 User.get_or_fetch_by_nickname(query)
302 where: fragment("(to_tsvector('english', ?) || to_tsvector('english', ?)) @@ plainto_tsquery('english', ?)", u.nickname, u.name, ^query),
307 def block(user, %{ap_id: ap_id}) do
308 blocks = user.info["blocks"] || []
309 new_blocks = Enum.uniq([ap_id | blocks])
310 new_info = Map.put(user.info, "blocks", new_blocks)
312 cs = User.info_changeset(user, %{info: new_info})
316 def unblock(user, %{ap_id: ap_id}) do
317 blocks = user.info["blocks"] || []
318 new_blocks = List.delete(blocks, ap_id)
319 new_info = Map.put(user.info, "blocks", new_blocks)
321 cs = User.info_changeset(user, %{info: new_info})
325 def blocks?(user, %{ap_id: ap_id}) do
326 blocks = user.info["blocks"] || []
327 Enum.member?(blocks, ap_id)