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 banner_url(user) do
38 case user.info["banner"] do
39 %{"url" => [%{"href" => href} | _]} -> href
44 def ap_id(%User{nickname: nickname}) do
45 "#{Web.base_url}/users/#{nickname}"
48 def ap_followers(%User{} = user) do
49 "#{ap_id(user)}/followers"
52 def follow_changeset(struct, params \\ %{}) do
54 |> cast(params, [:following])
55 |> validate_required([:following])
58 def info_changeset(struct, params \\ %{}) do
60 |> cast(params, [:info])
61 |> validate_required([:info])
64 def user_info(%User{} = user) do
66 following_count: length(user.following),
67 note_count: user.info["note_count"] || 0,
68 follower_count: user.info["follower_count"] || 0
72 @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])?)*$/
73 def remote_user_creation(params) do
75 |> cast(params, [:bio, :name, :ap_id, :nickname, :info, :avatar])
76 |> validate_required([:name, :ap_id, :nickname])
77 |> unique_constraint(:nickname)
78 |> validate_format(:nickname, @email_regex)
79 |> validate_length(:bio, max: 5000)
80 |> validate_length(:name, max: 100)
81 |> put_change(:local, false)
83 followers = User.ap_followers(%User{nickname: changes.changes[:nickname]})
85 |> put_change(:follower_address, followers)
91 def update_changeset(struct, params \\ %{}) do
93 |> cast(params, [:bio, :name])
94 |> unique_constraint(:nickname)
95 |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
96 |> validate_length(:bio, min: 1, max: 1000)
97 |> validate_length(:name, min: 1, max: 100)
100 def register_changeset(struct, params \\ %{}) do
102 |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
103 |> validate_required([:bio, :email, :name, :nickname, :password, :password_confirmation])
104 |> validate_confirmation(:password)
105 |> unique_constraint(:email)
106 |> unique_constraint(:nickname)
107 |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
108 |> validate_format(:email, @email_regex)
109 |> validate_length(:bio, min: 1, max: 1000)
110 |> validate_length(:name, min: 1, max: 100)
112 if changeset.valid? do
113 hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
114 ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]})
115 followers = User.ap_followers(%User{nickname: changeset.changes[:nickname]})
117 |> put_change(:password_hash, hashed)
118 |> put_change(:ap_id, ap_id)
119 |> put_change(:following, [followers])
120 |> put_change(:follower_address, followers)
126 def follow(%User{} = follower, %User{} = followed) do
127 ap_followers = followed.follower_address
128 if following?(follower, followed) do
130 "Could not follow user: #{followed.nickname} is already on your list."}
132 if !followed.local && follower.local do
133 Websub.subscribe(follower, followed)
136 following = [ap_followers | follower.following]
140 |> follow_changeset(%{following: following})
143 {:ok, followed} = update_follower_count(followed)
149 def unfollow(%User{} = follower, %User{} = followed) do
150 ap_followers = followed.follower_address
151 if following?(follower, followed) do
152 following = follower.following
153 |> List.delete(ap_followers)
155 { :ok, follower } = follower
156 |> follow_changeset(%{following: following})
159 {:ok, followed} = update_follower_count(followed)
161 {:ok, follower, Utils.fetch_latest_follow(follower, followed)}
163 {:error, "Not subscribed!"}
167 def following?(%User{} = follower, %User{} = followed) do
168 Enum.member?(follower.following, followed.follower_address)
171 def get_by_ap_id(ap_id) do
172 Repo.get_by(User, ap_id: ap_id)
175 def get_cached_by_ap_id(ap_id) do
176 key = "ap_id:#{ap_id}"
177 Cachex.get!(:user_cache, key, fallback: fn(_) -> get_by_ap_id(ap_id) end)
180 def get_cached_by_nickname(nickname) do
181 key = "nickname:#{nickname}"
182 Cachex.get!(:user_cache, key, fallback: fn(_) -> get_or_fetch_by_nickname(nickname) end)
185 def get_by_nickname(nickname) do
186 Repo.get_by(User, nickname: nickname)
189 def get_cached_user_info(user) do
190 key = "user_info:#{user.id}"
191 Cachex.get!(:user_cache, key, fallback: fn(_) -> user_info(user) end)
194 def get_or_fetch_by_nickname(nickname) do
195 with %User{} = user <- get_by_nickname(nickname) do
198 with [nick, domain] <- String.split(nickname, "@"),
199 {:ok, user} <- OStatus.make_user(nickname) do
206 # TODO: these queries could be more efficient if the type in postgresql wasn't map, but array.
207 def get_followers(%User{id: id, follower_address: follower_address}) do
209 where: fragment("? @> ?", u.following, ^follower_address ),
215 def get_friends(%User{id: id, following: following}) do
217 where: u.follower_address in ^following,
223 def update_note_count(%User{} = user) do
224 note_count_query = from a in Object,
225 where: fragment("? @> ?", a.data, ^%{actor: user.ap_id, type: "Note"}),
228 note_count = Repo.one(note_count_query)
230 new_info = Map.put(user.info, "note_count", note_count)
232 cs = info_changeset(user, %{info: new_info})
237 def update_follower_count(%User{} = user) do
238 follower_count_query = from u in User,
239 where: fragment("? @> ?", u.following, ^user.follower_address),
242 follower_count = Repo.one(follower_count_query)
244 new_info = Map.put(user.info, "follower_count", follower_count)
246 cs = info_changeset(user, %{info: new_info})
251 def get_notified_from_activity(%Activity{data: %{"to" => to}} = activity) do
252 query = from u in User,
253 where: u.ap_id in ^to,
254 where: u.local == true