MastoAPI: Fix date in account view.
[akkoma] / lib / pleroma / web / common_api / utils.ex
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
5
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
10 activity
11 else
12 Activity.get_create_activity_by_object_ap_id(activity.data["object"])
13 end
14 end
15
16 def get_replied_to_activity(id) when not is_nil(id) do
17 Repo.get(Activity, id)
18 end
19 def get_replied_to_activity(_), do: nil
20
21 def attachments_from_ids(ids) do
22 Enum.map(ids || [], fn (media_id) ->
23 Repo.get(Object, media_id).data
24 end)
25 end
26
27 def to_for_user_and_mentions(user, mentions, inReplyTo) do
28 default_to = [
29 user.follower_address,
30 "https://www.w3.org/ns/activitystreams#Public"
31 ]
32
33 to = default_to ++ Enum.map(mentions, fn ({_, %{ap_id: ap_id}}) -> ap_id end)
34 if inReplyTo do
35 Enum.uniq([inReplyTo.data["actor"] | to])
36 else
37 to
38 end
39 end
40
41 def make_content_html(status, mentions, attachments) do
42 status
43 |> format_input(mentions)
44 |> add_attachments(attachments)
45 end
46
47 def make_context(%Activity{data: %{"context" => context}}), do: context
48 def make_context(_), do: Utils.generate_context_id
49
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>"
55 _ -> ""
56 end)
57 Enum.join([text | attachment_text], "<br>\n")
58 end
59
60 def format_input(text, mentions) do
61 HtmlSanitizeEx.strip_tags(text)
62 |> Formatter.linkify
63 |> String.replace("\n", "<br>\n")
64 |> add_user_links(mentions)
65 end
66
67 def add_user_links(text, mentions) do
68 mentions = mentions
69 |> Enum.sort_by(fn ({name, _}) -> -String.length(name) end)
70 |> Enum.map(fn({name, user}) -> {name, user, Ecto.UUID.generate} end)
71
72 # This replaces the mention with a unique reference first so it doesn't
73 # contain parts of other replaced mentions. There probably is a better
74 # solution for this...
75 step_one = mentions
76 |> Enum.reduce(text, fn ({match, _user, uuid}, text) ->
77 String.replace(text, match, uuid)
78 end)
79
80 Enum.reduce(mentions, step_one, fn ({match, %User{ap_id: ap_id}, uuid}, text) ->
81 short_match = String.split(match, "@") |> tl() |> hd()
82 String.replace(text, uuid, "<a href='#{ap_id}'>@#{short_match}</a>")
83 end)
84 end
85
86 def make_note_data(actor, to, context, content_html, attachments, inReplyTo, tags) do
87 object = %{
88 "type" => "Note",
89 "to" => to,
90 "content" => content_html,
91 "context" => context,
92 "attachment" => attachments,
93 "actor" => actor,
94 "tag" => tags |> Enum.map(fn ({_, tag}) -> tag end)
95 }
96
97 if inReplyTo do
98 object
99 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
100 |> Map.put("inReplyToStatusId", inReplyTo.id)
101 else
102 object
103 end
104 end
105
106 def format_naive_asctime(date) do
107 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
108 end
109
110 def format_asctime(date) do
111 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
112 end
113
114 def date_to_asctime(date) do
115 with {:ok, date, _offset} <- date |> DateTime.from_iso8601 do
116 format_asctime(date)
117 else _e ->
118 ""
119 end
120 end
121
122 def to_masto_date(%NaiveDateTime{} = date) do
123 date
124 |> NaiveDateTime.to_iso8601
125 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
126 end
127
128 def to_masto_date(date) do
129 try do
130 date
131 |> NaiveDateTime.from_iso8601!
132 |> NaiveDateTime.to_iso8601
133 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
134 rescue
135 _e -> ""
136 end
137 end
138
139 defp shortname(name) do
140 if String.length(name) < 30 do
141 name
142 else
143 String.slice(name, 0..30) <> "…"
144 end
145 end
146 end