X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fweb%2Fcommon_api%2Futils.ex;h=22594be4605f24fb81b3166f1b002e9aa1df4c15;hb=063cc61fc11cd80981d0e23e6d0dfa778cc91bbf;hp=7c49c1fb1748ec9b1557d82663dd9be76b9fdf3b;hpb=4f79bbbc31c10c1d55c9fee4002f36ef16b95dbf;p=akkoma
diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex
index 7c49c1fb1..22594be46 100644
--- a/lib/pleroma/web/common_api/utils.ex
+++ b/lib/pleroma/web/common_api/utils.ex
@@ -1,10 +1,9 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors
+# Copyright © 2017-2021 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.CommonAPI.Utils do
import Pleroma.Web.Gettext
- import Pleroma.Web.ControllerHelper, only: [truthy_param?: 1]
alias Calendar.Strftime
alias Pleroma.Activity
@@ -18,7 +17,7 @@ defmodule Pleroma.Web.CommonAPI.Utils do
alias Pleroma.Web.ActivityPub.Visibility
alias Pleroma.Web.CommonAPI.ActivityDraft
alias Pleroma.Web.MediaProxy
- alias Pleroma.Web.Plugs.AuthenticationPlug
+ alias Pleroma.Web.Utils.Params
require Logger
require Pleroma.Constants
@@ -37,7 +36,7 @@ defmodule Pleroma.Web.CommonAPI.Utils do
def attachments_from_ids_no_descs(ids) do
Enum.map(ids, fn media_id ->
- case Repo.get(Object, media_id) do
+ case get_attachment(media_id) do
%Object{data: data} -> data
_ -> nil
end
@@ -51,13 +50,17 @@ defmodule Pleroma.Web.CommonAPI.Utils do
{_, descs} = Jason.decode(descs_str)
Enum.map(ids, fn media_id ->
- with %Object{data: data} <- Repo.get(Object, media_id) do
+ with %Object{data: data} <- get_attachment(media_id) do
Map.put(data, "name", descs[media_id])
end
end)
|> Enum.reject(&is_nil/1)
end
+ defp get_attachment(media_id) do
+ Repo.get(Object, media_id)
+ end
+
@spec get_to_and_cc(ActivityDraft.t()) :: {list(String.t()), list(String.t())}
def get_to_and_cc(%{in_reply_to_conversation: %Participation{} = participation}) do
@@ -65,8 +68,13 @@ defmodule Pleroma.Web.CommonAPI.Utils do
{Enum.map(participation.recipients, & &1.ap_id), []}
end
- def get_to_and_cc(%{visibility: "public"} = draft) do
- to = [public_uri(draft) | draft.mentions]
+ def get_to_and_cc(%{visibility: visibility} = draft) when visibility in ["public", "local"] do
+ to =
+ case visibility do
+ "public" -> [Pleroma.Constants.as_public() | draft.mentions]
+ "local" -> [Utils.as_local_public() | draft.mentions]
+ end
+
cc = [draft.user.follower_address]
if draft.in_reply_to do
@@ -78,7 +86,7 @@ defmodule Pleroma.Web.CommonAPI.Utils do
def get_to_and_cc(%{visibility: "unlisted"} = draft) do
to = [draft.user.follower_address | draft.mentions]
- cc = [public_uri(draft)]
+ cc = [Pleroma.Constants.as_public()]
if draft.in_reply_to do
{Enum.uniq([draft.in_reply_to.data["actor"] | to]), cc}
@@ -103,9 +111,6 @@ defmodule Pleroma.Web.CommonAPI.Utils do
def get_to_and_cc(%{visibility: {:list, _}, mentions: mentions}), do: {mentions, []}
- defp public_uri(%{params: %{local_only: true}}), do: Pleroma.Web.base_url() <> "/#Public"
- defp public_uri(_), do: Pleroma.Constants.as_public()
-
def get_addressed_users(_, to) when is_list(to) do
User.get_ap_ids_by_nicknames(to)
end
@@ -158,7 +163,7 @@ defmodule Pleroma.Web.CommonAPI.Utils do
|> DateTime.add(expires_in)
|> DateTime.to_iso8601()
- key = if truthy_param?(data.poll[:multiple]), do: "anyOf", else: "oneOf"
+ key = if Params.truthy_param?(data.poll[:multiple]), do: "anyOf", else: "oneOf"
poll = %{"type" => "Question", key => option_notes, "closed" => end_time}
{:ok, {poll, emoji}}
@@ -201,7 +206,7 @@ defmodule Pleroma.Web.CommonAPI.Utils do
attachment_links =
draft.params
|> Map.get("attachment_links", Config.get([:instance, :attachment_links]))
- |> truthy_param?()
+ |> Params.truthy_param?()
content_type = get_content_type(draft.params[:content_type])
@@ -215,10 +220,9 @@ defmodule Pleroma.Web.CommonAPI.Utils do
draft.status
|> format_input(content_type, options)
|> maybe_add_attachments(draft.attachments, attachment_links)
- |> maybe_add_nsfw_tag(draft.params)
end
- defp get_content_type(content_type) do
+ def get_content_type(content_type) do
if Enum.member?(Config.get([:instance, :allowed_post_formats]), content_type) do
content_type
else
@@ -226,13 +230,6 @@ defmodule Pleroma.Web.CommonAPI.Utils do
end
end
- defp maybe_add_nsfw_tag({text, mentions, tags}, %{"sensitive" => sensitive})
- when sensitive in [true, "True", "true", "1"] do
- {text, mentions, [{"#nsfw", "nsfw"} | tags]}
- end
-
- defp maybe_add_nsfw_tag(data, _), do: data
-
def make_context(_, %Participation{} = participation) do
Repo.preload(participation, :conversation).conversation.ap_id
end
@@ -263,7 +260,7 @@ defmodule Pleroma.Web.CommonAPI.Utils do
def format_input(text, format, options \\ [])
@doc """
- Formatting text to plain text.
+ Formatting text to plain text, BBCode, HTML, or Markdown
"""
def format_input(text, "text/plain", options) do
text
@@ -274,9 +271,6 @@ defmodule Pleroma.Web.CommonAPI.Utils do
end).()
end
- @doc """
- Formatting text as BBCode.
- """
def format_input(text, "text/bbcode", options) do
text
|> String.replace(~r/\r/, "")
@@ -286,51 +280,27 @@ defmodule Pleroma.Web.CommonAPI.Utils do
|> Formatter.linkify(options)
end
- @doc """
- Formatting text to html.
- """
def format_input(text, "text/html", options) do
text
|> Formatter.html_escape("text/html")
|> Formatter.linkify(options)
end
- @doc """
- Formatting text to markdown.
- """
- def format_input(text, "text/markdown", options) do
+ def format_input(text, "text/x.misskeymarkdown", options) do
text
- |> Formatter.mentions_escape(options)
- |> Earmark.as_html!(%Earmark.Options{renderer: Pleroma.EarmarkRenderer})
+ |> Formatter.markdown_to_html()
+ |> MfmParser.Parser.parse()
+ |> MfmParser.Encoder.to_html()
|> Formatter.linkify(options)
|> Formatter.html_escape("text/html")
end
- def make_note_data(%ActivityDraft{} = draft) do
- %{
- "type" => "Note",
- "to" => draft.to,
- "cc" => draft.cc,
- "content" => draft.content_html,
- "summary" => draft.summary,
- "sensitive" => draft.sensitive,
- "context" => draft.context,
- "attachment" => draft.attachments,
- "actor" => draft.user.ap_id,
- "tag" => Keyword.values(draft.tags) |> Enum.uniq()
- }
- |> add_in_reply_to(draft.in_reply_to)
- |> Map.merge(draft.extra)
- end
-
- defp add_in_reply_to(object, nil), do: object
-
- defp add_in_reply_to(object, in_reply_to) do
- with %Object{} = in_reply_to_object <- Object.normalize(in_reply_to) do
- Map.put(object, "inReplyTo", in_reply_to_object.data["id"])
- else
- _ -> object
- end
+ def format_input(text, "text/markdown", options) do
+ text
+ |> Formatter.mentions_escape(options)
+ |> Formatter.markdown_to_html()
+ |> Formatter.linkify(options)
+ |> Formatter.html_escape("text/html")
end
def format_naive_asctime(date) do
@@ -385,7 +355,7 @@ defmodule Pleroma.Web.CommonAPI.Utils do
@spec confirm_current_password(User.t(), String.t()) :: {:ok, User.t()} | {:error, String.t()}
def confirm_current_password(user, password) do
with %User{local: true} = db_user <- User.get_cached_by_id(user.id),
- true <- AuthenticationPlug.checkpw(password, db_user.password_hash) do
+ true <- Pleroma.Password.checkpw(password, db_user.password_hash) do
{:ok, db_user}
else
_ -> {:error, dgettext("errors", "Invalid password.")}
@@ -406,7 +376,7 @@ defmodule Pleroma.Web.CommonAPI.Utils do
%Activity{data: %{"to" => _to, "type" => type} = data} = activity
)
when type == "Create" do
- object = Object.normalize(activity, false)
+ object = Object.normalize(activity, fetch: false)
object_data =
cond do
@@ -427,19 +397,14 @@ defmodule Pleroma.Web.CommonAPI.Utils do
def maybe_notify_mentioned_recipients(recipients, _), do: recipients
- # Do not notify subscribers if author is making a reply
- def maybe_notify_subscribers(recipients, %Activity{
- object: %Object{data: %{"inReplyTo" => _ap_id}}
- }) do
- recipients
- end
-
def maybe_notify_subscribers(
recipients,
- %Activity{data: %{"actor" => actor, "type" => type}} = activity
- )
- when type == "Create" do
- with %User{} = user <- User.get_cached_by_ap_id(actor) do
+ %Activity{data: %{"actor" => actor, "type" => "Create"}} = activity
+ ) do
+ # Do not notify subscribers if author is making a reply
+ with %Object{data: object} <- Object.normalize(activity, fetch: false),
+ nil <- object["inReplyTo"],
+ %User{} = user <- User.get_cached_by_ap_id(actor) do
subscriber_ids =
user
|> User.subscriber_users()
@@ -496,35 +461,6 @@ defmodule Pleroma.Web.CommonAPI.Utils do
def get_report_statuses(_, _), do: {:ok, nil}
- # DEPRECATED mostly, context objects are now created at insertion time.
- def context_to_conversation_id(context) do
- with %Object{id: id} <- Object.get_cached_by_ap_id(context) do
- id
- else
- _e ->
- changeset = Object.context_mapping(context)
-
- case Repo.insert(changeset) do
- {:ok, %{id: id}} ->
- id
-
- # This should be solved by an upsert, but it seems ecto
- # has problems accessing the constraint inside the jsonb.
- {:error, _} ->
- Object.get_cached_by_ap_id(context).id
- end
- end
- end
-
- def conversation_id_to_context(id) do
- with %Object{data: %{"id" => context}} <- Repo.get(Object, id) do
- context
- else
- _e ->
- {:error, dgettext("errors", "No such conversation")}
- end
- end
-
def validate_character_limit("" = _full_payload, [] = _attachments) do
{:error, dgettext("errors", "Cannot post an empty status without attachments")}
end