Merge branch 'feature/rich-text' into 'develop'
[akkoma] / lib / pleroma / web / common_api / common_api.ex
1 defmodule Pleroma.Web.CommonAPI do
2 alias Pleroma.{User, Repo, Activity, Object}
3 alias Pleroma.Web.ActivityPub.ActivityPub
4 alias Pleroma.Formatter
5
6 import Pleroma.Web.CommonAPI.Utils
7
8 def delete(activity_id, user) do
9 with %Activity{data: %{"object" => %{"id" => 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
13 {:ok, delete}
14 end
15 end
16
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"]["id"]) do
20 ActivityPub.announce(user, object)
21 else
22 _ ->
23 {:error, "Could not repeat"}
24 end
25 end
26
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"]["id"]) do
30 ActivityPub.unannounce(user, object)
31 else
32 _ ->
33 {:error, "Could not unrepeat"}
34 end
35 end
36
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.normalize(activity.data["object"]["id"]) do
41 ActivityPub.like(user, object)
42 else
43 _ ->
44 {:error, "Could not favorite"}
45 end
46 end
47
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.normalize(activity.data["object"]["id"]) do
52 ActivityPub.unlike(user, object)
53 else
54 _ ->
55 {:error, "Could not unfavorite"}
56 end
57 end
58
59 def get_visibility(%{"visibility" => visibility})
60 when visibility in ~w{public unlisted private direct},
61 do: visibility
62
63 def get_visibility(%{"in_reply_to_status_id" => status_id}) when not is_nil(status_id) do
64 case get_replied_to_activity(status_id) do
65 nil ->
66 "public"
67
68 inReplyTo ->
69 Pleroma.Web.MastodonAPI.StatusView.get_visibility(inReplyTo.data["object"])
70 end
71 end
72
73 def get_visibility(_), do: "public"
74
75 @instance Application.get_env(:pleroma, :instance)
76 @limit Keyword.get(@instance, :limit)
77 def post(user, %{"status" => status} = data) do
78 visibility = get_visibility(data)
79
80 with status <- String.trim(status),
81 length when length in 1..@limit <- String.length(status),
82 attachments <- attachments_from_ids(data["media_ids"]),
83 mentions <- Formatter.parse_mentions(status),
84 inReplyTo <- get_replied_to_activity(data["in_reply_to_status_id"]),
85 {to, cc} <- to_for_user_and_mentions(user, mentions, inReplyTo, visibility),
86 tags <- Formatter.parse_tags(status, data),
87 content_html <-
88 make_content_html(
89 status,
90 mentions,
91 attachments,
92 tags,
93 data["content_type"] || "text/plain",
94 data["no_attachment_links"]
95 ),
96 context <- make_context(inReplyTo),
97 cw <- data["spoiler_text"],
98 object <-
99 make_note_data(
100 user.ap_id,
101 to,
102 context,
103 content_html,
104 attachments,
105 inReplyTo,
106 tags,
107 cw,
108 cc
109 ),
110 object <-
111 Map.put(
112 object,
113 "emoji",
114 Formatter.get_emoji(status)
115 |> Enum.reduce(%{}, fn {name, file}, acc ->
116 Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
117 end)
118 ) do
119 res =
120 ActivityPub.create(%{
121 to: to,
122 actor: user,
123 context: context,
124 object: object,
125 additional: %{"cc" => cc}
126 })
127
128 res
129 end
130 end
131
132 def update(user) do
133 user =
134 with emoji <- emoji_from_profile(user),
135 source_data <- (user.info["source_data"] || %{}) |> Map.put("tag", emoji),
136 new_info <- Map.put(user.info, "source_data", source_data),
137 change <- User.info_changeset(user, %{info: new_info}),
138 {:ok, user} <- User.update_and_set_cache(change) do
139 user
140 else
141 _e ->
142 user
143 end
144
145 ActivityPub.update(%{
146 local: true,
147 to: [user.follower_address],
148 cc: [],
149 actor: user.ap_id,
150 object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
151 })
152 end
153 end