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
14 field :nickname, :string
15 field :password_hash, :string
16 field :password, :string, virtual: true
17 field :password_confirmation, :string, virtual: true
18 field :following, {:array, :string}, default: []
21 field :local, :boolean, default: true
22 field :info, :map, default: %{}
27 def avatar_url(user) do
29 %{"url" => [%{"href" => href} | _]} -> href
30 _ -> "https://placehold.it/48x48"
34 def ap_id(%User{nickname: nickname}) do
35 "#{Web.base_url}/users/#{nickname}"
38 def ap_followers(%User{} = user) do
39 "#{ap_id(user)}/followers"
42 def follow_changeset(struct, params \\ %{}) do
44 |> cast(params, [:following])
45 |> validate_required([:following])
48 def user_info(%User{} = user) do
49 note_count_query = from a in Object,
50 where: fragment("? @> ?", a.data, ^%{actor: user.ap_id, type: "Note"}),
53 follower_count_query = from u in User,
54 where: fragment("? @> ?", u.following, ^User.ap_followers(user)),
58 following_count: length(user.following),
59 note_count: Repo.one(note_count_query),
60 follower_count: Repo.one(follower_count_query)
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: 1000)
72 |> validate_length(:name, max: 100)
73 |> put_change(:local, false)
76 def register_changeset(struct, params \\ %{}) do
78 |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
79 |> validate_required([:bio, :email, :name, :nickname, :password, :password_confirmation])
80 |> validate_confirmation(:password)
81 |> unique_constraint(:email)
82 |> unique_constraint(:nickname)
83 |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
84 |> validate_format(:email, @email_regex)
85 |> validate_length(:bio, max: 1000)
86 |> validate_length(:name, max: 100)
88 if changeset.valid? do
89 hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
90 ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]})
91 followers = User.ap_followers(%User{nickname: changeset.changes[:nickname]})
93 |> put_change(:password_hash, hashed)
94 |> put_change(:ap_id, ap_id)
95 |> put_change(:following, [followers])
101 def follow(%User{} = follower, %User{} = followed) do
102 ap_followers = User.ap_followers(followed)
103 if following?(follower, followed) do
105 "Could not follow user: #{followed.nickname} is already on your list."}
107 if !followed.local && follower.local do
108 Websub.subscribe(follower, followed)
111 following = [ap_followers | follower.following]
115 |> follow_changeset(%{following: following})
120 def unfollow(%User{} = follower, %User{} = followed) do
121 ap_followers = User.ap_followers(followed)
122 if following?(follower, followed) do
123 following = follower.following
124 |> List.delete(ap_followers)
126 { :ok, follower } = follower
127 |> follow_changeset(%{following: following})
129 { :ok, follower, ActivityPub.fetch_latest_follow(follower, followed)}
131 {:error, "Not subscribed!"}
135 def following?(%User{} = follower, %User{} = followed) do
136 Enum.member?(follower.following, User.ap_followers(followed))
139 def get_cached_by_ap_id(ap_id) do
140 key = "ap_id:#{ap_id}"
141 Cachex.get!(:user_cache, key, fallback: fn(_) -> Repo.get_by(User, ap_id: ap_id) end)
144 def get_cached_by_nickname(nickname) do
145 key = "nickname:#{nickname}"
146 Cachex.get!(:user_cache, key, fallback: fn(_) -> get_or_fetch_by_nickname(nickname) end)
149 def get_by_nickname(nickname) do
150 Repo.get_by(User, nickname: nickname)
153 def get_cached_user_info(user) do
154 key = "user_info:#{user.id}"
155 Cachex.get!(:user_cache, key, fallback: fn(_) -> user_info(user) end)
158 def get_or_fetch_by_nickname(nickname) do
159 with %User{} = user <- get_by_nickname(nickname) do
162 with [nick, domain] <- String.split(nickname, "@"),
163 {:ok, user} <- OStatus.make_user(nickname) do