Remove deps from Streaming/Persisting behaviors
[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.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 extra: 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 new(user, params) do
39 %__MODULE__{user: user}
40 |> put_params(params)
41 end
42
43 def create(user, params) do
44 user
45 |> new(params)
46 |> status()
47 |> summary()
48 |> with_valid(&attachments/1)
49 |> full_payload()
50 |> expires_at()
51 |> poll()
52 |> with_valid(&in_reply_to/1)
53 |> with_valid(&in_reply_to_conversation/1)
54 |> with_valid(&visibility/1)
55 |> content()
56 |> with_valid(&to_and_cc/1)
57 |> with_valid(&context/1)
58 |> sensitive()
59 |> with_valid(&object/1)
60 |> preview?()
61 |> with_valid(&changes/1)
62 |> validate()
63 end
64
65 def listen(user, params) do
66 user
67 |> new(params)
68 |> visibility()
69 |> to_and_cc()
70 |> context()
71 |> listen_object()
72 |> with_valid(&changes/1)
73 |> validate()
74 end
75
76 defp listen_object(draft) do
77 object =
78 draft.params
79 |> Map.take([:album, :artist, :title, :length])
80 |> Map.new(fn {key, value} -> {to_string(key), value} end)
81 |> Map.put("type", "Audio")
82 |> Map.put("to", draft.to)
83 |> Map.put("cc", draft.cc)
84 |> Map.put("actor", draft.user.ap_id)
85
86 %__MODULE__{draft | object: object}
87 end
88
89 defp put_params(draft, params) do
90 params = Map.put_new(params, :in_reply_to_status_id, params[:in_reply_to_id])
91 %__MODULE__{draft | params: params}
92 end
93
94 defp status(%{params: %{status: status}} = draft) do
95 %__MODULE__{draft | status: String.trim(status)}
96 end
97
98 defp summary(%{params: params} = draft) do
99 %__MODULE__{draft | summary: Map.get(params, :spoiler_text, "")}
100 end
101
102 defp full_payload(%{status: status, summary: summary} = draft) do
103 full_payload = String.trim(status <> summary)
104
105 case Utils.validate_character_limit(full_payload, draft.attachments) do
106 :ok -> %__MODULE__{draft | full_payload: full_payload}
107 {:error, message} -> add_error(draft, message)
108 end
109 end
110
111 defp attachments(%{params: params} = draft) do
112 attachments = Utils.attachments_from_ids(params)
113 %__MODULE__{draft | attachments: attachments}
114 end
115
116 defp in_reply_to(%{params: %{in_reply_to_status_id: ""}} = draft), do: draft
117
118 defp in_reply_to(%{params: %{in_reply_to_status_id: id}} = draft) when is_binary(id) do
119 %__MODULE__{draft | in_reply_to: Activity.get_by_id(id)}
120 end
121
122 defp in_reply_to(%{params: %{in_reply_to_status_id: %Activity{} = in_reply_to}} = draft) do
123 %__MODULE__{draft | in_reply_to: in_reply_to}
124 end
125
126 defp in_reply_to(draft), do: draft
127
128 defp in_reply_to_conversation(draft) do
129 in_reply_to_conversation = Participation.get(draft.params[:in_reply_to_conversation_id])
130 %__MODULE__{draft | in_reply_to_conversation: in_reply_to_conversation}
131 end
132
133 defp visibility(%{params: params} = draft) do
134 case CommonAPI.get_visibility(params, draft.in_reply_to, draft.in_reply_to_conversation) do
135 {visibility, "direct"} when visibility != "direct" ->
136 add_error(draft, dgettext("errors", "The message visibility must be direct"))
137
138 {visibility, _} ->
139 %__MODULE__{draft | visibility: visibility}
140 end
141 end
142
143 defp expires_at(draft) do
144 case CommonAPI.check_expiry_date(draft.params[:expires_in]) do
145 {:ok, expires_at} -> %__MODULE__{draft | expires_at: expires_at}
146 {:error, message} -> add_error(draft, message)
147 end
148 end
149
150 defp poll(draft) do
151 case Utils.make_poll_data(draft.params) do
152 {:ok, {poll, poll_emoji}} ->
153 %__MODULE__{draft | extra: poll, emoji: Map.merge(draft.emoji, poll_emoji)}
154
155 {:error, message} ->
156 add_error(draft, message)
157 end
158 end
159
160 defp content(draft) do
161 {content_html, mentioned_users, tags} = Utils.make_content_html(draft)
162
163 mentions =
164 mentioned_users
165 |> Enum.map(fn {_, mentioned_user} -> mentioned_user.ap_id end)
166 |> Utils.get_addressed_users(draft.params[:to])
167
168 %__MODULE__{draft | content_html: content_html, mentions: mentions, tags: tags}
169 end
170
171 defp to_and_cc(draft) do
172 {to, cc} = Utils.get_to_and_cc(draft)
173 %__MODULE__{draft | to: to, cc: cc}
174 end
175
176 defp context(draft) do
177 context = Utils.make_context(draft.in_reply_to, draft.in_reply_to_conversation)
178 %__MODULE__{draft | context: context}
179 end
180
181 defp sensitive(draft) do
182 sensitive = draft.params[:sensitive] || Enum.member?(draft.tags, {"#nsfw", "nsfw"})
183 %__MODULE__{draft | sensitive: sensitive}
184 end
185
186 defp object(draft) do
187 emoji = Map.merge(Pleroma.Emoji.Formatter.get_emoji_map(draft.full_payload), draft.emoji)
188
189 object =
190 Utils.make_note_data(draft)
191 |> Map.put("emoji", emoji)
192 |> Map.put("source", draft.status)
193 |> Map.put("generator", draft.params[:generator])
194
195 %__MODULE__{draft | object: object}
196 end
197
198 defp preview?(draft) do
199 preview? = Pleroma.Web.ControllerHelper.truthy_param?(draft.params[:preview])
200 %__MODULE__{draft | preview?: preview?}
201 end
202
203 defp changes(draft) do
204 direct? = draft.visibility == "direct"
205 additional = %{"cc" => draft.cc, "directMessage" => direct?}
206
207 additional =
208 case draft.expires_at do
209 %DateTime{} = expires_at -> Map.put(additional, "expires_at", expires_at)
210 _ -> additional
211 end
212
213 changes =
214 %{
215 to: draft.to,
216 actor: draft.user,
217 context: draft.context,
218 object: draft.object,
219 additional: additional
220 }
221 |> Utils.maybe_add_list_data(draft.user, draft.visibility)
222
223 %__MODULE__{draft | changes: changes}
224 end
225
226 defp with_valid(%{valid?: true} = draft, func), do: func.(draft)
227 defp with_valid(draft, _func), do: draft
228
229 defp add_error(draft, message) do
230 %__MODULE__{draft | valid?: false, errors: [message | draft.errors]}
231 end
232
233 defp validate(%{valid?: true} = draft), do: {:ok, draft}
234 defp validate(%{errors: [message | _]}), do: {:error, message}
235 end