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
85 |> String.replace("\n", "<br>")
86 |> add_user_links(mentions)
87 # |> add_tag_links(tags)
90 def add_tag_links(text, tags) do
92 |> Enum.sort_by(fn ({tag, _}) -> -String.length(tag) end)
94 Enum.reduce(tags, text, fn({full, tag}, text) ->
95 url = "#<a href='#{Pleroma.Web.base_url}/tag/#{tag}' rel='tag'>#{tag}</a>"
96 String.replace(text, full, url)
100 def add_user_links(text, mentions) do
102 |> Enum.sort_by(fn ({name, _}) -> -String.length(name) end)
103 |> Enum.map(fn({name, user}) -> {name, user, Ecto.UUID.generate} end)
105 # This replaces the mention with a unique reference first so it doesn't
106 # contain parts of other replaced mentions. There probably is a better
107 # solution for this...
109 |> Enum.reduce(text, fn ({match, _user, uuid}, text) ->
110 String.replace(text, match, uuid)
113 Enum.reduce(mentions, step_one, fn ({match, %User{ap_id: ap_id}, uuid}, text) ->
114 short_match = String.split(match, "@") |> tl() |> hd()
115 String.replace(text, uuid, "<span><a href='#{ap_id}'>@<span>#{short_match}</span></a></span>")
119 def make_note_data(actor, to, context, content_html, attachments, inReplyTo, tags, cw \\ nil, cc \\ []) do
124 "content" => content_html,
126 "context" => context,
127 "attachment" => attachments,
129 "tag" => tags |> Enum.map(fn ({_, tag}) -> tag end)
134 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
135 |> Map.put("inReplyToStatusId", inReplyTo.id)
141 def format_naive_asctime(date) do
142 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
145 def format_asctime(date) do
146 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
149 def date_to_asctime(date) do
150 with {:ok, date, _offset} <- date |> DateTime.from_iso8601 do
157 def to_masto_date(%NaiveDateTime{} = date) do
159 |> NaiveDateTime.to_iso8601
160 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
163 def to_masto_date(date) do
166 |> NaiveDateTime.from_iso8601!
167 |> NaiveDateTime.to_iso8601
168 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
174 defp shortname(name) do
175 if String.length(name) < 30 do
178 String.slice(name, 0..30) <> "…"