Merge branch 'oauth2' into 'develop'
[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 ap_id(%User{nickname: nickname}) do
38 "#{Web.base_url}/users/#{nickname}"
39 end
40
41 def ap_followers(%User{} = user) do
42 "#{ap_id(user)}/followers"
43 end
44
45 def follow_changeset(struct, params \\ %{}) do
46 struct
47 |> cast(params, [:following])
48 |> validate_required([:following])
49 end
50
51 def info_changeset(struct, params \\ %{}) do
52 struct
53 |> cast(params, [:info])
54 |> validate_required([:info])
55 end
56
57 def user_info(%User{} = user) do
58 %{
59 following_count: length(user.following),
60 note_count: user.info["note_count"] || 0,
61 follower_count: user.info["follower_count"] || 0
62 }
63 end
64
65 @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])?)*$/
66 def remote_user_creation(params) do
67 changes = %User{}
68 |> cast(params, [:bio, :name, :ap_id, :nickname, :info, :avatar])
69 |> validate_required([:name, :ap_id, :nickname])
70 |> unique_constraint(:nickname)
71 |> validate_format(:nickname, @email_regex)
72 |> validate_length(:bio, max: 5000)
73 |> validate_length(:name, max: 100)
74 |> put_change(:local, false)
75 if changes.valid? do
76 followers = User.ap_followers(%User{nickname: changes.changes[:nickname]})
77 changes
78 |> put_change(:follower_address, followers)
79 else
80 changes
81 end
82 end
83
84 def update_changeset(struct, params \\ %{}) do
85 changeset = struct
86 |> cast(params, [:bio, :name])
87 |> unique_constraint(:nickname)
88 |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
89 |> validate_length(:bio, min: 1, max: 1000)
90 |> validate_length(:name, min: 1, max: 100)
91 end
92
93 def register_changeset(struct, params \\ %{}) do
94 changeset = struct
95 |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
96 |> validate_required([:bio, :email, :name, :nickname, :password, :password_confirmation])
97 |> validate_confirmation(:password)
98 |> unique_constraint(:email)
99 |> unique_constraint(:nickname)
100 |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
101 |> validate_format(:email, @email_regex)
102 |> validate_length(:bio, min: 1, max: 1000)
103 |> validate_length(:name, min: 1, max: 100)
104
105 if changeset.valid? do
106 hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
107 ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]})
108 followers = User.ap_followers(%User{nickname: changeset.changes[:nickname]})
109 changeset
110 |> put_change(:password_hash, hashed)
111 |> put_change(:ap_id, ap_id)
112 |> put_change(:following, [followers])
113 |> put_change(:follower_address, followers)
114 else
115 changeset
116 end
117 end
118
119 def follow(%User{} = follower, %User{} = followed) do
120 ap_followers = followed.follower_address
121 if following?(follower, followed) do
122 {:error,
123 "Could not follow user: #{followed.nickname} is already on your list."}
124 else
125 if !followed.local && follower.local do
126 Websub.subscribe(follower, followed)
127 end
128
129 following = [ap_followers | follower.following]
130 |> Enum.uniq
131
132 follower = follower
133 |> follow_changeset(%{following: following})
134 |> Repo.update
135
136 {:ok, followed} = update_follower_count(followed)
137
138 follower
139 end
140 end
141
142 def unfollow(%User{} = follower, %User{} = followed) do
143 ap_followers = followed.follower_address
144 if following?(follower, followed) do
145 following = follower.following
146 |> List.delete(ap_followers)
147
148 { :ok, follower } = follower
149 |> follow_changeset(%{following: following})
150 |> Repo.update
151
152 {:ok, followed} = update_follower_count(followed)
153
154 {:ok, follower, Utils.fetch_latest_follow(follower, followed)}
155 else
156 {:error, "Not subscribed!"}
157 end
158 end
159
160 def following?(%User{} = follower, %User{} = followed) do
161 Enum.member?(follower.following, followed.follower_address)
162 end
163
164 def get_by_ap_id(ap_id) do
165 Repo.get_by(User, ap_id: ap_id)
166 end
167
168 def get_cached_by_ap_id(ap_id) do
169 key = "ap_id:#{ap_id}"
170 Cachex.get!(:user_cache, key, fallback: fn(_) -> get_by_ap_id(ap_id) end)
171 end
172
173 def get_cached_by_nickname(nickname) do
174 key = "nickname:#{nickname}"
175 Cachex.get!(:user_cache, key, fallback: fn(_) -> get_or_fetch_by_nickname(nickname) end)
176 end
177
178 def get_by_nickname(nickname) do
179 Repo.get_by(User, nickname: nickname)
180 end
181
182 def get_cached_user_info(user) do
183 key = "user_info:#{user.id}"
184 Cachex.get!(:user_cache, key, fallback: fn(_) -> user_info(user) end)
185 end
186
187 def get_or_fetch_by_nickname(nickname) do
188 with %User{} = user <- get_by_nickname(nickname) do
189 user
190 else _e ->
191 with [nick, domain] <- String.split(nickname, "@"),
192 {:ok, user} <- OStatus.make_user(nickname) do
193 user
194 else _e -> nil
195 end
196 end
197 end
198
199 # TODO: these queries could be more efficient if the type in postgresql wasn't map, but array.
200 def get_followers(%User{id: id, follower_address: follower_address}) do
201 q = from u in User,
202 where: fragment("? @> ?", u.following, ^follower_address ),
203 where: u.id != ^id
204
205 {:ok, Repo.all(q)}
206 end
207
208 def get_friends(%User{id: id, following: following}) do
209 q = from u in User,
210 where: u.follower_address in ^following,
211 where: u.id != ^id
212
213 {:ok, Repo.all(q)}
214 end
215
216 def update_note_count(%User{} = user) do
217 note_count_query = from a in Object,
218 where: fragment("? @> ?", a.data, ^%{actor: user.ap_id, type: "Note"}),
219 select: count(a.id)
220
221 note_count = Repo.one(note_count_query)
222
223 new_info = Map.put(user.info, "note_count", note_count)
224
225 cs = info_changeset(user, %{info: new_info})
226
227 Repo.update(cs)
228 end
229
230 def update_follower_count(%User{} = user) do
231 follower_count_query = from u in User,
232 where: fragment("? @> ?", u.following, ^user.follower_address),
233 select: count(u.id)
234
235 follower_count = Repo.one(follower_count_query)
236
237 new_info = Map.put(user.info, "follower_count", follower_count)
238
239 cs = info_changeset(user, %{info: new_info})
240
241 Repo.update(cs)
242 end
243
244 def get_notified_from_activity(%Activity{data: %{"to" => to}} = activity) do
245 query = from u in User,
246 where: u.ap_id in ^to,
247 where: u.local == true
248
249 Repo.all(query)
250 end
251 end