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>")
60 def format_input(text, mentions, tags) do
61 Phoenix.HTML.html_escape(text)
64 |> String.replace("\n", "<br>")
65 |> add_user_links(mentions)
66 # |> add_tag_links(tags)
69 def add_tag_links(text, tags) do
71 |> Enum.sort_by(fn ({tag, _}) -> -String.length(tag) end)
73 Enum.reduce(tags, text, fn({full, tag}, text) ->
74 url = "#<a href='#{Pleroma.Web.base_url}/tag/#{tag}' rel='tag'>#{tag}</a>"
75 String.replace(text, full, url)
79 def add_user_links(text, mentions) do
81 |> Enum.sort_by(fn ({name, _}) -> -String.length(name) end)
82 |> Enum.map(fn({name, user}) -> {name, user, Ecto.UUID.generate} end)
84 # This replaces the mention with a unique reference first so it doesn't
85 # contain parts of other replaced mentions. There probably is a better
86 # solution for this...
88 |> Enum.reduce(text, fn ({match, _user, uuid}, text) ->
89 String.replace(text, match, uuid)
92 Enum.reduce(mentions, step_one, fn ({match, %User{ap_id: ap_id}, uuid}, text) ->
93 short_match = String.split(match, "@") |> tl() |> hd()
94 String.replace(text, uuid, "<a href='#{ap_id}'>@#{short_match}</a>")
98 def make_note_data(actor, to, context, content_html, attachments, inReplyTo, tags, cw \\ nil) do
102 "content" => content_html,
104 "context" => context,
105 "attachment" => attachments,
107 "tag" => tags |> Enum.map(fn ({_, tag}) -> tag end)
112 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
113 |> Map.put("inReplyToStatusId", inReplyTo.id)
119 def format_naive_asctime(date) do
120 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
123 def format_asctime(date) do
124 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
127 def date_to_asctime(date) do
128 with {:ok, date, _offset} <- date |> DateTime.from_iso8601 do
135 def to_masto_date(%NaiveDateTime{} = date) do
137 |> NaiveDateTime.to_iso8601
138 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
141 def to_masto_date(date) do
144 |> NaiveDateTime.from_iso8601!
145 |> NaiveDateTime.to_iso8601
146 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
152 defp shortname(name) do
153 if String.length(name) < 30 do
156 String.slice(name, 0..30) <> "…"