move avi.png & banner.png from /priv/static/static to /priv/static/images
[akkoma] / lib / pleroma / user.ex
1 defmodule Pleroma.User do
2 use Ecto.Schema
3
4 import Ecto.{Changeset, Query}
5 alias Pleroma.{Repo, User, Object, Web, Activity, Notification}
6 alias Comeonin.Pbkdf2
7 alias Pleroma.Web.{OStatus, Websub}
8 alias Pleroma.Web.ActivityPub.{Utils, ActivityPub}
9
10 schema "users" do
11 field :bio, :string
12 field :email, :string
13 field :name, :string
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: []
19 field :ap_id, :string
20 field :avatar, :map
21 field :local, :boolean, default: true
22 field :info, :map, default: %{}
23 field :follower_address, :string
24 has_many :notifications, Notification
25
26 timestamps()
27 end
28
29 def avatar_url(user) do
30 case user.avatar do
31 %{"url" => [%{"href" => href} | _]} -> href
32 _ -> "#{Web.base_url()}/images/avi.png"
33 end
34 end
35
36 def banner_url(user) do
37 case user.info["banner"] do
38 %{"url" => [%{"href" => href} | _]} -> href
39 _ -> "#{Web.base_url()}/images/banner.png"
40 end
41 end
42
43 def ap_id(%User{nickname: nickname}) do
44 "#{Web.base_url}/users/#{nickname}"
45 end
46
47 def ap_followers(%User{} = user) do
48 "#{ap_id(user)}/followers"
49 end
50
51 def follow_changeset(struct, params \\ %{}) do
52 struct
53 |> cast(params, [:following])
54 |> validate_required([:following])
55 end
56
57 def info_changeset(struct, params \\ %{}) do
58 struct
59 |> cast(params, [:info])
60 |> validate_required([:info])
61 end
62
63 def user_info(%User{} = user) do
64 oneself = if user.local, do: 1, else: 0
65 %{
66 following_count: length(user.following) - oneself,
67 note_count: user.info["note_count"] || 0,
68 follower_count: user.info["follower_count"] || 0
69 }
70 end
71
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
74 changes = %User{}
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)
82 if changes.valid? do
83 followers = User.ap_followers(%User{nickname: changes.changes[:nickname]})
84 changes
85 |> put_change(:follower_address, followers)
86 else
87 changes
88 end
89 end
90
91 def update_changeset(struct, params \\ %{}) do
92 struct
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)
98 end
99
100 def password_update_changeset(struct, params) do
101 changeset = struct
102 |> cast(params, [:password, :password_confirmation])
103 |> validate_required([:password, :password_confirmation])
104 |> validate_confirmation(:password)
105
106 if changeset.valid? do
107 hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
108 changeset
109 |> put_change(:password_hash, hashed)
110 else
111 changeset
112 end
113 end
114
115 def reset_password(user, data) do
116 update_and_set_cache(password_update_changeset(user, data))
117 end
118
119 def register_changeset(struct, params \\ %{}) do
120 changeset = struct
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)
130
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]})
135 changeset
136 |> put_change(:password_hash, hashed)
137 |> put_change(:ap_id, ap_id)
138 |> put_change(:following, [followers])
139 |> put_change(:follower_address, followers)
140 else
141 changeset
142 end
143 end
144
145 def follow(%User{} = follower, %User{info: info} = followed) do
146 ap_followers = followed.follower_address
147 if following?(follower, followed) or info["deactivated"] do
148 {:error,
149 "Could not follow user: #{followed.nickname} is already on your list."}
150 else
151 if !followed.local && follower.local do
152 Websub.subscribe(follower, followed)
153 end
154
155 following = [ap_followers | follower.following]
156 |> Enum.uniq
157
158 follower = follower
159 |> follow_changeset(%{following: following})
160 |> update_and_set_cache
161
162 {:ok, _} = update_follower_count(followed)
163
164 follower
165 end
166 end
167
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)
173
174 { :ok, follower } = follower
175 |> follow_changeset(%{following: following})
176 |> update_and_set_cache
177
178 {:ok, followed} = update_follower_count(followed)
179
180 {:ok, follower, Utils.fetch_latest_follow(follower, followed)}
181 else
182 {:error, "Not subscribed!"}
183 end
184 end
185
186 def following?(%User{} = follower, %User{} = followed) do
187 Enum.member?(follower.following, followed.follower_address)
188 end
189
190 def get_by_ap_id(ap_id) do
191 Repo.get_by(User, ap_id: ap_id)
192 end
193
194 def update_and_set_cache(changeset) do
195 with {:ok, user} <- Repo.update(changeset) do
196 Cachex.set(:user_cache, "ap_id:#{user.ap_id}", user)
197 Cachex.set(:user_cache, "nickname:#{user.nickname}", user)
198 Cachex.set(:user_cache, "user_info:#{user.id}", user_info(user))
199 {:ok, user}
200 else
201 e -> e
202 end
203 end
204
205 def get_cached_by_ap_id(ap_id) do
206 key = "ap_id:#{ap_id}"
207 Cachex.get!(:user_cache, key, fallback: fn(_) -> get_by_ap_id(ap_id) end)
208 end
209
210 def get_cached_by_nickname(nickname) do
211 key = "nickname:#{nickname}"
212 Cachex.get!(:user_cache, key, fallback: fn(_) -> get_or_fetch_by_nickname(nickname) end)
213 end
214
215 def get_by_nickname(nickname) do
216 Repo.get_by(User, nickname: nickname)
217 end
218
219 def get_cached_user_info(user) do
220 key = "user_info:#{user.id}"
221 Cachex.get!(:user_cache, key, fallback: fn(_) -> user_info(user) end)
222 end
223
224 def get_or_fetch_by_nickname(nickname) do
225 with %User{} = user <- get_by_nickname(nickname) do
226 user
227 else _e ->
228 with [_nick, _domain] <- String.split(nickname, "@"),
229 {:ok, user} <- OStatus.make_user(nickname) do
230 user
231 else _e -> nil
232 end
233 end
234 end
235
236 # TODO: these queries could be more efficient if the type in postgresql wasn't map, but array.
237 def get_followers(%User{id: id, follower_address: follower_address}) do
238 q = from u in User,
239 where: fragment("? @> ?", u.following, ^follower_address ),
240 where: u.id != ^id
241
242 {:ok, Repo.all(q)}
243 end
244
245 def get_friends(%User{id: id, following: following}) do
246 q = from u in User,
247 where: u.follower_address in ^following,
248 where: u.id != ^id
249
250 {:ok, Repo.all(q)}
251 end
252
253 def increase_note_count(%User{} = user) do
254 note_count = (user.info["note_count"] || 0) + 1
255 new_info = Map.put(user.info, "note_count", note_count)
256
257 cs = info_changeset(user, %{info: new_info})
258
259 update_and_set_cache(cs)
260 end
261
262 def update_note_count(%User{} = user) do
263 note_count_query = from a in Object,
264 where: fragment("?->>'actor' = ? and ?->>'type' = 'Note'", a.data, ^user.ap_id, a.data),
265 select: count(a.id)
266
267 note_count = Repo.one(note_count_query)
268
269 new_info = Map.put(user.info, "note_count", note_count)
270
271 cs = info_changeset(user, %{info: new_info})
272
273 update_and_set_cache(cs)
274 end
275
276 def update_follower_count(%User{} = user) do
277 follower_count_query = from u in User,
278 where: fragment("? @> ?", u.following, ^user.follower_address),
279 where: u.id != ^user.id,
280 select: count(u.id)
281
282 follower_count = Repo.one(follower_count_query)
283
284 new_info = Map.put(user.info, "follower_count", follower_count)
285
286 cs = info_changeset(user, %{info: new_info})
287
288 update_and_set_cache(cs)
289 end
290
291 def get_notified_from_activity(%Activity{data: %{"to" => to}}) do
292 query = from u in User,
293 where: u.ap_id in ^to,
294 where: u.local == true
295
296 Repo.all(query)
297 end
298
299 def get_recipients_from_activity(%Activity{data: %{"to" => to}}) do
300 query = from u in User,
301 where: u.ap_id in ^to,
302 or_where: fragment("? \\\?| ?", u.following, ^to)
303
304 query = from u in query,
305 where: u.local == true
306
307 Repo.all(query)
308 end
309
310 def search(query, resolve) do
311 if resolve do
312 User.get_or_fetch_by_nickname(query)
313 end
314 q = from u in User,
315 where: fragment("(to_tsvector('english', ?) || to_tsvector('english', ?)) @@ plainto_tsquery('english', ?)", u.nickname, u.name, ^query),
316 limit: 20
317 Repo.all(q)
318 end
319
320 def block(user, %{ap_id: ap_id}) do
321 blocks = user.info["blocks"] || []
322 new_blocks = Enum.uniq([ap_id | blocks])
323 new_info = Map.put(user.info, "blocks", new_blocks)
324
325 cs = User.info_changeset(user, %{info: new_info})
326 update_and_set_cache(cs)
327 end
328
329 def unblock(user, %{ap_id: ap_id}) do
330 blocks = user.info["blocks"] || []
331 new_blocks = List.delete(blocks, ap_id)
332 new_info = Map.put(user.info, "blocks", new_blocks)
333
334 cs = User.info_changeset(user, %{info: new_info})
335 update_and_set_cache(cs)
336 end
337
338 def blocks?(user, %{ap_id: ap_id}) do
339 blocks = user.info["blocks"] || []
340 Enum.member?(blocks, ap_id)
341 end
342
343 def local_user_query() do
344 from u in User,
345 where: u.local == true
346 end
347
348 def deactivate (%User{} = user) do
349 new_info = Map.put(user.info, "deactivated", true)
350 cs = User.info_changeset(user, %{info: new_info})
351 update_and_set_cache(cs)
352 end
353
354 def delete (%User{} = user) do
355 {:ok, user} = User.deactivate(user)
356
357 # Remove all relationships
358 {:ok, followers } = User.get_followers(user)
359 followers
360 |> Enum.each(fn (follower) -> User.unfollow(follower, user) end)
361
362 {:ok, friends} = User.get_friends(user)
363 friends
364 |> Enum.each(fn (followed) -> User.unfollow(user, followed) end)
365
366 query = from a in Activity,
367 where: a.actor == ^user.ap_id
368
369 Repo.all(query)
370 |> Enum.each(fn (activity) ->
371 case activity.data["type"] do
372 "Create" -> ActivityPub.delete(Object.get_by_ap_id(activity.data["object"]["id"]))
373 _ -> "Doing nothing" # TODO: Do something with likes, follows, repeats.
374 end
375 end)
376
377 :ok
378 end
379 end