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