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