1 defmodule Pleroma.User do
4 import Ecto.{Changeset, Query}
5 alias Pleroma.{Repo, User, Object, Web}
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
29 def avatar_url(user) do
31 %{"url" => [%{"href" => href} | _]} -> href
32 _ -> "https://placehold.it/48x48"
36 def ap_id(%User{nickname: nickname}) do
37 "#{Web.base_url}/users/#{nickname}"
40 def ap_followers(%User{} = user) do
41 "#{ap_id(user)}/followers"
44 def follow_changeset(struct, params \\ %{}) do
46 |> cast(params, [:following])
47 |> validate_required([:following])
50 def info_changeset(struct, params \\ %{}) do
52 |> cast(params, [:info])
53 |> validate_required([:info])
56 def user_info(%User{} = user) do
58 following_count: length(user.following),
59 note_count: user.info["note_count"] || 0,
60 follower_count: user.info["follower_count"] || 0
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
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)
75 followers = User.ap_followers(%User{nickname: changes.changes[:nickname]})
77 |> put_change(:follower_address, followers)
83 def register_changeset(struct, params \\ %{}) do
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)
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]})
100 |> put_change(:password_hash, hashed)
101 |> put_change(:ap_id, ap_id)
102 |> put_change(:following, [followers])
103 |> put_change(:follower_address, followers)
109 def follow(%User{} = follower, %User{} = followed) do
110 ap_followers = followed.follower_address
111 if following?(follower, followed) do
113 "Could not follow user: #{followed.nickname} is already on your list."}
115 if !followed.local && follower.local do
116 Websub.subscribe(follower, followed)
119 following = [ap_followers | follower.following]
123 |> follow_changeset(%{following: following})
126 {:ok, followed} = update_follower_count(followed)
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)
138 { :ok, follower } = follower
139 |> follow_changeset(%{following: following})
142 {:ok, followed} = update_follower_count(followed)
144 {:ok, follower, Utils.fetch_latest_follow(follower, followed)}
146 {:error, "Not subscribed!"}
150 def following?(%User{} = follower, %User{} = followed) do
151 Enum.member?(follower.following, followed.follower_address)
154 def get_by_ap_id(ap_id) do
155 Repo.get_by(User, ap_id: ap_id)
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)
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)
168 def get_by_nickname(nickname) do
169 Repo.get_by(User, nickname: nickname)
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)
177 def get_or_fetch_by_nickname(nickname) do
178 with %User{} = user <- get_by_nickname(nickname) do
181 with [nick, domain] <- String.split(nickname, "@"),
182 {:ok, user} <- OStatus.make_user(nickname) do
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
192 where: fragment("? @> ?", u.following, ^follower_address ),
198 def get_friends(%User{id: id, following: following}) do
200 where: u.follower_address in ^following,
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"}),
211 note_count = Repo.one(note_count_query)
213 new_info = Map.put(user.info, "note_count", note_count)
215 cs = info_changeset(user, %{info: new_info})
220 def update_follower_count(%User{} = user) do
221 follower_count_query = from u in User,
222 where: fragment("? @> ?", u.following, ^user.follower_address),
225 follower_count = Repo.one(follower_count_query)
227 new_info = Map.put(user.info, "follower_count", follower_count)
229 cs = info_changeset(user, %{info: new_info})