Pleroma/lib/pleroma/web/common_api/activity_draft.ex

235 lines
6.6 KiB
Elixir
Raw Normal View History

2019-09-24 11:10:54 +02:00
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
2019-09-24 11:10:54 +02:00
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.CommonAPI.ActivityDraft do
alias Pleroma.Activity
alias Pleroma.Conversation.Participation
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,
2020-10-02 19:00:50 +02:00
extra: nil,
2019-09-24 11:10:54 +02:00
emoji: %{},
content_html: nil,
mentions: [],
tags: [],
to: [],
cc: [],
context: nil,
sensitive: false,
object: nil,
preview?: false,
changes: %{}
2020-10-02 19:00:50 +02:00
def new(user, params) do
2019-09-24 11:10:54 +02:00
%__MODULE__{user: user}
|> put_params(params)
2020-10-02 19:00:50 +02:00
end
def create(user, params) do
user
|> new(params)
2019-09-24 11:10:54 +02:00
|> status()
|> summary()
|> with_valid(&attachments/1)
2019-09-24 11:10:54 +02:00
|> full_payload()
|> expires_at()
|> poll()
2019-09-23 13:52:41 +02:00
|> with_valid(&in_reply_to/1)
|> with_valid(&in_reply_to_conversation/1)
|> with_valid(&visibility/1)
2019-09-24 11:10:54 +02:00
|> content()
2019-09-23 13:52:41 +02:00
|> with_valid(&to_and_cc/1)
|> with_valid(&context/1)
2019-09-24 11:10:54 +02:00
|> sensitive()
2019-09-23 13:52:41 +02:00
|> with_valid(&object/1)
2019-09-24 11:10:54 +02:00
|> preview?()
2019-09-23 13:52:41 +02:00
|> with_valid(&changes/1)
2019-09-24 11:10:54 +02:00
|> validate()
end
2020-10-02 19:00:50 +02:00
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
2019-09-24 11:10:54 +02:00
defp put_params(draft, params) do
2020-05-12 21:59:26 +02:00
params = Map.put_new(params, :in_reply_to_status_id, params[:in_reply_to_id])
2019-09-24 11:10:54 +02:00
%__MODULE__{draft | params: params}
end
2020-05-12 21:59:26 +02:00
defp status(%{params: %{status: status}} = draft) do
2019-09-24 11:10:54 +02:00
%__MODULE__{draft | status: String.trim(status)}
end
defp summary(%{params: params} = draft) do
2020-05-12 21:59:26 +02:00
%__MODULE__{draft | summary: Map.get(params, :spoiler_text, "")}
2019-09-24 11:10:54 +02:00
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
2020-05-12 21:59:26 +02:00
defp in_reply_to(%{params: %{in_reply_to_status_id: ""}} = draft), do: draft
2019-12-04 07:49:17 +01:00
2020-05-12 21:59:26 +02:00
defp in_reply_to(%{params: %{in_reply_to_status_id: id}} = draft) when is_binary(id) do
2019-12-04 07:49:17 +01:00
%__MODULE__{draft | in_reply_to: Activity.get_by_id(id)}
2019-09-24 11:10:54 +02:00
end
2020-05-12 21:59:26 +02:00
defp in_reply_to(%{params: %{in_reply_to_status_id: %Activity{} = in_reply_to}} = draft) do
2019-12-04 07:49:17 +01:00
%__MODULE__{draft | in_reply_to: in_reply_to}
end
defp in_reply_to(draft), do: draft
2019-09-24 11:10:54 +02:00
defp in_reply_to_conversation(draft) do
2020-05-12 21:59:26 +02:00
in_reply_to_conversation = Participation.get(draft.params[:in_reply_to_conversation_id])
2019-09-24 11:10:54 +02:00
%__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
2020-05-12 21:59:26 +02:00
case CommonAPI.check_expiry_date(draft.params[:expires_in]) do
2019-09-24 11:10:54 +02:00
{: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}} ->
2020-10-02 19:00:50 +02:00
%__MODULE__{draft | extra: poll, emoji: Map.merge(draft.emoji, poll_emoji)}
2019-09-24 11:10:54 +02:00
{:error, message} ->
add_error(draft, message)
end
end
defp content(draft) do
2020-10-02 19:00:50 +02:00
{content_html, mentioned_users, tags} = Utils.make_content_html(draft)
2019-09-24 11:10:54 +02:00
2020-10-02 19:00:50 +02:00
mentions =
mentioned_users
2019-09-24 11:10:54 +02:00
|> Enum.map(fn {_, mentioned_user} -> mentioned_user.ap_id end)
2020-05-12 21:59:26 +02:00
|> Utils.get_addressed_users(draft.params[:to])
2019-09-24 11:10:54 +02:00
2020-10-02 19:00:50 +02:00
%__MODULE__{draft | content_html: content_html, mentions: mentions, tags: tags}
end
2019-09-24 11:10:54 +02:00
2020-10-02 19:00:50 +02:00
defp to_and_cc(draft) do
{to, cc} = Utils.get_to_and_cc(draft)
2019-09-24 11:10:54 +02:00
%__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
2020-05-12 21:59:26 +02:00
sensitive = draft.params[:sensitive] || Enum.member?(draft.tags, {"#nsfw", "nsfw"})
2019-09-24 11:10:54 +02:00
%__MODULE__{draft | sensitive: sensitive}
end
defp object(draft) do
emoji = Map.merge(Pleroma.Emoji.Formatter.get_emoji_map(draft.full_payload), draft.emoji)
object =
2020-10-02 19:00:50 +02:00
Utils.make_note_data(draft)
2019-09-24 11:10:54 +02:00
|> Map.put("emoji", emoji)
2020-06-26 05:39:35 +02:00
|> Map.put("source", draft.status)
2019-09-24 11:10:54 +02:00
%__MODULE__{draft | object: object}
end
defp preview?(draft) do
2020-05-12 21:59:26 +02:00
preview? = Pleroma.Web.ControllerHelper.truthy_param?(draft.params[:preview])
2019-09-24 11:10:54 +02:00
%__MODULE__{draft | preview?: preview?}
end
defp changes(draft) do
direct? = draft.visibility == "direct"
additional = %{"cc" => draft.cc, "directMessage" => direct?}
additional =
case draft.expires_at do
2020-08-22 19:46:01 +02:00
%DateTime{} = expires_at -> Map.put(additional, "expires_at", expires_at)
_ -> additional
end
2019-09-24 11:10:54 +02:00
changes =
%{
to: draft.to,
actor: draft.user,
context: draft.context,
object: draft.object,
additional: additional
2019-09-24 11:10:54 +02:00
}
|> Utils.maybe_add_list_data(draft.user, draft.visibility)
%__MODULE__{draft | changes: changes}
end
2019-09-23 13:52:41 +02:00
defp with_valid(%{valid?: true} = draft, func), do: func.(draft)
defp with_valid(draft, _func), do: draft
2019-09-24 11:10:54 +02:00
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