e8199200eec225ad5f60c624d360094bceccd783
[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 get_visibility(%{"visibility" => visibility}, in_reply_to)
123 when visibility in ~w{public unlisted private direct},
124 do: {visibility, get_replied_to_visibility(in_reply_to)}
125
126 def get_visibility(%{"visibility" => "list:" <> list_id}, in_reply_to) do
127 visibility = {:list, String.to_integer(list_id)}
128 {visibility, get_replied_to_visibility(in_reply_to)}
129 end
130
131 def get_visibility(_, in_reply_to) when not is_nil(in_reply_to) do
132 visibility = get_replied_to_visibility(in_reply_to)
133 {visibility, visibility}
134 end
135
136 def get_visibility(_, in_reply_to), do: {"public", get_replied_to_visibility(in_reply_to)}
137
138 def get_replied_to_visibility(nil), do: nil
139
140 def get_replied_to_visibility(activity) do
141 with %Object{} = object <- Object.normalize(activity) do
142 Pleroma.Web.ActivityPub.Visibility.get_visibility(object)
143 end
144 end
145
146 def post(user, %{"status" => status} = data) do
147 limit = Pleroma.Config.get([:instance, :limit])
148
149 with status <- String.trim(status),
150 attachments <- attachments_from_ids(data),
151 in_reply_to <- get_replied_to_activity(data["in_reply_to_status_id"]),
152 {visibility, in_reply_to_visibility} <- get_visibility(data, in_reply_to),
153 {_, false} <-
154 {:private_to_public, in_reply_to_visibility == "direct" && visibility != "direct"},
155 {content_html, mentions, tags} <-
156 make_content_html(
157 status,
158 attachments,
159 data,
160 visibility
161 ),
162 {to, cc} <- to_for_user_and_mentions(user, mentions, in_reply_to, visibility),
163 bcc <- bcc_for_list(user, visibility),
164 context <- make_context(in_reply_to),
165 cw <- data["spoiler_text"] || "",
166 sensitive <- data["sensitive"] || Enum.member?(tags, {"#nsfw", "nsfw"}),
167 full_payload <- String.trim(status <> cw),
168 length when length in 1..limit <- String.length(full_payload),
169 object <-
170 make_note_data(
171 user.ap_id,
172 to,
173 context,
174 content_html,
175 attachments,
176 in_reply_to,
177 tags,
178 cw,
179 cc,
180 sensitive
181 ),
182 object <-
183 Map.put(
184 object,
185 "emoji",
186 Formatter.get_emoji_map(full_payload)
187 ) do
188 ActivityPub.create(
189 %{
190 to: to,
191 actor: user,
192 context: context,
193 object: object,
194 additional: %{"cc" => cc, "bcc" => bcc, "directMessage" => visibility == "direct"}
195 },
196 Pleroma.Web.ControllerHelper.truthy_param?(data["preview"]) || false
197 )
198 else
199 e -> {:error, e}
200 end
201 end
202
203 # Updates the emojis for a user based on their profile
204 def update(user) do
205 user =
206 with emoji <- emoji_from_profile(user),
207 source_data <- (user.info.source_data || %{}) |> Map.put("tag", emoji),
208 info_cng <- User.Info.set_source_data(user.info, source_data),
209 change <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
210 {:ok, user} <- User.update_and_set_cache(change) do
211 user
212 else
213 _e ->
214 user
215 end
216
217 ActivityPub.update(%{
218 local: true,
219 to: [user.follower_address],
220 cc: [],
221 actor: user.ap_id,
222 object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
223 })
224 end
225
226 def pin(id_or_ap_id, %{ap_id: user_ap_id} = user) do
227 with %Activity{
228 actor: ^user_ap_id,
229 data: %{
230 "type" => "Create"
231 },
232 object: %Object{
233 data: %{
234 "to" => object_to,
235 "type" => "Note"
236 }
237 }
238 } = activity <- get_by_id_or_ap_id(id_or_ap_id),
239 true <- Enum.member?(object_to, "https://www.w3.org/ns/activitystreams#Public"),
240 %{valid?: true} = info_changeset <-
241 User.Info.add_pinnned_activity(user.info, activity),
242 changeset <-
243 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
244 {:ok, _user} <- User.update_and_set_cache(changeset) do
245 {:ok, activity}
246 else
247 %{errors: [pinned_activities: {err, _}]} ->
248 {:error, err}
249
250 _ ->
251 {:error, "Could not pin"}
252 end
253 end
254
255 def unpin(id_or_ap_id, user) do
256 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
257 %{valid?: true} = info_changeset <-
258 User.Info.remove_pinnned_activity(user.info, activity),
259 changeset <-
260 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
261 {:ok, _user} <- User.update_and_set_cache(changeset) do
262 {:ok, activity}
263 else
264 %{errors: [pinned_activities: {err, _}]} ->
265 {:error, err}
266
267 _ ->
268 {:error, "Could not unpin"}
269 end
270 end
271
272 def add_mute(user, activity) do
273 with {:ok, _} <- ThreadMute.add_mute(user.id, activity.data["context"]) do
274 {:ok, activity}
275 else
276 {:error, _} -> {:error, "conversation is already muted"}
277 end
278 end
279
280 def remove_mute(user, activity) do
281 ThreadMute.remove_mute(user.id, activity.data["context"])
282 {:ok, activity}
283 end
284
285 def thread_muted?(%{id: nil} = _user, _activity), do: false
286
287 def thread_muted?(user, activity) do
288 with [] <- ThreadMute.check_muted(user.id, activity.data["context"]) do
289 false
290 else
291 _ -> true
292 end
293 end
294
295 def bookmarked?(user, activity) do
296 with %Bookmark{} <- Bookmark.get(user.id, activity.id) do
297 true
298 else
299 _ ->
300 false
301 end
302 end
303
304 def report(user, data) do
305 with {:account_id, %{"account_id" => account_id}} <- {:account_id, data},
306 {:account, %User{} = account} <- {:account, User.get_cached_by_id(account_id)},
307 {:ok, {content_html, _, _}} <- make_report_content_html(data["comment"]),
308 {:ok, statuses} <- get_report_statuses(account, data),
309 {:ok, activity} <-
310 ActivityPub.flag(%{
311 context: Utils.generate_context_id(),
312 actor: user,
313 account: account,
314 statuses: statuses,
315 content: content_html,
316 forward: data["forward"] || false
317 }) do
318 {:ok, activity}
319 else
320 {:error, err} -> {:error, err}
321 {:account_id, %{}} -> {:error, "Valid `account_id` required"}
322 {:account, nil} -> {:error, "Account not found"}
323 end
324 end
325
326 def update_report_state(activity_id, state) do
327 with %Activity{} = activity <- Activity.get_by_id(activity_id),
328 {:ok, activity} <- Utils.update_report_state(activity, state) do
329 {:ok, activity}
330 else
331 nil ->
332 {:error, :not_found}
333
334 {:error, reason} ->
335 {:error, reason}
336
337 _ ->
338 {:error, "Could not update state"}
339 end
340 end
341
342 def update_activity_scope(activity_id, opts \\ %{}) do
343 with %Activity{} = activity <- Activity.get_by_id_with_object(activity_id),
344 {:ok, activity} <- toggle_sensitive(activity, opts),
345 {:ok, activity} <- set_visibility(activity, opts) do
346 {:ok, activity}
347 else
348 nil ->
349 {:error, :not_found}
350
351 {:error, reason} ->
352 {:error, reason}
353 end
354 end
355
356 defp toggle_sensitive(activity, %{"sensitive" => sensitive}) when sensitive in ~w(true false) do
357 toggle_sensitive(activity, %{"sensitive" => String.to_existing_atom(sensitive)})
358 end
359
360 defp toggle_sensitive(%Activity{object: object} = activity, %{"sensitive" => sensitive})
361 when is_boolean(sensitive) do
362 new_data = Map.put(object.data, "sensitive", sensitive)
363
364 {:ok, object} =
365 object
366 |> Object.change(%{data: new_data})
367 |> Object.update_and_set_cache()
368
369 {:ok, Map.put(activity, :object, object)}
370 end
371
372 defp toggle_sensitive(activity, _), do: {:ok, activity}
373
374 defp set_visibility(activity, %{"visibility" => visibility}) do
375 Utils.update_activity_visibility(activity, visibility)
376 end
377
378 defp set_visibility(activity, _), do: {:ok, activity}
379
380 def hide_reblogs(user, muted) do
381 ap_id = muted.ap_id
382
383 if ap_id not in user.info.muted_reblogs do
384 info_changeset = User.Info.add_reblog_mute(user.info, ap_id)
385 changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset)
386 User.update_and_set_cache(changeset)
387 end
388 end
389
390 def show_reblogs(user, muted) do
391 ap_id = muted.ap_id
392
393 if ap_id in user.info.muted_reblogs do
394 info_changeset = User.Info.remove_reblog_mute(user.info, ap_id)
395 changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset)
396 User.update_and_set_cache(changeset)
397 end
398 end
399 end