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.Web.MediaProxy
7 alias Calendar.Strftime
10 # This is a hack for twidere.
11 def get_by_id_or_ap_id(id) do
12 activity = Repo.get(Activity, id) || Activity.get_create_activity_by_object_ap_id(id)
15 if activity.data["type"] == "Create" do
18 Activity.get_create_activity_by_object_ap_id(activity.data["object"])
22 def get_replied_to_activity(""), do: nil
24 def get_replied_to_activity(id) when not is_nil(id) do
25 Repo.get(Activity, id)
28 def get_replied_to_activity(_), do: nil
30 def attachments_from_ids(ids) do
31 Enum.map(ids || [], fn media_id ->
32 Repo.get(Object, media_id).data
36 def to_for_user_and_mentions(user, mentions, inReplyTo, "public") do
37 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
39 to = ["https://www.w3.org/ns/activitystreams#Public" | mentioned_users]
40 cc = [user.follower_address]
43 {Enum.uniq([inReplyTo.data["actor"] | to]), cc}
49 def to_for_user_and_mentions(user, mentions, inReplyTo, "unlisted") do
50 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
52 to = [user.follower_address | mentioned_users]
53 cc = ["https://www.w3.org/ns/activitystreams#Public"]
56 {Enum.uniq([inReplyTo.data["actor"] | to]), cc}
62 def to_for_user_and_mentions(user, mentions, inReplyTo, "private") do
63 {to, cc} = to_for_user_and_mentions(user, mentions, inReplyTo, "direct")
64 {[user.follower_address | to], cc}
67 def to_for_user_and_mentions(_user, mentions, inReplyTo, "direct") do
68 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
71 {Enum.uniq([inReplyTo.data["actor"] | mentioned_users]), []}
77 def make_content_html(
83 no_attachment_links \\ false
86 |> format_input(mentions, tags, content_type)
87 |> maybe_add_attachments(attachments, no_attachment_links)
90 def make_context(%Activity{data: %{"context" => context}}), do: context
91 def make_context(_), do: Utils.generate_context_id()
93 def maybe_add_attachments(text, _attachments, _no_links = true), do: text
95 def maybe_add_attachments(text, attachments, _no_links) do
96 add_attachments(text, attachments)
99 def add_attachments(text, attachments) do
101 Enum.map(attachments, fn
102 %{"url" => [%{"href" => href} | _]} = attachment ->
103 name = attachment["name"] || URI.decode(Path.basename(href))
104 href = MediaProxy.url(href)
105 "<a href=\"#{href}\" class='attachment'>#{shortname(name)}</a>"
111 Enum.join([text | attachment_text], "<br>")
114 def format_input(text, mentions, tags, "text/plain") do
116 |> Formatter.html_escape("text/plain")
117 |> String.replace(~r/\r?\n/, "<br>")
119 |> Formatter.add_links()
120 |> Formatter.add_user_links(mentions)
121 |> Formatter.add_hashtag_links(tags)
122 |> Formatter.finalize()
125 def format_input(text, mentions, tags, "text/html") do
127 |> Formatter.html_escape("text/html")
128 |> String.replace(~r/\r?\n/, "<br>")
130 |> Formatter.add_user_links(mentions)
131 |> Formatter.finalize()
134 def format_input(text, mentions, tags, "text/markdown") do
136 |> Earmark.as_html!()
137 |> Formatter.html_escape("text/html")
138 |> String.replace(~r/\r?\n/, "")
140 |> Formatter.add_user_links(mentions)
141 |> Formatter.add_hashtag_links(tags)
142 |> Formatter.finalize()
145 def add_tag_links(text, tags) do
148 |> Enum.sort_by(fn {tag, _} -> -String.length(tag) end)
150 Enum.reduce(tags, text, fn {full, tag}, text ->
151 url = "<a href='#{Pleroma.Web.base_url()}/tag/#{tag}' rel='tag'>##{tag}</a>"
152 String.replace(text, full, url)
171 "content" => content_html,
173 "context" => context,
174 "attachment" => attachments,
176 "tag" => tags |> Enum.map(fn {_, tag} -> tag end) |> Enum.uniq()
181 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
182 |> Map.put("inReplyToStatusId", inReplyTo.id)
188 def format_naive_asctime(date) do
189 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
192 def format_asctime(date) do
193 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
196 def date_to_asctime(date) do
197 with {:ok, date, _offset} <- date |> DateTime.from_iso8601() do
205 def to_masto_date(%NaiveDateTime{} = date) do
207 |> NaiveDateTime.to_iso8601()
208 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
211 def to_masto_date(date) do
214 |> NaiveDateTime.from_iso8601!()
215 |> NaiveDateTime.to_iso8601()
216 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
222 defp shortname(name) do
223 if String.length(name) < 30 do
226 String.slice(name, 0..30) <> "…"
230 def confirm_current_password(user, password) do
231 with %User{local: true} = db_user <- Repo.get(User, user.id),
232 true <- Pbkdf2.checkpw(password, db_user.password_hash) do
235 _ -> {:error, "Invalid password."}
239 def emoji_from_profile(%{info: info} = user) do
240 (Formatter.get_emoji(user.bio) ++ Formatter.get_emoji(user.name))
241 |> Enum.map(fn {shortcode, url} ->
244 "icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}#{url}"},
245 "name" => ":#{shortcode}:"