Merge branch 'openapi/statuses' into 'develop'
[akkoma] / lib / pleroma / web / common_api / activity_draft.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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(%{params: %{in_reply_to_status_id: ""}} = draft), do: draft
88
89 defp in_reply_to(%{params: %{in_reply_to_status_id: id}} = draft) when is_binary(id) do
90 %__MODULE__{draft | in_reply_to: Activity.get_by_id(id)}
91 end
92
93 defp in_reply_to(%{params: %{in_reply_to_status_id: %Activity{} = in_reply_to}} = draft) do
94 %__MODULE__{draft | in_reply_to: in_reply_to}
95 end
96
97 defp in_reply_to(draft), do: draft
98
99 defp in_reply_to_conversation(draft) do
100 in_reply_to_conversation = Participation.get(draft.params[:in_reply_to_conversation_id])
101 %__MODULE__{draft | in_reply_to_conversation: in_reply_to_conversation}
102 end
103
104 defp visibility(%{params: params} = draft) do
105 case CommonAPI.get_visibility(params, draft.in_reply_to, draft.in_reply_to_conversation) do
106 {visibility, "direct"} when visibility != "direct" ->
107 add_error(draft, dgettext("errors", "The message visibility must be direct"))
108
109 {visibility, _} ->
110 %__MODULE__{draft | visibility: visibility}
111 end
112 end
113
114 defp expires_at(draft) do
115 case CommonAPI.check_expiry_date(draft.params[:expires_in]) do
116 {:ok, expires_at} -> %__MODULE__{draft | expires_at: expires_at}
117 {:error, message} -> add_error(draft, message)
118 end
119 end
120
121 defp poll(draft) do
122 case Utils.make_poll_data(draft.params) do
123 {:ok, {poll, poll_emoji}} ->
124 %__MODULE__{draft | poll: poll, emoji: Map.merge(draft.emoji, poll_emoji)}
125
126 {:error, message} ->
127 add_error(draft, message)
128 end
129 end
130
131 defp content(draft) do
132 {content_html, mentions, tags} =
133 Utils.make_content_html(
134 draft.status,
135 draft.attachments,
136 draft.params,
137 draft.visibility
138 )
139
140 %__MODULE__{draft | content_html: content_html, mentions: mentions, tags: tags}
141 end
142
143 defp to_and_cc(draft) do
144 addressed_users =
145 draft.mentions
146 |> Enum.map(fn {_, mentioned_user} -> mentioned_user.ap_id end)
147 |> Utils.get_addressed_users(draft.params[:to])
148
149 {to, cc} =
150 Utils.get_to_and_cc(
151 draft.user,
152 addressed_users,
153 draft.in_reply_to,
154 draft.visibility,
155 draft.in_reply_to_conversation
156 )
157
158 %__MODULE__{draft | to: to, cc: cc}
159 end
160
161 defp context(draft) do
162 context = Utils.make_context(draft.in_reply_to, draft.in_reply_to_conversation)
163 %__MODULE__{draft | context: context}
164 end
165
166 defp sensitive(draft) do
167 sensitive = draft.params[:sensitive] || Enum.member?(draft.tags, {"#nsfw", "nsfw"})
168 %__MODULE__{draft | sensitive: sensitive}
169 end
170
171 defp object(draft) do
172 emoji = Map.merge(Pleroma.Emoji.Formatter.get_emoji_map(draft.full_payload), draft.emoji)
173
174 object =
175 Utils.make_note_data(
176 draft.user.ap_id,
177 draft.to,
178 draft.context,
179 draft.content_html,
180 draft.attachments,
181 draft.in_reply_to,
182 draft.tags,
183 draft.summary,
184 draft.cc,
185 draft.sensitive,
186 draft.poll
187 )
188 |> Map.put("emoji", emoji)
189
190 %__MODULE__{draft | object: object}
191 end
192
193 defp preview?(draft) do
194 preview? = Pleroma.Web.ControllerHelper.truthy_param?(draft.params[:preview])
195 %__MODULE__{draft | preview?: preview?}
196 end
197
198 defp changes(draft) do
199 direct? = draft.visibility == "direct"
200
201 changes =
202 %{
203 to: draft.to,
204 actor: draft.user,
205 context: draft.context,
206 object: draft.object,
207 additional: %{"cc" => draft.cc, "directMessage" => direct?}
208 }
209 |> Utils.maybe_add_list_data(draft.user, draft.visibility)
210
211 %__MODULE__{draft | changes: changes}
212 end
213
214 defp with_valid(%{valid?: true} = draft, func), do: func.(draft)
215 defp with_valid(draft, _func), do: draft
216
217 defp add_error(draft, message) do
218 %__MODULE__{draft | valid?: false, errors: [message | draft.errors]}
219 end
220
221 defp validate(%{valid?: true} = draft), do: {:ok, draft}
222 defp validate(%{errors: [message | _]}), do: {:error, message}
223 end