Merge branch 'fix/theora-detection-read-bytes' 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.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 object
181 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
182 |> Map.put("inReplyToStatusId", inReplyTo.id)
183 else
184 object
185 end
186 end
187
188 def format_naive_asctime(date) do
189 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
190 end
191
192 def format_asctime(date) do
193 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
194 end
195
196 def date_to_asctime(date) do
197 with {:ok, date, _offset} <- date |> DateTime.from_iso8601() do
198 format_asctime(date)
199 else
200 _e ->
201 ""
202 end
203 end
204
205 def to_masto_date(%NaiveDateTime{} = date) do
206 date
207 |> NaiveDateTime.to_iso8601()
208 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
209 end
210
211 def to_masto_date(date) do
212 try do
213 date
214 |> NaiveDateTime.from_iso8601!()
215 |> NaiveDateTime.to_iso8601()
216 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
217 rescue
218 _e -> ""
219 end
220 end
221
222 defp shortname(name) do
223 if String.length(name) < 30 do
224 name
225 else
226 String.slice(name, 0..30) <> "…"
227 end
228 end
229
230 def confirm_current_password(user, password) do
231 with %User{local: true} = db_user <- Repo.get(User, user.id),
232 true <- Pbkdf2.checkpw(password, db_user.password_hash) do
233 {:ok, db_user}
234 else
235 _ -> {:error, "Invalid password."}
236 end
237 end
238
239 def emoji_from_profile(%{info: _info} = user) do
240 (Formatter.get_emoji(user.bio) ++ Formatter.get_emoji(user.name))
241 |> Enum.map(fn {shortcode, url} ->
242 %{
243 "type" => "Emoji",
244 "icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}#{url}"},
245 "name" => ":#{shortcode}:"
246 }
247 end)
248 end
249 end