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