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