1 defmodule Pleroma.User do
5 alias Pleroma.{Repo, User, Object}
6 alias Pleroma.Web.ActivityPub.ActivityPub
12 field :nickname, :string
13 field :password_hash, :string
14 field :password, :string, virtual: true
15 field :password_confirmation, :string, virtual: true
16 field :following, { :array, :string }, default: []
23 def avatar_url(user) do
25 %{"url" => [%{"href" => href} | _]} -> href
26 _ -> "https://placehold.it/48x48"
30 def ap_id(%User{nickname: nickname}) do
31 "#{Pleroma.Web.base_url}/users/#{nickname}"
34 def ap_followers(%User{} = user) do
35 "#{ap_id(user)}/followers"
38 def follow_changeset(struct, params \\ %{}) do
40 |> cast(params, [:following])
41 |> validate_required([:following])
44 def user_info(%User{} = user) do
45 note_count_query = from a in Object,
46 where: fragment("? @> ?", a.data, ^%{actor: user.ap_id, type: "Note"}),
49 follower_count_query = from u in User,
50 where: fragment("? @> ?", u.following, ^User.ap_followers(user)),
54 following_count: length(user.following),
55 note_count: Repo.one(note_count_query),
56 follower_count: Repo.one(follower_count_query)
60 def register_changeset(struct, params \\ %{}) do
62 |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
63 |> validate_required([:bio, :email, :name, :nickname, :password, :password_confirmation])
64 |> validate_confirmation(:password)
65 |> unique_constraint(:email)
66 |> unique_constraint(:nickname)
67 |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
69 if changeset.valid? do
70 hashed = Comeonin.Pbkdf2.hashpwsalt(changeset.changes[:password])
71 ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]})
72 followers = User.ap_followers(%User{nickname: changeset.changes[:nickname]})
74 |> put_change(:password_hash, hashed)
75 |> put_change(:ap_id, ap_id)
76 |> put_change(:following, [followers])
82 def follow(%User{} = follower, %User{} = followed) do
83 ap_followers = User.ap_followers(followed)
84 if following?(follower, followed) do
86 "Could not follow user: #{followed.nickname} is already on your list." }
88 following = [ap_followers | follower.following]
92 |> follow_changeset(%{following: following})
97 def unfollow(%User{} = follower, %User{} = followed) do
98 ap_followers = User.ap_followers(followed)
99 if following?(follower, followed) do
100 following = follower.following
101 |> List.delete(ap_followers)
103 { :ok, follower } = follower
104 |> follow_changeset(%{following: following})
106 { :ok, follower, ActivityPub.fetch_latest_follow(follower, followed)}
108 { :error, "Not subscribed!" }
112 def following?(%User{} = follower, %User{} = followed) do
113 Enum.member?(follower.following, User.ap_followers(followed))
116 def get_cached_by_ap_id(ap_id) do
117 key = "ap_id:#{ap_id}"
118 Cachex.get!(:user_cache, key, fallback: fn(_) -> Repo.get_by(User, ap_id: ap_id) end)
121 def get_cached_by_nickname(nickname) do
122 key = "nickname:#{nickname}"
123 Cachex.get!(:user_cache, key, fallback: fn(_) -> Repo.get_by(User, nickname: nickname) end)