Add callback to websub subscription.
[akkoma] / lib / pleroma / user.ex
1 defmodule Pleroma.User do
2 use Ecto.Schema
3 import Ecto.Changeset
4 import Ecto.Query
5 alias Pleroma.{Repo, User, Activity, Object}
6
7 schema "users" do
8 field :bio, :string
9 field :email, :string
10 field :name, :string
11 field :nickname, :string
12 field :password_hash, :string
13 field :password, :string, virtual: true
14 field :password_confirmation, :string, virtual: true
15 field :following, { :array, :string }, default: []
16 field :ap_id, :string
17 field :avatar, :map
18
19 timestamps()
20 end
21
22 def avatar_url(user) do
23 case user.avatar do
24 %{"url" => [%{"href" => href} | _]} -> href
25 _ -> "https://placehold.it/48x48"
26 end
27 end
28
29 def ap_id(%User{nickname: nickname}) do
30 "#{Pleroma.Web.base_url}/users/#{nickname}"
31 end
32
33 def ap_followers(%User{} = user) do
34 "#{ap_id(user)}/followers"
35 end
36
37 def follow_changeset(struct, params \\ %{}) do
38 struct
39 |> cast(params, [:following])
40 |> validate_required([:following])
41 end
42
43 def user_info(%User{} = user) do
44 note_count_query = from a in Object,
45 where: fragment("? @> ?", a.data, ^%{actor: user.ap_id, type: "Note"}),
46 select: count(a.id)
47
48 follower_count_query = from u in User,
49 where: fragment("? @> ?", u.following, ^User.ap_followers(user)),
50 select: count(u.id)
51
52 %{
53 following_count: length(user.following),
54 note_count: Repo.one(note_count_query),
55 follower_count: Repo.one(follower_count_query)
56 }
57 end
58
59 def register_changeset(struct, params \\ %{}) do
60 changeset = struct
61 |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
62 |> validate_required([:bio, :email, :name, :nickname, :password, :password_confirmation])
63 |> validate_confirmation(:password)
64 |> unique_constraint(:email)
65 |> unique_constraint(:nickname)
66
67 if changeset.valid? do
68 hashed = Comeonin.Pbkdf2.hashpwsalt(changeset.changes[:password])
69 ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]})
70 followers = User.ap_followers(%User{nickname: changeset.changes[:nickname]})
71 changeset
72 |> put_change(:password_hash, hashed)
73 |> put_change(:ap_id, ap_id)
74 |> put_change(:following, [followers])
75 else
76 changeset
77 end
78 end
79
80 def follow(%User{} = follower, %User{} = followed) do
81 ap_followers = User.ap_followers(followed)
82 if following?(follower, followed) do
83 { :error,
84 "Could not follow user: #{followed.nickname} is already on your list." }
85 else
86 following = [ap_followers | follower.following]
87 |> Enum.uniq
88
89 follower
90 |> follow_changeset(%{following: following})
91 |> Repo.update
92 end
93 end
94
95 def unfollow(%User{} = follower, %User{} = followed) do
96 ap_followers = User.ap_followers(followed)
97 if following?(follower, followed) do
98 following = follower.following
99 |> List.delete(ap_followers)
100
101 follower
102 |> follow_changeset(%{following: following})
103 |> Repo.update
104 else
105 { :error, "Not subscribed!" }
106 end
107 end
108
109 def following?(%User{} = follower, %User{} = followed) do
110 Enum.member?(follower.following, User.ap_followers(followed))
111 end
112
113 def get_cached_by_ap_id(ap_id) do
114 key = "ap_id:#{ap_id}"
115 Cachex.get!(:user_cache, key, fallback: fn(_) -> Repo.get_by(User, ap_id: ap_id) end)
116 end
117
118 def get_cached_by_nickname(nickname) do
119 key = "nickname:#{nickname}"
120 Cachex.get!(:user_cache, key, fallback: fn(_) -> Repo.get_by(User, nickname: nickname) end)
121 end
122 end