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