1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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(draft) do
88 case Map.get(draft.params, "in_reply_to_status_id") do
91 id -> %__MODULE__{draft | in_reply_to: Activity.get_by_id(id)}
95 defp in_reply_to_conversation(draft) do
96 in_reply_to_conversation = Participation.get(draft.params["in_reply_to_conversation_id"])
97 %__MODULE__{draft | in_reply_to_conversation: in_reply_to_conversation}
100 defp visibility(%{params: params} = draft) do
101 case CommonAPI.get_visibility(params, draft.in_reply_to, draft.in_reply_to_conversation) do
102 {visibility, "direct"} when visibility != "direct" ->
103 add_error(draft, dgettext("errors", "The message visibility must be direct"))
106 %__MODULE__{draft | visibility: visibility}
110 defp expires_at(draft) do
111 case CommonAPI.check_expiry_date(draft.params["expires_in"]) do
112 {:ok, expires_at} -> %__MODULE__{draft | expires_at: expires_at}
113 {:error, message} -> add_error(draft, message)
118 case Utils.make_poll_data(draft.params) do
119 {:ok, {poll, poll_emoji}} ->
120 %__MODULE__{draft | poll: poll, emoji: Map.merge(draft.emoji, poll_emoji)}
123 add_error(draft, message)
127 defp content(draft) do
128 {content_html, mentions, tags} =
129 Utils.make_content_html(
136 %__MODULE__{draft | content_html: content_html, mentions: mentions, tags: tags}
139 defp to_and_cc(draft) do
142 |> Enum.map(fn {_, mentioned_user} -> mentioned_user.ap_id end)
143 |> Utils.get_addressed_users(draft.params["to"])
151 draft.in_reply_to_conversation
154 %__MODULE__{draft | to: to, cc: cc}
157 defp context(draft) do
158 context = Utils.make_context(draft.in_reply_to, draft.in_reply_to_conversation)
159 %__MODULE__{draft | context: context}
162 defp sensitive(draft) do
163 sensitive = draft.params["sensitive"] || Enum.member?(draft.tags, {"#nsfw", "nsfw"})
164 %__MODULE__{draft | sensitive: sensitive}
167 defp object(draft) do
168 emoji = Map.merge(Pleroma.Emoji.Formatter.get_emoji_map(draft.full_payload), draft.emoji)
171 Utils.make_note_data(
184 |> Map.put("emoji", emoji)
186 %__MODULE__{draft | object: object}
189 defp preview?(draft) do
190 preview? = Pleroma.Web.ControllerHelper.truthy_param?(draft.params["preview"]) || false
191 %__MODULE__{draft | preview?: preview?}
194 defp changes(draft) do
195 direct? = draft.visibility == "direct"
201 context: draft.context,
202 object: draft.object,
203 additional: %{"cc" => draft.cc, "directMessage" => direct?}
205 |> Utils.maybe_add_list_data(draft.user, draft.visibility)
207 %__MODULE__{draft | changes: changes}
210 defp with_valid(%{valid?: true} = draft, func), do: func.(draft)
211 defp with_valid(draft, _func), do: draft
213 defp add_error(draft, message) do
214 %__MODULE__{draft | valid?: false, errors: [message | draft.errors]}
217 defp validate(%{valid?: true} = draft), do: {:ok, draft}
218 defp validate(%{errors: [message | _]}), do: {:error, message}