1 defmodule Pleroma.Web.CommonAPI do
2 alias Pleroma.{Repo, Activity, Object, User}
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.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 favorite(id_or_ap_id, user) do
28 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
29 false <- activity.data["actor"] == user.ap_id,
30 object <- Object.get_by_ap_id(activity.data["object"]["id"]) do
31 ActivityPub.like(user, object)
34 {:error, "Could not favorite"}
38 def unfavorite(id_or_ap_id, user) do
39 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
40 false <- activity.data["actor"] == user.ap_id,
41 object <- Object.get_by_ap_id(activity.data["object"]["id"]) do
42 ActivityPub.unlike(user, object)
45 {:error, "Could not unfavorite"}
49 @instance Application.get_env(:pleroma, :instance)
50 @limit Keyword.get(@instance, :limit)
51 def post(user, %{"status" => status} = data) do
52 with status <- String.trim(status),
53 length when length in 1..@limit <- String.length(status),
54 attachments <- attachments_from_ids(data["media_ids"]),
55 mentions <- Formatter.parse_mentions(status),
56 inReplyTo <- get_replied_to_activity(data["in_reply_to_status_id"]),
57 to <- to_for_user_and_mentions(user, mentions, inReplyTo),
58 tags <- Formatter.parse_tags(status, data),
59 content_html <- make_content_html(status, mentions, attachments, tags, data["no_attachment_links"]),
60 context <- make_context(inReplyTo),
61 cw <- data["spoiler_text"],
62 object <- make_note_data(user.ap_id, to, context, content_html, attachments, inReplyTo, tags, cw),
63 object <- Map.put(object, "emoji", Formatter.get_emoji(status) |> Enum.reduce(%{}, fn({name, file}, acc) -> Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url}#{file}") end)) do
64 res = ActivityPub.create(%{to: to, actor: user, context: context, object: object, additional: %{"cc" => to}})
65 User.increase_note_count(user)