1 defmodule Pleroma.Web.TwitterAPI.Utils do
2 alias Pleroma.{Repo, Object, Formatter, User, Activity}
3 alias Pleroma.Web.ActivityPub.Utils
4 alias Calendar.Strftime
6 def attachments_from_ids(ids) do
7 Enum.map(ids || [], fn (media_id) ->
8 Repo.get(Object, media_id).data
12 def add_attachments(text, attachments) do
13 attachment_text = Enum.map(attachments, fn
14 (%{"url" => [%{"href" => href} | _]}) ->
15 "<a href=\"#{URI.encode(href)}\" class='attachment'>#{Path.basename(href)}</a>"
18 Enum.join([text | attachment_text], "<br>\n")
21 def format_input(text, mentions) do
22 HtmlSanitizeEx.strip_tags(text)
24 |> String.replace("\n", "<br>\n")
25 |> add_user_links(mentions)
28 def add_user_links(text, mentions) do
30 |> Enum.sort_by(fn ({name, _}) -> -String.length(name) end)
31 |> Enum.map(fn({name, user}) -> {name, user, Ecto.UUID.generate} end)
33 # This replaces the mention with a unique reference first so it doesn't
34 # contain parts of other replaced mentions. There probably is a better
35 # solution for this...
37 |> Enum.reduce(text, fn ({match, _user, uuid}, text) ->
38 String.replace(text, match, uuid)
41 Enum.reduce(mentions, step_one, fn ({match, %User{ap_id: ap_id}, uuid}, text) ->
42 short_match = String.split(match, "@") |> tl() |> hd()
43 String.replace(text, uuid, "<a href='#{ap_id}'>@#{short_match}</a>")
47 def make_content_html(status, mentions, attachments) do
49 |> format_input(mentions)
50 |> add_attachments(attachments)
53 def make_context(%Activity{data: %{"context" => context}}), do: context
54 def make_context(_), do: Utils.generate_context_id
56 # TODO: Move this to a more fitting space
57 def make_note_data(actor, to, context, content_html, attachments, inReplyTo, tags) do
61 "content" => content_html,
63 "attachment" => attachments,
65 "tag" => tags |> Enum.map(fn ({_, tag}) -> tag end)
70 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
71 |> Map.put("inReplyToStatusId", inReplyTo.id)
77 def format_naive_asctime(date) do
78 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
81 def format_asctime(date) do
82 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
85 def date_to_asctime(date) do
86 with {:ok, date, _offset} <- date |> DateTime.from_iso8601 do