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