Merge branch 'bugfix/prismo.news_article_url' 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 object <- Object.normalize(activity.data["object"]["id"]) do
40 ActivityPub.like(user, object)
41 else
42 _ ->
43 {:error, "Could not favorite"}
44 end
45 end
46
47 def unfavorite(id_or_ap_id, user) do
48 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
49 object <- Object.normalize(activity.data["object"]["id"]) do
50 ActivityPub.unlike(user, object)
51 else
52 _ ->
53 {:error, "Could not unfavorite"}
54 end
55 end
56
57 def get_visibility(%{"visibility" => visibility})
58 when visibility in ~w{public unlisted private direct},
59 do: visibility
60
61 def get_visibility(%{"in_reply_to_status_id" => status_id}) when not is_nil(status_id) do
62 case get_replied_to_activity(status_id) do
63 nil ->
64 "public"
65
66 inReplyTo ->
67 Pleroma.Web.MastodonAPI.StatusView.get_visibility(inReplyTo.data["object"])
68 end
69 end
70
71 def get_visibility(_), do: "public"
72
73 @instance Application.get_env(:pleroma, :instance)
74 @allowed_post_formats Keyword.get(@instance, :allowed_post_formats)
75
76 defp get_content_type(content_type) when content_type in @allowed_post_formats, do: content_type
77 defp get_content_type(_), do: "text/plain"
78
79 @limit Keyword.get(@instance, :limit)
80 def post(user, %{"status" => status} = data) do
81 visibility = get_visibility(data)
82
83 with status <- String.trim(status),
84 attachments <- attachments_from_ids(data["media_ids"]),
85 mentions <- Formatter.parse_mentions(status),
86 inReplyTo <- get_replied_to_activity(data["in_reply_to_status_id"]),
87 {to, cc} <- to_for_user_and_mentions(user, mentions, inReplyTo, visibility),
88 tags <- Formatter.parse_tags(status, data),
89 content_html <-
90 make_content_html(
91 status,
92 mentions,
93 attachments,
94 tags,
95 get_content_type(data["content_type"]),
96 data["no_attachment_links"]
97 ),
98 context <- make_context(inReplyTo),
99 cw <- data["spoiler_text"],
100 full_payload <- String.trim(status <> (data["spoiler_text"] || "")),
101 length when length in 1..@limit <- String.length(full_payload),
102 object <-
103 make_note_data(
104 user.ap_id,
105 to,
106 context,
107 content_html,
108 attachments,
109 inReplyTo,
110 tags,
111 cw,
112 cc
113 ),
114 object <-
115 Map.put(
116 object,
117 "emoji",
118 Formatter.get_emoji(status)
119 |> Enum.reduce(%{}, fn {name, file}, acc ->
120 Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
121 end)
122 ) do
123 res =
124 ActivityPub.create(%{
125 to: to,
126 actor: user,
127 context: context,
128 object: object,
129 additional: %{"cc" => cc}
130 })
131
132 res
133 end
134 end
135
136 def update(user) do
137 user =
138 with emoji <- emoji_from_profile(user),
139 source_data <- (user.info["source_data"] || %{}) |> Map.put("tag", emoji),
140 new_info <- Map.put(user.info, "source_data", source_data),
141 change <- User.info_changeset(user, %{info: new_info}),
142 {:ok, user} <- User.update_and_set_cache(change) do
143 user
144 else
145 _e ->
146 user
147 end
148
149 ActivityPub.update(%{
150 local: true,
151 to: [user.follower_address],
152 cc: [],
153 actor: user.ap_id,
154 object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
155 })
156 end
157 end