1 defmodule Pleroma.User do
4 import Ecto.{Changeset, Query}
5 alias Pleroma.{Repo, User, Object, Web}
7 alias Pleroma.Web.{OStatus, Websub}
13 field :nickname, :string
14 field :password_hash, :string
15 field :password, :string, virtual: true
16 field :password_confirmation, :string, virtual: true
17 field :following, {:array, :string}, default: []
20 field :local, :boolean, default: true
21 field :info, :map, default: %{}
26 def avatar_url(user) do
28 %{"url" => [%{"href" => href} | _]} -> href
29 _ -> "https://placehold.it/48x48"
33 def ap_id(%User{nickname: nickname}) do
34 "#{Web.base_url}/users/#{nickname}"
37 def ap_followers(%User{} = user) do
38 "#{ap_id(user)}/followers"
41 def follow_changeset(struct, params \\ %{}) do
43 |> cast(params, [:following])
44 |> validate_required([:following])
47 def user_info(%User{} = user) do
48 note_count_query = from a in Object,
49 where: fragment("? @> ?", a.data, ^%{actor: user.ap_id, type: "Note"}),
52 follower_count_query = from u in User,
53 where: fragment("? @> ?", u.following, ^User.ap_followers(user)),
57 following_count: length(user.following),
58 note_count: Repo.one(note_count_query),
59 follower_count: Repo.one(follower_count_query)
63 def register_changeset(struct, params \\ %{}) do
65 |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
66 |> validate_required([:bio, :email, :name, :nickname, :password, :password_confirmation])
67 |> validate_confirmation(:password)
68 |> unique_constraint(:email)
69 |> unique_constraint(:nickname)
70 |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
72 if changeset.valid? do
73 hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
74 ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]})
75 followers = User.ap_followers(%User{nickname: changeset.changes[:nickname]})
77 |> put_change(:password_hash, hashed)
78 |> put_change(:ap_id, ap_id)
79 |> put_change(:following, [followers])
85 def follow(%User{} = follower, %User{} = followed) do
86 ap_followers = User.ap_followers(followed)
87 if following?(follower, followed) do
89 "Could not follow user: #{followed.nickname} is already on your list."}
92 Websub.subscribe(follower, followed)
95 following = [ap_followers | follower.following]
99 |> follow_changeset(%{following: following})
104 def unfollow(%User{} = follower, %User{} = followed) do
105 ap_followers = User.ap_followers(followed)
106 if following?(follower, followed) do
107 following = follower.following
108 |> List.delete(ap_followers)
111 |> follow_changeset(%{following: following})
114 {:error, "Not subscribed!"}
118 def following?(%User{} = follower, %User{} = followed) do
119 Enum.member?(follower.following, User.ap_followers(followed))
122 def get_cached_by_ap_id(ap_id) do
123 key = "ap_id:#{ap_id}"
124 Cachex.get!(:user_cache, key, fallback: fn(_) -> Repo.get_by(User, ap_id: ap_id) end)
127 def get_cached_by_nickname(nickname) do
128 key = "nickname:#{nickname}"
129 Cachex.get!(:user_cache, key, fallback: fn(_) -> get_or_fetch_by_nickname(nickname) end)
132 def get_by_nickname(nickname) do
133 Repo.get_by(User, nickname: nickname)
136 def get_cached_user_info(user) do
137 key = "user_info:#{user.id}"
138 Cachex.get!(:user_cache, key, fallback: fn(_) -> user_info(user) end)
141 def get_or_fetch_by_nickname(nickname) do
142 with %User{} = user <- get_by_nickname(nickname) do
145 with [nick, domain] <- String.split(nickname, "@"),
146 {:ok, user} <- OStatus.make_user(nickname) do