Add Undo of Follow Activity insertion
[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 ap_id(%User{nickname: nickname}) do
24 "#{Pleroma.Web.base_url}/users/#{nickname}"
25 end
26
27 def ap_followers(%User{} = user) do
28 "#{ap_id(user)}/followers"
29 end
30
31 def follow_changeset(struct, params \\ %{}) do
32 struct
33 |> cast(params, [:following])
34 |> validate_required([:following])
35 end
36
37 def user_info(%User{} = user) do
38 note_count_query = from a in Object,
39 where: fragment("? @> ?", a.data, ^%{actor: user.ap_id, type: "Note"}),
40 select: count(a.id)
41
42 follower_count_query = from u in User,
43 where: fragment("? @> ?", u.following, ^User.ap_followers(user)),
44 select: count(u.id)
45
46 %{
47 following_count: length(user.following),
48 note_count: Repo.one(note_count_query),
49 follower_count: Repo.one(follower_count_query)
50 }
51 end
52
53 def register_changeset(struct, params \\ %{}) do
54 changeset = struct
55 |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
56 |> validate_required([:bio, :email, :name, :nickname, :password, :password_confirmation])
57 |> validate_confirmation(:password)
58 |> unique_constraint(:email)
59 |> unique_constraint(:nickname)
60
61 if changeset.valid? do
62 hashed = Comeonin.Pbkdf2.hashpwsalt(changeset.changes[:password])
63 ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]})
64 followers = User.ap_followers(%User{nickname: changeset.changes[:nickname]})
65 changeset
66 |> put_change(:password_hash, hashed)
67 |> put_change(:ap_id, ap_id)
68 |> put_change(:following, [followers])
69 else
70 changeset
71 end
72 end
73
74 def follow(%User{} = follower, %User{} = followed) do
75 ap_followers = User.ap_followers(followed)
76 if following?(follower, followed) do
77 { :error,
78 "Could not follow user: #{followed.nickname} is already on your list." }
79 else
80 following = [ap_followers | follower.following]
81 |> Enum.uniq
82
83 follower
84 |> follow_changeset(%{following: following})
85 |> Repo.update
86 end
87 end
88
89 def unfollow(%User{} = follower, %User{} = followed) do
90 ap_followers = User.ap_followers(followed)
91 if following?(follower, followed) do
92 following = follower.following
93 |> List.delete(ap_followers)
94
95 { :ok, follower } = follower
96 |> follow_changeset(%{following: following})
97 |> Repo.update
98 { :ok, follower, ActivityPub.fetch_latest_follow(follower, followed)}
99 else
100 { :error, "Not subscribed!" }
101 end
102 end
103
104 def following?(%User{} = follower, %User{} = followed) do
105 Enum.member?(follower.following, User.ap_followers(followed))
106 end
107
108 def get_cached_by_ap_id(ap_id) do
109 key = "ap_id:#{ap_id}"
110 Cachex.get!(:user_cache, key, fallback: fn(_) -> Repo.get_by(User, ap_id: ap_id) end)
111 end
112
113 def get_cached_by_nickname(nickname) do
114 key = "nickname:#{nickname}"
115 Cachex.get!(:user_cache, key, fallback: fn(_) -> Repo.get_by(User, nickname: nickname) end)
116 end
117 end