1 defmodule Pleroma.User do
5 alias Pleroma.{Repo, User, Activity, Object}
6 alias Pleroma.Web.OStatus
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: []
19 field :local, :boolean, default: true
20 field :info, :map, default: %{}
25 def avatar_url(user) do
27 %{"url" => [%{"href" => href} | _]} -> href
28 _ -> "https://placehold.it/48x48"
32 def ap_id(%User{nickname: nickname}) do
33 "#{Pleroma.Web.base_url}/users/#{nickname}"
36 def ap_followers(%User{} = user) do
37 "#{ap_id(user)}/followers"
40 def follow_changeset(struct, params \\ %{}) do
42 |> cast(params, [:following])
43 |> validate_required([:following])
46 def user_info(%User{} = user) do
47 note_count_query = from a in Object,
48 where: fragment("? @> ?", a.data, ^%{actor: user.ap_id, type: "Note"}),
51 follower_count_query = from u in User,
52 where: fragment("? @> ?", u.following, ^User.ap_followers(user)),
56 following_count: length(user.following),
57 note_count: Repo.one(note_count_query),
58 follower_count: Repo.one(follower_count_query)
62 def register_changeset(struct, params \\ %{}) do
64 |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
65 |> validate_required([:bio, :email, :name, :nickname, :password, :password_confirmation])
66 |> validate_confirmation(:password)
67 |> unique_constraint(:email)
68 |> unique_constraint(:nickname)
69 |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
71 if changeset.valid? do
72 hashed = Comeonin.Pbkdf2.hashpwsalt(changeset.changes[:password])
73 ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]})
74 followers = User.ap_followers(%User{nickname: changeset.changes[:nickname]})
76 |> put_change(:password_hash, hashed)
77 |> put_change(:ap_id, ap_id)
78 |> put_change(:following, [followers])
84 def follow(%User{} = follower, %User{} = followed) do
85 ap_followers = User.ap_followers(followed)
86 if following?(follower, followed) do
88 "Could not follow user: #{followed.nickname} is already on your list." }
90 following = [ap_followers | follower.following]
94 |> follow_changeset(%{following: following})
99 def unfollow(%User{} = follower, %User{} = followed) do
100 ap_followers = User.ap_followers(followed)
101 if following?(follower, followed) do
102 following = follower.following
103 |> List.delete(ap_followers)
106 |> follow_changeset(%{following: following})
109 { :error, "Not subscribed!" }
113 def following?(%User{} = follower, %User{} = followed) do
114 Enum.member?(follower.following, User.ap_followers(followed))
117 def get_cached_by_ap_id(ap_id) do
118 key = "ap_id:#{ap_id}"
119 Cachex.get!(:user_cache, key, fallback: fn(_) -> Repo.get_by(User, ap_id: ap_id) end)
122 def get_cached_by_nickname(nickname) do
123 key = "nickname:#{nickname}"
124 Cachex.get!(:user_cache, key, fallback: fn(_) -> get_or_fetch_by_nickname(nickname) end)
127 def get_by_nickname(nickname) do
128 Repo.get_by(User, nickname: nickname)
131 def get_cached_user_info(user) do
132 key = "user_info:#{user.id}"
133 Cachex.get!(:user_cache, key, fallback: fn(_) -> user_info(user) end)
136 def get_or_fetch_by_nickname(nickname) do
137 with %User{} = user <- get_by_nickname(nickname) do
140 with [nick, domain] <- String.split(nickname, "@"),
141 {:ok, user} <- OStatus.make_user(nickname) do