Merge branch 'feld-varnish' into 'develop'
[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
10 if activity.data["type"] == "Create" do
11 activity
12 else
13 Activity.get_create_activity_by_object_ap_id(activity.data["object"])
14 end
15 end
16
17 def get_replied_to_activity(id) when not is_nil(id) do
18 Repo.get(Activity, id)
19 end
20
21 def get_replied_to_activity(_), do: nil
22
23 def attachments_from_ids(ids) do
24 Enum.map(ids || [], fn media_id ->
25 Repo.get(Object, media_id).data
26 end)
27 end
28
29 def to_for_user_and_mentions(user, mentions, inReplyTo, "public") do
30 to = ["https://www.w3.org/ns/activitystreams#Public"]
31
32 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
33 cc = [user.follower_address | mentioned_users]
34
35 if inReplyTo do
36 {to, Enum.uniq([inReplyTo.data["actor"] | cc])}
37 else
38 {to, cc}
39 end
40 end
41
42 def to_for_user_and_mentions(user, mentions, inReplyTo, "unlisted") do
43 {to, cc} = to_for_user_and_mentions(user, mentions, inReplyTo, "public")
44 {cc, to}
45 end
46
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}
50 end
51
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)
54
55 if inReplyTo do
56 {Enum.uniq([inReplyTo.data["actor"] | mentioned_users]), []}
57 else
58 {mentioned_users, []}
59 end
60 end
61
62 def make_content_html(status, mentions, attachments, tags, no_attachment_links \\ false) do
63 status
64 |> format_input(mentions, tags)
65 |> maybe_add_attachments(attachments, no_attachment_links)
66 end
67
68 def make_context(%Activity{data: %{"context" => context}}), do: context
69 def make_context(_), do: Utils.generate_context_id()
70
71 def maybe_add_attachments(text, attachments, _no_links = true), do: text
72
73 def maybe_add_attachments(text, attachments, _no_links) do
74 add_attachments(text, attachments)
75 end
76
77 def add_attachments(text, attachments) do
78 attachment_text =
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>"
83
84 _ ->
85 ""
86 end)
87
88 Enum.join([text | attachment_text], "<br>")
89 end
90
91 def format_input(text, mentions, tags) do
92 text
93 |> Formatter.html_escape()
94 |> String.replace("\n", "<br>")
95 |> (&{[], &1}).()
96 |> Formatter.add_links()
97 |> Formatter.add_user_links(mentions)
98 |> Formatter.add_hashtag_links(tags)
99 |> Formatter.finalize()
100 end
101
102 def add_tag_links(text, tags) do
103 tags =
104 tags
105 |> Enum.sort_by(fn {tag, _} -> -String.length(tag) end)
106
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)
110 end)
111 end
112
113 def make_note_data(
114 actor,
115 to,
116 context,
117 content_html,
118 attachments,
119 inReplyTo,
120 tags,
121 cw \\ nil,
122 cc \\ []
123 ) do
124 object = %{
125 "type" => "Note",
126 "to" => to,
127 "cc" => cc,
128 "content" => content_html,
129 "summary" => cw,
130 "context" => context,
131 "attachment" => attachments,
132 "actor" => actor,
133 "tag" => tags |> Enum.map(fn {_, tag} -> tag end)
134 }
135
136 if inReplyTo do
137 object
138 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
139 |> Map.put("inReplyToStatusId", inReplyTo.id)
140 else
141 object
142 end
143 end
144
145 def format_naive_asctime(date) do
146 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
147 end
148
149 def format_asctime(date) do
150 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
151 end
152
153 def date_to_asctime(date) do
154 with {:ok, date, _offset} <- date |> DateTime.from_iso8601() do
155 format_asctime(date)
156 else
157 _e ->
158 ""
159 end
160 end
161
162 def to_masto_date(%NaiveDateTime{} = date) do
163 date
164 |> NaiveDateTime.to_iso8601()
165 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
166 end
167
168 def to_masto_date(date) do
169 try do
170 date
171 |> NaiveDateTime.from_iso8601!()
172 |> NaiveDateTime.to_iso8601()
173 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
174 rescue
175 _e -> ""
176 end
177 end
178
179 defp shortname(name) do
180 if String.length(name) < 30 do
181 name
182 else
183 String.slice(name, 0..30) <> "…"
184 end
185 end
186 end