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" => 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"]) 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"]) 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"]) 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"]) 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 # XXX: these heuristics should be moved out of MastodonAPI.
68 with %Object{} = object <- Object.normalize(inReplyTo.data["object"]) do
69 Pleroma.Web.MastodonAPI.StatusView.get_visibility(object.data)
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})