Merge branch 'develop' into dtluna/pleroma-refactor/1
[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
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 following = [ap_followers | follower.following]
92 |> Enum.uniq
93
94 follower
95 |> follow_changeset(%{following: following})
96 |> Repo.update
97 end
98 end
99
100 def unfollow(%User{} = follower, %User{} = followed) do
101 ap_followers = User.ap_followers(followed)
102 if following?(follower, followed) do
103 following = follower.following
104 |> List.delete(ap_followers)
105
106 follower
107 |> follow_changeset(%{following: following})
108 |> Repo.update
109 else
110 {:error, "Not subscribed!"}
111 end
112 end
113
114 def following?(%User{} = follower, %User{} = followed) do
115 Enum.member?(follower.following, User.ap_followers(followed))
116 end
117
118 def get_cached_by_ap_id(ap_id) do
119 key = "ap_id:#{ap_id}"
120 Cachex.get!(:user_cache, key, fallback: fn(_) -> Repo.get_by(User, ap_id: ap_id) end)
121 end
122
123 def get_cached_by_nickname(nickname) do
124 key = "nickname:#{nickname}"
125 Cachex.get!(:user_cache, key, fallback: fn(_) -> get_or_fetch_by_nickname(nickname) end)
126 end
127
128 def get_by_nickname(nickname) do
129 Repo.get_by(User, nickname: nickname)
130 end
131
132 def get_cached_user_info(user) do
133 key = "user_info:#{user.id}"
134 Cachex.get!(:user_cache, key, fallback: fn(_) -> user_info(user) end)
135 end
136
137 def get_or_fetch_by_nickname(nickname) do
138 with %User{} = user <- get_by_nickname(nickname) do
139 user
140 else _e ->
141 with [nick, domain] <- String.split(nickname, "@"),
142 {:ok, user} <- OStatus.make_user(nickname) do
143 user
144 else _e -> nil
145 end
146 end
147 end
148 end