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