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,
40 def new(user, params) do
41 %__MODULE__{user: user}
45 def create(user, params) do
50 |> with_valid(&attachments/1)
54 |> with_valid(&in_reply_to/1)
55 |> with_valid(&in_reply_to_conversation/1)
56 |> with_valid(&visibility/1)
58 |> with_valid(&to_and_cc/1)
59 |> with_valid(&context/1)
61 |> with_valid(&object/1)
63 |> with_valid(&changes/1)
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}
72 defp status(%{params: %{status: status}} = draft) do
73 %__MODULE__{draft | status: String.trim(status)}
76 defp summary(%{params: params} = draft) do
77 %__MODULE__{draft | summary: Map.get(params, :spoiler_text, "")}
80 defp full_payload(%{status: status, summary: summary} = draft) do
81 full_payload = String.trim(status <> summary)
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)
89 defp attachments(%{params: params} = draft) do
90 attachments = Utils.attachments_from_ids(params)
91 %__MODULE__{draft | attachments: attachments}
94 defp in_reply_to(%{params: %{in_reply_to_status_id: ""}} = draft), do: draft
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)}
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}
104 defp in_reply_to(draft), do: draft
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}
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"))
117 %__MODULE__{draft | visibility: visibility}
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)
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)}
134 add_error(draft, message)
138 defp content(draft) do
139 {content_html, mentioned_users, tags} = Utils.make_content_html(draft)
143 |> Enum.map(fn {_, mentioned_user} -> mentioned_user.ap_id end)
144 |> Utils.get_addressed_users(draft.params[:to])
146 %__MODULE__{draft | content_html: content_html, mentions: mentions, tags: tags}
149 defp to_and_cc(draft) do
150 {to, cc} = Utils.get_to_and_cc(draft)
151 %__MODULE__{draft | to: to, cc: cc}
154 defp context(draft) do
155 context = Utils.make_context(draft.in_reply_to, draft.in_reply_to_conversation)
156 %__MODULE__{draft | context: context}
159 defp sensitive(draft) do
160 sensitive = draft.params[:sensitive]
161 %__MODULE__{draft | sensitive: sensitive}
164 defp object(draft) do
165 emoji = Map.merge(Pleroma.Emoji.Formatter.get_emoji_map(draft.full_payload), draft.emoji)
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
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)
191 emoji = Map.merge(emoji, summary_emoji)
192 {:ok, note_data, _meta} = Builder.note(draft)
196 |> Map.put("emoji", emoji)
197 |> Map.put("source", %{
198 "content" => draft.status,
199 "mediaType" => draft.params[:content_type]
201 |> Map.put("generator", draft.params[:generator])
203 %__MODULE__{draft | object: object}
206 defp preview?(draft) do
207 preview? = Pleroma.Web.Utils.Params.truthy_param?(draft.params[:preview])
208 %__MODULE__{draft | preview?: preview?}
211 defp changes(draft) do
212 direct? = draft.visibility == "direct"
213 additional = %{"cc" => draft.cc, "directMessage" => direct?}
216 case draft.expires_at do
217 %DateTime{} = expires_at -> Map.put(additional, "expires_at", expires_at)
225 context: draft.context,
226 object: draft.object,
227 additional: additional
229 |> Utils.maybe_add_list_data(draft.user, draft.visibility)
231 %__MODULE__{draft | changes: changes}
234 defp with_valid(%{valid?: true} = draft, func), do: func.(draft)
235 defp with_valid(draft, _func), do: draft
237 defp add_error(draft, message) do
238 %__MODULE__{draft | valid?: false, errors: [message | draft.errors]}
241 defp validate(%{valid?: true} = draft), do: {:ok, draft}
242 defp validate(%{errors: [message | _]}), do: {:error, message}