f54f8a7b9e9f4e27ddef5ee1ce06f002226a1b2d
[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.Bookmark
8 alias Pleroma.Formatter
9 alias Pleroma.Object
10 alias Pleroma.ThreadMute
11 alias Pleroma.User
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.ActivityPub.Utils
14
15 import Pleroma.Web.CommonAPI.Utils
16
17 def follow(follower, followed) do
18 with {:ok, follower} <- User.maybe_direct_follow(follower, followed),
19 {:ok, activity} <- ActivityPub.follow(follower, followed),
20 {:ok, follower, followed} <-
21 User.wait_and_refresh(
22 Pleroma.Config.get([:activitypub, :follow_handshake_timeout]),
23 follower,
24 followed
25 ) do
26 {:ok, follower, followed, activity}
27 end
28 end
29
30 def unfollow(follower, unfollowed) do
31 with {:ok, follower, _follow_activity} <- User.unfollow(follower, unfollowed),
32 {:ok, _activity} <- ActivityPub.unfollow(follower, unfollowed) do
33 {:ok, follower}
34 end
35 end
36
37 def accept_follow_request(follower, followed) do
38 with {:ok, follower} <- User.maybe_follow(follower, followed),
39 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
40 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "accept"),
41 {:ok, _activity} <-
42 ActivityPub.accept(%{
43 to: [follower.ap_id],
44 actor: followed,
45 object: follow_activity.data["id"],
46 type: "Accept"
47 }) do
48 {:ok, follower}
49 end
50 end
51
52 def reject_follow_request(follower, followed) do
53 with %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
54 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "reject"),
55 {:ok, _activity} <-
56 ActivityPub.reject(%{
57 to: [follower.ap_id],
58 actor: followed,
59 object: follow_activity.data["id"],
60 type: "Reject"
61 }) do
62 {:ok, follower}
63 end
64 end
65
66 def delete(activity_id, user) do
67 with %Activity{data: %{"object" => _}} = activity <-
68 Activity.get_by_id_with_object(activity_id),
69 %Object{} = object <- Object.normalize(activity),
70 true <- User.superuser?(user) || user.ap_id == object.data["actor"],
71 {:ok, _} <- unpin(activity_id, user),
72 {:ok, delete} <- ActivityPub.delete(object) do
73 {:ok, delete}
74 else
75 _ ->
76 {:error, "Could not delete"}
77 end
78 end
79
80 def repeat(id_or_ap_id, user) do
81 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
82 object <- Object.normalize(activity),
83 nil <- Utils.get_existing_announce(user.ap_id, object) do
84 ActivityPub.announce(user, object)
85 else
86 _ ->
87 {:error, "Could not repeat"}
88 end
89 end
90
91 def unrepeat(id_or_ap_id, user) do
92 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
93 object <- Object.normalize(activity) do
94 ActivityPub.unannounce(user, object)
95 else
96 _ ->
97 {:error, "Could not unrepeat"}
98 end
99 end
100
101 def favorite(id_or_ap_id, user) do
102 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
103 object <- Object.normalize(activity),
104 nil <- Utils.get_existing_like(user.ap_id, object) do
105 ActivityPub.like(user, object)
106 else
107 _ ->
108 {:error, "Could not favorite"}
109 end
110 end
111
112 def unfavorite(id_or_ap_id, user) do
113 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
114 object <- Object.normalize(activity) do
115 ActivityPub.unlike(user, object)
116 else
117 _ ->
118 {:error, "Could not unfavorite"}
119 end
120 end
121
122 def vote(user, object, choices) do
123 with "Question" <- object.data["type"],
124 {:author, false} <- {:author, object.data["actor"] == user.ap_id},
125 {:existing_votes, []} <- {:existing_votes, Utils.get_existing_votes(user.ap_id, object)},
126 {options, max_count} <- get_options_and_max_count(object),
127 option_count <- Enum.count(options),
128 {:choice_check, {choices, true}} <-
129 {:choice_check, normalize_and_validate_choice_indices(choices, option_count)},
130 {:count_check, true} <- {:count_check, Enum.count(choices) <= max_count} do
131 answer_activities =
132 Enum.map(choices, fn index ->
133 answer_data = make_answer_data(user, object, Enum.at(options, index)["name"])
134
135 ActivityPub.create(%{
136 to: answer_data["to"],
137 actor: user,
138 context: object.data["context"],
139 object: answer_data,
140 additional: %{"cc" => answer_data["cc"]}
141 })
142 end)
143
144 {:ok, answer_activities, object}
145 else
146 {:author, _} -> {:error, "Already voted"}
147 {:existing_votes, _} -> {:error, "Already voted"}
148 {:choice_check, {_, false}} -> {:error, "Invalid indices"}
149 {:count_check, false} -> {:error, "Too many choices"}
150 end
151 end
152
153 defp get_options_and_max_count(object) do
154 if Map.has_key?(object.data, "anyOf") do
155 {object.data["anyOf"], Enum.count(object.data["anyOf"])}
156 else
157 {object.data["oneOf"], 1}
158 end
159 end
160
161 defp normalize_and_validate_choice_indices(choices, count) do
162 Enum.map_reduce(choices, true, fn index, valid ->
163 index = if is_binary(index), do: String.to_integer(index), else: index
164 {index, if(valid, do: index < count, else: valid)}
165 end)
166 end
167
168 def get_visibility(%{"visibility" => visibility}, in_reply_to)
169 when visibility in ~w{public unlisted private direct},
170 do: {visibility, get_replied_to_visibility(in_reply_to)}
171
172 def get_visibility(_, in_reply_to) when not is_nil(in_reply_to) do
173 visibility = get_replied_to_visibility(in_reply_to)
174 {visibility, visibility}
175 end
176
177 def get_visibility(_, in_reply_to), do: {"public", get_replied_to_visibility(in_reply_to)}
178
179 def get_replied_to_visibility(nil), do: nil
180
181 def get_replied_to_visibility(activity) do
182 with %Object{} = object <- Object.normalize(activity) do
183 Pleroma.Web.ActivityPub.Visibility.get_visibility(object)
184 end
185 end
186
187 def post(user, %{"status" => status} = data) do
188 limit = Pleroma.Config.get([:instance, :limit])
189
190 with status <- String.trim(status),
191 attachments <- attachments_from_ids(data),
192 in_reply_to <- get_replied_to_activity(data["in_reply_to_status_id"]),
193 {visibility, in_reply_to_visibility} <- get_visibility(data, in_reply_to),
194 {_, false} <-
195 {:private_to_public, in_reply_to_visibility == "direct" && visibility != "direct"},
196 {content_html, mentions, tags} <-
197 make_content_html(
198 status,
199 attachments,
200 data,
201 visibility
202 ),
203 {poll, poll_emoji} <- make_poll_data(data),
204 {to, cc} <- to_for_user_and_mentions(user, mentions, in_reply_to, visibility),
205 context <- make_context(in_reply_to),
206 cw <- data["spoiler_text"] || "",
207 sensitive <- data["sensitive"] || Enum.member?(tags, {"#nsfw", "nsfw"}),
208 full_payload <- String.trim(status <> cw),
209 length when length in 1..limit <- String.length(full_payload),
210 object <-
211 make_note_data(
212 user.ap_id,
213 to,
214 context,
215 content_html,
216 attachments,
217 in_reply_to,
218 tags,
219 cw,
220 cc,
221 sensitive,
222 poll
223 ),
224 object <-
225 Map.put(
226 object,
227 "emoji",
228 Map.merge(Formatter.get_emoji_map(full_payload), poll_emoji)
229 ) do
230 res =
231 ActivityPub.create(
232 %{
233 to: to,
234 actor: user,
235 context: context,
236 object: object,
237 additional: %{"cc" => cc, "directMessage" => visibility == "direct"}
238 },
239 Pleroma.Web.ControllerHelper.truthy_param?(data["preview"]) || false
240 )
241
242 res
243 else
244 e -> {:error, e}
245 end
246 end
247
248 # Updates the emojis for a user based on their profile
249 def update(user) do
250 user =
251 with emoji <- emoji_from_profile(user),
252 source_data <- (user.info.source_data || %{}) |> Map.put("tag", emoji),
253 info_cng <- User.Info.set_source_data(user.info, source_data),
254 change <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
255 {:ok, user} <- User.update_and_set_cache(change) do
256 user
257 else
258 _e ->
259 user
260 end
261
262 ActivityPub.update(%{
263 local: true,
264 to: [user.follower_address],
265 cc: [],
266 actor: user.ap_id,
267 object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
268 })
269 end
270
271 def pin(id_or_ap_id, %{ap_id: user_ap_id} = user) do
272 with %Activity{
273 actor: ^user_ap_id,
274 data: %{
275 "type" => "Create"
276 },
277 object: %Object{
278 data: %{
279 "to" => object_to,
280 "type" => "Note"
281 }
282 }
283 } = activity <- get_by_id_or_ap_id(id_or_ap_id),
284 true <- Enum.member?(object_to, "https://www.w3.org/ns/activitystreams#Public"),
285 %{valid?: true} = info_changeset <-
286 User.Info.add_pinnned_activity(user.info, activity),
287 changeset <-
288 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
289 {:ok, _user} <- User.update_and_set_cache(changeset) do
290 {:ok, activity}
291 else
292 %{errors: [pinned_activities: {err, _}]} ->
293 {:error, err}
294
295 _ ->
296 {:error, "Could not pin"}
297 end
298 end
299
300 def unpin(id_or_ap_id, user) do
301 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
302 %{valid?: true} = info_changeset <-
303 User.Info.remove_pinnned_activity(user.info, activity),
304 changeset <-
305 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
306 {:ok, _user} <- User.update_and_set_cache(changeset) do
307 {:ok, activity}
308 else
309 %{errors: [pinned_activities: {err, _}]} ->
310 {:error, err}
311
312 _ ->
313 {:error, "Could not unpin"}
314 end
315 end
316
317 def add_mute(user, activity) do
318 with {:ok, _} <- ThreadMute.add_mute(user.id, activity.data["context"]) do
319 {:ok, activity}
320 else
321 {:error, _} -> {:error, "conversation is already muted"}
322 end
323 end
324
325 def remove_mute(user, activity) do
326 ThreadMute.remove_mute(user.id, activity.data["context"])
327 {:ok, activity}
328 end
329
330 def thread_muted?(%{id: nil} = _user, _activity), do: false
331
332 def thread_muted?(user, activity) do
333 with [] <- ThreadMute.check_muted(user.id, activity.data["context"]) do
334 false
335 else
336 _ -> true
337 end
338 end
339
340 def bookmarked?(user, activity) do
341 with %Bookmark{} <- Bookmark.get(user.id, activity.id) do
342 true
343 else
344 _ ->
345 false
346 end
347 end
348
349 def report(user, data) do
350 with {:account_id, %{"account_id" => account_id}} <- {:account_id, data},
351 {:account, %User{} = account} <- {:account, User.get_cached_by_id(account_id)},
352 {:ok, {content_html, _, _}} <- make_report_content_html(data["comment"]),
353 {:ok, statuses} <- get_report_statuses(account, data),
354 {:ok, activity} <-
355 ActivityPub.flag(%{
356 context: Utils.generate_context_id(),
357 actor: user,
358 account: account,
359 statuses: statuses,
360 content: content_html,
361 forward: data["forward"] || false
362 }) do
363 {:ok, activity}
364 else
365 {:error, err} -> {:error, err}
366 {:account_id, %{}} -> {:error, "Valid `account_id` required"}
367 {:account, nil} -> {:error, "Account not found"}
368 end
369 end
370
371 def update_report_state(activity_id, state) do
372 with %Activity{} = activity <- Activity.get_by_id(activity_id),
373 {:ok, activity} <- Utils.update_report_state(activity, state) do
374 {:ok, activity}
375 else
376 nil ->
377 {:error, :not_found}
378
379 {:error, reason} ->
380 {:error, reason}
381
382 _ ->
383 {:error, "Could not update state"}
384 end
385 end
386
387 def update_activity_scope(activity_id, opts \\ %{}) do
388 with %Activity{} = activity <- Activity.get_by_id_with_object(activity_id),
389 {:ok, activity} <- toggle_sensitive(activity, opts),
390 {:ok, activity} <- set_visibility(activity, opts) do
391 {:ok, activity}
392 else
393 nil ->
394 {:error, :not_found}
395
396 {:error, reason} ->
397 {:error, reason}
398 end
399 end
400
401 defp toggle_sensitive(activity, %{"sensitive" => sensitive}) when sensitive in ~w(true false) do
402 toggle_sensitive(activity, %{"sensitive" => String.to_existing_atom(sensitive)})
403 end
404
405 defp toggle_sensitive(%Activity{object: object} = activity, %{"sensitive" => sensitive})
406 when is_boolean(sensitive) do
407 new_data = Map.put(object.data, "sensitive", sensitive)
408
409 {:ok, object} =
410 object
411 |> Object.change(%{data: new_data})
412 |> Object.update_and_set_cache()
413
414 {:ok, Map.put(activity, :object, object)}
415 end
416
417 defp toggle_sensitive(activity, _), do: {:ok, activity}
418
419 defp set_visibility(activity, %{"visibility" => visibility}) do
420 Utils.update_activity_visibility(activity, visibility)
421 end
422
423 defp set_visibility(activity, _), do: {:ok, activity}
424
425 def hide_reblogs(user, muted) do
426 ap_id = muted.ap_id
427
428 if ap_id not in user.info.muted_reblogs do
429 info_changeset = User.Info.add_reblog_mute(user.info, ap_id)
430 changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset)
431 User.update_and_set_cache(changeset)
432 end
433 end
434
435 def show_reblogs(user, muted) do
436 ap_id = muted.ap_id
437
438 if ap_id in user.info.muted_reblogs do
439 info_changeset = User.Info.remove_reblog_mute(user.info, ap_id)
440 changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset)
441 User.update_and_set_cache(changeset)
442 end
443 end
444 end