f6960bf4146fa5267b90fcb7be9a682bc3b162dc
[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(status, mentions, attachments, tags, no_attachment_links \\ false) do
67 status
68 |> String.replace("\r", "")
69 |> format_input(mentions, tags)
70 |> maybe_add_attachments(attachments, no_attachment_links)
71 end
72
73 def make_context(%Activity{data: %{"context" => context}}), do: context
74 def make_context(_), do: Utils.generate_context_id()
75
76 def maybe_add_attachments(text, _attachments, _no_links = true), do: text
77
78 def maybe_add_attachments(text, attachments, _no_links) do
79 add_attachments(text, attachments)
80 end
81
82 def add_attachments(text, attachments) do
83 attachment_text =
84 Enum.map(attachments, fn
85 %{"url" => [%{"href" => href} | _]} ->
86 name = URI.decode(Path.basename(href))
87 "<a href=\"#{href}\" class='attachment'>#{shortname(name)}</a>"
88
89 _ ->
90 ""
91 end)
92
93 Enum.join([text | attachment_text], "<br>")
94 end
95
96 def format_input(text, mentions, tags) do
97 text
98 |> Formatter.html_escape()
99 |> String.replace("\n", "<br>")
100 |> (&{[], &1}).()
101 |> Formatter.add_links()
102 |> Formatter.add_user_links(mentions)
103 |> Formatter.add_hashtag_links(tags)
104 |> Formatter.finalize()
105 end
106
107 def add_tag_links(text, tags) do
108 tags =
109 tags
110 |> Enum.sort_by(fn {tag, _} -> -String.length(tag) end)
111
112 Enum.reduce(tags, text, fn {full, tag}, text ->
113 url = "#<a href='#{Pleroma.Web.base_url()}/tag/#{tag}' rel='tag'>#{tag}</a>"
114 String.replace(text, full, url)
115 end)
116 end
117
118 def make_note_data(
119 actor,
120 to,
121 context,
122 content_html,
123 attachments,
124 inReplyTo,
125 tags,
126 cw \\ nil,
127 cc \\ []
128 ) do
129 object = %{
130 "type" => "Note",
131 "to" => to,
132 "cc" => cc,
133 "content" => content_html,
134 "summary" => cw,
135 "context" => context,
136 "attachment" => attachments,
137 "actor" => actor,
138 "tag" => tags |> Enum.map(fn {_, tag} -> tag end) |> Enum.uniq()
139 }
140
141 if inReplyTo do
142 object
143 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
144 |> Map.put("inReplyToStatusId", inReplyTo.id)
145 else
146 object
147 end
148 end
149
150 def format_naive_asctime(date) do
151 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
152 end
153
154 def format_asctime(date) do
155 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
156 end
157
158 def date_to_asctime(date) do
159 with {:ok, date, _offset} <- date |> DateTime.from_iso8601() do
160 format_asctime(date)
161 else
162 _e ->
163 ""
164 end
165 end
166
167 def to_masto_date(%NaiveDateTime{} = date) do
168 date
169 |> NaiveDateTime.to_iso8601()
170 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
171 end
172
173 def to_masto_date(date) do
174 try do
175 date
176 |> NaiveDateTime.from_iso8601!()
177 |> NaiveDateTime.to_iso8601()
178 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
179 rescue
180 _e -> ""
181 end
182 end
183
184 defp shortname(name) do
185 if String.length(name) < 30 do
186 name
187 else
188 String.slice(name, 0..30) <> "…"
189 end
190 end
191
192 def confirm_current_password(user, password) do
193 with %User{local: true} = db_user <- Repo.get(User, user.id),
194 true <- Pbkdf2.checkpw(password, db_user.password_hash) do
195 {:ok, db_user}
196 else
197 _ -> {:error, "Invalid password."}
198 end
199 end
200
201 def emoji_from_profile(%{info: info} = user) do
202 (Formatter.get_emoji(user.bio) ++ Formatter.get_emoji(user.name))
203 |> Enum.map(fn {shortcode, url} ->
204 %{
205 "type" => "Emoji",
206 "icon" => %{"url" => "#{Endpoint.url()}#{url}"},
207 "name" => ":#{shortcode}:"
208 }
209 end)
210 end
211 end