activity_draft.ex 7.61 KB
Newer Older
minibikini's avatar
minibikini committed
1
# Pleroma: A lightweight social networking server
Haelwenn's avatar
Haelwenn committed
2
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
minibikini's avatar
minibikini committed
3 4 5 6 7
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.CommonAPI.ActivityDraft do
  alias Pleroma.Activity
  alias Pleroma.Conversation.Participation
8
  alias Pleroma.Object
9
  alias Pleroma.Web.ActivityPub.Builder
minibikini's avatar
minibikini committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
  alias Pleroma.Web.CommonAPI
  alias Pleroma.Web.CommonAPI.Utils

  import Pleroma.Web.Gettext

  defstruct valid?: true,
            errors: [],
            user: nil,
            params: %{},
            status: nil,
            summary: nil,
            full_payload: nil,
            attachments: [],
            in_reply_to: nil,
            in_reply_to_conversation: nil,
            visibility: nil,
            expires_at: nil,
minibikini's avatar
minibikini committed
27
            extra: nil,
minibikini's avatar
minibikini committed
28 29 30 31 32 33 34 35 36 37 38 39
            emoji: %{},
            content_html: nil,
            mentions: [],
            tags: [],
            to: [],
            cc: [],
            context: nil,
            sensitive: false,
            object: nil,
            preview?: false,
            changes: %{}

minibikini's avatar
minibikini committed
40
  def new(user, params) do
minibikini's avatar
minibikini committed
41 42
    %__MODULE__{user: user}
    |> put_params(params)
minibikini's avatar
minibikini committed
43 44 45 46 47
  end

  def create(user, params) do
    user
    |> new(params)
minibikini's avatar
minibikini committed
48 49
    |> status()
    |> summary()
50
    |> with_valid(&attachments/1)
minibikini's avatar
minibikini committed
51 52 53
    |> full_payload()
    |> expires_at()
    |> poll()
minibikini's avatar
minibikini committed
54 55 56
    |> with_valid(&in_reply_to/1)
    |> with_valid(&in_reply_to_conversation/1)
    |> with_valid(&visibility/1)
minibikini's avatar
minibikini committed
57
    |> content()
minibikini's avatar
minibikini committed
58 59
    |> with_valid(&to_and_cc/1)
    |> with_valid(&context/1)
minibikini's avatar
minibikini committed
60
    |> sensitive()
minibikini's avatar
minibikini committed
61
    |> with_valid(&object/1)
minibikini's avatar
minibikini committed
62
    |> preview?()
minibikini's avatar
minibikini committed
63
    |> with_valid(&changes/1)
minibikini's avatar
minibikini committed
64 65 66
    |> validate()
  end

minibikini's avatar
minibikini committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  def listen(user, params) do
    user
    |> new(params)
    |> visibility()
    |> to_and_cc()
    |> context()
    |> listen_object()
    |> with_valid(&changes/1)
    |> validate()
  end

  defp listen_object(draft) do
    object =
      draft.params
      |> Map.take([:album, :artist, :title, :length])
      |> Map.new(fn {key, value} -> {to_string(key), value} end)
      |> Map.put("type", "Audio")
      |> Map.put("to", draft.to)
      |> Map.put("cc", draft.cc)
      |> Map.put("actor", draft.user.ap_id)

    %__MODULE__{draft | object: object}
  end

minibikini's avatar
minibikini committed
91
  defp put_params(draft, params) do
92
    params = Map.put_new(params, :in_reply_to_status_id, params[:in_reply_to_id])
minibikini's avatar
minibikini committed
93 94 95
    %__MODULE__{draft | params: params}
  end

96
  defp status(%{params: %{status: status}} = draft) do
minibikini's avatar
minibikini committed
97 98 99 100
    %__MODULE__{draft | status: String.trim(status)}
  end

  defp summary(%{params: params} = draft) do
101
    %__MODULE__{draft | summary: Map.get(params, :spoiler_text, "")}
minibikini's avatar
minibikini committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  end

  defp full_payload(%{status: status, summary: summary} = draft) do
    full_payload = String.trim(status <> summary)

    case Utils.validate_character_limit(full_payload, draft.attachments) do
      :ok -> %__MODULE__{draft | full_payload: full_payload}
      {:error, message} -> add_error(draft, message)
    end
  end

  defp attachments(%{params: params} = draft) do
    attachments = Utils.attachments_from_ids(params)
    %__MODULE__{draft | attachments: attachments}
  end

118
  defp in_reply_to(%{params: %{in_reply_to_status_id: ""}} = draft), do: draft
Alexander Strizhakov's avatar
Alexander Strizhakov committed
119

120
  defp in_reply_to(%{params: %{in_reply_to_status_id: id}} = draft) when is_binary(id) do
Alexander Strizhakov's avatar
Alexander Strizhakov committed
121
    %__MODULE__{draft | in_reply_to: Activity.get_by_id(id)}
minibikini's avatar
minibikini committed
122 123
  end

124
  defp in_reply_to(%{params: %{in_reply_to_status_id: %Activity{} = in_reply_to}} = draft) do
Alexander Strizhakov's avatar
Alexander Strizhakov committed
125 126 127 128 129
    %__MODULE__{draft | in_reply_to: in_reply_to}
  end

  defp in_reply_to(draft), do: draft

minibikini's avatar
minibikini committed
130
  defp in_reply_to_conversation(draft) do
131
    in_reply_to_conversation = Participation.get(draft.params[:in_reply_to_conversation_id])
