summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/formatter.ex6
-rw-r--r--lib/pleroma/web/common_api/common_api.ex14
-rw-r--r--lib/pleroma/web/common_api/utils.ex34
-rw-r--r--lib/pleroma/web/nodeinfo/nodeinfo_controller.ex3
-rw-r--r--lib/pleroma/web/twitter_api/controllers/util_controller.ex1
-rw-r--r--lib/pleroma/web/twitter_api/twitter_api_controller.ex2
6 files changed, 52 insertions, 8 deletions
diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex
index d5565a2ca..5b63fb795 100644
--- a/lib/pleroma/formatter.ex
+++ b/lib/pleroma/formatter.ex
@@ -192,7 +192,11 @@ defmodule Pleroma.Formatter do
]
# TODO: make it use something other than @link_regex
- def html_escape(text) do
+ def html_escape(text, "text/html") do
+ HTML.filter_tags(text)
+ end
+
+ def html_escape(text, "text/plain") do
Regex.split(@link_regex, text, include_captures: true)
|> Enum.map_every(2, fn chunk ->
{:safe, part} = Phoenix.HTML.html_escape(chunk)
diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex
index 125c57d05..d4a973e36 100644
--- a/lib/pleroma/web/common_api/common_api.ex
+++ b/lib/pleroma/web/common_api/common_api.ex
@@ -73,6 +73,11 @@ defmodule Pleroma.Web.CommonAPI do
def get_visibility(_), do: "public"
@instance Application.get_env(:pleroma, :instance)
+ @allowed_post_formats Keyword.get(@instance, :allowed_post_formats)
+
+ defp get_content_type(content_type) when content_type in @allowed_post_formats, do: content_type
+ defp get_content_type(_), do: "text/plain"
+
@limit Keyword.get(@instance, :limit)
def post(user, %{"status" => status} = data) do
visibility = get_visibility(data)
@@ -85,7 +90,14 @@ defmodule Pleroma.Web.CommonAPI do
{to, cc} <- to_for_user_and_mentions(user, mentions, inReplyTo, visibility),
tags <- Formatter.parse_tags(status, data),
content_html <-
- make_content_html(status, mentions, attachments, tags, data["no_attachment_links"]),
+ make_content_html(
+ status,
+ mentions,
+ attachments,
+ tags,
+ get_content_type(data["content_type"]),
+ data["no_attachment_links"]
+ ),
context <- make_context(inReplyTo),
cw <- data["spoiler_text"],
object <-
diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex
index 358ca22ac..667027c02 100644
--- a/lib/pleroma/web/common_api/utils.ex
+++ b/lib/pleroma/web/common_api/utils.ex
@@ -63,9 +63,16 @@ defmodule Pleroma.Web.CommonAPI.Utils do
end
end
- def make_content_html(status, mentions, attachments, tags, no_attachment_links \\ false) do
+ def make_content_html(
+ status,
+ mentions,
+ attachments,
+ tags,
+ content_type,
+ no_attachment_links \\ false
+ ) do
status
- |> format_input(mentions, tags)
+ |> format_input(mentions, tags, content_type)
|> maybe_add_attachments(attachments, no_attachment_links)
end
@@ -92,9 +99,9 @@ defmodule Pleroma.Web.CommonAPI.Utils do
Enum.join([text | attachment_text], "<br>")
end
- def format_input(text, mentions, tags) do
+ def format_input(text, mentions, tags, "text/plain") do
text
- |> Formatter.html_escape()
+ |> Formatter.html_escape("text/plain")
|> String.replace(~r/\r?\n/, "<br>")
|> (&{[], &1}).()
|> Formatter.add_links()
@@ -103,6 +110,25 @@ defmodule Pleroma.Web.CommonAPI.Utils do
|> Formatter.finalize()
end
+ def format_input(text, mentions, tags, "text/html") do
+ text
+ |> Formatter.html_escape("text/html")
+ |> String.replace(~r/\r?\n/, "<br>")
+ |> (&{[], &1}).()
+ |> Formatter.add_user_links(mentions)
+ |> Formatter.finalize()
+ end
+
+ def format_input(text, mentions, tags, "text/markdown") do
+ text
+ |> Earmark.as_html!()
+ |> Formatter.html_escape("text/html")
+ |> String.replace(~r/\r?\n/, "")
+ |> (&{[], &1}).()
+ |> Formatter.add_user_links(mentions)
+ |> Formatter.finalize()
+ end
+
def add_tag_links(text, tags) do
tags =
tags
diff --git a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
index 860468506..a14000c61 100644
--- a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
+++ b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
@@ -92,7 +92,8 @@ defmodule Pleroma.Web.Nodeinfo.NodeinfoController do
mrf_policies: mrf_policies,
mrf_simple: mrf_simple,
quarantined_instances: quarantined
- }
+ },
+ postFormats: Keyword.get(instance, :allowed_post_formats)
}
}
diff --git a/lib/pleroma/web/twitter_api/controllers/util_controller.ex b/lib/pleroma/web/twitter_api/controllers/util_controller.ex
index 886b70f5f..4aaf28869 100644
--- a/lib/pleroma/web/twitter_api/controllers/util_controller.ex
+++ b/lib/pleroma/web/twitter_api/controllers/util_controller.ex
@@ -176,6 +176,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do
chatDisabled: !Keyword.get(@instance_chat, :enabled),
showInstanceSpecificPanel: Keyword.get(@instance_fe, :show_instance_panel),
scopeOptionsEnabled: Keyword.get(@instance_fe, :scope_options_enabled),
+ formattingOptionsEnabled: Keyword.get(@instance_fe, :formatting_options_enabled),
collapseMessageWithSubject: Keyword.get(@instance_fe, :collapse_message_with_subject)
}
diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex
index cd2bb5b57..c6637e38d 100644
--- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex
+++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex
@@ -423,7 +423,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
{String.trim(name, ":"), url}
end)
- bio_html = CommonUtils.format_input(bio, mentions, tags)
+ bio_html = CommonUtils.format_input(bio, mentions, tags, "text/plain")
Map.put(params, "bio", bio_html |> Formatter.emojify(emoji))
else
params