7114d6de6b54988fc43b89ab28b56684148a19ae
[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.User
7 alias Pleroma.Repo
8 alias Pleroma.Activity
9 alias Pleroma.Object
10 alias Pleroma.ThreadMute
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.ActivityPub.Utils
13 alias Pleroma.Formatter
14
15 import Pleroma.Web.CommonAPI.Utils
16
17 def delete(activity_id, user) do
18 with %Activity{data: %{"object" => %{"id" => object_id}}} <- Repo.get(Activity, activity_id),
19 %Object{} = object <- Object.normalize(object_id),
20 true <- user.info.is_moderator || user.ap_id == object.data["actor"],
21 {:ok, _} <- unpin(activity_id, user),
22 {:ok, delete} <- ActivityPub.delete(object) do
23 {:ok, delete}
24 end
25 end
26
27 def repeat(id_or_ap_id, user) do
28 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
29 object <- Object.normalize(activity.data["object"]["id"]),
30 nil <- Utils.get_existing_announce(user.ap_id, object) do
31 ActivityPub.announce(user, object)
32 else
33 _ ->
34 {:error, "Could not repeat"}
35 end
36 end
37
38 def unrepeat(id_or_ap_id, user) do
39 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
40 object <- Object.normalize(activity.data["object"]["id"]) do
41 ActivityPub.unannounce(user, object)
42 else
43 _ ->
44 {:error, "Could not unrepeat"}
45 end
46 end
47
48 def favorite(id_or_ap_id, user) do
49 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
50 object <- Object.normalize(activity.data["object"]["id"]),
51 nil <- Utils.get_existing_like(user.ap_id, object) do
52 ActivityPub.like(user, object)
53 else
54 _ ->
55 {:error, "Could not favorite"}
56 end
57 end
58
59 def unfavorite(id_or_ap_id, user) do
60 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
61 object <- Object.normalize(activity.data["object"]["id"]) do
62 ActivityPub.unlike(user, object)
63 else
64 _ ->
65 {:error, "Could not unfavorite"}
66 end
67 end
68
69 def get_visibility(%{"visibility" => visibility})
70 when visibility in ~w{public unlisted private direct},
71 do: visibility
72
73 def get_visibility(%{"in_reply_to_status_id" => status_id}) when not is_nil(status_id) do
74 case get_replied_to_activity(status_id) do
75 nil ->
76 "public"
77
78 inReplyTo ->
79 Pleroma.Web.MastodonAPI.StatusView.get_visibility(inReplyTo.data["object"])
80 end
81 end
82
83 def get_visibility(_), do: "public"
84
85 def post(user, %{"status" => status} = data) do
86 visibility = get_visibility(data)
87 limit = Pleroma.Config.get([:instance, :limit])
88
89 with status <- String.trim(status),
90 attachments <- attachments_from_ids(data),
91 inReplyTo <- get_replied_to_activity(data["in_reply_to_status_id"]),
92 {content_html, mentions, tags} <-
93 make_content_html(
94 status,
95 attachments,
96 data
97 ),
98 {to, cc} <- to_for_user_and_mentions(user, mentions, inReplyTo, visibility),
99 context <- make_context(inReplyTo),
100 cw <- data["spoiler_text"],
101 full_payload <- String.trim(status <> (data["spoiler_text"] || "")),
102 length when length in 1..limit <- String.length(full_payload),
103 object <-
104 make_note_data(
105 user.ap_id,
106 to,
107 context,
108 content_html,
109 attachments,
110 inReplyTo,
111 tags,
112 cw,
113 cc
114 ),
115 object <-
116 Map.put(
117 object,
118 "emoji",
119 (Formatter.get_emoji(status) ++ Formatter.get_emoji(data["spoiler_text"]))
120 |> Enum.reduce(%{}, fn {name, file}, acc ->
121 Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
122 end)
123 ) do
124 res =
125 ActivityPub.create(%{
126 to: to,
127 actor: user,
128 context: context,
129 object: object,
130 additional: %{"cc" => cc, "directMessage" => visibility == "direct"}
131 })
132
133 res
134 end
135 end
136
137 # Updates the emojis for a user based on their profile
138 def update(user) do
139 user =
140 with emoji <- emoji_from_profile(user),
141 source_data <- (user.info.source_data || %{}) |> Map.put("tag", emoji),
142 info_cng <- Pleroma.User.Info.set_source_data(user.info, source_data),
143 change <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
144 {:ok, user} <- User.update_and_set_cache(change) do
145 user
146 else
147 _e ->
148 user
149 end
150
151 ActivityPub.update(%{
152 local: true,
153 to: [user.follower_address],
154 cc: [],
155 actor: user.ap_id,
156 object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
157 })
158 end
159
160 def pin(id_or_ap_id, %{ap_id: user_ap_id} = user) do
161 with %Activity{
162 actor: ^user_ap_id,
163 data: %{
164 "type" => "Create",
165 "object" => %{
166 "to" => object_to,
167 "type" => "Note"
168 }
169 }
170 } = activity <- get_by_id_or_ap_id(id_or_ap_id),
171 true <- Enum.member?(object_to, "https://www.w3.org/ns/activitystreams#Public"),
172 %{valid?: true} = info_changeset <-
173 Pleroma.User.Info.add_pinnned_activity(user.info, activity),
174 changeset <-
175 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
176 {:ok, _user} <- User.update_and_set_cache(changeset) do
177 {:ok, activity}
178 else
179 %{errors: [pinned_activities: {err, _}]} ->
180 {:error, err}
181
182 _ ->
183 {:error, "Could not pin"}
184 end
185 end
186
187 def unpin(id_or_ap_id, user) do
188 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
189 %{valid?: true} = info_changeset <-
190 Pleroma.User.Info.remove_pinnned_activity(user.info, activity),
191 changeset <-
192 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
193 {:ok, _user} <- User.update_and_set_cache(changeset) do
194 {:ok, activity}
195 else
196 %{errors: [pinned_activities: {err, _}]} ->
197 {:error, err}
198
199 _ ->
200 {:error, "Could not unpin"}
201 end
202 end
203
204 def add_mute(user, activity) do
205 with {:ok, _} <- ThreadMute.add_mute(user.id, activity.data["context"]) do
206 {:ok, activity}
207 else
208 {:error, _} -> {:error, "conversation is already muted"}
209 end
210 end
211
212 def remove_mute(user, activity) do
213 ThreadMute.remove_mute(user.id, activity.data["context"])
214 {:ok, activity}
215 end
216
217 def thread_muted?(%{id: nil} = _user, _activity), do: false
218
219 def thread_muted?(user, activity) do
220 with [] <- ThreadMute.check_muted(user.id, activity.data["context"]) do
221 false
222 else
223 _ -> true
224 end
225 end
226
227 def report(user, data) do
228 with {:account_id, %{"account_id" => account_id}} <- {:account_id, data},
229 {:account, %User{} = account} <- {:account, User.get_by_id(account_id)},
230 {:ok, {content_html, _, _}} <- make_report_content_html(data["comment"]),
231 {:ok, statuses} <- get_report_statuses(account, data),
232 {:ok, activity} <-
233 ActivityPub.flag(%{
234 context: Utils.generate_context_id(),
235 actor: user,
236 account: account,
237 statuses: statuses,
238 content: content_html
239 }) do
240 Enum.each(User.all_superusers(), fn superuser ->
241 superuser
242 |> Pleroma.AdminEmail.report(user, account, statuses, content_html)
243 |> Pleroma.Mailer.deliver_async()
244 end)
245
246 {:ok, activity}
247 else
248 {:error, err} -> {:error, err}
249 {:account_id, %{}} -> {:error, "Valid `account_id` required"}
250 {:account, nil} -> {:error, "Account not found"}
251 end
252 end
253 end