TwitterAPI: Use correct endpoint for tag timeline.
[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.ActivityPub
9 alias Pleroma.Web.ActivityPub.Utils
10
11 schema "users" do
12 field :bio, :string
13 field :email, :string
14 field :name, :string
15 field :nickname, :string
16 field :password_hash, :string
17 field :password, :string, virtual: true
18 field :password_confirmation, :string, virtual: true
19 field :following, {:array, :string}, default: []
20 field :ap_id, :string
21 field :avatar, :map
22 field :local, :boolean, default: true
23 field :info, :map, default: %{}
24 field :follower_address, :string
25 has_many :notifications, Notification
26
27 timestamps()
28 end
29
30 def avatar_url(user) do
31 case user.avatar do
32 %{"url" => [%{"href" => href} | _]} -> href
33 _ -> "https://placehold.it/48x48"
34 end
35 end
36
37 def banner_url(user) do
38 case user.info["banner"] do
39 %{"url" => [%{"href" => href} | _]} -> href
40 _ -> nil
41 end
42 end
43
44 def ap_id(%User{nickname: nickname}) do
45 "#{Web.base_url}/users/#{nickname}"
46 end
47
48 def ap_followers(%User{} = user) do
49 "#{ap_id(user)}/followers"
50 end
51
52 def follow_changeset(struct, params \\ %{}) do
53 struct
54 |> cast(params, [:following])
55 |> validate_required([:following])
56 end
57
58 def info_changeset(struct, params \\ %{}) do
59 struct
60 |> cast(params, [:info])
61 |> validate_required([:info])
62 end
63
64 def user_info(%User{} = user) do
65 %{
66 following_count: length(user.following),
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 changeset = 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 register_changeset(struct, params \\ %{}) do
101 changeset = struct
102 |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
103 |> validate_required([:bio, :email, :name, :nickname, :password, :password_confirmation])
104 |> validate_confirmation(:password)
105 |> unique_constraint(:email)
106 |> unique_constraint(:nickname)
107 |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
108 |> validate_format(:email, @email_regex)
109 |> validate_length(:bio, min: 1, max: 1000)
110 |> validate_length(:name, min: 1, max: 100)
111
112 if changeset.valid? do
113 hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
114 ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]})
115 followers = User.ap_followers(%User{nickname: changeset.changes[:nickname]})
116 changeset
117 |> put_change(:password_hash, hashed)
118 |> put_change(:ap_id, ap_id)
119 |> put_change(:following, [followers])
120 |> put_change(:follower_address, followers)
121 else
122 changeset
123 end
124 end
125
126 def follow(%User{} = follower, %User{} = followed) do
127 ap_followers = followed.follower_address
128 if following?(follower, followed) do
129 {:error,
130 "Could not follow user: #{followed.nickname} is already on your list."}
131 else
132 if !followed.local && follower.local do
133 Websub.subscribe(follower, followed)
134 end
135
136 following = [ap_followers | follower.following]
137 |> Enum.uniq
138
139 follower = follower
140 |> follow_changeset(%{following: following})
141 |> Repo.update
142
143 {:ok, followed} = update_follower_count(followed)
144
145 follower
146 end
147 end
148
149 def unfollow(%User{} = follower, %User{} = followed) do
150 ap_followers = followed.follower_address
151 if following?(follower, followed) do
152 following = follower.following
153 |> List.delete(ap_followers)
154
155 { :ok, follower } = follower
156 |> follow_changeset(%{following: following})
157 |> Repo.update
158
159 {:ok, followed} = update_follower_count(followed)
160
161 {:ok, follower, Utils.fetch_latest_follow(follower, followed)}
162 else
163 {:error, "Not subscribed!"}
164 end
165 end
166
167 def following?(%User{} = follower, %User{} = followed) do
168 Enum.member?(follower.following, followed.follower_address)
169 end
170
171 def get_by_ap_id(ap_id) do
172 Repo.get_by(User, ap_id: ap_id)
173 end
174
175 def get_cached_by_ap_id(ap_id) do
176 key = "ap_id:#{ap_id}"
177 Cachex.get!(:user_cache, key, fallback: fn(_) -> get_by_ap_id(ap_id) end)
178 end
179
180 def get_cached_by_nickname(nickname) do
181 key = "nickname:#{nickname}"
182 Cachex.get!(:user_cache, key, fallback: fn(_) -> get_or_fetch_by_nickname(nickname) end)
183 end
184
185 def get_by_nickname(nickname) do
186 Repo.get_by(User, nickname: nickname)
187 end
188
189 def get_cached_user_info(user) do
190 key = "user_info:#{user.id}"
191 Cachex.get!(:user_cache, key, fallback: fn(_) -> user_info(user) end)
192 end
193
194 def get_or_fetch_by_nickname(nickname) do
195 with %User{} = user <- get_by_nickname(nickname) do
196 user
197 else _e ->
198 with [nick, domain] <- String.split(nickname, "@"),
199 {:ok, user} <- OStatus.make_user(nickname) do
200 user
201 else _e -> nil
202 end
203 end
204 end
205
206 # TODO: these queries could be more efficient if the type in postgresql wasn't map, but array.
207 def get_followers(%User{id: id, follower_address: follower_address}) do
208 q = from u in User,
209 where: fragment("? @> ?", u.following, ^follower_address ),
210 where: u.id != ^id
211
212 {:ok, Repo.all(q)}
213 end
214
215 def get_friends(%User{id: id, following: following}) do
216 q = from u in User,
217 where: u.follower_address in ^following,
218 where: u.id != ^id
219
220 {:ok, Repo.all(q)}
221 end
222
223 def update_note_count(%User{} = user) do
224 note_count_query = from a in Object,
225 where: fragment("? @> ?", a.data, ^%{actor: user.ap_id, type: "Note"}),
226 select: count(a.id)
227
228 note_count = Repo.one(note_count_query)
229
230 new_info = Map.put(user.info, "note_count", note_count)
231
232 cs = info_changeset(user, %{info: new_info})
233
234 Repo.update(cs)
235 end
236
237 def update_follower_count(%User{} = user) do
238 follower_count_query = from u in User,
239 where: fragment("? @> ?", u.following, ^user.follower_address),
240 select: count(u.id)
241
242 follower_count = Repo.one(follower_count_query)
243
244 new_info = Map.put(user.info, "follower_count", follower_count)
245
246 cs = info_changeset(user, %{info: new_info})
247
248 Repo.update(cs)
249 end
250
251 def get_notified_from_activity(%Activity{data: %{"to" => to}} = activity) do
252 query = from u in User,
253 where: u.ap_id in ^to,
254 where: u.local == true
255
256 Repo.all(query)
257 end
258 end