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) do
43 |> format_input(mentions, tags)
44 |> add_attachments(attachments)
47 def make_context(%Activity{data: %{"context" => context}}), do: context
48 def make_context(_), do: Utils.generate_context_id
50 def add_attachments(text, attachments) do
51 attachment_text = Enum.map(attachments, fn
52 (%{"url" => [%{"href" => href} | _]}) ->
53 name = URI.decode(Path.basename(href))
54 "<a href=\"#{href}\" class='attachment'>#{shortname(name)}</a>"
57 Enum.join([text | attachment_text], "<br>\n")
60 def format_input(text, mentions, tags) do
61 HtmlSanitizeEx.strip_tags(text)
63 |> String.replace("\n", "<br>\n")
64 |> add_user_links(mentions)
65 # |> add_tag_links(tags)
68 def add_tag_links(text, tags) do
70 |> Enum.sort_by(fn ({tag, _}) -> -String.length(tag) end)
72 Enum.reduce(tags, text, fn({full, tag}, text) ->
73 url = "#<a href='#{Pleroma.Web.base_url}/tag/#{tag}' rel='tag'>#{tag}</a>"
74 String.replace(text, full, url)
78 def add_user_links(text, mentions) do
80 |> Enum.sort_by(fn ({name, _}) -> -String.length(name) end)
81 |> Enum.map(fn({name, user}) -> {name, user, Ecto.UUID.generate} end)
83 # This replaces the mention with a unique reference first so it doesn't
84 # contain parts of other replaced mentions. There probably is a better
85 # solution for this...
87 |> Enum.reduce(text, fn ({match, _user, uuid}, text) ->
88 String.replace(text, match, uuid)
91 Enum.reduce(mentions, step_one, fn ({match, %User{ap_id: ap_id}, uuid}, text) ->
92 short_match = String.split(match, "@") |> tl() |> hd()
93 String.replace(text, uuid, "<a href='#{ap_id}'>@#{short_match}</a>")
97 def make_note_data(actor, to, context, content_html, attachments, inReplyTo, tags, cw \\ nil) do
101 "content" => content_html,
103 "context" => context,
104 "attachment" => attachments,
106 "tag" => tags |> Enum.map(fn ({_, tag}) -> tag end)
111 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
112 |> Map.put("inReplyToStatusId", inReplyTo.id)
118 def format_naive_asctime(date) do
119 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
122 def format_asctime(date) do
123 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
126 def date_to_asctime(date) do
127 with {:ok, date, _offset} <- date |> DateTime.from_iso8601 do
134 def to_masto_date(%NaiveDateTime{} = date) do
136 |> NaiveDateTime.to_iso8601
137 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
140 def to_masto_date(date) do
143 |> NaiveDateTime.from_iso8601!
144 |> NaiveDateTime.to_iso8601
145 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
151 defp shortname(name) do
152 if String.length(name) < 30 do
155 String.slice(name, 0..30) <> "…"