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