1 defmodule Pleroma.Web.CommonAPI do
2 alias Pleroma.{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.get_by_ap_id(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.get_by_ap_id(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.get_by_ap_id(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 false <- activity.data["actor"] == user.ap_id,
40 object <- Object.get_by_ap_id(activity.data["object"]["id"]) do
41 ActivityPub.like(user, object)
44 {:error, "Could not favorite"}
48 def unfavorite(id_or_ap_id, user) do
49 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
50 false <- activity.data["actor"] == user.ap_id,
51 object <- Object.get_by_ap_id(activity.data["object"]["id"]) do
52 ActivityPub.unlike(user, object)
55 {:error, "Could not unfavorite"}
59 def get_visibility(%{"visibility" => visibility})
60 when visibility in ~w{public unlisted private direct},
63 def get_visibility(%{"in_reply_to_status_id" => status_id}) when not is_nil(status_id) do
64 inReplyTo = get_replied_to_activity(status_id)
65 Pleroma.Web.MastodonAPI.StatusView.get_visibility(inReplyTo.data["object"])
68 def get_visibility(_), do: "public"
70 @instance Application.get_env(:pleroma, :instance)
71 @limit Keyword.get(@instance, :limit)
72 def post(user, %{"status" => status} = data) do
73 visibility = get_visibility(data)
75 with status <- String.trim(status),
76 length when length in 1..@limit <- String.length(status),
77 attachments <- attachments_from_ids(data["media_ids"]),
78 mentions <- Formatter.parse_mentions(status),
79 inReplyTo <- get_replied_to_activity(data["in_reply_to_status_id"]),
80 {to, cc} <- to_for_user_and_mentions(user, mentions, inReplyTo, visibility),
81 tags <- Formatter.parse_tags(status, data),
83 make_content_html(status, mentions, attachments, tags, data["no_attachment_links"]),
84 context <- make_context(inReplyTo),
85 cw <- data["spoiler_text"],
102 Formatter.get_emoji(status)
103 |> Enum.reduce(%{}, fn {name, file}, acc ->
104 Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
108 ActivityPub.create(%{
113 additional: %{"cc" => cc}
121 ActivityPub.update(%{
123 to: [user.follower_address],
126 object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})