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