formatting
[akkoma] / lib / pleroma / web / common_api / utils.ex
1 defmodule Pleroma.Web.CommonAPI.Utils do
2 alias Pleroma.{Repo, Object, Formatter, Activity}
3 alias Pleroma.Web.ActivityPub.Utils
4 alias Pleroma.Web.Endpoint
5 alias Pleroma.Web.MediaProxy
6 alias Pleroma.User
7 alias Calendar.Strftime
8 alias Comeonin.Pbkdf2
9
10 # This is a hack for twidere.
11 def get_by_id_or_ap_id(id) do
12 activity = Repo.get(Activity, id) || Activity.get_create_activity_by_object_ap_id(id)
13
14 activity &&
15 if activity.data["type"] == "Create" do
16 activity
17 else
18 Activity.get_create_activity_by_object_ap_id(activity.data["object"])
19 end
20 end
21
22 def get_replied_to_activity(""), do: nil
23
24 def get_replied_to_activity(id) when not is_nil(id) do
25 Repo.get(Activity, id)
26 end
27
28 def get_replied_to_activity(_), do: nil
29
30 def attachments_from_ids(ids) do
31 Enum.map(ids || [], fn media_id ->
32 Repo.get(Object, media_id).data
33 end)
34 end
35
36 def to_for_user_and_mentions(user, mentions, inReplyTo, "public") do
37 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
38
39 to = ["https://www.w3.org/ns/activitystreams#Public" | mentioned_users]
40 cc = [user.follower_address]
41
42 if inReplyTo do
43 {Enum.uniq([inReplyTo.data["actor"] | to]), cc}
44 else
45 {to, cc}
46 end
47 end
48
49 def to_for_user_and_mentions(user, mentions, inReplyTo, "unlisted") do
50 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
51
52 to = [user.follower_address | mentioned_users]
53 cc = ["https://www.w3.org/ns/activitystreams#Public"]
54
55 if inReplyTo do
56 {Enum.uniq([inReplyTo.data["actor"] | to]), cc}
57 else
58 {to, cc}
59 end
60 end
61
62 def to_for_user_and_mentions(user, mentions, inReplyTo, "private") do
63 {to, cc} = to_for_user_and_mentions(user, mentions, inReplyTo, "direct")
64 {[user.follower_address | to], cc}
65 end
66
67 def to_for_user_and_mentions(_user, mentions, inReplyTo, "direct") do
68 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
69
70 if inReplyTo do
71 {Enum.uniq([inReplyTo.data["actor"] | mentioned_users]), []}
72 else
73 {mentioned_users, []}
74 end
75 end
76
77 def make_content_html(
78 status,
79 mentions,
80 attachments,
81 tags,
82 content_type,
83 no_attachment_links \\ false
84 ) do
85 status
86 |> format_input(mentions, tags, content_type)
87 |> maybe_add_attachments(attachments, no_attachment_links)
88 end
89
90 def make_context(%Activity{data: %{"context" => context}}), do: context
91 def make_context(_), do: Utils.generate_context_id()
92
93 def maybe_add_attachments(text, _attachments, _no_links = true), do: text
94
95 def maybe_add_attachments(text, attachments, _no_links) do
96 add_attachments(text, attachments)
97 end
98
99 def add_attachments(text, attachments) do
100 attachment_text =
101 Enum.map(attachments, fn
102 %{"url" => [%{"href" => href} | _]} = attachment ->
103 name = attachment["name"] || URI.decode(Path.basename(href))
104 href = MediaProxy.url(href)
105 "<a href=\"#{href}\" class='attachment'>#{shortname(name)}</a>"
106
107 _ ->
108 ""
109 end)
110
111 Enum.join([text | attachment_text], "<br>")
112 end
113
114 def format_input(text, mentions, tags, "text/plain") do
115 text
116 |> Formatter.html_escape("text/plain")
117 |> String.replace(~r/\r?\n/, "<br>")
118 |> (&{[], &1}).()
119 |> Formatter.add_links()
120 |> Formatter.add_user_links(mentions)
121 |> Formatter.add_hashtag_links(tags)
122 |> Formatter.finalize()
123 end
124
125 def format_input(text, mentions, tags, "text/html") do
126 text
127 |> Formatter.html_escape("text/html")
128 |> String.replace(~r/\r?\n/, "<br>")
129 |> (&{[], &1}).()
130 |> Formatter.add_user_links(mentions)
131 |> Formatter.finalize()
132 end
133
134 def format_input(text, mentions, tags, "text/markdown") do
135 text
136 |> Earmark.as_html!()
137 |> Formatter.html_escape("text/html")
138 |> String.replace(~r/\r?\n/, "")
139 |> (&{[], &1}).()
140 |> Formatter.add_user_links(mentions)
141 |> Formatter.add_hashtag_links(tags)
142 |> Formatter.finalize()
143 end
144
145 def add_tag_links(text, tags) do
146 tags =
147 tags
148 |> Enum.sort_by(fn {tag, _} -> -String.length(tag) end)
149
150 Enum.reduce(tags, text, fn {full, tag}, text ->
151 url = "<a href='#{Pleroma.Web.base_url()}/tag/#{tag}' rel='tag'>##{tag}</a>"
152 String.replace(text, full, url)
153 end)
154 end
155
156 def make_note_data(
157 actor,
158 to,
159 context,
160 content_html,
161 attachments,
162 inReplyTo,
163 tags,
164 cw \\ nil,
165 cc \\ []
166 ) do
167 object = %{
168 "type" => "Note",
169 "to" => to,
170 "cc" => cc,
171 "content" => content_html,
172 "summary" => cw,
173 "context" => context,
174 "attachment" => attachments,
175 "actor" => actor,
176 "tag" => tags |> Enum.map(fn {_, tag} -> tag end) |> Enum.uniq()
177 }
178
179 if inReplyTo do
180 inReplyToObject = Object.normalize(inReplyTo.data["object"])
181
182 object
183 |> Map.put("inReplyTo", inReplyToObject.data["id"])
184 |> Map.put("inReplyToStatusId", inReplyTo.id)
185 else
186 object
187 end
188 end
189
190 def format_naive_asctime(date) do
191 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
192 end
193
194 def format_asctime(date) do
195 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
196 end
197
198 def date_to_asctime(date) do
199 with {:ok, date, _offset} <- date |> DateTime.from_iso8601() do
200 format_asctime(date)
201 else
202 _e ->
203 ""
204 end
205 end
206
207 def to_masto_date(%NaiveDateTime{} = date) do
208 date
209 |> NaiveDateTime.to_iso8601()
210 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
211 end
212
213 def to_masto_date(date) do
214 try do
215 date
216 |> NaiveDateTime.from_iso8601!()
217 |> NaiveDateTime.to_iso8601()
218 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
219 rescue
220 _e -> ""
221 end
222 end
223
224 defp shortname(name) do
225 if String.length(name) < 30 do
226 name
227 else
228 String.slice(name, 0..30) <> "…"
229 end
230 end
231
232 def confirm_current_password(user, password) do
233 with %User{local: true} = db_user <- Repo.get(User, user.id),
234 true <- Pbkdf2.checkpw(password, db_user.password_hash) do
235 {:ok, db_user}
236 else
237 _ -> {:error, "Invalid password."}
238 end
239 end
240
241 def emoji_from_profile(%{info: info} = user) do
242 (Formatter.get_emoji(user.bio) ++ Formatter.get_emoji(user.name))
243 |> Enum.map(fn {shortcode, url} ->
244 %{
245 "type" => "Emoji",
246 "icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}#{url}"},
247 "name" => ":#{shortcode}:"
248 }
249 end)
250 end
251 end