1 defmodule Pleroma.Web.CommonAPI do
2 alias Pleroma.{Repo, Activity, Object}
3 alias Pleroma.Web.ActivityPub.ActivityPub
5 def delete(activity_id, user) do
6 with %Activity{data: %{"object" => %{"id" => object_id}}} <- Repo.get(Activity, activity_id),
7 %Object{} = object <- Object.get_by_ap_id(object_id),
8 true <- user.ap_id == object.data["actor"],
9 {:ok, delete} <- ActivityPub.delete(object) do
14 def repeat(id_or_ap_id, user) do
15 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
16 false <- activity.data["actor"] == user.ap_id,
17 object <- Object.get_by_ap_id(activity.data["object"]["id"]) do
18 ActivityPub.announce(user, object)
21 {:error, "Could not repeat"}
25 def favorite(id_or_ap_id, user) do
26 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
27 false <- activity.data["actor"] == user.ap_id,
28 object <- Object.get_by_ap_id(activity.data["object"]["id"]) do
29 ActivityPub.like(user, object)
32 {:error, "Could not favorite"}
36 # This is a hack for twidere.
37 def get_by_id_or_ap_id(id) do
38 activity = Repo.get(Activity, id) || Activity.get_create_activity_by_object_ap_id(id)
39 if activity.data["type"] == "Create" do
42 Activity.get_create_activity_by_object_ap_id(activity.data["object"])