e1a7befaa7f0f2e7ae20455c72298ae964c101bd
[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, Object}
6 alias Pleroma.Web.ActivityPub.ActivityPub
7
8 schema "users" do
9 field :bio, :string
10 field :email, :string
11 field :name, :string
12 field :nickname, :string
13 field :password_hash, :string
14 field :password, :string, virtual: true
15 field :password_confirmation, :string, virtual: true
16 field :following, { :array, :string }, default: []
17 field :ap_id, :string
18 field :avatar, :map
19
20 timestamps()
21 end
22
23 def avatar_url(user) do
24 case user.avatar do
25 %{"url" => [%{"href" => href} | _]} -> href
26 _ -> "https://placehold.it/48x48"
27 end
28 end
29
30 def ap_id(%User{nickname: nickname}) do
31 "#{Pleroma.Web.base_url}/users/#{nickname}"
32 end
33
34 def ap_followers(%User{} = user) do
35 "#{ap_id(user)}/followers"
36 end
37
38 def follow_changeset(struct, params \\ %{}) do
39 struct
40 |> cast(params, [:following])
41 |> validate_required([:following])
42 end
43
44 def user_info(%User{} = user) do
45 note_count_query = from a in Object,
46 where: fragment("? @> ?", a.data, ^%{actor: user.ap_id, type: "Note"}),
47 select: count(a.id)
48
49 follower_count_query = from u in User,
50 where: fragment("? @> ?", u.following, ^User.ap_followers(user)),
51 select: count(u.id)
52
53 %{
54 following_count: length(user.following),
55 note_count: Repo.one(note_count_query),
56 follower_count: Repo.one(follower_count_query)
57 }
58 end
59
60 def register_changeset(struct, params \\ %{}) do
61 changeset = struct
62 |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
63 |> validate_required([:bio, :email, :name, :nickname, :password, :password_confirmation])
64 |> validate_confirmation(:password)
65 |> unique_constraint(:email)
66 |> unique_constraint(:nickname)
67 |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
68
69 if changeset.valid? do
70 hashed = Comeonin.Pbkdf2.hashpwsalt(changeset.changes[:password])
71 ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]})
72 followers = User.ap_followers(%User{nickname: changeset.changes[:nickname]})
73 changeset
74 |> put_change(:password_hash, hashed)
75 |> put_change(:ap_id, ap_id)
76 |> put_change(:following, [followers])
77 else
78 changeset
79 end
80 end
81
82 def follow(%User{} = follower, %User{} = followed) do
83 ap_followers = User.ap_followers(followed)
84 if following?(follower, followed) do
85 { :error,
86 "Could not follow user: #{followed.nickname} is already on your list." }
87 else
88 following = [ap_followers | follower.following]
89 |> Enum.uniq
90
91 follower
92 |> follow_changeset(%{following: following})
93 |> Repo.update
94 end
95 end
96
97 def unfollow(%User{} = follower, %User{} = followed) do
98 ap_followers = User.ap_followers(followed)
99 if following?(follower, followed) do
100 following = follower.following
101 |> List.delete(ap_followers)
102
103 { :ok, follower } = follower
104 |> follow_changeset(%{following: following})
105 |> Repo.update
106 { :ok, follower, ActivityPub.fetch_latest_follow(follower, followed)}
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 end