1 defmodule Pleroma.Web.CommonAPI.Utils do
2 alias Calendar.Strftime
4 alias Pleroma.{Activity, Formatter, Object, Repo}
7 alias Pleroma.Web.ActivityPub.Utils
8 alias Pleroma.Web.Endpoint
9 alias Pleroma.Web.MediaProxy
11 # This is a hack for twidere.
12 def get_by_id_or_ap_id(id) do
13 activity = Repo.get(Activity, id) || Activity.get_create_activity_by_object_ap_id(id)
16 if activity.data["type"] == "Create" do
19 Activity.get_create_activity_by_object_ap_id(activity.data["object"])
23 def get_replied_to_activity(""), do: nil
25 def get_replied_to_activity(id) when not is_nil(id) do
26 Repo.get(Activity, id)
29 def get_replied_to_activity(_), do: nil
31 def attachments_from_ids(ids) do
32 Enum.map(ids || [], fn media_id ->
33 Repo.get(Object, media_id).data
37 def to_for_user_and_mentions(user, mentions, inReplyTo, "public") do
38 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
40 to = ["https://www.w3.org/ns/activitystreams#Public" | mentioned_users]
41 cc = [user.follower_address]
44 {Enum.uniq([inReplyTo.data["actor"] | to]), cc}
50 def to_for_user_and_mentions(user, mentions, inReplyTo, "unlisted") do
51 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
53 to = [user.follower_address | mentioned_users]
54 cc = ["https://www.w3.org/ns/activitystreams#Public"]
57 {Enum.uniq([inReplyTo.data["actor"] | to]), cc}
63 def to_for_user_and_mentions(user, mentions, inReplyTo, "private") do
64 {to, cc} = to_for_user_and_mentions(user, mentions, inReplyTo, "direct")
65 {[user.follower_address | to], cc}
68 def to_for_user_and_mentions(_user, mentions, inReplyTo, "direct") do
69 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
72 {Enum.uniq([inReplyTo.data["actor"] | mentioned_users]), []}
78 def make_content_html(
84 no_attachment_links \\ false
87 |> format_input(mentions, tags, content_type)
88 |> maybe_add_attachments(attachments, no_attachment_links)
91 def make_context(%Activity{data: %{"context" => context}}), do: context
92 def make_context(_), do: Utils.generate_context_id()
94 def maybe_add_attachments(text, _attachments, _no_links = true), do: text
96 def maybe_add_attachments(text, attachments, _no_links) do
97 add_attachments(text, attachments)
100 def add_attachments(text, attachments) do
102 Enum.map(attachments, fn
103 %{"url" => [%{"href" => href} | _]} = attachment ->
104 name = attachment["name"] || URI.decode(Path.basename(href))
105 href = MediaProxy.url(href)
106 "<a href=\"#{href}\" class='attachment'>#{shortname(name)}</a>"
112 Enum.join([text | attachment_text], "<br>")
115 def format_input(text, mentions, tags, "text/plain") do
117 |> Formatter.html_escape("text/plain")
118 |> String.replace(~r/\r?\n/, "<br>")
120 |> Formatter.add_links()
121 |> Formatter.add_user_links(mentions)
122 |> Formatter.add_hashtag_links(tags)
123 |> Formatter.finalize()
126 def format_input(text, mentions, _tags, "text/html") do
128 |> Formatter.html_escape("text/html")
129 |> String.replace(~r/\r?\n/, "<br>")
131 |> Formatter.add_user_links(mentions)
132 |> Formatter.finalize()
135 def format_input(text, mentions, tags, "text/markdown") do
137 |> Earmark.as_html!()
138 |> Formatter.html_escape("text/html")
139 |> String.replace(~r/\r?\n/, "")
141 |> Formatter.add_user_links(mentions)
142 |> Formatter.add_hashtag_links(tags)
143 |> Formatter.finalize()
146 def add_tag_links(text, tags) do
149 |> Enum.sort_by(fn {tag, _} -> -String.length(tag) end)
151 Enum.reduce(tags, text, fn {full, tag}, text ->
152 url = "<a href='#{Web.base_url()}/tag/#{tag}' rel='tag'>##{tag}</a>"
153 String.replace(text, full, url)
172 "content" => content_html,
174 "context" => context,
175 "attachment" => attachments,
177 "tag" => tags |> Enum.map(fn {_, tag} -> tag end) |> Enum.uniq()
182 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
183 |> Map.put("inReplyToStatusId", inReplyTo.id)
189 def format_naive_asctime(date) do
190 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
193 def format_asctime(date) do
194 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
197 def date_to_asctime(date) do
198 with {:ok, date, _offset} <- date |> DateTime.from_iso8601() do
206 def to_masto_date(%NaiveDateTime{} = date) do
208 |> NaiveDateTime.to_iso8601()
209 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
212 def to_masto_date(date) do
215 |> NaiveDateTime.from_iso8601!()
216 |> NaiveDateTime.to_iso8601()
217 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
223 defp shortname(name) do
224 if String.length(name) < 30 do
227 String.slice(name, 0..30) <> "…"
231 def confirm_current_password(user, password) do
232 with %User{local: true} = db_user <- Repo.get(User, user.id),
233 true <- Pbkdf2.checkpw(password, db_user.password_hash) do
236 _ -> {:error, "Invalid password."}
240 def emoji_from_profile(%{info: _info} = user) do
241 (Formatter.get_emoji(user.bio) ++ Formatter.get_emoji(user.name))
242 |> Enum.map(fn {shortcode, url} ->
245 "icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}#{url}"},
246 "name" => ":#{shortcode}:"