Merge remote-tracking branch 'upstream/develop' into patch-image-description
[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 defp get_content_type(content_type) do
86 if Enum.member?(Pleroma.Config.get([:instance, :allowed_post_formats]), content_type) do
87 content_type
88 else
89 "text/plain"
90 end
91 end
92
93 def post(user, %{"status" => status} = data) do
94 visibility = get_visibility(data)
95 limit = Pleroma.Config.get([:instance, :limit])
96
97 with status <- String.trim(status),
98 attachments <- attachments_from_ids(data),
99 mentions <- Formatter.parse_mentions(status),
100 inReplyTo <- get_replied_to_activity(data["in_reply_to_status_id"]),
101 {to, cc} <- to_for_user_and_mentions(user, mentions, inReplyTo, visibility),
102 tags <- Formatter.parse_tags(status, data),
103 content_html <-
104 make_content_html(
105 status,
106 mentions,
107 attachments,
108 tags,
109 get_content_type(data["content_type"]),
110 Enum.member?(
111 [true, "true"],
112 Map.get(
113 data,
114 "no_attachment_links",
115 Pleroma.Config.get([:instance, :no_attachment_links], false)
116 )
117 )
118 ),
119 context <- make_context(inReplyTo),
120 cw <- data["spoiler_text"],
121 full_payload <- String.trim(status <> (data["spoiler_text"] || "")),
122 length when length in 1..limit <- String.length(full_payload),
123 object <-
124 make_note_data(
125 user.ap_id,
126 to,
127 context,
128 content_html,
129 attachments,
130 inReplyTo,
131 tags,
132 cw,
133 cc
134 ),
135 object <-
136 Map.put(
137 object,
138 "emoji",
139 (Formatter.get_emoji(status) ++ Formatter.get_emoji(data["spoiler_text"]))
140 |> Enum.reduce(%{}, fn {name, file}, acc ->
141 Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
142 end)
143 ) do
144 res =
145 ActivityPub.create(%{
146 to: to,
147 actor: user,
148 context: context,
149 object: object,
150 additional: %{"cc" => cc, "directMessage" => visibility == "direct"}
151 })
152
153 res
154 end
155 end
156
157 # Updates the emojis for a user based on their profile
158 def update(user) do
159 user =
160 with emoji <- emoji_from_profile(user),
161 source_data <- (user.info.source_data || %{}) |> Map.put("tag", emoji),
162 info_cng <- Pleroma.User.Info.set_source_data(user.info, source_data),
163 change <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
164 {:ok, user} <- User.update_and_set_cache(change) do
165 user
166 else
167 _e ->
168 user
169 end
170
171 ActivityPub.update(%{
172 local: true,
173 to: [user.follower_address],
174 cc: [],
175 actor: user.ap_id,
176 object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
177 })
178 end
179
180 def pin(id_or_ap_id, %{ap_id: user_ap_id} = user) do
181 with %Activity{
182 actor: ^user_ap_id,
183 data: %{
184 "type" => "Create",
185 "object" => %{
186 "to" => object_to,
187 "type" => "Note"
188 }
189 }
190 } = activity <- get_by_id_or_ap_id(id_or_ap_id),
191 true <- Enum.member?(object_to, "https://www.w3.org/ns/activitystreams#Public"),
192 %{valid?: true} = info_changeset <-
193 Pleroma.User.Info.add_pinnned_activity(user.info, activity),
194 changeset <-
195 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
196 {:ok, _user} <- User.update_and_set_cache(changeset) do
197 {:ok, activity}
198 else
199 %{errors: [pinned_activities: {err, _}]} ->
200 {:error, err}
201
202 _ ->
203 {:error, "Could not pin"}
204 end
205 end
206
207 def unpin(id_or_ap_id, user) do
208 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
209 %{valid?: true} = info_changeset <-
210 Pleroma.User.Info.remove_pinnned_activity(user.info, activity),
211 changeset <-
212 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
213 {:ok, _user} <- User.update_and_set_cache(changeset) do
214 {:ok, activity}
215 else
216 %{errors: [pinned_activities: {err, _}]} ->
217 {:error, err}
218
219 _ ->
220 {:error, "Could not unpin"}
221 end
222 end
223
224 def add_mute(user, activity) do
225 with {:ok, _} <- ThreadMute.add_mute(user.id, activity.data["context"]) do
226 {:ok, activity}
227 else
228 {:error, _} -> {:error, "conversation is already muted"}
229 end
230 end
231
232 def remove_mute(user, activity) do
233 ThreadMute.remove_mute(user.id, activity.data["context"])
234 {:ok, activity}
235 end
236
237 def thread_muted?(%{id: nil} = _user, _activity), do: false
238
239 def thread_muted?(user, activity) do
240 with [] <- ThreadMute.check_muted(user.id, activity.data["context"]) do
241 false
242 else
243 _ -> true
244 end
245 end
246 end