1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 create(user, params) do
39 %__MODULE__{user: user}
43 |> with_valid(&attachments/1)
47 |> with_valid(&in_reply_to/1)
48 |> with_valid(&in_reply_to_conversation/1)
49 |> with_valid(&visibility/1)
51 |> with_valid(&to_and_cc/1)
52 |> with_valid(&context/1)
54 |> with_valid(&object/1)
56 |> with_valid(&changes/1)
60 defp put_params(draft, params) do
61 params = Map.put_new(params, :in_reply_to_status_id, params[:in_reply_to_id])
62 %__MODULE__{draft | params: params}
65 defp status(%{params: %{status: status}} = draft) do
66 %__MODULE__{draft | status: String.trim(status)}
69 defp summary(%{params: params} = draft) do
70 %__MODULE__{draft | summary: Map.get(params, :spoiler_text, "")}
73 defp full_payload(%{status: status, summary: summary} = draft) do
74 full_payload = String.trim(status <> summary)
76 case Utils.validate_character_limit(full_payload, draft.attachments) do
77 :ok -> %__MODULE__{draft | full_payload: full_payload}
78 {:error, message} -> add_error(draft, message)
82 defp attachments(%{params: params} = draft) do
83 attachments = Utils.attachments_from_ids(params)
84 %__MODULE__{draft | attachments: attachments}
87 defp in_reply_to(%{params: %{in_reply_to_status_id: ""}} = draft), do: draft
89 defp in_reply_to(%{params: %{in_reply_to_status_id: id}} = draft) when is_binary(id) do
90 %__MODULE__{draft | in_reply_to: Activity.get_by_id(id)}
93 defp in_reply_to(%{params: %{in_reply_to_status_id: %Activity{} = in_reply_to}} = draft) do
94 %__MODULE__{draft | in_reply_to: in_reply_to}
97 defp in_reply_to(draft), do: draft
99 defp in_reply_to_conversation(draft) do
100 in_reply_to_conversation = Participation.get(draft.params[:in_reply_to_conversation_id])
101 %__MODULE__{draft | in_reply_to_conversation: in_reply_to_conversation}
104 defp visibility(%{params: params} = draft) do
105 case CommonAPI.get_visibility(params, draft.in_reply_to, draft.in_reply_to_conversation) do
106 {visibility, "direct"} when visibility != "direct" ->
107 add_error(draft, dgettext("errors", "The message visibility must be direct"))
110 %__MODULE__{draft | visibility: visibility}
114 defp expires_at(draft) do
115 case CommonAPI.check_expiry_date(draft.params[:expires_in]) do
116 {:ok, expires_at} -> %__MODULE__{draft | expires_at: expires_at}
117 {:error, message} -> add_error(draft, message)
122 case Utils.make_poll_data(draft.params) do
123 {:ok, {poll, poll_emoji}} ->
124 %__MODULE__{draft | poll: poll, emoji: Map.merge(draft.emoji, poll_emoji)}
127 add_error(draft, message)
131 defp content(draft) do
132 {content_html, mentions, tags} =
133 Utils.make_content_html(
140 %__MODULE__{draft | content_html: content_html, mentions: mentions, tags: tags}
143 defp to_and_cc(draft) do
146 |> Enum.map(fn {_, mentioned_user} -> mentioned_user.ap_id end)
147 |> Utils.get_addressed_users(draft.params[:to])
155 draft.in_reply_to_conversation
158 %__MODULE__{draft | to: to, cc: cc}
161 defp context(draft) do
162 context = Utils.make_context(draft.in_reply_to, draft.in_reply_to_conversation)
163 %__MODULE__{draft | context: context}
166 defp sensitive(draft) do
167 sensitive = draft.params[:sensitive] || Enum.member?(draft.tags, {"#nsfw", "nsfw"})
168 %__MODULE__{draft | sensitive: sensitive}
171 defp object(draft) do
172 emoji = Map.merge(Pleroma.Emoji.Formatter.get_emoji_map(draft.full_payload), draft.emoji)
175 Utils.make_note_data(
188 |> Map.put("emoji", emoji)
189 |> Map.put("source", draft.status)
191 %__MODULE__{draft | object: object}
194 defp preview?(draft) do
195 preview? = Pleroma.Web.ControllerHelper.truthy_param?(draft.params[:preview])
196 %__MODULE__{draft | preview?: preview?}
199 defp changes(draft) do
200 direct? = draft.visibility == "direct"
201 additional = %{"cc" => draft.cc, "directMessage" => direct?}
204 case draft.expires_at do
205 %NaiveDateTime{} = expires_at -> Map.put(additional, "expires_at", expires_at)
213 context: draft.context,
214 object: draft.object,
215 additional: additional
217 |> Utils.maybe_add_list_data(draft.user, draft.visibility)
219 %__MODULE__{draft | changes: changes}
222 defp with_valid(%{valid?: true} = draft, func), do: func.(draft)
223 defp with_valid(draft, _func), do: draft
225 defp add_error(draft, message) do
226 %__MODULE__{draft | valid?: false, errors: [message | draft.errors]}
229 defp validate(%{valid?: true} = draft), do: {:ok, draft}
230 defp validate(%{errors: [message | _]}), do: {:error, message}