1 defmodule Pleroma.Web.CommonAPI do
2 alias Pleroma.{User, Repo, Activity, Object}
3 alias Pleroma.Web.ActivityPub.ActivityPub
4 alias Pleroma.Formatter
6 import Pleroma.Web.CommonAPI.Utils
8 def delete(activity_id, user) do
9 with %Activity{data: %{"object" => %{"id" => object_id}}} <- Repo.get(Activity, activity_id),
10 %Object{} = object <- Object.normalize(object_id),
11 true <- user.info.is_moderator || user.ap_id == object.data["actor"],
12 {:ok, delete} <- ActivityPub.delete(object) do
17 def repeat(id_or_ap_id, user) do
18 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
19 object <- Object.normalize(activity.data["object"]["id"]) do
20 ActivityPub.announce(user, object)
23 {:error, "Could not repeat"}
27 def unrepeat(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"]) do
30 ActivityPub.unannounce(user, object)
33 {:error, "Could not unrepeat"}
37 def favorite(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.like(user, object)
43 {:error, "Could not favorite"}
47 def unfavorite(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"]) do
50 ActivityPub.unlike(user, object)
53 {:error, "Could not unfavorite"}
57 def get_visibility(%{"visibility" => visibility})
58 when visibility in ~w{public unlisted private direct},
61 def get_visibility(%{"in_reply_to_status_id" => status_id}) when not is_nil(status_id) do
62 case get_replied_to_activity(status_id) do
67 Pleroma.Web.MastodonAPI.StatusView.get_visibility(inReplyTo.data["object"])
71 def get_visibility(_), do: "public"
73 defp get_content_type(content_type) do
74 if Enum.member?(Pleroma.Config.get([:instance, :allowed_post_formats]), content_type) do
81 def post(user, %{"status" => status} = data) do
82 visibility = get_visibility(data)
83 limit = Pleroma.Config.get([:instance, :limit])
85 with status <- String.trim(status),
86 attachments <- attachments_from_ids(data["media_ids"]),
87 mentions <- Formatter.parse_mentions(status),
88 inReplyTo <- get_replied_to_activity(data["in_reply_to_status_id"]),
89 {to, cc} <- to_for_user_and_mentions(user, mentions, inReplyTo, visibility),
90 tags <- Formatter.parse_tags(status, data),
97 get_content_type(data["content_type"]),
98 data["no_attachment_links"]
100 context <- make_context(inReplyTo),
101 cw <- data["spoiler_text"],
102 full_payload <- String.trim(status <> (data["spoiler_text"] || "")),
103 length when length in 1..limit <- String.length(full_payload),
120 Formatter.get_emoji(status)
121 |> Enum.reduce(%{}, fn {name, file}, acc ->
122 Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
126 ActivityPub.create(%{
131 additional: %{"cc" => cc}
140 with emoji <- emoji_from_profile(user),
141 source_data <- (user.info["source_data"] || %{}) |> Map.put("tag", emoji),
142 new_info <- Map.put(user.info, "source_data", source_data),
143 change <- User.info_changeset(user, %{info: new_info}),
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})