Add follower_address to users, add on generation.
[akkoma] / lib / pleroma / user.ex
1 defmodule Pleroma.User do
2 use Ecto.Schema
3
4 import Ecto.{Changeset, Query}
5 alias Pleroma.{Repo, User, Object, Web}
6 alias Comeonin.Pbkdf2
7 alias Pleroma.Web.{OStatus, Websub}
8 alias Pleroma.Web.ActivityPub.ActivityPub
9 alias Pleroma.Web.ActivityPub.Utils
10
11 schema "users" do
12 field :bio, :string
13 field :email, :string
14 field :name, :string
15 field :nickname, :string
16 field :password_hash, :string
17 field :password, :string, virtual: true
18 field :password_confirmation, :string, virtual: true
19 field :following, {:array, :string}, default: []
20 field :ap_id, :string
21 field :avatar, :map
22 field :local, :boolean, default: true
23 field :info, :map, default: %{}
24 field :follower_address, :string
25
26 timestamps()
27 end
28
29 def avatar_url(user) do
30 case user.avatar do
31 %{"url" => [%{"href" => href} | _]} -> href
32 _ -> "https://placehold.it/48x48"
33 end
34 end
35
36 def ap_id(%User{nickname: nickname}) do
37 "#{Web.base_url}/users/#{nickname}"
38 end
39
40 def ap_followers(%User{} = user) do
41 "#{ap_id(user)}/followers"
42 end
43
44 def follow_changeset(struct, params \\ %{}) do
45 struct
46 |> cast(params, [:following])
47 |> validate_required([:following])
48 end
49
50 def info_changeset(struct, params \\ %{}) do
51 struct
52 |> cast(params, [:info])
53 |> validate_required([:info])
54 end
55
56 def user_info(%User{} = user) do
57 note_count_query = from a in Object,
58 where: fragment("? @> ?", a.data, ^%{actor: user.ap_id, type: "Note"}),
59 select: count(a.id)
60
61 follower_count_query = from u in User,
62 where: fragment("? @> ?", u.following, ^user.follower_address),
63 select: count(u.id)
64
65 %{
66 following_count: length(user.following),
67 note_count: Repo.one(note_count_query),
68 follower_count: Repo.one(follower_count_query)
69 }
70 end
71
72 @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])?)*$/
73 def remote_user_creation(params) do
74 changes = %User{}
75 |> cast(params, [:bio, :name, :ap_id, :nickname, :info, :avatar])
76 |> validate_required([:name, :ap_id, :nickname])
77 |> unique_constraint(:nickname)
78 |> validate_format(:nickname, @email_regex)
79 |> validate_length(:bio, max: 5000)
80 |> validate_length(:name, max: 100)
81 |> put_change(:local, false)
82 if changes.valid? do
83 followers = User.ap_followers(%User{nickname: changes.changes[:nickname]})
84 changes
85 |> put_change(:follower_address, followers)
86 else
87 changes
88 end
89 end
90
91 def register_changeset(struct, params \\ %{}) do
92 changeset = struct
93 |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
94 |> validate_required([:bio, :email, :name, :nickname, :password, :password_confirmation])
95 |> validate_confirmation(:password)
96 |> unique_constraint(:email)
97 |> unique_constraint(:nickname)
98 |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
99 |> validate_format(:email, @email_regex)
100 |> validate_length(:bio, max: 1000)
101 |> validate_length(:name, max: 100)
102
103 if changeset.valid? do
104 hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
105 ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]})
106 followers = User.ap_followers(%User{nickname: changeset.changes[:nickname]})
107 changeset
108 |> put_change(:password_hash, hashed)
109 |> put_change(:ap_id, ap_id)
110 |> put_change(:following, [followers])
111 |> put_change(:follower_address, followers)
112 else
113 changeset
114 end
115 end
116
117 def follow(%User{} = follower, %User{} = followed) do
118 ap_followers = followed.follower_address
119 if following?(follower, followed) do
120 {:error,
121 "Could not follow user: #{followed.nickname} is already on your list."}
122 else
123 if !followed.local && follower.local do
124 Websub.subscribe(follower, followed)
125 end
126
127 following = [ap_followers | follower.following]
128 |> Enum.uniq
129
130 follower
131 |> follow_changeset(%{following: following})
132 |> Repo.update
133 end
134 end
135
136 def unfollow(%User{} = follower, %User{} = followed) do
137 ap_followers = followed.follower_address
138 if following?(follower, followed) do
139 following = follower.following
140 |> List.delete(ap_followers)
141
142 { :ok, follower } = follower
143 |> follow_changeset(%{following: following})
144 |> Repo.update
145 { :ok, follower, Utils.fetch_latest_follow(follower, followed)}
146 else
147 {:error, "Not subscribed!"}
148 end
149 end
150
151 def following?(%User{} = follower, %User{} = followed) do
152 Enum.member?(follower.following, followed.follower_address)
153 end
154
155 def get_by_ap_id(ap_id) do
156 Repo.get_by(User, ap_id: ap_id)
157 end
158
159 def get_cached_by_ap_id(ap_id) do
160 key = "ap_id:#{ap_id}"
161 Cachex.get!(:user_cache, key, fallback: fn(_) -> get_by_ap_id(ap_id) end)
162 end
163
164 def get_cached_by_nickname(nickname) do
165 key = "nickname:#{nickname}"
166 Cachex.get!(:user_cache, key, fallback: fn(_) -> get_or_fetch_by_nickname(nickname) end)
167 end
168
169 def get_by_nickname(nickname) do
170 Repo.get_by(User, nickname: nickname)
171 end
172
173 def get_cached_user_info(user) do
174 key = "user_info:#{user.id}"
175 Cachex.get!(:user_cache, key, fallback: fn(_) -> user_info(user) end)
176 end
177
178 def get_or_fetch_by_nickname(nickname) do
179 with %User{} = user <- get_by_nickname(nickname) do
180 user
181 else _e ->
182 with [nick, domain] <- String.split(nickname, "@"),
183 {:ok, user} <- OStatus.make_user(nickname) do
184 user
185 else _e -> nil
186 end
187 end
188 end
189 end