Add Custom Pleroma-dark theme
[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
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 _ -> "https://placehold.it/48x48"
33 end
34 end
35
36 def banner_url(user) do
37 case user.info["banner"] do
38 %{"url" => [%{"href" => href} | _]} -> href
39 _ -> nil
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 %{
65 following_count: length(user.following),
66 note_count: user.info["note_count"] || 0,
67 follower_count: user.info["follower_count"] || 0
68 }
69 end
70
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
73 changes = %User{}
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)
81 if changes.valid? do
82 followers = User.ap_followers(%User{nickname: changes.changes[:nickname]})
83 changes
84 |> put_change(:follower_address, followers)
85 else
86 changes
87 end
88 end
89
90 def update_changeset(struct, params \\ %{}) do
91 struct
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)
97 end
98
99 def password_update_changeset(struct, params) do
100 changeset = struct
101 |> cast(params, [:password, :password_confirmation])
102 |> validate_required([:password, :password_confirmation])
103 |> validate_confirmation(:password)
104
105 if changeset.valid? do
106 hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
107 changeset
108 |> put_change(:password_hash, hashed)
109 else
110 changeset
111 end
112 end
113
114 def reset_password(user, data) do
115 Repo.update(password_update_changeset(user, data))
116 end
117
118 def register_changeset(struct, params \\ %{}) do
119 changeset = struct
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)
129
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]})
134 changeset
135 |> put_change(:password_hash, hashed)
136 |> put_change(:ap_id, ap_id)
137 |> put_change(:following, [followers])
138 |> put_change(:follower_address, followers)
139 else
140 changeset
141 end
142 end
143
144 def follow(%User{} = follower, %User{} = followed) do
145 ap_followers = followed.follower_address
146 if following?(follower, followed) do
147 {:error,
148 "Could not follow user: #{followed.nickname} is already on your list."}
149 else
150 if !followed.local && follower.local do
151 Websub.subscribe(follower, followed)
152 end
153
154 following = [ap_followers | follower.following]
155 |> Enum.uniq
156
157 follower = follower
158 |> follow_changeset(%{following: following})
159 |> Repo.update
160
161 {:ok, _} = update_follower_count(followed)
162
163 follower
164 end
165 end
166
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)
172
173 { :ok, follower } = follower
174 |> follow_changeset(%{following: following})
175 |> Repo.update
176
177 {:ok, followed} = update_follower_count(followed)
178
179 {:ok, follower, Utils.fetch_latest_follow(follower, followed)}
180 else
181 {:error, "Not subscribed!"}
182 end
183 end
184
185 def following?(%User{} = follower, %User{} = followed) do
186 Enum.member?(follower.following, followed.follower_address)
187 end
188
189 def get_by_ap_id(ap_id) do
190 Repo.get_by(User, ap_id: ap_id)
191 end
192
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)
196 end
197
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)
201 end
202
203 def get_by_nickname(nickname) do
204 Repo.get_by(User, nickname: nickname)
205 end
206
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)
210 end
211
212 def get_or_fetch_by_nickname(nickname) do
213 with %User{} = user <- get_by_nickname(nickname) do
214 user
215 else _e ->
216 with [_nick, _domain] <- String.split(nickname, "@"),
217 {:ok, user} <- OStatus.make_user(nickname) do
218 user
219 else _e -> nil
220 end
221 end
222 end
223
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
226 q = from u in User,
227 where: fragment("? @> ?", u.following, ^follower_address ),
228 where: u.id != ^id
229
230 {:ok, Repo.all(q)}
231 end
232
233 def get_friends(%User{id: id, following: following}) do
234 q = from u in User,
235 where: u.follower_address in ^following,
236 where: u.id != ^id
237
238 {:ok, Repo.all(q)}
239 end
240
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)
244
245 cs = info_changeset(user, %{info: new_info})
246
247 Repo.update(cs)
248 end
249
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),
253 select: count(a.id)
254
255 note_count = Repo.one(note_count_query)
256
257 new_info = Map.put(user.info, "note_count", note_count)
258
259 cs = info_changeset(user, %{info: new_info})
260
261 Repo.update(cs)
262 end
263
264 def update_follower_count(%User{} = user) do
265 follower_count_query = from u in User,
266 where: fragment("? @> ?", u.following, ^user.follower_address),
267 select: count(u.id)
268
269 follower_count = Repo.one(follower_count_query)
270
271 new_info = Map.put(user.info, "follower_count", follower_count)
272
273 cs = info_changeset(user, %{info: new_info})
274
275 Repo.update(cs)
276 end
277
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
282
283 Repo.all(query)
284 end
285
286 def get_recipients_from_activity(%Activity{data: %{"to" => to}}) do
287 query = from u in User,
288 where: u.local == true
289
290 query = from u in query,
291 where: u.ap_id in ^to,
292 or_where: fragment("? \\\?| ?", u.following, ^to)
293
294 Repo.all(query)
295 end
296
297 def search(query, resolve) do
298 if resolve do
299 User.get_or_fetch_by_nickname(query)
300 end
301 q = from u in User,
302 where: fragment("(to_tsvector('english', ?) || to_tsvector('english', ?)) @@ plainto_tsquery('english', ?)", u.nickname, u.name, ^query),
303 limit: 20
304 Repo.all(q)
305 end
306
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)
311
312 cs = User.info_changeset(user, %{info: new_info})
313 Repo.update(cs)
314 end
315
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)
320
321 cs = User.info_changeset(user, %{info: new_info})
322 Repo.update(cs)
323 end
324
325 def blocks?(user, %{ap_id: ap_id}) do
326 blocks = user.info["blocks"] || []
327 Enum.member?(blocks, ap_id)
328 end
329
330 end