ea88213fbdae41c28149857b92c2846bb4d088ef
[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 defp put_params(draft, params) do
68 params = Map.put_new(params, :in_reply_to_status_id, params[:in_reply_to_id])
69 %__MODULE__{draft | params: params}
70 end
71
72 defp status(%{params: %{status: status}} = draft) do
73 %__MODULE__{draft | status: String.trim(status)}
74 end
75
76 defp summary(%{params: params} = draft) do
77 %__MODULE__{draft | summary: Map.get(params, :spoiler_text, "")}
78 end
79
80 defp full_payload(%{status: status, summary: summary} = draft) do
81 full_payload = String.trim(status <> summary)
82
83 case Utils.validate_character_limit(full_payload, draft.attachments) do
84 :ok -> %__MODULE__{draft | full_payload: full_payload}
85 {:error, message} -> add_error(draft, message)
86 end
87 end
88
89 defp attachments(%{params: params} = draft) do
90 attachments = Utils.attachments_from_ids(params)
91 %__MODULE__{draft | attachments: attachments}
92 end
93
94 defp in_reply_to(%{params: %{in_reply_to_status_id: ""}} = draft), do: draft
95
96 defp in_reply_to(%{params: %{in_reply_to_status_id: id}} = draft) when is_binary(id) do
97 %__MODULE__{draft | in_reply_to: Activity.get_by_id(id)}
98 end
99
100 defp in_reply_to(%{params: %{in_reply_to_status_id: %Activity{} = in_reply_to}} = draft) do
101 %__MODULE__{draft | in_reply_to: in_reply_to}
102 end
103
104 defp in_reply_to(draft), do: draft
105
106 defp in_reply_to_conversation(draft) do
107 in_reply_to_conversation = Participation.get(draft.params[:in_reply_to_conversation_id])
108 %__MODULE__{draft | in_reply_to_conversation: in_reply_to_conversation}
109 end
110
111 defp visibility(%{params: params} = draft) do
112 case CommonAPI.get_visibility(params, draft.in_reply_to, draft.in_reply_to_conversation) do
113 {visibility, "direct"} when visibility != "direct" ->
114 add_error(draft, dgettext("errors", "The message visibility must be direct"))
115
116 {visibility, _} ->
117 %__MODULE__{draft | visibility: visibility}
118 end
119 end
120
121 defp expires_at(draft) do
122 case CommonAPI.check_expiry_date(draft.params[:expires_in]) do
123 {:ok, expires_at} -> %__MODULE__{draft | expires_at: expires_at}
124 {:error, message} -> add_error(draft, message)
125 end
126 end
127
128 defp poll(draft) do
129 case Utils.make_poll_data(draft.params) do
130 {:ok, {poll, poll_emoji}} ->
131 %__MODULE__{draft | extra: poll, emoji: Map.merge(draft.emoji, poll_emoji)}
132
133 {:error, message} ->
134 add_error(draft, message)
135 end
136 end
137
138 defp content(draft) do
139 {content_html, mentioned_users, tags} = Utils.make_content_html(draft)
140
141 mentions =
142 mentioned_users
143 |> Enum.map(fn {_, mentioned_user} -> mentioned_user.ap_id end)
144 |> Utils.get_addressed_users(draft.params[:to])
145
146 %__MODULE__{draft | content_html: content_html, mentions: mentions, tags: tags}
147 end
148
149 defp to_and_cc(draft) do
150 {to, cc} = Utils.get_to_and_cc(draft)
151 %__MODULE__{draft | to: to, cc: cc}
152 end
153
154 defp context(draft) do
155 context = Utils.make_context(draft.in_reply_to, draft.in_reply_to_conversation)
156 %__MODULE__{draft | context: context}
157 end
158
159 defp sensitive(draft) do
160 sensitive = draft.params[:sensitive]
161 %__MODULE__{draft | sensitive: sensitive}
162 end
163
164 defp object(draft) do
165 emoji = Map.merge(Pleroma.Emoji.Formatter.get_emoji_map(draft.full_payload), draft.emoji)
166
167 # Sometimes people create posts with subject containing emoji,
168 # since subjects are usually copied this will result in a broken
169 # subject when someone replies from an instance that does not have
170 # the emoji or has it under different shortcode. This is an attempt
171 # to mitigate this by copying emoji from inReplyTo if they are present
172 # in the subject.
173 summary_emoji =
174 with %Activity{} <- draft.in_reply_to,
175 %Object{data: %{"tag" => [_ | _] = tag}} <- Object.normalize(draft.in_reply_to) do
176 Enum.reduce(tag, %{}, fn
177 %{"type" => "Emoji", "name" => name, "icon" => %{"url" => url}}, acc ->
178 if String.contains?(draft.summary, name) do
179 Map.put(acc, name, url)
180 else
181 acc
182 end
183
184 _, acc ->
185 acc
186 end)
187 else
188 _ -> %{}
189 end
190
191 emoji = Map.merge(emoji, summary_emoji)
192 {:ok, note_data, _meta} = Builder.note(draft)
193
194 object =
195 note_data
196 |> Map.put("emoji", emoji)
197 |> Map.put("source", %{
198 "content" => draft.status,
199 "mediaType" => draft.params[:content_type]
200 })
201 |> Map.put("generator", draft.params[:generator])
202
203 %__MODULE__{draft | object: object}
204 end
205
206 defp preview?(draft) do
207 preview? = Pleroma.Web.Utils.Params.truthy_param?(draft.params[:preview])
208 %__MODULE__{draft | preview?: preview?}
209 end
210
211 defp changes(draft) do
212 direct? = draft.visibility == "direct"
213 additional = %{"cc" => draft.cc, "directMessage" => direct?}
214
215 additional =
216 case draft.expires_at do
217 %DateTime{} = expires_at -> Map.put(additional, "expires_at", expires_at)
218 _ -> additional
219 end
220
221 changes =
222 %{
223 to: draft.to,
224 actor: draft.user,
225 context: draft.context,
226 object: draft.object,
227 additional: additional
228 }
229 |> Utils.maybe_add_list_data(draft.user, draft.visibility)
230
231 %__MODULE__{draft | changes: changes}
232 end
233
234 defp with_valid(%{valid?: true} = draft, func), do: func.(draft)
235 defp with_valid(draft, _func), do: draft
236
237 defp add_error(draft, message) do
238 %__MODULE__{draft | valid?: false, errors: [message | draft.errors]}
239 end
240
241 defp validate(%{valid?: true} = draft), do: {:ok, draft}
242 defp validate(%{errors: [message | _]}), do: {:error, message}
243 end