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