1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.CommonAPI.ActivityDraft do
7 alias Pleroma.Conversation.Participation
9 alias Pleroma.Web.ActivityPub.Builder
10 alias Pleroma.Web.CommonAPI
11 alias Pleroma.Web.CommonAPI.Utils
13 import Pleroma.Web.Gettext
15 defstruct valid?: true,
24 in_reply_to_conversation: nil,
44 def new(user, params) do
45 %__MODULE__{user: user}
49 def create(user, params) do
54 |> with_valid(&attachments/1)
58 |> with_valid(&in_reply_to/1)
59 |> with_valid(&in_reply_to_conversation/1)
60 |> with_valid(&visibility/1)
61 |> with_valid("e_id/1)
63 |> with_valid(&language/1)
64 |> with_valid(&to_and_cc/1)
65 |> with_valid(&context/1)
67 |> with_valid(&object/1)
69 |> with_valid(&changes/1)
73 defp put_params(draft, params) do
74 params = Map.put_new(params, :in_reply_to_status_id, params[:in_reply_to_id])
75 %__MODULE__{draft | params: params}
78 defp status(%{params: %{status: status}} = draft) do
79 %__MODULE__{draft | status: String.trim(status)}
82 defp summary(%{params: params} = draft) do
83 %__MODULE__{draft | summary: Map.get(params, :spoiler_text, "")}
86 defp full_payload(%{status: status, summary: summary} = draft) do
87 full_payload = String.trim(status <> summary)
89 case Utils.validate_character_limit(full_payload, draft.attachments) do
90 :ok -> %__MODULE__{draft | full_payload: full_payload}
91 {:error, message} -> add_error(draft, message)
95 defp attachments(%{params: params} = draft) do
96 attachments = Utils.attachments_from_ids(params)
97 %__MODULE__{draft | attachments: attachments}
100 defp in_reply_to(%{params: %{in_reply_to_status_id: ""}} = draft), do: draft
102 defp in_reply_to(%{params: %{in_reply_to_status_id: id}} = draft) when is_binary(id) do
103 %__MODULE__{draft | in_reply_to: Activity.get_by_id(id)}
106 defp in_reply_to(%{params: %{in_reply_to_status_id: %Activity{} = in_reply_to}} = draft) do
107 %__MODULE__{draft | in_reply_to: in_reply_to}
110 defp in_reply_to(draft), do: draft
112 defp in_reply_to_conversation(draft) do
113 in_reply_to_conversation = Participation.get(draft.params[:in_reply_to_conversation_id])
114 %__MODULE__{draft | in_reply_to_conversation: in_reply_to_conversation}
117 defp quote_id(%{params: %{quote_id: ""}} = draft), do: draft
119 defp quote_id(%{params: %{quote_id: id}} = draft) when is_binary(id) do
120 with {:activity, %Activity{} = quote} <- {:activity, Activity.get_by_id(id)},
121 visibility <- CommonAPI.get_quoted_visibility(quote),
122 {:visibility, true} <- {:visibility, visibility in ["public", "unlisted"]} do
123 %__MODULE__{draft | quote: Activity.get_by_id(id)}
126 add_error(draft, dgettext("errors", "You can't quote a status that doesn't exist"))
128 {:visibility, false} ->
129 add_error(draft, dgettext("errors", "You can only quote public or unlisted statuses"))
133 defp quote_id(%{params: %{quote_id: %Activity{} = quote}} = draft) do
134 %__MODULE__{draft | quote: quote}
137 defp quote_id(draft), do: draft
139 defp language(%{params: %{language: language}, content_html: content} = draft)
140 when is_binary(language) do
141 if Pleroma.ISO639.valid_alpha2?(language) do
142 %__MODULE__{draft | content_map: %{language => content}}
144 add_error(draft, dgettext("errors", "Invalid language"))
148 defp language(draft), do: draft
150 defp visibility(%{params: params} = draft) do
151 case CommonAPI.get_visibility(params, draft.in_reply_to, draft.in_reply_to_conversation) do
152 {visibility, "direct"} when visibility != "direct" ->
153 add_error(draft, dgettext("errors", "The message visibility must be direct"))
156 %__MODULE__{draft | visibility: visibility}
160 defp expires_at(draft) do
161 case CommonAPI.check_expiry_date(draft.params[:expires_in]) do
162 {:ok, expires_at} -> %__MODULE__{draft | expires_at: expires_at}
163 {:error, message} -> add_error(draft, message)
168 case Utils.make_poll_data(draft.params) do
169 {:ok, {poll, poll_emoji}} ->
170 %__MODULE__{draft | extra: poll, emoji: Map.merge(draft.emoji, poll_emoji)}
173 add_error(draft, message)
177 defp content(draft) do
178 {content_html, mentioned_users, tags} = Utils.make_content_html(draft)
182 |> Enum.map(fn {_, mentioned_user} -> mentioned_user.ap_id end)
183 |> Utils.get_addressed_users(draft.params[:to])
185 %__MODULE__{draft | content_html: content_html, mentions: mentions, tags: tags}
188 defp to_and_cc(draft) do
189 {to, cc} = Utils.get_to_and_cc(draft)
190 %__MODULE__{draft | to: to, cc: cc}
193 defp context(draft) do
194 context = Utils.make_context(draft)
195 %__MODULE__{draft | context: context}
198 defp sensitive(draft) do
199 sensitive = draft.params[:sensitive]
200 %__MODULE__{draft | sensitive: sensitive}
203 defp object(draft) do
204 emoji = Map.merge(Pleroma.Emoji.Formatter.get_emoji_map(draft.full_payload), draft.emoji)
206 # Sometimes people create posts with subject containing emoji,
207 # since subjects are usually copied this will result in a broken
208 # subject when someone replies from an instance that does not have
209 # the emoji or has it under different shortcode. This is an attempt
210 # to mitigate this by copying emoji from inReplyTo if they are present
213 with %Activity{} <- draft.in_reply_to,
214 %Object{data: %{"tag" => [_ | _] = tag}} <- Object.normalize(draft.in_reply_to) do
215 Enum.reduce(tag, %{}, fn
216 %{"type" => "Emoji", "name" => name, "icon" => %{"url" => url}}, acc ->
217 if String.contains?(draft.summary, name) do
218 Map.put(acc, name, url)
230 emoji = Map.merge(emoji, summary_emoji)
231 {:ok, note_data, _meta} = Builder.note(draft)
235 |> Map.put("emoji", emoji)
236 |> Map.put("source", %{
237 "content" => draft.status,
238 "mediaType" => Utils.get_content_type(draft.params[:content_type])
240 |> Map.put("generator", draft.params[:generator])
241 |> Map.put("contentMap", draft.content_map)
243 %__MODULE__{draft | object: object}
246 defp preview?(draft) do
247 preview? = Pleroma.Web.Utils.Params.truthy_param?(draft.params[:preview])
248 %__MODULE__{draft | preview?: preview?}
251 defp changes(draft) do
252 direct? = draft.visibility == "direct"
253 additional = %{"cc" => draft.cc, "directMessage" => direct?}
256 case draft.expires_at do
257 %DateTime{} = expires_at -> Map.put(additional, "expires_at", expires_at)
265 context: draft.context,
266 object: draft.object,
267 additional: additional
269 |> Utils.maybe_add_list_data(draft.user, draft.visibility)
271 %__MODULE__{draft | changes: changes}
274 defp with_valid(%{valid?: true} = draft, func), do: func.(draft)
275 defp with_valid(draft, _func), do: draft
277 defp add_error(draft, message) do
278 %__MODULE__{draft | valid?: false, errors: [message | draft.errors]}
281 defp validate(%{valid?: true} = draft), do: {:ok, draft}
282 defp validate(%{errors: [message | _]}), do: {:error, message}