1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.CommonAPI do
10 alias Pleroma.ThreadMute
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.ActivityPub.Utils
13 alias Pleroma.Formatter
15 import Pleroma.Web.CommonAPI.Utils
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
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)
34 {:error, "Could not repeat"}
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)
44 {:error, "Could not unrepeat"}
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)
55 {:error, "Could not favorite"}
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)
65 {:error, "Could not unfavorite"}
69 def get_visibility(%{"visibility" => visibility})
70 when visibility in ~w{public unlisted private direct},
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
79 Pleroma.Web.MastodonAPI.StatusView.get_visibility(inReplyTo.data["object"])
83 def get_visibility(_), do: "public"
85 def post(user, %{"status" => status} = data) do
86 visibility = get_visibility(data)
87 limit = Pleroma.Config.get([:instance, :limit])
89 with status <- String.trim(status),
90 attachments <- attachments_from_ids(data),
91 inReplyTo <- get_replied_to_activity(data["in_reply_to_status_id"]),
92 {content_html, mentions, tags} <-
98 {to, cc} <- to_for_user_and_mentions(user, mentions, inReplyTo, visibility),
99 context <- make_context(inReplyTo),
100 cw <- data["spoiler_text"],
101 full_payload <- String.trim(status <> (data["spoiler_text"] || "")),
102 length when length in 1..limit <- String.length(full_payload),
119 (Formatter.get_emoji(status) ++ Formatter.get_emoji(data["spoiler_text"]))
120 |> Enum.reduce(%{}, fn {name, file}, acc ->
121 Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
125 ActivityPub.create(%{
130 additional: %{"cc" => cc, "directMessage" => visibility == "direct"}
137 # Updates the emojis for a user based on their profile
140 with emoji <- emoji_from_profile(user),
141 source_data <- (user.info.source_data || %{}) |> Map.put("tag", emoji),
142 info_cng <- Pleroma.User.Info.set_source_data(user.info, source_data),
143 change <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
144 {:ok, user} <- User.update_and_set_cache(change) do
151 ActivityPub.update(%{
153 to: [user.follower_address],
156 object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
160 def pin(id_or_ap_id, %{ap_id: user_ap_id} = user) do
170 } = activity <- get_by_id_or_ap_id(id_or_ap_id),
171 true <- Enum.member?(object_to, "https://www.w3.org/ns/activitystreams#Public"),
172 %{valid?: true} = info_changeset <-
173 Pleroma.User.Info.add_pinnned_activity(user.info, activity),
175 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
176 {:ok, _user} <- User.update_and_set_cache(changeset) do
179 %{errors: [pinned_activities: {err, _}]} ->
183 {:error, "Could not pin"}
187 def unpin(id_or_ap_id, user) do
188 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
189 %{valid?: true} = info_changeset <-
190 Pleroma.User.Info.remove_pinnned_activity(user.info, activity),
192 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
193 {:ok, _user} <- User.update_and_set_cache(changeset) do
196 %{errors: [pinned_activities: {err, _}]} ->
200 {:error, "Could not unpin"}
204 def add_mute(user, activity) do
205 with {:ok, _} <- ThreadMute.add_mute(user.id, activity.data["context"]) do
208 {:error, _} -> {:error, "conversation is already muted"}
212 def remove_mute(user, activity) do
213 ThreadMute.remove_mute(user.id, activity.data["context"])
217 def thread_muted?(%{id: nil} = _user, _activity), do: false
219 def thread_muted?(user, activity) do
220 with [] <- ThreadMute.check_muted(user.id, activity.data["context"]) do
227 def report(user, data) do
228 with {:account_id, %{"account_id" => account_id}} <- {:account_id, data},
229 {:account, %User{} = account} <- {:account, User.get_by_id(account_id)},
230 {:ok, {content_html, _, _}} <- make_report_content_html(data["comment"]),
231 {:ok, statuses} <- get_report_statuses(account, data),
234 context: Utils.generate_context_id(),
238 content: content_html
240 Enum.each(User.all_superusers(), fn superuser ->
242 |> Pleroma.AdminEmail.report(user, account, statuses, content_html)
243 |> Pleroma.Mailer.deliver_async()
248 {:error, err} -> {:error, err}
249 {:account_id, %{}} -> {:error, "Valid `account_id` required"}
250 {:account, nil} -> {:error, "Account not found"}