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