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