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.CommonAPI
10 alias Pleroma.Web.CommonAPI.Utils
12 import Pleroma.Web.Gettext
14 defstruct valid?: true,
23 in_reply_to_conversation: nil,
39 def new(user, params) do
40 %__MODULE__{user: user}
44 def create(user, params) do
49 |> with_valid(&attachments/1)
53 |> with_valid(&in_reply_to/1)
54 |> with_valid(&in_reply_to_conversation/1)
55 |> with_valid(&visibility/1)
57 |> with_valid(&to_and_cc/1)
58 |> with_valid(&context/1)
60 |> with_valid(&object/1)
62 |> with_valid(&changes/1)
66 def listen(user, params) do
73 |> with_valid(&changes/1)
77 defp listen_object(draft) do
80 |> Map.take([:album, :artist, :title, :length])
81 |> Map.new(fn {key, value} -> {to_string(key), value} end)
82 |> Map.put("type", "Audio")
83 |> Map.put("to", draft.to)
84 |> Map.put("cc", draft.cc)
85 |> Map.put("actor", draft.user.ap_id)
87 %__MODULE__{draft | object: object}
90 defp put_params(draft, params) do
91 params = Map.put_new(params, :in_reply_to_status_id, params[:in_reply_to_id])
92 %__MODULE__{draft | params: params}
95 defp status(%{params: %{status: status}} = draft) do
96 %__MODULE__{draft | status: String.trim(status)}
99 defp summary(%{params: params} = draft) do
100 %__MODULE__{draft | summary: Map.get(params, :spoiler_text, "")}
103 defp full_payload(%{status: status, summary: summary} = draft) do
104 full_payload = String.trim(status <> summary)
106 case Utils.validate_character_limit(full_payload, draft.attachments) do
107 :ok -> %__MODULE__{draft | full_payload: full_payload}
108 {:error, message} -> add_error(draft, message)
112 defp attachments(%{params: params} = draft) do
113 attachments = Utils.attachments_from_ids(params)
114 %__MODULE__{draft | attachments: attachments}
117 defp in_reply_to(%{params: %{in_reply_to_status_id: ""}} = draft), do: draft
119 defp in_reply_to(%{params: %{in_reply_to_status_id: id}} = draft) when is_binary(id) do
120 %__MODULE__{draft | in_reply_to: Activity.get_by_id(id)}
123 defp in_reply_to(%{params: %{in_reply_to_status_id: %Activity{} = in_reply_to}} = draft) do
124 %__MODULE__{draft | in_reply_to: in_reply_to}
127 defp in_reply_to(draft), do: draft
129 defp in_reply_to_conversation(draft) do
130 in_reply_to_conversation = Participation.get(draft.params[:in_reply_to_conversation_id])
131 %__MODULE__{draft | in_reply_to_conversation: in_reply_to_conversation}
134 defp visibility(%{params: params} = draft) do
135 case CommonAPI.get_visibility(params, draft.in_reply_to, draft.in_reply_to_conversation) do
136 {visibility, "direct"} when visibility != "direct" ->
137 add_error(draft, dgettext("errors", "The message visibility must be direct"))
140 %__MODULE__{draft | visibility: visibility}
144 defp expires_at(draft) do
145 case CommonAPI.check_expiry_date(draft.params[:expires_in]) do
146 {:ok, expires_at} -> %__MODULE__{draft | expires_at: expires_at}
147 {:error, message} -> add_error(draft, message)
152 case Utils.make_poll_data(draft.params) do
153 {:ok, {poll, poll_emoji}} ->
154 %__MODULE__{draft | extra: poll, emoji: Map.merge(draft.emoji, poll_emoji)}
157 add_error(draft, message)
161 defp content(draft) do
162 {content_html, mentioned_users, tags} = Utils.make_content_html(draft)
166 |> Enum.map(fn {_, mentioned_user} -> mentioned_user.ap_id end)
167 |> Utils.get_addressed_users(draft.params[:to])
169 %__MODULE__{draft | content_html: content_html, mentions: mentions, tags: tags}
172 defp to_and_cc(draft) do
173 {to, cc} = Utils.get_to_and_cc(draft)
174 %__MODULE__{draft | to: to, cc: cc}
177 defp context(draft) do
178 context = Utils.make_context(draft.in_reply_to, draft.in_reply_to_conversation)
179 %__MODULE__{draft | context: context}
182 defp sensitive(draft) do
183 sensitive = draft.params[:sensitive]
184 %__MODULE__{draft | sensitive: sensitive}
187 defp object(draft) do
188 emoji = Map.merge(Pleroma.Emoji.Formatter.get_emoji_map(draft.full_payload), draft.emoji)
190 # Sometimes people create posts with subject containing emoji,
191 # since subjects are usually copied this will result in a broken
192 # subject when someone replies from an instance that does not have
193 # the emoji or has it under different shortcode. This is an attempt
194 # to mitigate this by copying emoji from inReplyTo if they are present
197 with %Activity{} <- draft.in_reply_to,
198 %Object{data: %{"tag" => [_ | _] = tag}} <- Object.normalize(draft.in_reply_to) do
199 Enum.reduce(tag, %{}, fn
200 %{"type" => "Emoji", "name" => name, "icon" => %{"url" => url}}, acc ->
201 if String.contains?(draft.summary, name) do
202 Map.put(acc, name, url)
214 emoji = Map.merge(emoji, summary_emoji)
217 Utils.make_note_data(draft)
218 |> Map.put("emoji", emoji)
219 |> Map.put("source", draft.status)
220 |> Map.put("generator", draft.params[:generator])
222 %__MODULE__{draft | object: object}
225 defp preview?(draft) do
226 preview? = Pleroma.Web.ControllerHelper.truthy_param?(draft.params[:preview])
227 %__MODULE__{draft | preview?: preview?}
230 defp changes(draft) do
231 direct? = draft.visibility == "direct"
232 additional = %{"cc" => draft.cc, "directMessage" => direct?}
235 case draft.expires_at do
236 %DateTime{} = expires_at -> Map.put(additional, "expires_at", expires_at)
244 context: draft.context,
245 object: draft.object,
246 additional: additional
248 |> Utils.maybe_add_list_data(draft.user, draft.visibility)
250 %__MODULE__{draft | changes: changes}
253 defp with_valid(%{valid?: true} = draft, func), do: func.(draft)
254 defp with_valid(draft, _func), do: draft
256 defp add_error(draft, message) do
257 %__MODULE__{draft | valid?: false, errors: [message | draft.errors]}
260 defp validate(%{valid?: true} = draft), do: {:ok, draft}
261 defp validate(%{errors: [message | _]}), do: {:error, message}