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