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