d47d5788ced936adc09b09d822c6fde726d02318
[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.ThreadMute
10 alias Pleroma.User
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.ActivityPub.Utils
13
14 import Pleroma.Web.CommonAPI.Utils
15
16 def follow(follower, followed) do
17 with {:ok, follower} <- User.maybe_direct_follow(follower, followed),
18 {:ok, activity} <- ActivityPub.follow(follower, followed),
19 {:ok, follower, followed} <-
20 User.wait_and_refresh(
21 Pleroma.Config.get([:activitypub, :follow_handshake_timeout]),
22 follower,
23 followed
24 ) do
25 {:ok, follower, followed, activity}
26 end
27 end
28
29 def unfollow(follower, unfollowed) do
30 with {:ok, follower, _follow_activity} <- User.unfollow(follower, unfollowed),
31 {:ok, _activity} <- ActivityPub.unfollow(follower, unfollowed) do
32 {:ok, follower}
33 end
34 end
35
36 def accept_follow_request(follower, followed) do
37 with {:ok, follower} <- User.maybe_follow(follower, followed),
38 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
39 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "accept"),
40 {:ok, _activity} <-
41 ActivityPub.accept(%{
42 to: [follower.ap_id],
43 actor: followed,
44 object: follow_activity.data["id"],
45 type: "Accept"
46 }) do
47 {:ok, follower}
48 end
49 end
50
51 def reject_follow_request(follower, followed) do
52 with %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
53 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "reject"),
54 {:ok, _activity} <-
55 ActivityPub.reject(%{
56 to: [follower.ap_id],
57 actor: followed,
58 object: follow_activity.data["id"],
59 type: "Reject"
60 }) do
61 {:ok, follower}
62 end
63 end
64
65 def delete(activity_id, user) do
66 with %Activity{data: %{"object" => _}} = activity <-
67 Activity.get_by_id_with_object(activity_id),
68 %Object{} = object <- Object.normalize(activity),
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),
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) 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),
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) 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(%{"visibility" => "list:" <> list_id}) do
123 {:list, String.to_integer(list_id)}
124 end
125
126 def get_visibility(%{"in_reply_to_status_id" => status_id}) when not is_nil(status_id) do
127 case get_replied_to_activity(status_id) do
128 nil ->
129 "public"
130
131 in_reply_to ->
132 # XXX: these heuristics should be moved out of MastodonAPI.
133 with %Object{} = object <- Object.normalize(in_reply_to) do
134 Pleroma.Web.MastodonAPI.StatusView.get_visibility(object)
135 end
136 end
137 end
138
139 def get_visibility(_), do: "public"
140
141 def post(user, %{"status" => status} = data) do
142 visibility = get_visibility(data)
143 limit = Pleroma.Config.get([:instance, :limit])
144
145 with status <- String.trim(status),
146 attachments <- attachments_from_ids(data),
147 in_reply_to <- get_replied_to_activity(data["in_reply_to_status_id"]),
148 {content_html, mentions, tags} <-
149 make_content_html(
150 status,
151 attachments,
152 data,
153 visibility
154 ),
155 {to, cc} <- to_for_user_and_mentions(user, mentions, in_reply_to, visibility),
156 bcc <- bcc_for_list(user, visibility),
157 context <- make_context(in_reply_to),
158 cw <- data["spoiler_text"],
159 full_payload <- String.trim(status <> (data["spoiler_text"] || "")),
160 length when length in 1..limit <- String.length(full_payload),
161 object <-
162 make_note_data(
163 user.ap_id,
164 to,
165 context,
166 content_html,
167 attachments,
168 in_reply_to,
169 tags,
170 cw,
171 cc
172 ),
173 object <-
174 Map.put(
175 object,
176 "emoji",
177 (Formatter.get_emoji(status) ++ Formatter.get_emoji(data["spoiler_text"]))
178 |> Enum.reduce(%{}, fn {name, file, _}, acc ->
179 Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
180 end)
181 ) do
182 ActivityPub.create(
183 %{
184 to: to,
185 actor: user,
186 context: context,
187 object: object,
188 additional: %{"cc" => cc, "bcc" => bcc, "directMessage" => visibility == "direct"}
189 },
190 Pleroma.Web.ControllerHelper.truthy_param?(data["preview"]) || false
191 )
192 end
193 end
194
195 # Updates the emojis for a user based on their profile
196 def update(user) do
197 user =
198 with emoji <- emoji_from_profile(user),
199 source_data <- (user.info.source_data || %{}) |> Map.put("tag", emoji),
200 info_cng <- User.Info.set_source_data(user.info, source_data),
201 change <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
202 {:ok, user} <- User.update_and_set_cache(change) do
203 user
204 else
205 _e ->
206 user
207 end
208
209 ActivityPub.update(%{
210 local: true,
211 to: [user.follower_address],
212 cc: [],
213 actor: user.ap_id,
214 object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
215 })
216 end
217
218 def pin(id_or_ap_id, %{ap_id: user_ap_id} = user) do
219 with %Activity{
220 actor: ^user_ap_id,
221 data: %{
222 "type" => "Create"
223 },
224 object: %Object{
225 data: %{
226 "to" => object_to,
227 "type" => "Note"
228 }
229 }
230 } = activity <- get_by_id_or_ap_id(id_or_ap_id),
231 true <- Enum.member?(object_to, "https://www.w3.org/ns/activitystreams#Public"),
232 %{valid?: true} = info_changeset <-
233 User.Info.add_pinnned_activity(user.info, activity),
234 changeset <-
235 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
236 {:ok, _user} <- User.update_and_set_cache(changeset) do
237 {:ok, activity}
238 else
239 %{errors: [pinned_activities: {err, _}]} ->
240 {:error, err}
241
242 _ ->
243 {:error, "Could not pin"}
244 end
245 end
246
247 def unpin(id_or_ap_id, user) do
248 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
249 %{valid?: true} = info_changeset <-
250 User.Info.remove_pinnned_activity(user.info, activity),
251 changeset <-
252 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
253 {:ok, _user} <- User.update_and_set_cache(changeset) do
254 {:ok, activity}
255 else
256 %{errors: [pinned_activities: {err, _}]} ->
257 {:error, err}
258
259 _ ->
260 {:error, "Could not unpin"}
261 end
262 end
263
264 def add_mute(user, activity) do
265 with {:ok, _} <- ThreadMute.add_mute(user.id, activity.data["context"]) do
266 {:ok, activity}
267 else
268 {:error, _} -> {:error, "conversation is already muted"}
269 end
270 end
271
272 def remove_mute(user, activity) do
273 ThreadMute.remove_mute(user.id, activity.data["context"])
274 {:ok, activity}
275 end
276
277 def thread_muted?(%{id: nil} = _user, _activity), do: false
278
279 def thread_muted?(user, activity) do
280 with [] <- ThreadMute.check_muted(user.id, activity.data["context"]) do
281 false
282 else
283 _ -> true
284 end
285 end
286
287 def report(user, data) do
288 with {:account_id, %{"account_id" => account_id}} <- {:account_id, data},
289 {:account, %User{} = account} <- {:account, User.get_cached_by_id(account_id)},
290 {:ok, {content_html, _, _}} <- make_report_content_html(data["comment"]),
291 {:ok, statuses} <- get_report_statuses(account, data),
292 {:ok, activity} <-
293 ActivityPub.flag(%{
294 context: Utils.generate_context_id(),
295 actor: user,
296 account: account,
297 statuses: statuses,
298 content: content_html,
299 forward: data["forward"] || false
300 }) do
301 {:ok, activity}
302 else
303 {:error, err} -> {:error, err}
304 {:account_id, %{}} -> {:error, "Valid `account_id` required"}
305 {:account, nil} -> {:error, "Account not found"}
306 end
307 end
308
309 def hide_reblogs(user, muted) do
310 ap_id = muted.ap_id
311
312 if ap_id not in user.info.muted_reblogs do
313 info_changeset = User.Info.add_reblog_mute(user.info, ap_id)
314 changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset)
315 User.update_and_set_cache(changeset)
316 end
317 end
318
319 def show_reblogs(user, muted) do
320 ap_id = muted.ap_id
321
322 if ap_id in user.info.muted_reblogs do
323 info_changeset = User.Info.remove_reblog_mute(user.info, ap_id)
324 changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset)
325 User.update_and_set_cache(changeset)
326 end
327 end
328 end