diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pleroma/formatter.ex | 84 | ||||
-rw-r--r-- | lib/pleroma/user.ex | 8 | ||||
-rw-r--r-- | lib/pleroma/web/activity_pub/transmogrifier.ex | 12 | ||||
-rw-r--r-- | lib/pleroma/web/common_api/utils.ex | 29 | ||||
-rw-r--r-- | lib/pleroma/web/twitter_api/twitter_api.ex | 2 | ||||
-rw-r--r-- | lib/pleroma/web/web_finger/web_finger.ex | 15 |
6 files changed, 109 insertions, 41 deletions
diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex index fd8465c1c..2d3487f6a 100644 --- a/lib/pleroma/formatter.ex +++ b/lib/pleroma/formatter.ex @@ -2,11 +2,6 @@ defmodule Pleroma.Formatter do alias Pleroma.User alias Pleroma.Web.MediaProxy - @link_regex ~r/https?:\/\/[\w\.\/?=\-#%&@~\(\)]+[\w\/]/u - def linkify(text) do - Regex.replace(@link_regex, text, "<a href='\\0'>\\0</a>") - end - @tag_regex ~r/\#\w+/u def parse_tags(text, data \\ %{}) do Regex.scan(@tag_regex, text) @@ -25,15 +20,6 @@ defmodule Pleroma.Formatter do |> Enum.filter(fn ({_match, user}) -> user end) end - def html_escape(text) do - Regex.split(@link_regex, text, include_captures: true) - |> Enum.map_every(2, fn chunk -> - {:safe, part} = Phoenix.HTML.html_escape(chunk) - part - end) - |> Enum.join("") - end - @finmoji [ "a_trusted_friend", "alandislands", @@ -145,4 +131,74 @@ defmodule Pleroma.Formatter do def get_custom_emoji() do @emoji end + + @link_regex ~r/https?:\/\/[\w\.\/?=\-#%&@~\(\)]+[\w\/]/u + + def html_escape(text) do + Regex.split(@link_regex, text, include_captures: true) + |> Enum.map_every(2, fn chunk -> + {:safe, part} = Phoenix.HTML.html_escape(chunk) + part + end) + |> Enum.join("") + end + + @doc "changes http:... links to html links" + def add_links({subs, text}) do + links = Regex.scan(@link_regex, text) + |> Enum.map(fn ([url]) -> {Ecto.UUID.generate, url} end) + + uuid_text = links + |> Enum.reduce(text, fn({uuid, url}, acc) -> String.replace(acc, url, uuid) end) + + subs = subs ++ Enum.map(links, fn({uuid, url}) -> + {uuid, "<a href='#{url}'>#{url}</a>"} + end) + + {subs, uuid_text} + end + + @doc "Adds the links to mentioned users" + def add_user_links({subs, text}, mentions) do + mentions = mentions + |> Enum.sort_by(fn ({name, _}) -> -String.length(name) end) + |> Enum.map(fn({name, user}) -> {name, user, Ecto.UUID.generate} end) + + uuid_text = mentions + |> Enum.reduce(text, fn ({match, _user, uuid}, text) -> + String.replace(text, match, uuid) + end) + + subs = subs ++ Enum.map(mentions, fn ({match, %User{ap_id: ap_id}, uuid}) -> + short_match = String.split(match, "@") |> tl() |> hd() + {uuid, "<span><a href='#{ap_id}'>@<span>#{short_match}</span></a></span>"} + end) + + {subs, uuid_text} + end + + @doc "Adds the hashtag links" + def add_hashtag_links({subs, text}, tags) do + tags = tags + |> Enum.sort_by(fn ({name, _}) -> -String.length(name) end) + |> Enum.map(fn({name, short}) -> {name, short, Ecto.UUID.generate} end) + + uuid_text = tags + |> Enum.reduce(text, fn ({match, _short, uuid}, text) -> + String.replace(text, match, uuid) + end) + + subs = subs ++ Enum.map(tags, fn ({_, tag, uuid}) -> + url = "#<a href='#{Pleroma.Web.base_url}/tag/#{tag}' rel='tag'>#{tag}</a>" + {uuid, url} + end) + + {subs, uuid_text} + end + + def finalize({subs, text}) do + Enum.reduce(subs, text, fn({uuid, replacement}, result_text) -> + String.replace(result_text, uuid, replacement) + end) + end end diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 5da146014..e92b85f52 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -458,4 +458,12 @@ defmodule Pleroma.User do def ap_enabled?(%User{info: info}), do: info["ap_enabled"] def ap_enabled?(_), do: false + + def get_or_fetch(uri_or_nickname) do + if String.starts_with?(uri_or_nickname, "http") do + get_or_fetch_by_ap_id(uri_or_nickname) + else + get_or_fetch_by_nickname(uri_or_nickname) + end + end end diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index 61631e1ea..6561b8d76 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -22,6 +22,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do |> fix_context |> fix_in_reply_to |> fix_emoji + |> fix_tag end def fix_in_reply_to(%{"inReplyTo" => in_reply_to_id} = object) when not is_nil(in_reply_to_id) do @@ -76,6 +77,17 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do |> Map.put("emoji", emoji) end + def fix_tag(object) do + tags = (object["tag"] || []) + |> Enum.filter(fn (data) -> data["type"] == "Hashtag" and data["name"] end) + |> Enum.map(fn (data) -> String.slice(data["name"], 1..-1) end) + + combined = (object["tag"] || []) ++ tags + + object + |> Map.put("tag", combined) + end + # TODO: validate those with a Ecto scheme # - tags # - emoji diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index 75c63e5f4..3c09f0cc7 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -78,13 +78,15 @@ 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) do text |> Formatter.html_escape - |> Formatter.linkify |> String.replace("\n", "<br>") - |> add_user_links(mentions) - # |> add_tag_links(tags) + |> (&({[], &1})).() + |> Formatter.add_links + |> Formatter.add_user_links(mentions) + |> Formatter.add_hashtag_links(tags) + |> Formatter.finalize end def add_tag_links(text, tags) do @@ -97,25 +99,6 @@ defmodule Pleroma.Web.CommonAPI.Utils do end) end - def add_user_links(text, mentions) do - mentions = mentions - |> Enum.sort_by(fn ({name, _}) -> -String.length(name) end) - |> Enum.map(fn({name, user}) -> {name, user, Ecto.UUID.generate} end) - - # This replaces the mention with a unique reference first so it doesn't - # contain parts of other replaced mentions. There probably is a better - # solution for this... - step_one = mentions - |> Enum.reduce(text, fn ({match, _user, uuid}, text) -> - String.replace(text, match, uuid) - end) - - Enum.reduce(mentions, step_one, fn ({match, %User{ap_id: ap_id}, uuid}, text) -> - short_match = String.split(match, "@") |> tl() |> hd() - String.replace(text, uuid, "<span><a href='#{ap_id}'>@<span>#{short_match}</span></a></span>") - end) - end - def make_note_data(actor, to, context, content_html, attachments, inReplyTo, tags, cw \\ nil, cc \\ []) do object = %{ "type" => "Note", diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 987a960bb..6e1f141f3 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -328,7 +328,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do end def get_external_profile(for_user, uri) do - with {:ok, %User{} = user} <- OStatus.find_or_make_user(uri) do + with %User{} = user <- User.get_or_fetch(uri) do spawn(fn -> with url <- user.info["topic"], {:ok, %{body: body}} <- @httpoison.get(url, [], follow_redirect: true, timeout: 10000, recv_timeout: 20000) do diff --git a/lib/pleroma/web/web_finger/web_finger.ex b/lib/pleroma/web/web_finger/web_finger.ex index d8eb6f3e0..7391edcd7 100644 --- a/lib/pleroma/web/web_finger/web_finger.ex +++ b/lib/pleroma/web/web_finger/web_finger.ex @@ -118,14 +118,23 @@ defmodule Pleroma.Web.WebFinger do {:ok, data} end - # TODO: maybe fill in other details from JRD webfinger response defp webfinger_from_json(doc) do data = Enum.reduce(doc["links"], %{"subject" => doc["subject"]}, fn (link, data) -> - case link["type"] do - "application/activity+json" -> + case {link["type"], link["rel"]} do + {"application/activity+json", "self"} -> Map.put(data, "ap_id", link["href"]) + {_, "magic-public-key"} -> + "data:application/magic-public-key," <> magic_key = link["href"] + Map.put(data, "magic_key", magic_key) + {"application/atom+xml", "http://schemas.google.com/g/2010#updates-from"} -> + Map.put(data, "topic", link["href"]) + {_, "salmon"} -> + Map.put(data, "salmon", link["href"]) + {_, "http://ostatus.org/schema/1.0/subscribe"} -> + Map.put(data, "subscribe_address", link["template"]) _ -> Logger.debug("Unhandled type: #{inspect(link["type"])}") + data end end) {:ok, data} |