Merge branch 'develop' into feature/database-compaction
[akkoma] / lib / pleroma / web / common_api / common_api.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.CommonAPI do
6 alias Pleroma.Activity
7 alias Pleroma.Formatter
8 alias Pleroma.Object
9 alias Pleroma.ThreadMute
10 alias Pleroma.User
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.ActivityPub.Utils
13
14 import Pleroma.Web.CommonAPI.Utils
15
16 def follow(follower, followed) do
17 with {:ok, follower} <- User.maybe_direct_follow(follower, followed),
18 {:ok, activity} <- ActivityPub.follow(follower, followed),
19 {:ok, follower, followed} <-
20 User.wait_and_refresh(
21 Pleroma.Config.get([:activitypub, :follow_handshake_timeout]),
22 follower,
23 followed
24 ) do
25 {:ok, follower, followed, activity}
26 end
27 end
28
29 def unfollow(follower, unfollowed) do
30 with {:ok, follower, _follow_activity} <- User.unfollow(follower, unfollowed),
31 {:ok, _activity} <- ActivityPub.unfollow(follower, unfollowed) do
32 {:ok, follower}
33 end
34 end
35
36 def accept_follow_request(follower, followed) do
37 with {:ok, follower} <- User.maybe_follow(follower, followed),
38 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
39 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "accept"),
40 {:ok, _activity} <-
41 ActivityPub.accept(%{
42 to: [follower.ap_id],
43 actor: followed,
44 object: follow_activity.data["id"],
45 type: "Accept"
46 }) do
47 {:ok, follower}
48 end
49 end
50
51 def reject_follow_request(follower, followed) do
52 with %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
53 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "reject"),
54 {:ok, _activity} <-
55 ActivityPub.reject(%{
56 to: [follower.ap_id],
57 actor: followed,
58 object: follow_activity.data["id"],
59 type: "Reject"
60 }) do
61 {:ok, follower}
62 end
63 end
64
65 def delete(activity_id, user) do
66 with %Activity{data: %{"object" => _}} = activity <-
67 Activity.get_by_id_with_object(activity_id),
68 %Object{} = object <- Object.normalize(activity),
69 true <- User.superuser?(user) || user.ap_id == object.data["actor"],
70 {:ok, _} <- unpin(activity_id, user),
71 {:ok, delete} <- ActivityPub.delete(object) do
72 {:ok, delete}
73 end
74 end
75
76 def repeat(id_or_ap_id, user) do
77 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
78 object <- Object.normalize(activity),
79 nil <- Utils.get_existing_announce(user.ap_id, object) do
80 ActivityPub.announce(user, object)
81 else
82 _ ->
83 {:error, "Could not repeat"}
84 end
85 end
86
87 def unrepeat(id_or_ap_id, user) do
88 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
89 object <- Object.normalize(activity) do
90 ActivityPub.unannounce(user, object)
91 else
92 _ ->
93 {:error, "Could not unrepeat"}
94 end
95 end
96
97 def favorite(id_or_ap_id, user) do
98 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
99 object <- Object.normalize(activity),
100 nil <- Utils.get_existing_like(user.ap_id, object) do
101 ActivityPub.like(user, object)
102 else
103 _ ->
104 {:error, "Could not favorite"}
105 end
106 end
107
108 def unfavorite(id_or_ap_id, user) do
109 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
110 object <- Object.normalize(activity) do
111 ActivityPub.unlike(user, object)
112 else
113 _ ->
114 {:error, "Could not unfavorite"}
115 end
116 end
117
118 def get_visibility(%{"visibility" => visibility})
119 when visibility in ~w{public unlisted private direct},
120 do: visibility
121
122 def get_visibility(%{"in_reply_to_status_id" => status_id}) when not is_nil(status_id) do
123 case get_replied_to_activity(status_id) do
124 nil ->
125 "public"
126
127 in_reply_to ->
128 # XXX: these heuristics should be moved out of MastodonAPI.
129 with %Object{} = object <- Object.normalize(in_reply_to) do
130 Pleroma.Web.MastodonAPI.StatusView.get_visibility(object.data)
131 end
132 end
133 end
134
135 def get_visibility(_), do: "public"
136
137 def post(user, %{"status" => status} = data) do
138 visibility = get_visibility(data)
139 limit = Pleroma.Config.get([:instance, :limit])
140
141 with status <- String.trim(status),
142 attachments <- attachments_from_ids(data),
143 in_reply_to <- get_replied_to_activity(data["in_reply_to_status_id"]),
144 {content_html, mentions, tags} <-
145 make_content_html(
146 status,
147 attachments,
148 data,
149 visibility
150 ),
151 {to, cc} <- to_for_user_and_mentions(user, mentions, in_reply_to, visibility),
152 context <- make_context(in_reply_to),
153 cw <- data["spoiler_text"],
154 full_payload <- String.trim(status <> (data["spoiler_text"] || "")),
155 length when length in 1..limit <- String.length(full_payload),
156 object <-
157 make_note_data(
158 user.ap_id,
159 to,
160 context,
161 content_html,
162 attachments,
163 in_reply_to,
164 tags,
165 cw,
166 cc
167 ),
168 object <-
169 Map.put(
170 object,
171 "emoji",
172 (Formatter.get_emoji(status) ++ Formatter.get_emoji(data["spoiler_text"]))
173 |> Enum.reduce(%{}, fn {name, file, _}, acc ->
174 Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
175 end)
176 ) do
177 res =
178 ActivityPub.create(
179 %{
180 to: to,
181 actor: user,
182 context: context,
183 object: object,
184 additional: %{"cc" => cc, "directMessage" => visibility == "direct"}
185 },
186 Pleroma.Web.ControllerHelper.truthy_param?(data["preview"]) || false
187 )
188
189 res
190 end
191 end
192
193 # Updates the emojis for a user based on their profile
194 def update(user) do
195 user =
196 with emoji <- emoji_from_profile(user),
197 source_data <- (user.info.source_data || %{}) |> Map.put("tag", emoji),
198 info_cng <- Pleroma.User.Info.set_source_data(user.info, source_data),
199 change <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
200 {:ok, user} <- User.update_and_set_cache(change) do
201 user
202 else
203 _e ->
204 user
205 end
206
207 ActivityPub.update(%{
208 local: true,
209 to: [user.follower_address],
210 cc: [],
211 actor: user.ap_id,
212 object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
213 })
214 end
215
216 def pin(id_or_ap_id, %{ap_id: user_ap_id} = user) do
217 with %Activity{
218 actor: ^user_ap_id,
219 data: %{
220 "type" => "Create",
221 "object" => %{
222 "to" => object_to,
223 "type" => "Note"
224 }
225 }
226 } = activity <- get_by_id_or_ap_id(id_or_ap_id),
227 true <- Enum.member?(object_to, "https://www.w3.org/ns/activitystreams#Public"),
228 %{valid?: true} = info_changeset <-
229 Pleroma.User.Info.add_pinnned_activity(user.info, activity),
230 changeset <-
231 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
232 {:ok, _user} <- User.update_and_set_cache(changeset) do
233 {:ok, activity}
234 else
235 %{errors: [pinned_activities: {err, _}]} ->
236 {:error, err}
237
238 _ ->
239 {:error, "Could not pin"}
240 end
241 end
242
243 def unpin(id_or_ap_id, user) do
244 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
245 %{valid?: true} = info_changeset <-
246 Pleroma.User.Info.remove_pinnned_activity(user.info, activity),
247 changeset <-
248 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
249 {:ok, _user} <- User.update_and_set_cache(changeset) do
250 {:ok, activity}
251 else
252 %{errors: [pinned_activities: {err, _}]} ->
253 {:error, err}
254
255 _ ->
256 {:error, "Could not unpin"}
257 end
258 end
259
260 def add_mute(user, activity) do
261 with {:ok, _} <- ThreadMute.add_mute(user.id, activity.data["context"]) do
262 {:ok, activity}
263 else
264 {:error, _} -> {:error, "conversation is already muted"}
265 end
266 end
267
268 def remove_mute(user, activity) do
269 ThreadMute.remove_mute(user.id, activity.data["context"])
270 {:ok, activity}
271 end
272
273 def thread_muted?(%{id: nil} = _user, _activity), do: false
274
275 def thread_muted?(user, activity) do
276 with [] <- ThreadMute.check_muted(user.id, activity.data["context"]) do
277 false
278 else
279 _ -> true
280 end
281 end
282
283 def report(user, data) do
284 with {:account_id, %{"account_id" => account_id}} <- {:account_id, data},
285 {:account, %User{} = account} <- {:account, User.get_by_id(account_id)},
286 {:ok, {content_html, _, _}} <- make_report_content_html(data["comment"]),
287 {:ok, statuses} <- get_report_statuses(account, data),
288 {:ok, activity} <-
289 ActivityPub.flag(%{
290 context: Utils.generate_context_id(),
291 actor: user,
292 account: account,
293 statuses: statuses,
294 content: content_html,
295 forward: data["forward"] || false
296 }) do
297 {:ok, activity}
298 else
299 {:error, err} -> {:error, err}
300 {:account_id, %{}} -> {:error, "Valid `account_id` required"}
301 {:account, nil} -> {:error, "Account not found"}
302 end
303 end
304
305 def hide_reblogs(user, muted) do
306 ap_id = muted.ap_id
307
308 if ap_id not in user.info.muted_reblogs do
309 info_changeset = User.Info.add_reblog_mute(user.info, ap_id)
310 changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset)
311 User.update_and_set_cache(changeset)
312 end
313 end
314
315 def show_reblogs(user, muted) do
316 ap_id = muted.ap_id
317
318 if ap_id in user.info.muted_reblogs do
319 info_changeset = User.Info.remove_reblog_mute(user.info, ap_id)
320 changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset)
321 User.update_and_set_cache(changeset)
322 end
323 end
324 end