[#1260] Merge remote-tracking branch 'remotes/upstream/develop' into 1260-rate-limite...
[akkoma] / lib / pleroma / web / common_api / activity_draft.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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.Web.CommonAPI
9 alias Pleroma.Web.CommonAPI.Utils
10
11 import Pleroma.Web.Gettext
12
13 defstruct valid?: true,
14 errors: [],
15 user: nil,
16 params: %{},
17 status: nil,
18 summary: nil,
19 full_payload: nil,
20 attachments: [],
21 in_reply_to: nil,
22 in_reply_to_conversation: nil,
23 visibility: nil,
24 expires_at: nil,
25 poll: nil,
26 emoji: %{},
27 content_html: nil,
28 mentions: [],
29 tags: [],
30 to: [],
31 cc: [],
32 context: nil,
33 sensitive: false,
34 object: nil,
35 preview?: false,
36 changes: %{}
37
38 def create(user, params) do
39 %__MODULE__{user: user}
40 |> put_params(params)
41 |> status()
42 |> summary()
43 |> with_valid(&attachments/1)
44 |> full_payload()
45 |> expires_at()
46 |> poll()
47 |> with_valid(&in_reply_to/1)
48 |> with_valid(&in_reply_to_conversation/1)
49 |> with_valid(&visibility/1)
50 |> content()
51 |> with_valid(&to_and_cc/1)
52 |> with_valid(&context/1)
53 |> sensitive()
54 |> with_valid(&object/1)
55 |> preview?()
56 |> with_valid(&changes/1)
57 |> validate()
58 end
59
60 defp put_params(draft, params) do
61 params = Map.put_new(params, "in_reply_to_status_id", params["in_reply_to_id"])
62 %__MODULE__{draft | params: params}
63 end
64
65 defp status(%{params: %{"status" => status}} = draft) do
66 %__MODULE__{draft | status: String.trim(status)}
67 end
68
69 defp summary(%{params: params} = draft) do
70 %__MODULE__{draft | summary: Map.get(params, "spoiler_text", "")}
71 end
72
73 defp full_payload(%{status: status, summary: summary} = draft) do
74 full_payload = String.trim(status <> summary)
75
76 case Utils.validate_character_limit(full_payload, draft.attachments) do
77 :ok -> %__MODULE__{draft | full_payload: full_payload}
78 {:error, message} -> add_error(draft, message)
79 end
80 end
81
82 defp attachments(%{params: params} = draft) do
83 attachments = Utils.attachments_from_ids(params)
84 %__MODULE__{draft | attachments: attachments}
85 end
86
87 defp in_reply_to(draft) do
88 case Map.get(draft.params, "in_reply_to_status_id") do
89 "" -> draft
90 nil -> draft
91 id -> %__MODULE__{draft | in_reply_to: Activity.get_by_id(id)}
92 end
93 end
94
95 defp in_reply_to_conversation(draft) do
96 in_reply_to_conversation = Participation.get(draft.params["in_reply_to_conversation_id"])
97 %__MODULE__{draft | in_reply_to_conversation: in_reply_to_conversation}
98 end
99
100 defp visibility(%{params: params} = draft) do
101 case CommonAPI.get_visibility(params, draft.in_reply_to, draft.in_reply_to_conversation) do
102 {visibility, "direct"} when visibility != "direct" ->
103 add_error(draft, dgettext("errors", "The message visibility must be direct"))
104
105 {visibility, _} ->
106 %__MODULE__{draft | visibility: visibility}
107 end
108 end
109
110 defp expires_at(draft) do
111 case CommonAPI.check_expiry_date(draft.params["expires_in"]) do
112 {:ok, expires_at} -> %__MODULE__{draft | expires_at: expires_at}
113 {:error, message} -> add_error(draft, message)
114 end
115 end
116
117 defp poll(draft) do
118 case Utils.make_poll_data(draft.params) do
119 {:ok, {poll, poll_emoji}} ->
120 %__MODULE__{draft | poll: poll, emoji: Map.merge(draft.emoji, poll_emoji)}
121
122 {:error, message} ->
123 add_error(draft, message)
124 end
125 end
126
127 defp content(draft) do
128 {content_html, mentions, tags} =
129 Utils.make_content_html(
130 draft.status,
131 draft.attachments,
132 draft.params,
133 draft.visibility
134 )
135
136 %__MODULE__{draft | content_html: content_html, mentions: mentions, tags: tags}
137 end
138
139 defp to_and_cc(draft) do
140 addressed_users =
141 draft.mentions
142 |> Enum.map(fn {_, mentioned_user} -> mentioned_user.ap_id end)
143 |> Utils.get_addressed_users(draft.params["to"])
144
145 {to, cc} =
146 Utils.get_to_and_cc(
147 draft.user,
148 addressed_users,
149 draft.in_reply_to,
150 draft.visibility,
151 draft.in_reply_to_conversation
152 )
153
154 %__MODULE__{draft | to: to, cc: cc}
155 end
156
157 defp context(draft) do
158 context = Utils.make_context(draft.in_reply_to, draft.in_reply_to_conversation)
159 %__MODULE__{draft | context: context}
160 end
161
162 defp sensitive(draft) do
163 sensitive = draft.params["sensitive"] || Enum.member?(draft.tags, {"#nsfw", "nsfw"})
164 %__MODULE__{draft | sensitive: sensitive}
165 end
166
167 defp object(draft) do
168 emoji = Map.merge(Pleroma.Emoji.Formatter.get_emoji_map(draft.full_payload), draft.emoji)
169
170 object =
171 Utils.make_note_data(
172 draft.user.ap_id,
173 draft.to,
174 draft.context,
175 draft.content_html,
176 draft.attachments,
177 draft.in_reply_to,
178 draft.tags,
179 draft.summary,
180 draft.cc,
181 draft.sensitive,
182 draft.poll
183 )
184 |> Map.put("emoji", emoji)
185
186 %__MODULE__{draft | object: object}
187 end
188
189 defp preview?(draft) do
190 preview? = Pleroma.Web.ControllerHelper.truthy_param?(draft.params["preview"]) || false
191 %__MODULE__{draft | preview?: preview?}
192 end
193
194 defp changes(draft) do
195 direct? = draft.visibility == "direct"
196
197 changes =
198 %{
199 to: draft.to,
200 actor: draft.user,
201 context: draft.context,
202 object: draft.object,
203 additional: %{"cc" => draft.cc, "directMessage" => direct?}
204 }
205 |> Utils.maybe_add_list_data(draft.user, draft.visibility)
206
207 %__MODULE__{draft | changes: changes}
208 end
209
210 defp with_valid(%{valid?: true} = draft, func), do: func.(draft)
211 defp with_valid(draft, _func), do: draft
212
213 defp add_error(draft, message) do
214 %__MODULE__{draft | valid?: false, errors: [message | draft.errors]}
215 end
216
217 defp validate(%{valid?: true} = draft), do: {:ok, draft}
218 defp validate(%{errors: [message | _]}), do: {:error, message}
219 end