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