minibikini's avatar
minibikini committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145
    %__MODULE__{draft | in_reply_to_conversation: in_reply_to_conversation}
  end

  defp visibility(%{params: params} = draft) do
    case CommonAPI.get_visibility(params, draft.in_reply_to, draft.in_reply_to_conversation) do
      {visibility, "direct"} when visibility != "direct" ->
        add_error(draft, dgettext("errors", "The message visibility must be direct"))

      {visibility, _} ->
        %__MODULE__{draft | visibility: visibility}
    end
  end

  defp expires_at(draft) do
146
    case CommonAPI.check_expiry_date(draft.params[:expires_in]) do
minibikini's avatar
minibikini committed
147 148 149 150 151 152 153 154
      {:ok, expires_at} -> %__MODULE__{draft | expires_at: expires_at}
      {:error, message} -> add_error(draft, message)
    end
  end

  defp poll(draft) do
    case Utils.make_poll_data(draft.params) do
      {:ok, {poll, poll_emoji}} ->
minibikini's avatar
minibikini committed
155
        %__MODULE__{draft | extra: poll, emoji: Map.merge(draft.emoji, poll_emoji)}
minibikini's avatar
minibikini committed
156 157 158 159 160 161 162

      {:error, message} ->
        add_error(draft, message)
    end
  end

  defp content(draft) do
minibikini's avatar
minibikini committed
163
    {content_html, mentioned_users, tags} = Utils.make_content_html(draft)
minibikini's avatar
minibikini committed
164

minibikini's avatar
minibikini committed
165 166
    mentions =
      mentioned_users
minibikini's avatar
minibikini committed
167
      |> Enum.map(fn {_, mentioned_user} -> mentioned_user.ap_id end)
168
      |> Utils.get_addressed_users(draft.params[:to])
minibikini's avatar
minibikini committed
169

minibikini's avatar
minibikini committed
170 171
    %__MODULE__{draft | content_html: content_html, mentions: mentions, tags: tags}
  end
minibikini's avatar
minibikini committed
172

minibikini's avatar
minibikini committed
173 174
  defp to_and_cc(draft) do
    {to, cc} = Utils.get_to_and_cc(draft)
minibikini's avatar
minibikini committed
175 176 177 178 179 180 181 182 183
    %__MODULE__{draft | to: to, cc: cc}
  end

  defp context(draft) do
    context = Utils.make_context(draft.in_reply_to, draft.in_reply_to_conversation)
    %__MODULE__{draft | context: context}
  end

  defp sensitive(draft) do
184
    sensitive = draft.params[:sensitive]
minibikini's avatar
minibikini committed
185 186 187 188 189 190
    %__MODULE__{draft | sensitive: sensitive}
  end

  defp object(draft) do
    emoji = Map.merge(Pleroma.Emoji.Formatter.get_emoji_map(draft.full_payload), draft.emoji)

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    # Sometimes people create posts with subject containing emoji,
    # since subjects are usually copied this will result in a broken
    # subject when someone replies from an instance that does not have
    # the emoji or has it under different shortcode. This is an attempt
    # to mitigate this by copying emoji from inReplyTo if they are present
    # in the subject.
    summary_emoji =
      with %Activity{} <- draft.in_reply_to,
           %Object{data: %{"tag" => [_ | _] = tag}} <- Object.normalize(draft.in_reply_to) do
        Enum.reduce(tag, %{}, fn
          %{"type" => "Emoji", "name" => name, "icon" => %{"url" => url}}, acc ->
            if String.contains?(draft.summary, name) do
              Map.put(acc, name, url)
            else
              acc
            end

          _, acc ->
            acc
        end)
      else
        _ -> %{}
      end

    emoji = Map.merge(emoji, summary_emoji)

217 218
    {:ok, note_data, _meta} = Builder.note(draft)

minibikini's avatar
minibikini committed
219
    object =
220
      note_data
minibikini's avatar
minibikini committed
221
      |> Map.put("emoji", emoji)
Haelwenn's avatar
Haelwenn committed
222
      |> Map.put("source", draft.status)
223
      |> Map.put("generator", draft.params[:generator])
minibikini's avatar
minibikini committed
224 225 226 227 228

    %__MODULE__{draft | object: object}
  end

  defp preview?(draft) do
229
    preview? = Pleroma.Web.Utils.Params.truthy_param?(draft.params[:preview])
minibikini's avatar
minibikini committed
230 231 232 233 234
    %__MODULE__{draft | preview?: preview?}
  end

  defp changes(draft) do
    direct? = draft.visibility == "direct"
235 236 237 238
    additional = %{"cc" => draft.cc, "directMessage" => direct?}

    additional =
      case draft.expires_at do
239
        %DateTime{} = expires_at -> Map.put(additional, "expires_at", expires_at)
240 241
        _ -> additional
      end
minibikini's avatar
minibikini committed
242 243 244 245 246 247 248

    changes =
      %{
        to: draft.to,
        actor: draft.user,
        context: draft.context,
        object: draft.object,
249
        additional: additional
minibikini's avatar
minibikini committed
250 251 252 253 254 255
      }
      |> Utils.maybe_add_list_data(draft.user, draft.visibility)

    %__MODULE__{draft | changes: changes}
  end

minibikini's avatar
minibikini committed
256 257 258
  defp with_valid(%{valid?: true} = draft, func), do: func.(draft)
  defp with_valid(draft, _func), do: draft

minibikini's avatar
minibikini committed
259 260 261 262 263 264 265
  defp add_error(draft, message) do
    %__MODULE__{draft | valid?: false, errors: [message | draft.errors]}
  end

  defp validate(%{valid?: true} = draft), do: {:ok, draft}
  defp validate(%{errors: [message | _]}), do: {:error, message}
end