Merge remote-tracking branch 'pleroma/develop' into feature/addressable-lists
[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 end
75 end
76
77 def repeat(id_or_ap_id, user) do
78 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
79 object <- Object.normalize(activity),
80 nil <- Utils.get_existing_announce(user.ap_id, object) do
81 ActivityPub.announce(user, object)
82 else
83 _ ->
84 {:error, "Could not repeat"}
85 end
86 end
87
88 def unrepeat(id_or_ap_id, user) do
89 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
90 object <- Object.normalize(activity) do
91 ActivityPub.unannounce(user, object)
92 else
93 _ ->
94 {:error, "Could not unrepeat"}
95 end
96 end
97
98 def favorite(id_or_ap_id, user) do
99 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
100 object <- Object.normalize(activity),
101 nil <- Utils.get_existing_like(user.ap_id, object) do
102 ActivityPub.like(user, object)
103 else
104 _ ->
105 {:error, "Could not favorite"}
106 end
107 end
108
109 def unfavorite(id_or_ap_id, user) do
110 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
111 object <- Object.normalize(activity) do
112 ActivityPub.unlike(user, object)
113 else
114 _ ->
115 {:error, "Could not unfavorite"}
116 end
117 end
118
119 def get_visibility(%{"visibility" => visibility})
120 when visibility in ~w{public unlisted private direct},
121 do: visibility
122
123 def get_visibility(%{"visibility" => "list:" <> list_id}) do
124 {:list, String.to_integer(list_id)}
125 end
126
127 def get_visibility(%{"in_reply_to_status_id" => status_id}) when not is_nil(status_id) do
128 case get_replied_to_activity(status_id) do
129 nil ->
130 "public"
131
132 in_reply_to ->
133 # XXX: these heuristics should be moved out of MastodonAPI.
134 with %Object{} = object <- Object.normalize(in_reply_to) do
135 Pleroma.Web.MastodonAPI.StatusView.get_visibility(object)
136 end
137 end
138 end
139
140 def get_visibility(_), do: "public"
141
142 def post(user, %{"status" => status} = data) do
143 visibility = get_visibility(data)
144 limit = Pleroma.Config.get([:instance, :limit])
145
146 with status <- String.trim(status),
147 attachments <- attachments_from_ids(data),
148 in_reply_to <- get_replied_to_activity(data["in_reply_to_status_id"]),
149 {content_html, mentions, tags} <-
150 make_content_html(
151 status,
152 attachments,
153 data,
154 visibility
155 ),
156 {to, cc} <- to_for_user_and_mentions(user, mentions, in_reply_to, visibility),
157 bcc <- bcc_for_list(user, 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 ActivityPub.create(
181 %{
182 to: to,
183 actor: user,
184 context: context,
185 object: object,
186 additional: %{"cc" => cc, "bcc" => bcc, "directMessage" => visibility == "direct"}
187 },
188 Pleroma.Web.ControllerHelper.truthy_param?(data["preview"]) || false
189 )
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 <- 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 },
222 object: %Object{
223 data: %{
224 "to" => object_to,
225 "type" => "Note"
226 }
227 }
228 } = activity <- get_by_id_or_ap_id(id_or_ap_id),
229 true <- Enum.member?(object_to, "https://www.w3.org/ns/activitystreams#Public"),
230 %{valid?: true} = info_changeset <-
231 User.Info.add_pinnned_activity(user.info, activity),
232 changeset <-
233 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
234 {:ok, _user} <- User.update_and_set_cache(changeset) do
235 {:ok, activity}
236 else
237 %{errors: [pinned_activities: {err, _}]} ->
238 {:error, err}
239
240 _ ->
241 {:error, "Could not pin"}
242 end
243 end
244
245 def unpin(id_or_ap_id, user) do
246 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
247 %{valid?: true} = info_changeset <-
248 User.Info.remove_pinnned_activity(user.info, activity),
249 changeset <-
250 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
251 {:ok, _user} <- User.update_and_set_cache(changeset) do
252 {:ok, activity}
253 else
254 %{errors: [pinned_activities: {err, _}]} ->
255 {:error, err}
256
257 _ ->
258 {:error, "Could not unpin"}
259 end
260 end
261
262 def add_mute(user, activity) do
263 with {:ok, _} <- ThreadMute.add_mute(user.id, activity.data["context"]) do
264 {:ok, activity}
265 else
266 {:error, _} -> {:error, "conversation is already muted"}
267 end
268 end
269
270 def remove_mute(user, activity) do
271 ThreadMute.remove_mute(user.id, activity.data["context"])
272 {:ok, activity}
273 end
274
275 def thread_muted?(%{id: nil} = _user, _activity), do: false
276
277 def thread_muted?(user, activity) do
278 with [] <- ThreadMute.check_muted(user.id, activity.data["context"]) do
279 false
280 else
281 _ -> true
282 end
283 end
284
285 def bookmarked?(user, activity) do
286 with %Bookmark{} <- Bookmark.get(user.id, activity.id) do
287 true
288 else
289 _ ->
290 false
291 end
292 end
293
294 def report(user, data) do
295 with {:account_id, %{"account_id" => account_id}} <- {:account_id, data},
296 {:account, %User{} = account} <- {:account, User.get_cached_by_id(account_id)},
297 {:ok, {content_html, _, _}} <- make_report_content_html(data["comment"]),
298 {:ok, statuses} <- get_report_statuses(account, data),
299 {:ok, activity} <-
300 ActivityPub.flag(%{
301 context: Utils.generate_context_id(),
302 actor: user,
303 account: account,
304 statuses: statuses,
305 content: content_html,
306 forward: data["forward"] || false
307 }) do
308 {:ok, activity}
309 else
310 {:error, err} -> {:error, err}
311 {:account_id, %{}} -> {:error, "Valid `account_id` required"}
312 {:account, nil} -> {:error, "Account not found"}
313 end
314 end
315
316 def hide_reblogs(user, muted) do
317 ap_id = muted.ap_id
318
319 if ap_id not in user.info.muted_reblogs do
320 info_changeset = User.Info.add_reblog_mute(user.info, ap_id)
321 changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset)
322 User.update_and_set_cache(changeset)
323 end
324 end
325
326 def show_reblogs(user, muted) do
327 ap_id = muted.ap_id
328
329 if ap_id in user.info.muted_reblogs do
330 info_changeset = User.Info.remove_reblog_mute(user.info, ap_id)
331 changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset)
332 User.update_and_set_cache(changeset)
333 end
334 end
335 end