Merge branch 'patch-2' 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.finalize()
130 end
131
132 def add_tag_links(text, tags) do
133 tags =
134 tags
135 |> Enum.sort_by(fn {tag, _} -> -String.length(tag) end)
136
137 Enum.reduce(tags, text, fn {full, tag}, text ->
138 url = "<a href='#{Pleroma.Web.base_url()}/tag/#{tag}' rel='tag'>##{tag}</a>"
139 String.replace(text, full, url)
140 end)
141 end
142
143 def make_note_data(
144 actor,
145 to,
146 context,
147 content_html,
148 attachments,
149 inReplyTo,
150 tags,
151 cw \\ nil,
152 cc \\ []
153 ) do
154 object = %{
155 "type" => "Note",
156 "to" => to,
157 "cc" => cc,
158 "content" => content_html,
159 "summary" => cw,
160 "context" => context,
161 "attachment" => attachments,
162 "actor" => actor,
163 "tag" => tags |> Enum.map(fn {_, tag} -> tag end) |> Enum.uniq()
164 }
165
166 if inReplyTo do
167 object
168 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
169 |> Map.put("inReplyToStatusId", inReplyTo.id)
170 else
171 object
172 end
173 end
174
175 def format_naive_asctime(date) do
176 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
177 end
178
179 def format_asctime(date) do
180 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
181 end
182
183 def date_to_asctime(date) do
184 with {:ok, date, _offset} <- date |> DateTime.from_iso8601() do
185 format_asctime(date)
186 else
187 _e ->
188 ""
189 end
190 end
191
192 def to_masto_date(%NaiveDateTime{} = date) do
193 date
194 |> NaiveDateTime.to_iso8601()
195 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
196 end
197
198 def to_masto_date(date) do
199 try do
200 date
201 |> NaiveDateTime.from_iso8601!()
202 |> NaiveDateTime.to_iso8601()
203 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
204 rescue
205 _e -> ""
206 end
207 end
208
209 defp shortname(name) do
210 if String.length(name) < 30 do
211 name
212 else
213 String.slice(name, 0..30) <> "…"
214 end
215 end
216
217 def confirm_current_password(user, password) do
218 with %User{local: true} = db_user <- Repo.get(User, user.id),
219 true <- Pbkdf2.checkpw(password, db_user.password_hash) do
220 {:ok, db_user}
221 else
222 _ -> {:error, "Invalid password."}
223 end
224 end
225
226 def emoji_from_profile(%{info: info} = user) do
227 (Formatter.get_emoji(user.bio) ++ Formatter.get_emoji(user.name))
228 |> Enum.map(fn {shortcode, url} ->
229 %{
230 "type" => "Emoji",
231 "icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}#{url}"},
232 "name" => ":#{shortcode}:"
233 }
234 end)
235 end
236 end