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