1 defmodule Pleroma.Web.CommonAPI.Utils do
2 alias Pleroma.{Repo, Object, Formatter, Activity}
3 alias Pleroma.Web.ActivityPub.Utils
5 alias Calendar.Strftime
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)
13 if activity.data["type"] == "Create" do
16 Activity.get_create_activity_by_object_ap_id(activity.data["object"])
20 def get_replied_to_activity(id) when not is_nil(id) do
21 Repo.get(Activity, id)
24 def get_replied_to_activity(_), do: nil
26 def attachments_from_ids(ids) do
27 Enum.map(ids || [], fn media_id ->
28 Repo.get(Object, media_id).data
32 def to_for_user_and_mentions(user, mentions, inReplyTo, "public") do
33 to = ["https://www.w3.org/ns/activitystreams#Public"]
35 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
36 cc = [user.follower_address | mentioned_users]
39 {to, Enum.uniq([inReplyTo.data["actor"] | cc])}
45 def to_for_user_and_mentions(user, mentions, inReplyTo, "unlisted") do
46 {to, cc} = to_for_user_and_mentions(user, mentions, inReplyTo, "public")
50 def to_for_user_and_mentions(user, mentions, inReplyTo, "private") do
51 {to, cc} = to_for_user_and_mentions(user, mentions, inReplyTo, "direct")
52 {[user.follower_address | to], cc}
55 def to_for_user_and_mentions(_user, mentions, inReplyTo, "direct") do
56 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
59 {Enum.uniq([inReplyTo.data["actor"] | mentioned_users]), []}
65 def make_content_html(status, mentions, attachments, tags, no_attachment_links \\ false) do
67 |> String.replace("\r", "")
68 |> format_input(mentions, tags)
69 |> maybe_add_attachments(attachments, no_attachment_links)
72 def make_context(%Activity{data: %{"context" => context}}), do: context
73 def make_context(_), do: Utils.generate_context_id()
75 def maybe_add_attachments(text, _attachments, _no_links = true), do: text
77 def maybe_add_attachments(text, attachments, _no_links) do
78 add_attachments(text, attachments)
81 def add_attachments(text, attachments) do
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>"
92 Enum.join([text | attachment_text], "<br>")
95 def format_input(text, mentions, tags) do
97 |> Formatter.html_escape()
98 |> String.replace("\n", "<br>")
100 |> Formatter.add_links()
101 |> Formatter.add_user_links(mentions)
102 |> Formatter.add_hashtag_links(tags)
103 |> Formatter.finalize()
106 def add_tag_links(text, tags) do
109 |> Enum.sort_by(fn {tag, _} -> -String.length(tag) end)
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)
132 "content" => content_html,
134 "context" => context,
135 "attachment" => attachments,
137 "tag" => tags |> Enum.map(fn {_, tag} -> tag end) |> Enum.uniq()
142 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
143 |> Map.put("inReplyToStatusId", inReplyTo.id)
149 def format_naive_asctime(date) do
150 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
153 def format_asctime(date) do
154 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
157 def date_to_asctime(date) do
158 with {:ok, date, _offset} <- date |> DateTime.from_iso8601() do
166 def to_masto_date(%NaiveDateTime{} = date) do
168 |> NaiveDateTime.to_iso8601()
169 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
172 def to_masto_date(date) do
175 |> NaiveDateTime.from_iso8601!()
176 |> NaiveDateTime.to_iso8601()
177 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
183 defp shortname(name) do
184 if String.length(name) < 30 do
187 String.slice(name, 0..30) <> "…"
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
196 _ -> {:error, "Invalid password."}