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