Output conversation id.
[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
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
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 follower
104 |> follow_changeset(%{following: following})
105 |> Repo.update
106 else
107 { :error, "Not subscribed!" }
108 end
109 end
110
111 def following?(%User{} = follower, %User{} = followed) do
112 Enum.member?(follower.following, User.ap_followers(followed))
113 end
114
115 def get_cached_by_ap_id(ap_id) do
116 key = "ap_id:#{ap_id}"
117 Cachex.get!(:user_cache, key, fallback: fn(_) -> Repo.get_by(User, ap_id: ap_id) end)
118 end
119
120 def get_cached_by_nickname(nickname) do
121 key = "nickname:#{nickname}"
122 Cachex.get!(:user_cache, key, fallback: fn(_) -> Repo.get_by(User, nickname: nickname) end)
123 end
124 end