1 defmodule Pleroma.Web.CommonAPI.Utils do
2 alias Pleroma.{Repo, Object, Formatter, User, Activity}
3 alias Pleroma.Web.ActivityPub.Utils
4 alias Calendar.Strftime
6 # This is a hack for twidere.
7 def get_by_id_or_ap_id(id) do
8 activity = Repo.get(Activity, id) || Activity.get_create_activity_by_object_ap_id(id)
9 if activity.data["type"] == "Create" do
12 Activity.get_create_activity_by_object_ap_id(activity.data["object"])
16 def get_replied_to_activity(id) when not is_nil(id) do
17 Repo.get(Activity, id)
19 def get_replied_to_activity(_), do: nil
21 def attachments_from_ids(ids) do
22 Enum.map(ids || [], fn (media_id) ->
23 Repo.get(Object, media_id).data
27 def to_for_user_and_mentions(user, mentions, inReplyTo) do
29 user.follower_address,
30 "https://www.w3.org/ns/activitystreams#Public"
33 to = default_to ++ Enum.map(mentions, fn ({_, %{ap_id: ap_id}}) -> ap_id end)
35 Enum.uniq([inReplyTo.data["actor"] | to])
41 def make_content_html(status, mentions, attachments, tags, no_attachment_links \\ false) do
43 |> format_input(mentions, tags)
44 |> maybe_add_attachments(attachments, no_attachment_links)
47 def make_context(%Activity{data: %{"context" => context}}), do: context
48 def make_context(_), do: Utils.generate_context_id
50 def maybe_add_attachments(text, attachments, _no_links = true), do: text
51 def maybe_add_attachments(text, attachments, _no_links) do
52 add_attachments(text, attachments)
54 def add_attachments(text, attachments) do
55 attachment_text = Enum.map(attachments, fn
56 (%{"url" => [%{"href" => href} | _]}) ->
57 name = URI.decode(Path.basename(href))
58 "<a href=\"#{href}\" class='attachment'>#{shortname(name)}</a>"
61 Enum.join([text | attachment_text], "<br>")
64 def format_input(text, mentions, _tags) do
66 |> Formatter.html_escape
68 |> String.replace("\n", "<br>")
69 |> add_user_links(mentions)
70 # |> add_tag_links(tags)
73 def add_tag_links(text, tags) do
75 |> Enum.sort_by(fn ({tag, _}) -> -String.length(tag) end)
77 Enum.reduce(tags, text, fn({full, tag}, text) ->
78 url = "#<a href='#{Pleroma.Web.base_url}/tag/#{tag}' rel='tag'>#{tag}</a>"
79 String.replace(text, full, url)
83 def add_user_links(text, mentions) do
85 |> Enum.sort_by(fn ({name, _}) -> -String.length(name) end)
86 |> Enum.map(fn({name, user}) -> {name, user, Ecto.UUID.generate} end)
88 # This replaces the mention with a unique reference first so it doesn't
89 # contain parts of other replaced mentions. There probably is a better
90 # solution for this...
92 |> Enum.reduce(text, fn ({match, _user, uuid}, text) ->
93 String.replace(text, match, uuid)
96 Enum.reduce(mentions, step_one, fn ({match, %User{ap_id: ap_id}, uuid}, text) ->
97 short_match = String.split(match, "@") |> tl() |> hd()
98 String.replace(text, uuid, "<span><a href='#{ap_id}'>@<span>#{short_match}</span></a></span>")
102 def make_note_data(actor, to, context, content_html, attachments, inReplyTo, tags, cw \\ nil) do
106 "content" => content_html,
108 "context" => context,
109 "attachment" => attachments,
111 "tag" => tags |> Enum.map(fn ({_, tag}) -> tag end)
116 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
117 |> Map.put("inReplyToStatusId", inReplyTo.id)
123 def format_naive_asctime(date) do
124 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
127 def format_asctime(date) do
128 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
131 def date_to_asctime(date) do
132 with {:ok, date, _offset} <- date |> DateTime.from_iso8601 do
139 def to_masto_date(%NaiveDateTime{} = date) do
141 |> NaiveDateTime.to_iso8601
142 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
145 def to_masto_date(date) do
148 |> NaiveDateTime.from_iso8601!
149 |> NaiveDateTime.to_iso8601
150 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
156 defp shortname(name) do
157 if String.length(name) < 30 do
160 String.slice(name, 0..30) <> "…"