Merge branch 'fix/masto-put-settings' 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.Web.ActivityPub.Utils
5 alias Pleroma.Formatter
6
7 import Pleroma.Web.CommonAPI.Utils
8
9 def delete(activity_id, user) do
10 with %Activity{data: %{"object" => %{"id" => object_id}}} <- Repo.get(Activity, activity_id),
11 %Object{} = object <- Object.normalize(object_id),
12 true <- user.info.is_moderator || user.ap_id == object.data["actor"],
13 {:ok, delete} <- ActivityPub.delete(object) do
14 {:ok, delete}
15 end
16 end
17
18 def repeat(id_or_ap_id, user) do
19 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
20 object <- Object.normalize(activity.data["object"]["id"]),
21 nil <- Utils.get_existing_announce(user.ap_id, object) do
22 ActivityPub.announce(user, object)
23 else
24 _ ->
25 {:error, "Could not repeat"}
26 end
27 end
28
29 def unrepeat(id_or_ap_id, user) do
30 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
31 object <- Object.normalize(activity.data["object"]["id"]) do
32 ActivityPub.unannounce(user, object)
33 else
34 _ ->
35 {:error, "Could not unrepeat"}
36 end
37 end
38
39 def favorite(id_or_ap_id, user) do
40 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
41 object <- Object.normalize(activity.data["object"]["id"]),
42 nil <- Utils.get_existing_like(user.ap_id, object) do
43 ActivityPub.like(user, object)
44 else
45 _ ->
46 {:error, "Could not favorite"}
47 end
48 end
49
50 def unfavorite(id_or_ap_id, user) do
51 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
52 object <- Object.normalize(activity.data["object"]["id"]) do
53 ActivityPub.unlike(user, object)
54 else
55 _ ->
56 {:error, "Could not unfavorite"}
57 end
58 end
59
60 def get_visibility(%{"visibility" => visibility})
61 when visibility in ~w{public unlisted private direct},
62 do: visibility
63
64 def get_visibility(%{"in_reply_to_status_id" => status_id}) when not is_nil(status_id) do
65 case get_replied_to_activity(status_id) do
66 nil ->
67 "public"
68
69 inReplyTo ->
70 Pleroma.Web.MastodonAPI.StatusView.get_visibility(inReplyTo.data["object"])
71 end
72 end
73
74 def get_visibility(_), do: "public"
75
76 defp get_content_type(content_type) do
77 if Enum.member?(Pleroma.Config.get([:instance, :allowed_post_formats]), content_type) do
78 content_type
79 else
80 "text/plain"
81 end
82 end
83
84 def post(user, %{"status" => status} = data) do
85 visibility = get_visibility(data)
86 limit = Pleroma.Config.get([:instance, :limit])
87
88 with status <- String.trim(status),
89 attachments <- attachments_from_ids(data["media_ids"]),
90 mentions <- Formatter.parse_mentions(status),
91 inReplyTo <- get_replied_to_activity(data["in_reply_to_status_id"]),
92 {to, cc} <- to_for_user_and_mentions(user, mentions, inReplyTo, visibility),
93 tags <- Formatter.parse_tags(status, data),
94 content_html <-
95 make_content_html(
96 status,
97 mentions,
98 attachments,
99 tags,
100 get_content_type(data["content_type"]),
101 data["no_attachment_links"]
102 ),
103 context <- make_context(inReplyTo),
104 cw <- data["spoiler_text"],
105 full_payload <- String.trim(status <> (data["spoiler_text"] || "")),
106 length when length in 1..limit <- String.length(full_payload),
107 object <-
108 make_note_data(
109 user.ap_id,
110 to,
111 context,
112 content_html,
113 attachments,
114 inReplyTo,
115 tags,
116 cw,
117 cc
118 ),
119 object <-
120 Map.put(
121 object,
122 "emoji",
123 Formatter.get_emoji(status)
124 |> Enum.reduce(%{}, fn {name, file}, acc ->
125 Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
126 end)
127 ) do
128 res =
129 ActivityPub.create(%{
130 to: to,
131 actor: user,
132 context: context,
133 object: object,
134 additional: %{"cc" => cc}
135 })
136
137 res
138 end
139 end
140
141 # Updates the emojis for a user based on their profile
142 def update(user) do
143 user =
144 with emoji <- emoji_from_profile(user),
145 source_data <- (user.info.source_data || %{}) |> Map.put("tag", emoji),
146 info_cng <- Pleroma.User.Info.set_source_data(user.info, source_data),
147 change <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
148 {:ok, user} <- User.update_and_set_cache(change) do
149 user
150 else
151 _e ->
152 user
153 end
154
155 ActivityPub.update(%{
156 local: true,
157 to: [user.follower_address],
158 cc: [],
159 actor: user.ap_id,
160 object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
161 })
162 end
163 end