1 defmodule Pleroma.User do
4 import Ecto.{Changeset, Query}
5 alias Pleroma.{Repo, User, Object, Web, Activity, Notification}
7 alias Pleroma.Web.{OStatus, Websub}
8 alias Pleroma.Web.ActivityPub.ActivityPub
9 alias Pleroma.Web.ActivityPub.Utils
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: []
22 field :local, :boolean, default: true
23 field :info, :map, default: %{}
24 field :follower_address, :string
25 has_many :notifications, Notification
30 def avatar_url(user) do
32 %{"url" => [%{"href" => href} | _]} -> href
33 _ -> "https://placehold.it/48x48"
37 def ap_id(%User{nickname: nickname}) do
38 "#{Web.base_url}/users/#{nickname}"
41 def ap_followers(%User{} = user) do
42 "#{ap_id(user)}/followers"
45 def follow_changeset(struct, params \\ %{}) do
47 |> cast(params, [:following])
48 |> validate_required([:following])
51 def info_changeset(struct, params \\ %{}) do
53 |> cast(params, [:info])
54 |> validate_required([:info])
57 def user_info(%User{} = user) do
59 following_count: length(user.following),
60 note_count: user.info["note_count"] || 0,
61 follower_count: user.info["follower_count"] || 0
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
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)
76 followers = User.ap_followers(%User{nickname: changes.changes[:nickname]})
78 |> put_change(:follower_address, followers)
84 def update_changeset(struct, params \\ %{}) do
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)
93 def register_changeset(struct, params \\ %{}) do
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)
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]})
110 |> put_change(:password_hash, hashed)
111 |> put_change(:ap_id, ap_id)
112 |> put_change(:following, [followers])
113 |> put_change(:follower_address, followers)
119 def follow(%User{} = follower, %User{} = followed) do
120 ap_followers = followed.follower_address
121 if following?(follower, followed) do
123 "Could not follow user: #{followed.nickname} is already on your list."}
125 if !followed.local && follower.local do
126 Websub.subscribe(follower, followed)
129 following = [ap_followers | follower.following]
133 |> follow_changeset(%{following: following})
136 {:ok, followed} = update_follower_count(followed)
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)
148 { :ok, follower } = follower
149 |> follow_changeset(%{following: following})
152 {:ok, followed} = update_follower_count(followed)
154 {:ok, follower, Utils.fetch_latest_follow(follower, followed)}
156 {:error, "Not subscribed!"}
160 def following?(%User{} = follower, %User{} = followed) do
161 Enum.member?(follower.following, followed.follower_address)
164 def get_by_ap_id(ap_id) do
165 Repo.get_by(User, ap_id: ap_id)
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)
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)
178 def get_by_nickname(nickname) do
179 Repo.get_by(User, nickname: nickname)
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)
187 def get_or_fetch_by_nickname(nickname) do
188 with %User{} = user <- get_by_nickname(nickname) do
191 with [nick, domain] <- String.split(nickname, "@"),
192 {:ok, user} <- OStatus.make_user(nickname) do
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
202 where: fragment("? @> ?", u.following, ^follower_address ),
208 def get_friends(%User{id: id, following: following}) do
210 where: u.follower_address in ^following,
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"}),
221 note_count = Repo.one(note_count_query)
223 new_info = Map.put(user.info, "note_count", note_count)
225 cs = info_changeset(user, %{info: new_info})
230 def update_follower_count(%User{} = user) do
231 follower_count_query = from u in User,
232 where: fragment("? @> ?", u.following, ^user.follower_address),
235 follower_count = Repo.one(follower_count_query)
237 new_info = Map.put(user.info, "follower_count", follower_count)
239 cs = info_changeset(user, %{info: new_info})
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