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