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, "public") do
28 to = ["https://www.w3.org/ns/activitystreams#Public"]
30 mentioned_users = Enum.map(mentions, fn ({_, %{ap_id: ap_id}}) -> ap_id end)
31 cc = [user.follower_address | mentioned_users]
33 {to, Enum.uniq([inReplyTo.data["actor"] | cc])}
39 def to_for_user_and_mentions(user, mentions, inReplyTo, "unlisted") do
40 {to, cc} = to_for_user_and_mentions(user, mentions, inReplyTo, "public")
44 def to_for_user_and_mentions(user, mentions, inReplyTo, "private") do
45 {to, cc} = to_for_user_and_mentions(user, mentions, inReplyTo, "direct")
46 {[user.follower_address | to], cc}
49 def to_for_user_and_mentions(user, mentions, inReplyTo, "direct") do
50 mentioned_users = Enum.map(mentions, fn ({_, %{ap_id: ap_id}}) -> ap_id end)
52 {Enum.uniq([inReplyTo.data["actor"] | mentioned_users]), []}
58 def make_content_html(status, mentions, attachments, tags, no_attachment_links \\ false) do
60 |> format_input(mentions, tags)
61 |> maybe_add_attachments(attachments, no_attachment_links)
64 def make_context(%Activity{data: %{"context" => context}}), do: context
65 def make_context(_), do: Utils.generate_context_id
67 def maybe_add_attachments(text, attachments, _no_links = true), do: text
68 def maybe_add_attachments(text, attachments, _no_links) do
69 add_attachments(text, attachments)
71 def add_attachments(text, attachments) do
72 attachment_text = Enum.map(attachments, fn
73 (%{"url" => [%{"href" => href} | _]}) ->
74 name = URI.decode(Path.basename(href))
75 "<a href=\"#{href}\" class='attachment'>#{shortname(name)}</a>"
78 Enum.join([text | attachment_text], "<br>")
81 def format_input(text, mentions, tags) do
83 |> Formatter.html_escape
84 |> String.replace("\n", "<br>")
86 |> Formatter.add_links
87 |> Formatter.add_user_links(mentions)
88 |> Formatter.add_hashtag_links(tags)
92 def add_tag_links(text, tags) do
94 |> Enum.sort_by(fn ({tag, _}) -> -String.length(tag) end)
96 Enum.reduce(tags, text, fn({full, tag}, text) ->
97 url = "#<a href='#{Pleroma.Web.base_url}/tag/#{tag}' rel='tag'>#{tag}</a>"
98 String.replace(text, full, url)
102 def make_note_data(actor, to, context, content_html, attachments, inReplyTo, tags, cw \\ nil, cc \\ []) do
107 "content" => content_html,
109 "context" => context,
110 "attachment" => attachments,
112 "tag" => tags |> Enum.map(fn ({_, tag}) -> tag end)
117 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
118 |> Map.put("inReplyToStatusId", inReplyTo.id)
124 def format_naive_asctime(date) do
125 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
128 def format_asctime(date) do
129 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
132 def date_to_asctime(date) do
133 with {:ok, date, _offset} <- date |> DateTime.from_iso8601 do
140 def to_masto_date(%NaiveDateTime{} = date) do
142 |> NaiveDateTime.to_iso8601
143 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
146 def to_masto_date(date) do
149 |> NaiveDateTime.from_iso8601!
150 |> NaiveDateTime.to_iso8601
151 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
157 defp shortname(name) do
158 if String.length(name) < 30 do
161 String.slice(name, 0..30) <> "…"