Merge branch 'develop' into dtluna/pleroma-feature/unfollow-activity
[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, Websub}
8 alias Pleroma.Web.ActivityPub.ActivityPub
9
10 schema "users" do
11 field :bio, :string
12 field :email, :string
13 field :name, :string
14 field :nickname, :string
15 field :password_hash, :string
16 field :password, :string, virtual: true
17 field :password_confirmation, :string, virtual: true
18 field :following, {:array, :string}, default: []
19 field :ap_id, :string
20 field :avatar, :map
21 field :local, :boolean, default: true
22 field :info, :map, default: %{}
23
24 timestamps()
25 end
26
27 def avatar_url(user) do
28 case user.avatar do
29 %{"url" => [%{"href" => href} | _]} -> href
30 _ -> "https://placehold.it/48x48"
31 end
32 end
33
34 def ap_id(%User{nickname: nickname}) do
35 "#{Web.base_url}/users/#{nickname}"
36 end
37
38 def ap_followers(%User{} = user) do
39 "#{ap_id(user)}/followers"
40 end
41
42 def follow_changeset(struct, params \\ %{}) do
43 struct
44 |> cast(params, [:following])
45 |> validate_required([:following])
46 end
47
48 def user_info(%User{} = user) do
49 note_count_query = from a in Object,
50 where: fragment("? @> ?", a.data, ^%{actor: user.ap_id, type: "Note"}),
51 select: count(a.id)
52
53 follower_count_query = from u in User,
54 where: fragment("? @> ?", u.following, ^User.ap_followers(user)),
55 select: count(u.id)
56
57 %{
58 following_count: length(user.following),
59 note_count: Repo.one(note_count_query),
60 follower_count: Repo.one(follower_count_query)
61 }
62 end
63
64 def register_changeset(struct, params \\ %{}) do
65 changeset = struct
66 |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
67 |> validate_required([:bio, :email, :name, :nickname, :password, :password_confirmation])
68 |> validate_confirmation(:password)
69 |> unique_constraint(:email)
70 |> unique_constraint(:nickname)
71 |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/)
72
73 if changeset.valid? do
74 hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
75 ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]})
76 followers = User.ap_followers(%User{nickname: changeset.changes[:nickname]})
77 changeset
78 |> put_change(:password_hash, hashed)
79 |> put_change(:ap_id, ap_id)
80 |> put_change(:following, [followers])
81 else
82 changeset
83 end
84 end
85
86 def follow(%User{} = follower, %User{} = followed) do
87 ap_followers = User.ap_followers(followed)
88 if following?(follower, followed) do
89 {:error,
90 "Could not follow user: #{followed.nickname} is already on your list."}
91 else
92 if !followed.local do
93 Websub.subscribe(follower, followed)
94 end
95
96 following = [ap_followers | follower.following]
97 |> Enum.uniq
98
99 follower
100 |> follow_changeset(%{following: following})
101 |> Repo.update
102 end
103 end
104
105 def unfollow(%User{} = follower, %User{} = followed) do
106 ap_followers = User.ap_followers(followed)
107 if following?(follower, followed) do
108 following = follower.following
109 |> List.delete(ap_followers)
110
111 { :ok, follower } = follower
112 |> follow_changeset(%{following: following})
113 |> Repo.update
114 { :ok, follower, ActivityPub.fetch_latest_follow(follower, followed)}
115 else
116 {:error, "Not subscribed!"}
117 end
118 end
119
120 def following?(%User{} = follower, %User{} = followed) do
121 Enum.member?(follower.following, User.ap_followers(followed))
122 end
123
124 def get_cached_by_ap_id(ap_id) do
125 key = "ap_id:#{ap_id}"
126 Cachex.get!(:user_cache, key, fallback: fn(_) -> Repo.get_by(User, ap_id: ap_id) end)
127 end
128
129 def get_cached_by_nickname(nickname) do
130 key = "nickname:#{nickname}"
131 Cachex.get!(:user_cache, key, fallback: fn(_) -> get_or_fetch_by_nickname(nickname) end)
132 end
133
134 def get_by_nickname(nickname) do
135 Repo.get_by(User, nickname: nickname)
136 end
137
138 def get_cached_user_info(user) do
139 key = "user_info:#{user.id}"
140 Cachex.get!(:user_cache, key, fallback: fn(_) -> user_info(user) end)
141 end
142
143 def get_or_fetch_by_nickname(nickname) do
144 with %User{} = user <- get_by_nickname(nickname) do
145 user
146 else _e ->
147 with [nick, domain] <- String.split(nickname, "@"),
148 {:ok, user} <- OStatus.make_user(nickname) do
149 user
150 else _e -> nil
151 end
152 end
153 end
154 end