1 defmodule Pleroma.Web.CommonAPI.Utils do
2 alias Pleroma.{Repo, Object, Formatter, 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)
10 if activity.data["type"] == "Create" do
13 Activity.get_create_activity_by_object_ap_id(activity.data["object"])
17 def get_replied_to_activity(id) when not is_nil(id) do
18 Repo.get(Activity, id)
21 def get_replied_to_activity(_), do: nil
23 def attachments_from_ids(ids) do
24 Enum.map(ids || [], fn media_id ->
25 Repo.get(Object, media_id).data
29 def to_for_user_and_mentions(user, mentions, inReplyTo, "public") do
30 to = ["https://www.w3.org/ns/activitystreams#Public"]
32 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
33 cc = [user.follower_address | mentioned_users]
36 {to, Enum.uniq([inReplyTo.data["actor"] | cc])}
42 def to_for_user_and_mentions(user, mentions, inReplyTo, "unlisted") do
43 {to, cc} = to_for_user_and_mentions(user, mentions, inReplyTo, "public")
47 def to_for_user_and_mentions(user, mentions, inReplyTo, "private") do
48 {to, cc} = to_for_user_and_mentions(user, mentions, inReplyTo, "direct")
49 {[user.follower_address | to], cc}
52 def to_for_user_and_mentions(_user, mentions, inReplyTo, "direct") do
53 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
56 {Enum.uniq([inReplyTo.data["actor"] | mentioned_users]), []}
62 def make_content_html(status, mentions, attachments, tags, no_attachment_links \\ false) do
64 |> String.replace("\r", "")
65 |> format_input(mentions, tags)
66 |> maybe_add_attachments(attachments, no_attachment_links)
69 def make_context(%Activity{data: %{"context" => context}}), do: context
70 def make_context(_), do: Utils.generate_context_id()
72 def maybe_add_attachments(text, _attachments, _no_links = true), do: text
74 def maybe_add_attachments(text, attachments, _no_links) do
75 add_attachments(text, attachments)
78 def add_attachments(text, attachments) do
80 Enum.map(attachments, fn
81 %{"url" => [%{"href" => href} | _]} ->
82 name = URI.decode(Path.basename(href))
83 "<a href=\"#{href}\" class='attachment'>#{shortname(name)}</a>"
89 Enum.join([text | attachment_text], "<br>")
92 def format_input(text, mentions, tags) do
94 |> Formatter.html_escape()
95 |> String.replace("\n", "<br>")
97 |> Formatter.add_links()
98 |> Formatter.add_user_links(mentions)
99 |> Formatter.add_hashtag_links(tags)
100 |> Formatter.finalize()
103 def add_tag_links(text, tags) do
106 |> Enum.sort_by(fn {tag, _} -> -String.length(tag) end)
108 Enum.reduce(tags, text, fn {full, tag}, text ->
109 url = "#<a href='#{Pleroma.Web.base_url()}/tag/#{tag}' rel='tag'>#{tag}</a>"
110 String.replace(text, full, url)
129 "content" => content_html,
131 "context" => context,
132 "attachment" => attachments,
134 "tag" => tags |> Enum.map(fn {_, tag} -> tag end)
139 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
140 |> Map.put("inReplyToStatusId", inReplyTo.id)
146 def format_naive_asctime(date) do
147 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
150 def format_asctime(date) do
151 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
154 def date_to_asctime(date) do
155 with {:ok, date, _offset} <- date |> DateTime.from_iso8601() do
163 def to_masto_date(%NaiveDateTime{} = date) do
165 |> NaiveDateTime.to_iso8601()
166 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
169 def to_masto_date(date) do
172 |> NaiveDateTime.from_iso8601!()
173 |> NaiveDateTime.to_iso8601()
174 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
180 defp shortname(name) do
181 if String.length(name) < 30 do
184 String.slice(name, 0..30) <> "…"