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