1 defmodule Pleroma.Web.CommonAPI do
2 alias Pleroma.{User, Repo, Activity, Object}
3 alias Pleroma.Web.ActivityPub.ActivityPub
4 alias Pleroma.Web.ActivityPub.Utils
5 alias Pleroma.Formatter
7 import Pleroma.Web.CommonAPI.Utils
9 def delete(activity_id, user) do
10 with %Activity{data: %{"object" => %{"id" => object_id}}} <- Repo.get(Activity, activity_id),
11 %Object{} = object <- Object.normalize(object_id),
12 true <- user.info.is_moderator || user.ap_id == object.data["actor"],
13 {:ok, delete} <- ActivityPub.delete(object) do
18 def repeat(id_or_ap_id, user) do
19 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
20 object <- Object.normalize(activity.data["object"]["id"]),
21 nil <- Utils.get_existing_announce(user.ap_id, object) do
22 ActivityPub.announce(user, object)
25 {:error, "Could not repeat"}
29 def unrepeat(id_or_ap_id, user) do
30 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
31 object <- Object.normalize(activity.data["object"]["id"]) do
32 ActivityPub.unannounce(user, object)
35 {:error, "Could not unrepeat"}
39 def favorite(id_or_ap_id, user) do
40 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
41 object <- Object.normalize(activity.data["object"]["id"]),
42 nil <- Utils.get_existing_like(user.ap_id, object) do
43 ActivityPub.like(user, object)
46 {:error, "Could not favorite"}
50 def unfavorite(id_or_ap_id, user) do
51 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
52 object <- Object.normalize(activity.data["object"]["id"]) do
53 ActivityPub.unlike(user, object)
56 {:error, "Could not unfavorite"}
60 def get_visibility(%{"visibility" => visibility})
61 when visibility in ~w{public unlisted private direct},
64 def get_visibility(%{"in_reply_to_status_id" => status_id}) when not is_nil(status_id) do
65 case get_replied_to_activity(status_id) do
70 Pleroma.Web.MastodonAPI.StatusView.get_visibility(inReplyTo.data["object"])
74 def get_visibility(_), do: "public"
76 defp get_content_type(content_type) do
77 if Enum.member?(Pleroma.Config.get([:instance, :allowed_post_formats]), content_type) do
84 def post(user, %{"status" => status} = data) do
85 visibility = get_visibility(data)
86 limit = Pleroma.Config.get([:instance, :limit])
88 with status <- String.trim(status),
89 attachments <- attachments_from_ids(data["media_ids"]),
90 mentions <- Formatter.parse_mentions(status),
91 inReplyTo <- get_replied_to_activity(data["in_reply_to_status_id"]),
92 {to, cc} <- to_for_user_and_mentions(user, mentions, inReplyTo, visibility),
93 tags <- Formatter.parse_tags(status, data),
100 get_content_type(data["content_type"]),
101 data["no_attachment_links"]
103 context <- make_context(inReplyTo),
104 cw <- data["spoiler_text"],
105 full_payload <- String.trim(status <> (data["spoiler_text"] || "")),
106 length when length in 1..limit <- String.length(full_payload),
123 Formatter.get_emoji(status)
124 |> Enum.reduce(%{}, fn {name, file}, acc ->
125 Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
129 ActivityPub.create(%{
134 additional: %{"cc" => cc}
141 # Updates the emojis for a user based on their profile
144 with emoji <- emoji_from_profile(user),
145 source_data <- (user.info.source_data || %{}) |> Map.put("tag", emoji),
146 info_cng <- Pleroma.User.Info.set_source_data(user.info, source_data),
147 change <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
148 {:ok, user} <- User.update_and_set_cache(change) do
155 ActivityPub.update(%{
157 to: [user.follower_address],
160 object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})