1 defmodule Pleroma.Web.TwitterAPI.Utils do
2 alias Pleroma.{Repo, Object, Formatter, User, Activity}
3 alias Pleroma.Web.ActivityPub.Utils
5 def attachments_from_ids(ids) do
6 Enum.map(ids || [], fn (media_id) ->
7 Repo.get(Object, media_id).data
11 def add_attachments(text, attachments) do
12 attachment_text = Enum.map(attachments, fn
13 (%{"url" => [%{"href" => href} | _]}) ->
14 "<a href='#{href}' class='attachment'>#{Path.basename(href)}</a>"
17 Enum.join([text | attachment_text], "<br>\n")
20 def format_input(text, mentions) do
21 HtmlSanitizeEx.strip_tags(text)
23 |> String.replace("\n", "<br>\n")
24 |> add_user_links(mentions)
27 def add_user_links(text, mentions) do
29 |> Enum.sort_by(fn ({name, _}) -> -String.length(name) end)
30 |> Enum.map(fn({name, user}) -> {name, user, Ecto.UUID.generate} end)
32 # This replaces the mention with a unique reference first so it doesn't
33 # contain parts of other replaced mentions. There probably is a better
34 # solution for this...
36 |> Enum.reduce(text, fn ({match, _user, uuid}, text) ->
37 String.replace(text, match, uuid)
40 Enum.reduce(mentions, step_one, fn ({match, %User{ap_id: ap_id}, uuid}, text) ->
41 String.replace(text, uuid, "<a href='#{ap_id}'>#{match}</a>")
45 def make_content_html(status, mentions, attachments) do
47 |> format_input(mentions)
48 |> add_attachments(attachments)
51 def make_context(%Activity{data: %{"context" => context}}), do: context
52 def make_context(_), do: Utils.generate_context_id
54 def make_note_data(actor, to, context, content_html, attachments, inReplyTo, tags) do
58 "content" => content_html,
60 "attachment" => attachments,
62 "tag" => tags |> Enum.map(fn ({_, tag}) -> tag end)
67 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
68 |> Map.put("inReplyToStatusId", inReplyTo.id)