Merge branch 'hotfix/delete-activities' into 'develop'
[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.Repo
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" => %{"id" => object_id}}} <- Repo.get(Activity, activity_id),
68 %Object{} = object <- Object.normalize(object_id),
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.data["object"]["id"]),
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.data["object"]["id"]) 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.data["object"]["id"]),
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.data["object"]["id"]) 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 Pleroma.Web.MastodonAPI.StatusView.get_visibility(in_reply_to.data["object"])
129 end
130 end
131
132 def get_visibility(_), do: "public"
133
134 def post(user, %{"status" => status} = data) do
135 visibility = get_visibility(data)
136 limit = Pleroma.Config.get([:instance, :limit])
137
138 with status <- String.trim(status),
139 attachments <- attachments_from_ids(data),
140 in_reply_to <- get_replied_to_activity(data["in_reply_to_status_id"]),
141 {content_html, mentions, tags} <-
142 make_content_html(
143 status,
144 attachments,
145 data
146 ),
147 {to, cc} <- to_for_user_and_mentions(user, mentions, in_reply_to, visibility),
148 context <- make_context(in_reply_to),
149 cw <- data["spoiler_text"],
150 full_payload <- String.trim(status <> (data["spoiler_text"] || "")),
151 length when length in 1..limit <- String.length(full_payload),
152 object <-
153 make_note_data(
154 user.ap_id,
155 to,
156 context,
157 content_html,
158 attachments,
159 in_reply_to,
160 tags,
161 cw,
162 cc
163 ),
164 object <-
165 Map.put(
166 object,
167 "emoji",
168 (Formatter.get_emoji(status) ++ Formatter.get_emoji(data["spoiler_text"]))
169 |> Enum.reduce(%{}, fn {name, file}, acc ->
170 Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
171 end)
172 ) do
173 res =
174 ActivityPub.create(%{
175 to: to,
176 actor: user,
177 context: context,
178 object: object,
179 additional: %{"cc" => cc, "directMessage" => visibility == "direct"}
180 })
181
182 res
183 end
184 end
185
186 # Updates the emojis for a user based on their profile
187 def update(user) do
188 user =
189 with emoji <- emoji_from_profile(user),
190 source_data <- (user.info.source_data || %{}) |> Map.put("tag", emoji),
191 info_cng <- Pleroma.User.Info.set_source_data(user.info, source_data),
192 change <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
193 {:ok, user} <- User.update_and_set_cache(change) do
194 user
195 else
196 _e ->
197 user
198 end
199
200 ActivityPub.update(%{
201 local: true,
202 to: [user.follower_address],
203 cc: [],
204 actor: user.ap_id,
205 object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
206 })
207 end
208
209 def pin(id_or_ap_id, %{ap_id: user_ap_id} = user) do
210 with %Activity{
211 actor: ^user_ap_id,
212 data: %{
213 "type" => "Create",
214 "object" => %{
215 "to" => object_to,
216 "type" => "Note"
217 }
218 }
219 } = activity <- get_by_id_or_ap_id(id_or_ap_id),
220 true <- Enum.member?(object_to, "https://www.w3.org/ns/activitystreams#Public"),
221 %{valid?: true} = info_changeset <-
222 Pleroma.User.Info.add_pinnned_activity(user.info, activity),
223 changeset <-
224 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
225 {:ok, _user} <- User.update_and_set_cache(changeset) do
226 {:ok, activity}
227 else
228 %{errors: [pinned_activities: {err, _}]} ->
229 {:error, err}
230
231 _ ->
232 {:error, "Could not pin"}
233 end
234 end
235
236 def unpin(id_or_ap_id, user) do
237 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
238 %{valid?: true} = info_changeset <-
239 Pleroma.User.Info.remove_pinnned_activity(user.info, activity),
240 changeset <-
241 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
242 {:ok, _user} <- User.update_and_set_cache(changeset) do
243 {:ok, activity}
244 else
245 %{errors: [pinned_activities: {err, _}]} ->
246 {:error, err}
247
248 _ ->
249 {:error, "Could not unpin"}
250 end
251 end
252
253 def add_mute(user, activity) do
254 with {:ok, _} <- ThreadMute.add_mute(user.id, activity.data["context"]) do
255 {:ok, activity}
256 else
257 {:error, _} -> {:error, "conversation is already muted"}
258 end
259 end
260
261 def remove_mute(user, activity) do
262 ThreadMute.remove_mute(user.id, activity.data["context"])
263 {:ok, activity}
264 end
265
266 def thread_muted?(%{id: nil} = _user, _activity), do: false
267
268 def thread_muted?(user, activity) do
269 with [] <- ThreadMute.check_muted(user.id, activity.data["context"]) do
270 false
271 else
272 _ -> true
273 end
274 end
275
276 def report(user, data) do
277 with {:account_id, %{"account_id" => account_id}} <- {:account_id, data},
278 {:account, %User{} = account} <- {:account, User.get_by_id(account_id)},
279 {:ok, {content_html, _, _}} <- make_report_content_html(data["comment"]),
280 {:ok, statuses} <- get_report_statuses(account, data),
281 {:ok, activity} <-
282 ActivityPub.flag(%{
283 context: Utils.generate_context_id(),
284 actor: user,
285 account: account,
286 statuses: statuses,
287 content: content_html
288 }) do
289 Enum.each(User.all_superusers(), fn superuser ->
290 superuser
291 |> Pleroma.AdminEmail.report(user, account, statuses, content_html)
292 |> Pleroma.Mailer.deliver_async()
293 end)
294
295 {:ok, activity}
296 else
297 {:error, err} -> {:error, err}
298 {:account_id, %{}} -> {:error, "Valid `account_id` required"}
299 {:account, nil} -> {:error, "Account not found"}
300 end
301 end
302 end