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)
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 |> format_input(mentions, tags)
65 |> maybe_add_attachments(attachments, no_attachment_links)
68 def make_context(%Activity{data: %{"context" => context}}), do: context
69 def make_context(_), do: Utils.generate_context_id()
71 def maybe_add_attachments(text, attachments, _no_links = true), do: text
73 def maybe_add_attachments(text, attachments, _no_links) do
74 add_attachments(text, attachments)
77 def add_attachments(text, attachments) do
79 Enum.map(attachments, fn
80 %{"url" => [%{"href" => href} | _]} ->
81 name = URI.decode(Path.basename(href))
82 "<a href=\"#{href}\" class='attachment'>#{shortname(name)}</a>"
88 Enum.join([text | attachment_text], "<br>")
91 def format_input(text, mentions, tags) do
93 |> Formatter.html_escape()
94 |> String.replace("\n", "<br>")
96 |> Formatter.add_links()
97 |> Formatter.add_user_links(mentions)
98 |> Formatter.add_hashtag_links(tags)
99 |> Formatter.finalize()
102 def add_tag_links(text, tags) do
105 |> Enum.sort_by(fn {tag, _} -> -String.length(tag) end)
107 Enum.reduce(tags, text, fn {full, tag}, text ->
108 url = "#<a href='#{Pleroma.Web.base_url()}/tag/#{tag}' rel='tag'>#{tag}</a>"
109 String.replace(text, full, url)
128 "content" => content_html,
130 "context" => context,
131 "attachment" => attachments,
133 "tag" => tags |> Enum.map(fn {_, tag} -> tag end)
138 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
139 |> Map.put("inReplyToStatusId", inReplyTo.id)
145 def format_naive_asctime(date) do
146 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
149 def format_asctime(date) do
150 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
153 def date_to_asctime(date) do
154 with {:ok, date, _offset} <- date |> DateTime.from_iso8601() do
162 def to_masto_date(%NaiveDateTime{} = date) do
164 |> NaiveDateTime.to_iso8601()
165 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
168 def to_masto_date(date) do
171 |> NaiveDateTime.from_iso8601!()
172 |> NaiveDateTime.to_iso8601()
173 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
179 defp shortname(name) do
180 if String.length(name) < 30 do
183 String.slice(name, 0..30) <> "…"