43cec91216115eb20b8eaa5242c562ba60db3569
[akkoma] / lib / pleroma / web / common_api / common_api.ex
1 defmodule Pleroma.Web.CommonAPI do
2 alias Pleroma.{Repo, Activity, Object}
3 alias Pleroma.Web.ActivityPub.ActivityPub
4
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
10 {:ok, delete}
11 end
12 end
13
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)
19 else
20 _ ->
21 {:error, "Could not repeat"}
22 end
23 end
24
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)
30 else
31 _ ->
32 {:error, "Could not favorite"}
33 end
34 end
35
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
40 activity
41 else
42 Activity.get_create_activity_by_object_ap_id(activity.data["object"])
43 end
44 end
45 end