mix format
[akkoma] / lib / pleroma / web / common_api / activity_draft.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.CommonAPI.ActivityDraft do
6 alias Pleroma.Activity
7 alias Pleroma.Conversation.Participation
8 alias Pleroma.Object
9 alias Pleroma.Web.ActivityPub.Builder
10 alias Pleroma.Web.CommonAPI
11 alias Pleroma.Web.CommonAPI.Utils
12
13 import Pleroma.Web.Gettext
14
15 defstruct valid?: true,
16 errors: [],
17 user: nil,
18 params: %{},
19 status: nil,
20 summary: nil,
21 full_payload: nil,
22 attachments: [],
23 in_reply_to: nil,
24 in_reply_to_conversation: nil,
25 visibility: nil,
26 expires_at: nil,
27 extra: nil,
28 emoji: %{},
29 content_html: nil,
30 mentions: [],
31 tags: [],
32 to: [],
33 cc: [],
34 context: nil,
35 sensitive: false,
36 object: nil,
37 preview?: false,
38 changes: %{}
39
40 def new(user, params) do
41 %__MODULE__{user: user}
42 |> put_params(params)
43 end
44
45 def create(user, params) do
46 user
47 |> new(params)
48 |> status()
49 |> summary()
50 |> with_valid(&attachments/1)
51 |> full_payload()
52 |> expires_at()
53 |> poll()
54 |> with_valid(&in_reply_to/1)
55 |> with_valid(&in_reply_to_conversation/1)
56 |> with_valid(&visibility/1)
57 |> content()
58 |> with_valid(&to_and_cc/1)
59 |> with_valid(&context/1)
60 |> sensitive()
61 |> with_valid(&object/1)
62 |> preview?()
63 |> with_valid(&changes/1)
64 |> validate()
65 end
66
67 def listen(user, params) do
68 user
69 |> new(params)
70 |> visibility()
71 |> to_and_cc()
72 |> context()
73 |> listen_object()
74 |> with_valid(&changes/1)
75 |> validate()
76 end
77
78 defp listen_object(draft) do
79 object =
80 draft.params
81 |> Map.take([:album, :artist, :title, :length])
82 |> Map.new(fn {key, value} -> {to_string(key), value} end)
83 |> Map.put("type", "Audio")
84 |> Map.put("to", draft.to)
85 |> Map.put("cc", draft.cc)
86 |> Map.put("actor", draft.user.ap_id)
87
88 %__MODULE__{draft | object: object}
89 end
90
91 defp put_params(draft, params) do
92 params = Map.put_new(params, :in_reply_to_status_id, params[:in_reply_to_id])
93 %__MODULE__{draft | params: params}
94 end
95
96 defp status(%{params: %{status: status}} = draft) do
97 %__MODULE__{draft | status: String.trim(status)}
98 end
99
100 defp summary(%{params: params} = draft) do
101 %__MODULE__{draft | summary: Map.get(params, :spoiler_text, "")}
102 end
103
104 defp full_payload(%{status: status, summary: summary} = draft) do
105 full_payload = String.trim(status <> summary)
106
107 case Utils.validate_character_limit(full_payload, draft.attachments) do
108 :ok -> %__MODULE__{draft | full_payload: full_payload}
109 {:error, message} -> add_error(draft, message)
110 end
111 end
112
113 defp attachments(%{params: params} = draft) do
114 attachments = Utils.attachments_from_ids(params)
115 %__MODULE__{draft | attachments: attachments}
116 end
117
118 defp in_reply_to(%{params: %{in_reply_to_status_id: ""}} = draft), do: draft
119
120 defp in_reply_to(%{params: %{in_reply_to_status_id: id}} = draft) when is_binary(id) do
121 %__MODULE__{draft | in_reply_to: Activity.get_by_id(id)}
122 end
123
124 defp in_reply_to(%{params: %{in_reply_to_status_id: %Activity{} = in_reply_to}} = draft) do
125 %__MODULE__{draft | in_reply_to: in_reply_to}
126 end
127
128 defp in_reply_to(draft), do: draft
129
130 defp in_reply_to_conversation(draft) do
131 in_reply_to_conversation = Participation.get(draft.params[:in_reply_to_conversation_id])
132 %__MODULE__{draft | in_reply_to_conversation: in_reply_to_conversation}
133 end
134
135 defp visibility(%{params: params} = draft) do
136 case CommonAPI.get_visibility(params, draft.in_reply_to, draft.in_reply_to_conversation) do
137 {visibility, "direct"} when visibility != "direct" ->
138 add_error(draft, dgettext("errors", "The message visibility must be direct"))
139
140 {visibility, _} ->
141 %__MODULE__{draft | visibility: visibility}
142 end
143 end
144
145 defp expires_at(draft) do
146 case CommonAPI.check_expiry_date(draft.params[:expires_in]) do
147 {:ok, expires_at} -> %__MODULE__{draft | expires_at: expires_at}
148 {:error, message} -> add_error(draft, message)
149 end
150 end
151
152 defp poll(draft) do
153 case Utils.make_poll_data(draft.params) do
154 {:ok, {poll, poll_emoji}} ->
155 %__MODULE__{draft | extra: poll, emoji: Map.merge(draft.emoji, poll_emoji)}
156
157 {:error, message} ->
158 add_error(draft, message)
159 end
160 end
161
162 defp content(draft) do
163 {content_html, mentioned_users, tags} = Utils.make_content_html(draft)
164
165 mentions =
166 mentioned_users
167 |> Enum.map(fn {_, mentioned_user} -> mentioned_user.ap_id end)
168 |> Utils.get_addressed_users(draft.params[:to])
169
170 %__MODULE__{draft | content_html: content_html, mentions: mentions, tags: tags}
171 end
172
173 defp to_and_cc(draft) do
174 {to, cc} = Utils.get_to_and_cc(draft)
175 %__MODULE__{draft | to: to, cc: cc}
176 end
177
178 defp context(draft) do
179 context = Utils.make_context(draft.in_reply_to, draft.in_reply_to_conversation)
180 %__MODULE__{draft | context: context}
181 end
182
183 defp sensitive(draft) do
184 sensitive = draft.params[:sensitive]
185 %__MODULE__{draft | sensitive: sensitive}
186 end
187
188 defp object(draft) do
189 emoji = Map.merge(Pleroma.Emoji.Formatter.get_emoji_map(draft.full_payload), draft.emoji)
190
191 # Sometimes people create posts with subject containing emoji,
192 # since subjects are usually copied this will result in a broken
193 # subject when someone replies from an instance that does not have
194 # the emoji or has it under different shortcode. This is an attempt
195 # to mitigate this by copying emoji from inReplyTo if they are present
196 # in the subject.
197 summary_emoji =
198 with %Activity{} <- draft.in_reply_to,
199 %Object{data: %{"tag" => [_ | _] = tag}} <- Object.normalize(draft.in_reply_to) do
200 Enum.reduce(tag, %{}, fn
201 %{"type" => "Emoji", "name" => name, "icon" => %{"url" => url}}, acc ->
202 if String.contains?(draft.summary, name) do
203 Map.put(acc, name, url)
204 else
205 acc
206 end
207
208 _, acc ->
209 acc
210 end)
211 else
212 _ -> %{}
213 end
214
215 emoji = Map.merge(emoji, summary_emoji)
216 {:ok, note_data, _meta} = Builder.note(draft)
217
218 object =
219 note_data
220 |> Map.put("emoji", emoji)
221 |> Map.put("source", %{
222 "content" => draft.status,
223 "mediaType" => draft.params[:content_type]
224 })
225 |> Map.put("generator", draft.params[:generator])
226
227 %__MODULE__{draft | object: object}
228 end
229
230 defp preview?(draft) do
231 preview? = Pleroma.Web.Utils.Params.truthy_param?(draft.params[:preview])
232 %__MODULE__{draft | preview?: preview?}
233 end
234
235 defp changes(draft) do
236 direct? = draft.visibility == "direct"
237 additional = %{"cc" => draft.cc, "directMessage" => direct?}
238
239 additional =
240 case draft.expires_at do
241 %DateTime{} = expires_at -> Map.put(additional, "expires_at", expires_at)
242 _ -> additional
243 end
244
245 changes =
246 %{
247 to: draft.to,
248 actor: draft.user,
249 context: draft.context,
250 object: draft.object,
251 additional: additional
252 }
253 |> Utils.maybe_add_list_data(draft.user, draft.visibility)
254
255 %__MODULE__{draft | changes: changes}
256 end
257
258 defp with_valid(%{valid?: true} = draft, func), do: func.(draft)
259 defp with_valid(draft, _func), do: draft
260
261 defp add_error(draft, message) do
262 %__MODULE__{draft | valid?: false, errors: [message | draft.errors]}
263 end
264
265 defp validate(%{valid?: true} = draft), do: {:ok, draft}
266 defp validate(%{errors: [message | _]}), do: {:error, message}
267 end