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: %{}
28 def avatar_url(user) do
30 %{"url" => [%{"href" => href} | _]} -> href
31 _ -> "https://placehold.it/48x48"
35 def ap_id(%User{nickname: nickname}) do
36 "#{Web.base_url}/users/#{nickname}"
39 def ap_followers(%User{} = user) do
40 "#{ap_id(user)}/followers"
43 def follow_changeset(struct, params \\ %{}) do
45 |> cast(params, [:following])
46 |> validate_required([:following])
49 def user_info(%User{} = user) do
50 note_count_query = from a in Object,
51 where: fragment("? @> ?", a.data, ^%{actor: user.ap_id, type: "Note"}),
54 follower_count_query = from u in User,
55 where: fragment("? @> ?", u.following, ^User.ap_followers(user)),
59 following_count: length(user.following),
60 note_count: Repo.one(note_count_query),
61 follower_count: Repo.one(follower_count_query)
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: 1000)
73 |> validate_length(:name, max: 100)
74 |> put_change(:local, false)
77 def register_changeset(struct, params \\ %{}) do
79 |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
80 |> validate_required([:bio, :email, :name, :nickname, :password, :password_confirmation])
81 |> validate_confirmation(:password)
82 |> unique_constraint(:email)
83 |> unique_constraint(:nickname)
84 |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
85 |> validate_format(:email, @email_regex)
86 |> validate_length(:bio, max: 1000)
87 |> validate_length(:name, max: 100)
89 if changeset.valid? do
90 hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
91 ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]})
92 followers = User.ap_followers(%User{nickname: changeset.changes[:nickname]})
94 |> put_change(:password_hash, hashed)
95 |> put_change(:ap_id, ap_id)
96 |> put_change(:following, [followers])
102 def follow(%User{} = follower, %User{} = followed) do
103 ap_followers = User.ap_followers(followed)
104 if following?(follower, followed) do
106 "Could not follow user: #{followed.nickname} is already on your list."}
108 if !followed.local && follower.local do
109 Websub.subscribe(follower, followed)
112 following = [ap_followers | follower.following]
116 |> follow_changeset(%{following: following})
121 def unfollow(%User{} = follower, %User{} = followed) do
122 ap_followers = User.ap_followers(followed)
123 if following?(follower, followed) do
124 following = follower.following
125 |> List.delete(ap_followers)
127 { :ok, follower } = follower
128 |> follow_changeset(%{following: following})
130 { :ok, follower, Utils.fetch_latest_follow(follower, followed)}
132 {:error, "Not subscribed!"}
136 def following?(%User{} = follower, %User{} = followed) do
137 Enum.member?(follower.following, User.ap_followers(followed))
140 def get_by_ap_id(ap_id) do
141 Repo.get_by(User, ap_id: ap_id)
144 def get_cached_by_ap_id(ap_id) do
145 key = "ap_id:#{ap_id}"
146 Cachex.get!(:user_cache, key, fallback: fn(_) -> get_by_ap_id(ap_id) end)
149 def get_cached_by_nickname(nickname) do
150 key = "nickname:#{nickname}"
151 Cachex.get!(:user_cache, key, fallback: fn(_) -> get_or_fetch_by_nickname(nickname) end)
154 def get_by_nickname(nickname) do
155 Repo.get_by(User, nickname: nickname)
158 def get_cached_user_info(user) do
159 key = "user_info:#{user.id}"
160 Cachex.get!(:user_cache, key, fallback: fn(_) -> user_info(user) end)
163 def get_or_fetch_by_nickname(nickname) do
164 with %User{} = user <- get_by_nickname(nickname) do
167 with [nick, domain] <- String.split(nickname, "@"),
168 {:ok, user} <- OStatus.make_user(nickname) do