22e77c3df092ad3e9c455abe0a8ee0fcaaad4bb0
[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 %User{}
67 |> cast(params, [:bio, :name, :ap_id, :nickname, :info, :avatar])
68 |> validate_required([: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 |> put_change(:local, false)
74 end
75
76 def register_changeset(struct, params \\ %{}) do
77 changeset = struct
78 |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
79 |> validate_required([:bio, :email, :name, :nickname, :password, :password_confirmation])
80 |> validate_confirmation(:password)
81 |> unique_constraint(:email)
82 |> unique_constraint(:nickname)
83 |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
84 |> validate_format(:email, @email_regex)
85 |> validate_length(:bio, max: 1000)
86 |> validate_length(:name, max: 100)
87
88 if changeset.valid? do
89 hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
90 ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]})
91 followers = User.ap_followers(%User{nickname: changeset.changes[:nickname]})
92 changeset
93 |> put_change(:password_hash, hashed)
94 |> put_change(:ap_id, ap_id)
95 |> put_change(:following, [followers])
96 else
97 changeset
98 end
99 end
100
101 def follow(%User{} = follower, %User{} = followed) do
102 ap_followers = User.ap_followers(followed)
103 if following?(follower, followed) do
104 {:error,
105 "Could not follow user: #{followed.nickname} is already on your list."}
106 else
107 if !followed.local do
108 Websub.subscribe(follower, followed)
109 end
110
111 following = [ap_followers | follower.following]
112 |> Enum.uniq
113
114 follower
115 |> follow_changeset(%{following: following})
116 |> Repo.update
117 end
118 end
119
120 def unfollow(%User{} = follower, %User{} = followed) do
121 ap_followers = User.ap_followers(followed)
122 if following?(follower, followed) do
123 following = follower.following
124 |> List.delete(ap_followers)
125
126 { :ok, follower } = follower
127 |> follow_changeset(%{following: following})
128 |> Repo.update
129 { :ok, follower, ActivityPub.fetch_latest_follow(follower, followed)}
130 else
131 {:error, "Not subscribed!"}
132 end
133 end
134
135 def following?(%User{} = follower, %User{} = followed) do
136 Enum.member?(follower.following, User.ap_followers(followed))
137 end
138
139 def get_cached_by_ap_id(ap_id) do
140 key = "ap_id:#{ap_id}"
141 Cachex.get!(:user_cache, key, fallback: fn(_) -> Repo.get_by(User, ap_id: ap_id) end)
142 end
143
144 def get_cached_by_nickname(nickname) do
145 key = "nickname:#{nickname}"
146 Cachex.get!(:user_cache, key, fallback: fn(_) -> get_or_fetch_by_nickname(nickname) end)
147 end
148
149 def get_by_nickname(nickname) do
150 Repo.get_by(User, nickname: nickname)
151 end
152
153 def get_cached_user_info(user) do
154 key = "user_info:#{user.id}"
155 Cachex.get!(:user_cache, key, fallback: fn(_) -> user_info(user) end)
156 end
157
158 def get_or_fetch_by_nickname(nickname) do
159 with %User{} = user <- get_by_nickname(nickname) do
160 user
161 else _e ->
162 with [nick, domain] <- String.split(nickname, "@"),
163 {:ok, user} <- OStatus.make_user(nickname) do
164 user
165 else _e -> nil
166 end
167 end
168 end
169 end