Merge branch 'refactor/common_api' into 'develop'
Refactor `CommonAPI.post/2` See merge request pleroma/pleroma!1713
This commit is contained in:
commit
219771288c
4 changed files with 397 additions and 267 deletions
219
lib/pleroma/web/common_api/activity_draft.ex
Normal file
219
lib/pleroma/web/common_api/activity_draft.ex
Normal file
|
@ -0,0 +1,219 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# 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,
|
||||||
|
poll: nil,
|
||||||
|
emoji: %{},
|
||||||
|
content_html: nil,
|
||||||
|
mentions: [],
|
||||||
|
tags: [],
|
||||||
|
to: [],
|
||||||
|
cc: [],
|
||||||
|
context: nil,
|
||||||
|
sensitive: false,
|
||||||
|
object: nil,
|
||||||
|
preview?: false,
|
||||||
|
changes: %{}
|
||||||
|
|
||||||
|
def create(user, params) do
|
||||||
|
%__MODULE__{user: user}
|
||||||
|
|> put_params(params)
|
||||||
|
|> status()
|
||||||
|
|> summary()
|
||||||
|
|> full_payload()
|
||||||
|
|> expires_at()
|
||||||
|
|> poll()
|
||||||
|
|> with_valid(&in_reply_to/1)
|
||||||
|
|> with_valid(&attachments/1)
|
||||||
|
|> with_valid(&in_reply_to_conversation/1)
|
||||||
|
|> with_valid(&visibility/1)
|
||||||
|
|> content()
|
||||||
|
|> with_valid(&to_and_cc/1)
|
||||||
|
|> with_valid(&context/1)
|
||||||
|
|> sensitive()
|
||||||
|
|> with_valid(&object/1)
|
||||||
|
|> preview?()
|
||||||
|
|> with_valid(&changes/1)
|
||||||
|
|> validate()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp put_params(draft, params) do
|
||||||
|
params = Map.put_new(params, "in_reply_to_status_id", params["in_reply_to_id"])
|
||||||
|
%__MODULE__{draft | params: params}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp status(%{params: %{"status" => status}} = draft) do
|
||||||
|
%__MODULE__{draft | status: String.trim(status)}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp summary(%{params: params} = draft) do
|
||||||
|
%__MODULE__{draft | summary: Map.get(params, "spoiler_text", "")}
|
||||||
|
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
|
||||||
|
|
||||||
|
defp in_reply_to(draft) do
|
||||||
|
case Map.get(draft.params, "in_reply_to_status_id") do
|
||||||
|
"" -> draft
|
||||||
|
nil -> draft
|
||||||
|
id -> %__MODULE__{draft | in_reply_to: Activity.get_by_id(id)}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp in_reply_to_conversation(draft) do
|
||||||
|
in_reply_to_conversation = Participation.get(draft.params["in_reply_to_conversation_id"])
|
||||||
|
%__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
|
||||||
|
case CommonAPI.check_expiry_date(draft.params["expires_in"]) do
|
||||||
|
{: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}} ->
|
||||||
|
%__MODULE__{draft | poll: poll, emoji: Map.merge(draft.emoji, poll_emoji)}
|
||||||
|
|
||||||
|
{:error, message} ->
|
||||||
|
add_error(draft, message)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp content(draft) do
|
||||||
|
{content_html, mentions, tags} =
|
||||||
|
Utils.make_content_html(
|
||||||
|
draft.status,
|
||||||
|
draft.attachments,
|
||||||
|
draft.params,
|
||||||
|
draft.visibility
|
||||||
|
)
|
||||||
|
|
||||||
|
%__MODULE__{draft | content_html: content_html, mentions: mentions, tags: tags}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp to_and_cc(draft) do
|
||||||
|
addressed_users =
|
||||||
|
draft.mentions
|
||||||
|
|> Enum.map(fn {_, mentioned_user} -> mentioned_user.ap_id end)
|
||||||
|
|> Utils.get_addressed_users(draft.params["to"])
|
||||||
|
|
||||||
|
{to, cc} =
|
||||||
|
Utils.get_to_and_cc(
|
||||||
|
draft.user,
|
||||||
|
addressed_users,
|
||||||
|
draft.in_reply_to,
|
||||||
|
draft.visibility,
|
||||||
|
draft.in_reply_to_conversation
|
||||||
|
)
|
||||||
|
|
||||||
|
%__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
|
||||||
|
sensitive = draft.params["sensitive"] || Enum.member?(draft.tags, {"#nsfw", "nsfw"})
|
||||||
|
%__MODULE__{draft | sensitive: sensitive}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp object(draft) do
|
||||||
|
emoji = Map.merge(Pleroma.Emoji.Formatter.get_emoji_map(draft.full_payload), draft.emoji)
|
||||||
|
|
||||||
|
object =
|
||||||
|
Utils.make_note_data(
|
||||||
|
draft.user.ap_id,
|
||||||
|
draft.to,
|
||||||
|
draft.context,
|
||||||
|
draft.content_html,
|
||||||
|
draft.attachments,
|
||||||
|
draft.in_reply_to,
|
||||||
|
draft.tags,
|
||||||
|
draft.summary,
|
||||||
|
draft.cc,
|
||||||
|
draft.sensitive,
|
||||||
|
draft.poll
|
||||||
|
)
|
||||||
|
|> Map.put("emoji", emoji)
|
||||||
|
|
||||||
|
%__MODULE__{draft | object: object}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp preview?(draft) do
|
||||||
|
preview? = Pleroma.Web.ControllerHelper.truthy_param?(draft.params["preview"]) || false
|
||||||
|
%__MODULE__{draft | preview?: preview?}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp changes(draft) do
|
||||||
|
direct? = draft.visibility == "direct"
|
||||||
|
|
||||||
|
changes =
|
||||||
|
%{
|
||||||
|
to: draft.to,
|
||||||
|
actor: draft.user,
|
||||||
|
context: draft.context,
|
||||||
|
object: draft.object,
|
||||||
|
additional: %{"cc" => draft.cc, "directMessage" => direct?}
|
||||||
|
}
|
||||||
|
|> Utils.maybe_add_list_data(draft.user, draft.visibility)
|
||||||
|
|
||||||
|
%__MODULE__{draft | changes: changes}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp with_valid(%{valid?: true} = draft, func), do: func.(draft)
|
||||||
|
defp with_valid(draft, _func), do: draft
|
||||||
|
|
||||||
|
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
|
|
@ -6,7 +6,6 @@ defmodule Pleroma.Web.CommonAPI do
|
||||||
alias Pleroma.Activity
|
alias Pleroma.Activity
|
||||||
alias Pleroma.ActivityExpiration
|
alias Pleroma.ActivityExpiration
|
||||||
alias Pleroma.Conversation.Participation
|
alias Pleroma.Conversation.Participation
|
||||||
alias Pleroma.Emoji
|
|
||||||
alias Pleroma.Object
|
alias Pleroma.Object
|
||||||
alias Pleroma.ThreadMute
|
alias Pleroma.ThreadMute
|
||||||
alias Pleroma.User
|
alias Pleroma.User
|
||||||
|
@ -18,14 +17,11 @@ defmodule Pleroma.Web.CommonAPI do
|
||||||
import Pleroma.Web.CommonAPI.Utils
|
import Pleroma.Web.CommonAPI.Utils
|
||||||
|
|
||||||
def follow(follower, followed) do
|
def follow(follower, followed) do
|
||||||
|
timeout = Pleroma.Config.get([:activitypub, :follow_handshake_timeout])
|
||||||
|
|
||||||
with {:ok, follower} <- User.maybe_direct_follow(follower, followed),
|
with {:ok, follower} <- User.maybe_direct_follow(follower, followed),
|
||||||
{:ok, activity} <- ActivityPub.follow(follower, followed),
|
{:ok, activity} <- ActivityPub.follow(follower, followed),
|
||||||
{:ok, follower, followed} <-
|
{:ok, follower, followed} <- User.wait_and_refresh(timeout, follower, followed) do
|
||||||
User.wait_and_refresh(
|
|
||||||
Pleroma.Config.get([:activitypub, :follow_handshake_timeout]),
|
|
||||||
follower,
|
|
||||||
followed
|
|
||||||
) do
|
|
||||||
{:ok, follower, followed, activity}
|
{:ok, follower, followed, activity}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -76,8 +72,7 @@ defmodule Pleroma.Web.CommonAPI do
|
||||||
{:ok, delete} <- ActivityPub.delete(object) do
|
{:ok, delete} <- ActivityPub.delete(object) do
|
||||||
{:ok, delete}
|
{:ok, delete}
|
||||||
else
|
else
|
||||||
_ ->
|
_ -> {:error, dgettext("errors", "Could not delete")}
|
||||||
{:error, dgettext("errors", "Could not delete")}
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -87,18 +82,16 @@ defmodule Pleroma.Web.CommonAPI do
|
||||||
nil <- Utils.get_existing_announce(user.ap_id, object) do
|
nil <- Utils.get_existing_announce(user.ap_id, object) do
|
||||||
ActivityPub.announce(user, object)
|
ActivityPub.announce(user, object)
|
||||||
else
|
else
|
||||||
_ ->
|
_ -> {:error, dgettext("errors", "Could not repeat")}
|
||||||
{:error, dgettext("errors", "Could not repeat")}
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def unrepeat(id_or_ap_id, user) do
|
def unrepeat(id_or_ap_id, user) do
|
||||||
with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
|
with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id) do
|
||||||
object <- Object.normalize(activity) do
|
object = Object.normalize(activity)
|
||||||
ActivityPub.unannounce(user, object)
|
ActivityPub.unannounce(user, object)
|
||||||
else
|
else
|
||||||
_ ->
|
_ -> {:error, dgettext("errors", "Could not unrepeat")}
|
||||||
{:error, dgettext("errors", "Could not unrepeat")}
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -108,30 +101,23 @@ defmodule Pleroma.Web.CommonAPI do
|
||||||
nil <- Utils.get_existing_like(user.ap_id, object) do
|
nil <- Utils.get_existing_like(user.ap_id, object) do
|
||||||
ActivityPub.like(user, object)
|
ActivityPub.like(user, object)
|
||||||
else
|
else
|
||||||
_ ->
|
_ -> {:error, dgettext("errors", "Could not favorite")}
|
||||||
{:error, dgettext("errors", "Could not favorite")}
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def unfavorite(id_or_ap_id, user) do
|
def unfavorite(id_or_ap_id, user) do
|
||||||
with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
|
with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id) do
|
||||||
object <- Object.normalize(activity) do
|
object = Object.normalize(activity)
|
||||||
ActivityPub.unlike(user, object)
|
ActivityPub.unlike(user, object)
|
||||||
else
|
else
|
||||||
_ ->
|
_ -> {:error, dgettext("errors", "Could not unfavorite")}
|
||||||
{:error, dgettext("errors", "Could not unfavorite")}
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def vote(user, object, choices) do
|
def vote(user, %{data: %{"type" => "Question"}} = object, choices) do
|
||||||
with "Question" <- object.data["type"],
|
with :ok <- validate_not_author(object, user),
|
||||||
{:author, false} <- {:author, object.data["actor"] == user.ap_id},
|
:ok <- validate_existing_votes(user, object),
|
||||||
{:existing_votes, []} <- {:existing_votes, Utils.get_existing_votes(user.ap_id, object)},
|
{:ok, options, choices} <- normalize_and_validate_choices(choices, object) do
|
||||||
{options, max_count} <- get_options_and_max_count(object),
|
|
||||||
option_count <- Enum.count(options),
|
|
||||||
{:choice_check, {choices, true}} <-
|
|
||||||
{:choice_check, normalize_and_validate_choice_indices(choices, option_count)},
|
|
||||||
{:count_check, true} <- {:count_check, Enum.count(choices) <= max_count} do
|
|
||||||
answer_activities =
|
answer_activities =
|
||||||
Enum.map(choices, fn index ->
|
Enum.map(choices, fn index ->
|
||||||
answer_data = make_answer_data(user, object, Enum.at(options, index)["name"])
|
answer_data = make_answer_data(user, object, Enum.at(options, index)["name"])
|
||||||
|
@ -150,32 +136,40 @@ defmodule Pleroma.Web.CommonAPI do
|
||||||
|
|
||||||
object = Object.get_cached_by_ap_id(object.data["id"])
|
object = Object.get_cached_by_ap_id(object.data["id"])
|
||||||
{:ok, answer_activities, object}
|
{:ok, answer_activities, object}
|
||||||
else
|
|
||||||
{:author, _} -> {:error, dgettext("errors", "Poll's author can't vote")}
|
|
||||||
{:existing_votes, _} -> {:error, dgettext("errors", "Already voted")}
|
|
||||||
{:choice_check, {_, false}} -> {:error, dgettext("errors", "Invalid indices")}
|
|
||||||
{:count_check, false} -> {:error, dgettext("errors", "Too many choices")}
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp get_options_and_max_count(object) do
|
defp validate_not_author(%{data: %{"actor" => ap_id}}, %{ap_id: ap_id}),
|
||||||
if Map.has_key?(object.data, "anyOf") do
|
do: {:error, dgettext("errors", "Poll's author can't vote")}
|
||||||
{object.data["anyOf"], Enum.count(object.data["anyOf"])}
|
|
||||||
|
defp validate_not_author(_, _), do: :ok
|
||||||
|
|
||||||
|
defp validate_existing_votes(%{ap_id: ap_id}, object) do
|
||||||
|
if Utils.get_existing_votes(ap_id, object) == [] do
|
||||||
|
:ok
|
||||||
else
|
else
|
||||||
{object.data["oneOf"], 1}
|
{:error, dgettext("errors", "Already voted")}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp normalize_and_validate_choice_indices(choices, count) do
|
defp get_options_and_max_count(%{data: %{"anyOf" => any_of}}), do: {any_of, Enum.count(any_of)}
|
||||||
Enum.map_reduce(choices, true, fn index, valid ->
|
defp get_options_and_max_count(%{data: %{"oneOf" => one_of}}), do: {one_of, 1}
|
||||||
index = if is_binary(index), do: String.to_integer(index), else: index
|
|
||||||
{index, if(valid, do: index < count, else: valid)}
|
defp normalize_and_validate_choices(choices, object) do
|
||||||
end)
|
choices = Enum.map(choices, fn i -> if is_binary(i), do: String.to_integer(i), else: i end)
|
||||||
|
{options, max_count} = get_options_and_max_count(object)
|
||||||
|
count = Enum.count(options)
|
||||||
|
|
||||||
|
with {_, true} <- {:valid_choice, Enum.all?(choices, &(&1 < count))},
|
||||||
|
{_, true} <- {:count_check, Enum.count(choices) <= max_count} do
|
||||||
|
{:ok, options, choices}
|
||||||
|
else
|
||||||
|
{:valid_choice, _} -> {:error, dgettext("errors", "Invalid indices")}
|
||||||
|
{:count_check, _} -> {:error, dgettext("errors", "Too many choices")}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_visibility(_, _, %Participation{}) do
|
def get_visibility(_, _, %Participation{}), do: {"direct", "direct"}
|
||||||
{"direct", "direct"}
|
|
||||||
end
|
|
||||||
|
|
||||||
def get_visibility(%{"visibility" => visibility}, in_reply_to, _)
|
def get_visibility(%{"visibility" => visibility}, in_reply_to, _)
|
||||||
when visibility in ~w{public unlisted private direct},
|
when visibility in ~w{public unlisted private direct},
|
||||||
|
@ -197,13 +191,13 @@ defmodule Pleroma.Web.CommonAPI do
|
||||||
|
|
||||||
def get_replied_to_visibility(activity) do
|
def get_replied_to_visibility(activity) do
|
||||||
with %Object{} = object <- Object.normalize(activity) do
|
with %Object{} = object <- Object.normalize(activity) do
|
||||||
Pleroma.Web.ActivityPub.Visibility.get_visibility(object)
|
Visibility.get_visibility(object)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp check_expiry_date({:ok, nil} = res), do: res
|
def check_expiry_date({:ok, nil} = res), do: res
|
||||||
|
|
||||||
defp check_expiry_date({:ok, in_seconds}) do
|
def check_expiry_date({:ok, in_seconds}) do
|
||||||
expiry = NaiveDateTime.utc_now() |> NaiveDateTime.add(in_seconds)
|
expiry = NaiveDateTime.utc_now() |> NaiveDateTime.add(in_seconds)
|
||||||
|
|
||||||
if ActivityExpiration.expires_late_enough?(expiry) do
|
if ActivityExpiration.expires_late_enough?(expiry) do
|
||||||
|
@ -213,107 +207,36 @@ defmodule Pleroma.Web.CommonAPI do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp check_expiry_date(expiry_str) do
|
def check_expiry_date(expiry_str) do
|
||||||
Ecto.Type.cast(:integer, expiry_str)
|
Ecto.Type.cast(:integer, expiry_str)
|
||||||
|> check_expiry_date()
|
|> check_expiry_date()
|
||||||
end
|
end
|
||||||
|
|
||||||
def post(user, %{"status" => status} = data) do
|
def post(user, %{"status" => _} = data) do
|
||||||
limit = Pleroma.Config.get([:instance, :limit])
|
with {:ok, draft} <- Pleroma.Web.CommonAPI.ActivityDraft.create(user, data) do
|
||||||
|
draft.changes
|
||||||
with status <- String.trim(status),
|
|> ActivityPub.create(draft.preview?)
|
||||||
attachments <- attachments_from_ids(data),
|
|> maybe_create_activity_expiration(draft.expires_at)
|
||||||
in_reply_to <- get_replied_to_activity(data["in_reply_to_status_id"]),
|
|
||||||
in_reply_to_conversation <- Participation.get(data["in_reply_to_conversation_id"]),
|
|
||||||
{visibility, in_reply_to_visibility} <-
|
|
||||||
get_visibility(data, in_reply_to, in_reply_to_conversation),
|
|
||||||
{_, false} <-
|
|
||||||
{:private_to_public, in_reply_to_visibility == "direct" && visibility != "direct"},
|
|
||||||
{content_html, mentions, tags} <-
|
|
||||||
make_content_html(
|
|
||||||
status,
|
|
||||||
attachments,
|
|
||||||
data,
|
|
||||||
visibility
|
|
||||||
),
|
|
||||||
mentioned_users <- for({_, mentioned_user} <- mentions, do: mentioned_user.ap_id),
|
|
||||||
addressed_users <- get_addressed_users(mentioned_users, data["to"]),
|
|
||||||
{poll, poll_emoji} <- make_poll_data(data),
|
|
||||||
{to, cc} <-
|
|
||||||
get_to_and_cc(user, addressed_users, in_reply_to, visibility, in_reply_to_conversation),
|
|
||||||
context <- make_context(in_reply_to, in_reply_to_conversation),
|
|
||||||
cw <- data["spoiler_text"] || "",
|
|
||||||
sensitive <- data["sensitive"] || Enum.member?(tags, {"#nsfw", "nsfw"}),
|
|
||||||
{:ok, expires_at} <- check_expiry_date(data["expires_in"]),
|
|
||||||
full_payload <- String.trim(status <> cw),
|
|
||||||
:ok <- validate_character_limit(full_payload, attachments, limit),
|
|
||||||
object <-
|
|
||||||
make_note_data(
|
|
||||||
user.ap_id,
|
|
||||||
to,
|
|
||||||
context,
|
|
||||||
content_html,
|
|
||||||
attachments,
|
|
||||||
in_reply_to,
|
|
||||||
tags,
|
|
||||||
cw,
|
|
||||||
cc,
|
|
||||||
sensitive,
|
|
||||||
poll
|
|
||||||
),
|
|
||||||
object <- put_emoji(object, full_payload, poll_emoji) do
|
|
||||||
preview? = Pleroma.Web.ControllerHelper.truthy_param?(data["preview"]) || false
|
|
||||||
direct? = visibility == "direct"
|
|
||||||
|
|
||||||
result =
|
|
||||||
%{
|
|
||||||
to: to,
|
|
||||||
actor: user,
|
|
||||||
context: context,
|
|
||||||
object: object,
|
|
||||||
additional: %{"cc" => cc, "directMessage" => direct?}
|
|
||||||
}
|
|
||||||
|> maybe_add_list_data(user, visibility)
|
|
||||||
|> ActivityPub.create(preview?)
|
|
||||||
|
|
||||||
if expires_at do
|
|
||||||
with {:ok, activity} <- result do
|
|
||||||
{:ok, _} = ActivityExpiration.create(activity, expires_at)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
result
|
|
||||||
else
|
|
||||||
{:private_to_public, true} ->
|
|
||||||
{:error, dgettext("errors", "The message visibility must be direct")}
|
|
||||||
|
|
||||||
{:error, _} = e ->
|
|
||||||
e
|
|
||||||
|
|
||||||
e ->
|
|
||||||
{:error, e}
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# parse and put emoji to object data
|
defp maybe_create_activity_expiration({:ok, activity}, %NaiveDateTime{} = expires_at) do
|
||||||
defp put_emoji(map, text, emojis) do
|
with {:ok, _} <- ActivityExpiration.create(activity, expires_at) do
|
||||||
Map.put(
|
{:ok, activity}
|
||||||
map,
|
end
|
||||||
"emoji",
|
|
||||||
Map.merge(Emoji.Formatter.get_emoji_map(text), emojis)
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp maybe_create_activity_expiration(result, _), do: result
|
||||||
|
|
||||||
# Updates the emojis for a user based on their profile
|
# Updates the emojis for a user based on their profile
|
||||||
def update(user) do
|
def update(user) do
|
||||||
emoji = emoji_from_profile(user)
|
emoji = emoji_from_profile(user)
|
||||||
source_data = user.info |> Map.get(:source_data, {}) |> Map.put("tag", emoji)
|
source_data = user.info |> Map.get(:source_data, %{}) |> Map.put("tag", emoji)
|
||||||
|
|
||||||
user =
|
user =
|
||||||
with {:ok, user} <- User.update_info(user, &User.Info.set_source_data(&1, source_data)) do
|
case User.update_info(user, &User.Info.set_source_data(&1, source_data)) do
|
||||||
user
|
{:ok, user} -> user
|
||||||
else
|
_ -> user
|
||||||
_e -> user
|
|
||||||
end
|
end
|
||||||
|
|
||||||
ActivityPub.update(%{
|
ActivityPub.update(%{
|
||||||
|
@ -328,14 +251,8 @@ defmodule Pleroma.Web.CommonAPI do
|
||||||
def pin(id_or_ap_id, %{ap_id: user_ap_id} = user) do
|
def pin(id_or_ap_id, %{ap_id: user_ap_id} = user) do
|
||||||
with %Activity{
|
with %Activity{
|
||||||
actor: ^user_ap_id,
|
actor: ^user_ap_id,
|
||||||
data: %{
|
data: %{"type" => "Create"},
|
||||||
"type" => "Create"
|
object: %Object{data: %{"type" => "Note"}}
|
||||||
},
|
|
||||||
object: %Object{
|
|
||||||
data: %{
|
|
||||||
"type" => "Note"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} = activity <- get_by_id_or_ap_id(id_or_ap_id),
|
} = activity <- get_by_id_or_ap_id(id_or_ap_id),
|
||||||
true <- Visibility.is_public?(activity),
|
true <- Visibility.is_public?(activity),
|
||||||
{:ok, _user} <- User.update_info(user, &User.Info.add_pinnned_activity(&1, activity)) do
|
{:ok, _user} <- User.update_info(user, &User.Info.add_pinnned_activity(&1, activity)) do
|
||||||
|
@ -372,51 +289,46 @@ defmodule Pleroma.Web.CommonAPI do
|
||||||
def thread_muted?(%{id: nil} = _user, _activity), do: false
|
def thread_muted?(%{id: nil} = _user, _activity), do: false
|
||||||
|
|
||||||
def thread_muted?(user, activity) do
|
def thread_muted?(user, activity) do
|
||||||
with [] <- ThreadMute.check_muted(user.id, activity.data["context"]) do
|
ThreadMute.check_muted(user.id, activity.data["context"]) != []
|
||||||
false
|
end
|
||||||
else
|
|
||||||
_ -> true
|
def report(user, %{"account_id" => account_id} = data) do
|
||||||
|
with {:ok, account} <- get_reported_account(account_id),
|
||||||
|
{:ok, {content_html, _, _}} <- make_report_content_html(data["comment"]),
|
||||||
|
{:ok, statuses} <- get_report_statuses(account, data) do
|
||||||
|
ActivityPub.flag(%{
|
||||||
|
context: Utils.generate_context_id(),
|
||||||
|
actor: user,
|
||||||
|
account: account,
|
||||||
|
statuses: statuses,
|
||||||
|
content: content_html,
|
||||||
|
forward: data["forward"] || false
|
||||||
|
})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def report(user, data) do
|
def report(_user, _params), do: {:error, dgettext("errors", "Valid `account_id` required")}
|
||||||
with {:account_id, %{"account_id" => account_id}} <- {:account_id, data},
|
|
||||||
{:account, %User{} = account} <- {:account, User.get_cached_by_id(account_id)},
|
defp get_reported_account(account_id) do
|
||||||
{:ok, {content_html, _, _}} <- make_report_content_html(data["comment"]),
|
case User.get_cached_by_id(account_id) do
|
||||||
{:ok, statuses} <- get_report_statuses(account, data),
|
%User{} = account -> {:ok, account}
|
||||||
{:ok, activity} <-
|
_ -> {:error, dgettext("errors", "Account not found")}
|
||||||
ActivityPub.flag(%{
|
|
||||||
context: Utils.generate_context_id(),
|
|
||||||
actor: user,
|
|
||||||
account: account,
|
|
||||||
statuses: statuses,
|
|
||||||
content: content_html,
|
|
||||||
forward: data["forward"] || false
|
|
||||||
}) do
|
|
||||||
{:ok, activity}
|
|
||||||
else
|
|
||||||
{:error, err} -> {:error, err}
|
|
||||||
{:account_id, %{}} -> {:error, dgettext("errors", "Valid `account_id` required")}
|
|
||||||
{:account, nil} -> {:error, dgettext("errors", "Account not found")}
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_report_state(activity_id, state) do
|
def update_report_state(activity_id, state) do
|
||||||
with %Activity{} = activity <- Activity.get_by_id(activity_id),
|
with %Activity{} = activity <- Activity.get_by_id(activity_id) do
|
||||||
{:ok, activity} <- Utils.update_report_state(activity, state) do
|
Utils.update_report_state(activity, state)
|
||||||
{:ok, activity}
|
|
||||||
else
|
else
|
||||||
nil -> {:error, :not_found}
|
nil -> {:error, :not_found}
|
||||||
{:error, reason} -> {:error, reason}
|
|
||||||
_ -> {:error, dgettext("errors", "Could not update state")}
|
_ -> {:error, dgettext("errors", "Could not update state")}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_activity_scope(activity_id, opts \\ %{}) do
|
def update_activity_scope(activity_id, opts \\ %{}) do
|
||||||
with %Activity{} = activity <- Activity.get_by_id_with_object(activity_id),
|
with %Activity{} = activity <- Activity.get_by_id_with_object(activity_id),
|
||||||
{:ok, activity} <- toggle_sensitive(activity, opts),
|
{:ok, activity} <- toggle_sensitive(activity, opts) do
|
||||||
{:ok, activity} <- set_visibility(activity, opts) do
|
set_visibility(activity, opts)
|
||||||
{:ok, activity}
|
|
||||||
else
|
else
|
||||||
nil -> {:error, :not_found}
|
nil -> {:error, :not_found}
|
||||||
{:error, reason} -> {:error, reason}
|
{:error, reason} -> {:error, reason}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
defmodule Pleroma.Web.CommonAPI.Utils do
|
defmodule Pleroma.Web.CommonAPI.Utils do
|
||||||
import Pleroma.Web.Gettext
|
import Pleroma.Web.Gettext
|
||||||
|
import Pleroma.Web.ControllerHelper, only: [truthy_param?: 1]
|
||||||
|
|
||||||
alias Calendar.Strftime
|
alias Calendar.Strftime
|
||||||
alias Pleroma.Activity
|
alias Pleroma.Activity
|
||||||
|
@ -41,14 +42,6 @@ defmodule Pleroma.Web.CommonAPI.Utils do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_replied_to_activity(""), do: nil
|
|
||||||
|
|
||||||
def get_replied_to_activity(id) when not is_nil(id) do
|
|
||||||
Activity.get_by_id(id)
|
|
||||||
end
|
|
||||||
|
|
||||||
def get_replied_to_activity(_), do: nil
|
|
||||||
|
|
||||||
def attachments_from_ids(%{"media_ids" => ids, "descriptions" => desc} = _) do
|
def attachments_from_ids(%{"media_ids" => ids, "descriptions" => desc} = _) do
|
||||||
attachments_from_ids_descs(ids, desc)
|
attachments_from_ids_descs(ids, desc)
|
||||||
end
|
end
|
||||||
|
@ -159,70 +152,74 @@ defmodule Pleroma.Web.CommonAPI.Utils do
|
||||||
|
|
||||||
def maybe_add_list_data(activity_params, _, _), do: activity_params
|
def maybe_add_list_data(activity_params, _, _), do: activity_params
|
||||||
|
|
||||||
|
def make_poll_data(%{"poll" => %{"expires_in" => expires_in}} = data)
|
||||||
|
when is_binary(expires_in) do
|
||||||
|
# In some cases mastofe sends out strings instead of integers
|
||||||
|
data
|
||||||
|
|> put_in(["poll", "expires_in"], String.to_integer(expires_in))
|
||||||
|
|> make_poll_data()
|
||||||
|
end
|
||||||
|
|
||||||
def make_poll_data(%{"poll" => %{"options" => options, "expires_in" => expires_in}} = data)
|
def make_poll_data(%{"poll" => %{"options" => options, "expires_in" => expires_in}} = data)
|
||||||
when is_list(options) do
|
when is_list(options) do
|
||||||
%{max_expiration: max_expiration, min_expiration: min_expiration} =
|
limits = Pleroma.Config.get([:instance, :poll_limits])
|
||||||
limits = Pleroma.Config.get([:instance, :poll_limits])
|
|
||||||
|
|
||||||
# XXX: There is probably a cleaner way of doing this
|
with :ok <- validate_poll_expiration(expires_in, limits),
|
||||||
try do
|
:ok <- validate_poll_options_amount(options, limits),
|
||||||
# In some cases mastofe sends out strings instead of integers
|
:ok <- validate_poll_options_length(options, limits) do
|
||||||
expires_in = if is_binary(expires_in), do: String.to_integer(expires_in), else: expires_in
|
{option_notes, emoji} =
|
||||||
|
|
||||||
if Enum.count(options) > limits.max_options do
|
|
||||||
raise ArgumentError, message: "Poll can't contain more than #{limits.max_options} options"
|
|
||||||
end
|
|
||||||
|
|
||||||
{poll, emoji} =
|
|
||||||
Enum.map_reduce(options, %{}, fn option, emoji ->
|
Enum.map_reduce(options, %{}, fn option, emoji ->
|
||||||
if String.length(option) > limits.max_option_chars do
|
note = %{
|
||||||
raise ArgumentError,
|
"name" => option,
|
||||||
message:
|
"type" => "Note",
|
||||||
"Poll options cannot be longer than #{limits.max_option_chars} characters each"
|
"replies" => %{"type" => "Collection", "totalItems" => 0}
|
||||||
end
|
}
|
||||||
|
|
||||||
{%{
|
{note, Map.merge(emoji, Emoji.Formatter.get_emoji_map(option))}
|
||||||
"name" => option,
|
|
||||||
"type" => "Note",
|
|
||||||
"replies" => %{"type" => "Collection", "totalItems" => 0}
|
|
||||||
}, Map.merge(emoji, Emoji.Formatter.get_emoji_map(option))}
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
case expires_in do
|
|
||||||
expires_in when expires_in > max_expiration ->
|
|
||||||
raise ArgumentError, message: "Expiration date is too far in the future"
|
|
||||||
|
|
||||||
expires_in when expires_in < min_expiration ->
|
|
||||||
raise ArgumentError, message: "Expiration date is too soon"
|
|
||||||
|
|
||||||
_ ->
|
|
||||||
:noop
|
|
||||||
end
|
|
||||||
|
|
||||||
end_time =
|
end_time =
|
||||||
NaiveDateTime.utc_now()
|
NaiveDateTime.utc_now()
|
||||||
|> NaiveDateTime.add(expires_in)
|
|> NaiveDateTime.add(expires_in)
|
||||||
|> NaiveDateTime.to_iso8601()
|
|> NaiveDateTime.to_iso8601()
|
||||||
|
|
||||||
poll =
|
key = if truthy_param?(data["poll"]["multiple"]), do: "anyOf", else: "oneOf"
|
||||||
if Pleroma.Web.ControllerHelper.truthy_param?(data["poll"]["multiple"]) do
|
poll = %{"type" => "Question", key => option_notes, "closed" => end_time}
|
||||||
%{"type" => "Question", "anyOf" => poll, "closed" => end_time}
|
|
||||||
else
|
|
||||||
%{"type" => "Question", "oneOf" => poll, "closed" => end_time}
|
|
||||||
end
|
|
||||||
|
|
||||||
{poll, emoji}
|
{:ok, {poll, emoji}}
|
||||||
rescue
|
|
||||||
e in ArgumentError -> e.message
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def make_poll_data(%{"poll" => poll}) when is_map(poll) do
|
def make_poll_data(%{"poll" => poll}) when is_map(poll) do
|
||||||
"Invalid poll"
|
{:error, "Invalid poll"}
|
||||||
end
|
end
|
||||||
|
|
||||||
def make_poll_data(_data) do
|
def make_poll_data(_data) do
|
||||||
{%{}, %{}}
|
{:ok, {%{}, %{}}}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp validate_poll_options_amount(options, %{max_options: max_options}) do
|
||||||
|
if Enum.count(options) > max_options do
|
||||||
|
{:error, "Poll can't contain more than #{max_options} options"}
|
||||||
|
else
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp validate_poll_options_length(options, %{max_option_chars: max_option_chars}) do
|
||||||
|
if Enum.any?(options, &(String.length(&1) > max_option_chars)) do
|
||||||
|
{:error, "Poll options cannot be longer than #{max_option_chars} characters each"}
|
||||||
|
else
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp validate_poll_expiration(expires_in, %{min_expiration: min, max_expiration: max}) do
|
||||||
|
cond do
|
||||||
|
expires_in > max -> {:error, "Expiration date is too far in the future"}
|
||||||
|
expires_in < min -> {:error, "Expiration date is too soon"}
|
||||||
|
true -> :ok
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def make_content_html(
|
def make_content_html(
|
||||||
|
@ -234,7 +231,7 @@ defmodule Pleroma.Web.CommonAPI.Utils do
|
||||||
no_attachment_links =
|
no_attachment_links =
|
||||||
data
|
data
|
||||||
|> Map.get("no_attachment_links", Config.get([:instance, :no_attachment_links]))
|
|> Map.get("no_attachment_links", Config.get([:instance, :no_attachment_links]))
|
||||||
|> Kernel.in([true, "true"])
|
|> truthy_param?()
|
||||||
|
|
||||||
content_type = get_content_type(data["content_type"])
|
content_type = get_content_type(data["content_type"])
|
||||||
|
|
||||||
|
@ -347,25 +344,25 @@ defmodule Pleroma.Web.CommonAPI.Utils do
|
||||||
attachments,
|
attachments,
|
||||||
in_reply_to,
|
in_reply_to,
|
||||||
tags,
|
tags,
|
||||||
cw \\ nil,
|
summary \\ nil,
|
||||||
cc \\ [],
|
cc \\ [],
|
||||||
sensitive \\ false,
|
sensitive \\ false,
|
||||||
merge \\ %{}
|
extra_params \\ %{}
|
||||||
) do
|
) do
|
||||||
%{
|
%{
|
||||||
"type" => "Note",
|
"type" => "Note",
|
||||||
"to" => to,
|
"to" => to,
|
||||||
"cc" => cc,
|
"cc" => cc,
|
||||||
"content" => content_html,
|
"content" => content_html,
|
||||||
"summary" => cw,
|
"summary" => summary,
|
||||||
"sensitive" => !Enum.member?(["false", "False", "0", false], sensitive),
|
"sensitive" => truthy_param?(sensitive),
|
||||||
"context" => context,
|
"context" => context,
|
||||||
"attachment" => attachments,
|
"attachment" => attachments,
|
||||||
"actor" => actor,
|
"actor" => actor,
|
||||||
"tag" => Keyword.values(tags) |> Enum.uniq()
|
"tag" => Keyword.values(tags) |> Enum.uniq()
|
||||||
}
|
}
|
||||||
|> add_in_reply_to(in_reply_to)
|
|> add_in_reply_to(in_reply_to)
|
||||||
|> Map.merge(merge)
|
|> Map.merge(extra_params)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp add_in_reply_to(object, nil), do: object
|
defp add_in_reply_to(object, nil), do: object
|
||||||
|
@ -434,12 +431,14 @@ defmodule Pleroma.Web.CommonAPI.Utils do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def emoji_from_profile(%{info: _info} = user) do
|
def emoji_from_profile(%User{bio: bio, name: name}) do
|
||||||
(Emoji.Formatter.get_emoji(user.bio) ++ Emoji.Formatter.get_emoji(user.name))
|
[bio, name]
|
||||||
|> Enum.map(fn {shortcode, %Emoji{file: url}} ->
|
|> Enum.map(&Emoji.Formatter.get_emoji/1)
|
||||||
|
|> Enum.concat()
|
||||||
|
|> Enum.map(fn {shortcode, %Emoji{file: path}} ->
|
||||||
%{
|
%{
|
||||||
"type" => "Emoji",
|
"type" => "Emoji",
|
||||||
"icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}#{url}"},
|
"icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}#{path}"},
|
||||||
"name" => ":#{shortcode}:"
|
"name" => ":#{shortcode}:"
|
||||||
}
|
}
|
||||||
end)
|
end)
|
||||||
|
@ -571,15 +570,16 @@ defmodule Pleroma.Web.CommonAPI.Utils do
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_character_limit(full_payload, attachments, limit) do
|
def validate_character_limit("" = _full_payload, [] = _attachments) do
|
||||||
|
{:error, dgettext("errors", "Cannot post an empty status without attachments")}
|
||||||
|
end
|
||||||
|
|
||||||
|
def validate_character_limit(full_payload, _attachments) do
|
||||||
|
limit = Pleroma.Config.get([:instance, :limit])
|
||||||
length = String.length(full_payload)
|
length = String.length(full_payload)
|
||||||
|
|
||||||
if length < limit do
|
if length < limit do
|
||||||
if length > 0 or Enum.count(attachments) > 0 do
|
:ok
|
||||||
:ok
|
|
||||||
else
|
|
||||||
{:error, dgettext("errors", "Cannot post an empty status without attachments")}
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
{:error, dgettext("errors", "The status is over the character limit")}
|
{:error, dgettext("errors", "The status is over the character limit")}
|
||||||
end
|
end
|
||||||
|
|
|
@ -518,14 +518,11 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def post_status(%{assigns: %{user: user}} = conn, %{"status" => _} = params) do
|
def post_status(
|
||||||
params =
|
%{assigns: %{user: user}} = conn,
|
||||||
params
|
%{"status" => _, "scheduled_at" => scheduled_at} = params
|
||||||
|> Map.put("in_reply_to_status_id", params["in_reply_to_id"])
|
) do
|
||||||
|
if ScheduledActivity.far_enough?(scheduled_at) do
|
||||||
scheduled_at = params["scheduled_at"]
|
|
||||||
|
|
||||||
if scheduled_at && ScheduledActivity.far_enough?(scheduled_at) do
|
|
||||||
with {:ok, scheduled_activity} <-
|
with {:ok, scheduled_activity} <-
|
||||||
ScheduledActivity.create(user, %{"params" => params, "scheduled_at" => scheduled_at}) do
|
ScheduledActivity.create(user, %{"params" => params, "scheduled_at" => scheduled_at}) do
|
||||||
conn
|
conn
|
||||||
|
@ -533,24 +530,26 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
|
||||||
|> render("show.json", %{scheduled_activity: scheduled_activity})
|
|> render("show.json", %{scheduled_activity: scheduled_activity})
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
params = Map.drop(params, ["scheduled_at"])
|
post_status(conn, Map.drop(params, ["scheduled_at"]))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
case CommonAPI.post(user, params) do
|
def post_status(%{assigns: %{user: user}} = conn, %{"status" => _} = params) do
|
||||||
{:error, message} ->
|
case CommonAPI.post(user, params) do
|
||||||
conn
|
{:ok, activity} ->
|
||||||
|> put_status(:unprocessable_entity)
|
conn
|
||||||
|> json(%{error: message})
|
|> put_view(StatusView)
|
||||||
|
|> try_render("status.json", %{
|
||||||
|
activity: activity,
|
||||||
|
for: user,
|
||||||
|
as: :activity,
|
||||||
|
with_direct_conversation_id: true
|
||||||
|
})
|
||||||
|
|
||||||
{:ok, activity} ->
|
{:error, message} ->
|
||||||
conn
|
conn
|
||||||
|> put_view(StatusView)
|
|> put_status(:unprocessable_entity)
|
||||||
|> try_render("status.json", %{
|
|> json(%{error: message})
|
||||||
activity: activity,
|
|
||||||
for: user,
|
|
||||||
as: :activity,
|
|
||||||
with_direct_conversation_id: true
|
|
||||||
})
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue