Default to disabled in the code in case the setting is absent from config.exs
[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?([true, "true"], Map.get(data, "no_attachment_links", Pleroma.Config.get([:instance, :no_attachment_links], false)))
107 ),
108 context <- make_context(inReplyTo),
109 cw <- data["spoiler_text"],
110 full_payload <- String.trim(status <> (data["spoiler_text"] || "")),
111 length when length in 1..limit <- String.length(full_payload),
112 object <-
113 make_note_data(
114 user.ap_id,
115 to,
116 context,
117 content_html,
118 attachments,
119 inReplyTo,
120 tags,
121 cw,
122 cc
123 ),
124 object <-
125 Map.put(
126 object,
127 "emoji",
128 (Formatter.get_emoji(status) ++ Formatter.get_emoji(data["spoiler_text"]))
129 |> Enum.reduce(%{}, fn {name, file}, acc ->
130 Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
131 end)
132 ) do
133 res =
134 ActivityPub.create(%{
135 to: to,
136 actor: user,
137 context: context,
138 object: object,
139 additional: %{"cc" => cc}
140 })
141
142 res
143 end
144 end
145
146 # Updates the emojis for a user based on their profile
147 def update(user) do
148 user =
149 with emoji <- emoji_from_profile(user),
150 source_data <- (user.info.source_data || %{}) |> Map.put("tag", emoji),
151 info_cng <- Pleroma.User.Info.set_source_data(user.info, source_data),
152 change <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
153 {:ok, user} <- User.update_and_set_cache(change) do
154 user
155 else
156 _e ->
157 user
158 end
159
160 ActivityPub.update(%{
161 local: true,
162 to: [user.follower_address],
163 cc: [],
164 actor: user.ap_id,
165 object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
166 })
167 end
168
169 def pin(id_or_ap_id, %{ap_id: user_ap_id} = user) do
170 with %Activity{
171 actor: ^user_ap_id,
172 data: %{
173 "type" => "Create",
174 "object" => %{
175 "to" => object_to,
176 "type" => "Note"
177 }
178 }
179 } = activity <- get_by_id_or_ap_id(id_or_ap_id),
180 true <- Enum.member?(object_to, "https://www.w3.org/ns/activitystreams#Public"),
181 %{valid?: true} = info_changeset <-
182 Pleroma.User.Info.add_pinnned_activity(user.info, activity),
183 changeset <-
184 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
185 {:ok, _user} <- User.update_and_set_cache(changeset) do
186 {:ok, activity}
187 else
188 %{errors: [pinned_activities: {err, _}]} ->
189 {:error, err}
190
191 _ ->
192 {:error, "Could not pin"}
193 end
194 end
195
196 def unpin(id_or_ap_id, user) do
197 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
198 %{valid?: true} = info_changeset <-
199 Pleroma.User.Info.remove_pinnned_activity(user.info, activity),
200 changeset <-
201 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
202 {:ok, _user} <- User.update_and_set_cache(changeset) do
203 {:ok, activity}
204 else
205 %{errors: [pinned_activities: {err, _}]} ->
206 {:error, err}
207
208 _ ->
209 {:error, "Could not unpin"}
210 end
211 end
212 end