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
8 alias Pleroma.Web.CommonAPI
9 alias Pleroma.Web.CommonAPI.Utils
11 import Pleroma.Web.Gettext
13 defstruct valid?: true,
22 in_reply_to_conversation: nil,
38 def new(user, params) do
39 %__MODULE__{user: user}
43 def create(user, params) do
48 |> with_valid(&attachments/1)
52 |> with_valid(&in_reply_to/1)
53 |> with_valid(&in_reply_to_conversation/1)
54 |> with_valid(&visibility/1)
56 |> with_valid(&to_and_cc/1)
57 |> with_valid(&context/1)
59 |> with_valid(&object/1)
61 |> with_valid(&changes/1)
65 def listen(user, params) do
72 |> with_valid(&changes/1)
76 defp listen_object(draft) do
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)
86 %__MODULE__{draft | object: object}
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}
94 defp status(%{params: %{status: status}} = draft) do
95 %__MODULE__{draft | status: String.trim(status)}
98 defp summary(%{params: params} = draft) do
99 %__MODULE__{draft | summary: Map.get(params, :spoiler_text, "")}
102 defp full_payload(%{status: status, summary: summary} = draft) do
103 full_payload = String.trim(status <> summary)
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)
111 defp attachments(%{params: params} = draft) do
112 attachments = Utils.attachments_from_ids(params)
113 %__MODULE__{draft | attachments: attachments}
116 defp in_reply_to(%{params: %{in_reply_to_status_id: ""}} = draft), do: draft
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)}
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}
126 defp in_reply_to(draft), do: draft
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}
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"))
139 %__MODULE__{draft | visibility: visibility}
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)
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)}
156 add_error(draft, message)
160 defp content(draft) do
161 {content_html, mentioned_users, tags} = Utils.make_content_html(draft)
165 |> Enum.map(fn {_, mentioned_user} -> mentioned_user.ap_id end)
166 |> Utils.get_addressed_users(draft.params[:to])
168 %__MODULE__{draft | content_html: content_html, mentions: mentions, tags: tags}
171 defp to_and_cc(draft) do
172 {to, cc} = Utils.get_to_and_cc(draft)
173 %__MODULE__{draft | to: to, cc: cc}
176 defp context(draft) do
177 context = Utils.make_context(draft.in_reply_to, draft.in_reply_to_conversation)
178 %__MODULE__{draft | context: context}
181 defp sensitive(draft) do
182 sensitive = draft.params[:sensitive] || Enum.member?(draft.tags, {"#nsfw", "nsfw"})
183 %__MODULE__{draft | sensitive: sensitive}
186 defp object(draft) do
187 emoji = Map.merge(Pleroma.Emoji.Formatter.get_emoji_map(draft.full_payload), draft.emoji)
190 Utils.make_note_data(draft)
191 |> Map.put("emoji", emoji)
192 |> Map.put("source", draft.status)
193 |> Map.put("generator", draft.params[:generator])
195 %__MODULE__{draft | object: object}
198 defp preview?(draft) do
199 preview? = Pleroma.Web.ControllerHelper.truthy_param?(draft.params[:preview])
200 %__MODULE__{draft | preview?: preview?}
203 defp changes(draft) do
204 direct? = draft.visibility == "direct"
205 additional = %{"cc" => draft.cc, "directMessage" => direct?}
208 case draft.expires_at do
209 %DateTime{} = expires_at -> Map.put(additional, "expires_at", expires_at)
217 context: draft.context,
218 object: draft.object,
219 additional: additional
221 |> Utils.maybe_add_list_data(draft.user, draft.visibility)
223 %__MODULE__{draft | changes: changes}
226 defp with_valid(%{valid?: true} = draft, func), do: func.(draft)
227 defp with_valid(draft, _func), do: draft
229 defp add_error(draft, message) do
230 %__MODULE__{draft | valid?: false, errors: [message | draft.errors]}
233 defp validate(%{valid?: true} = draft), do: {:ok, draft}
234 defp validate(%{errors: [message | _]}), do: {:error, message}