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
6 alias Calendar.Strftime
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)
14 if activity.data["type"] == "Create" do
17 Activity.get_create_activity_by_object_ap_id(activity.data["object"])
21 def get_replied_to_activity(id) when not is_nil(id) do
22 Repo.get(Activity, id)
25 def get_replied_to_activity(_), do: nil
27 def attachments_from_ids(ids) do
28 Enum.map(ids || [], fn media_id ->
29 Repo.get(Object, media_id).data
33 def to_for_user_and_mentions(user, mentions, inReplyTo, "public") do
34 to = ["https://www.w3.org/ns/activitystreams#Public"]
36 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
37 cc = [user.follower_address | mentioned_users]
40 {to, Enum.uniq([inReplyTo.data["actor"] | cc])}
46 def to_for_user_and_mentions(user, mentions, inReplyTo, "unlisted") do
47 {to, cc} = to_for_user_and_mentions(user, mentions, inReplyTo, "public")
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}
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)
60 {Enum.uniq([inReplyTo.data["actor"] | mentioned_users]), []}
66 def make_content_html(
72 no_attachment_links \\ false
75 |> format_input(mentions, tags, content_type)
76 |> maybe_add_attachments(attachments, no_attachment_links)
79 def make_context(%Activity{data: %{"context" => context}}), do: context
80 def make_context(_), do: Utils.generate_context_id()
82 def maybe_add_attachments(text, _attachments, _no_links = true), do: text
84 def maybe_add_attachments(text, attachments, _no_links) do
85 add_attachments(text, attachments)
88 def add_attachments(text, attachments) do
90 Enum.map(attachments, fn
91 %{"url" => [%{"href" => href} | _]} ->
92 name = URI.decode(Path.basename(href))
93 "<a href=\"#{href}\" class='attachment'>#{shortname(name)}</a>"
99 Enum.join([text | attachment_text], "<br>")
102 def format_input(text, mentions, tags, "text/plain") do
104 |> Formatter.html_escape("text/plain")
105 |> String.replace(~r/\r?\n/, "<br>")
107 |> Formatter.add_links()
108 |> Formatter.add_user_links(mentions)
109 |> Formatter.add_hashtag_links(tags)
110 |> Formatter.finalize()
113 def format_input(text, mentions, tags, "text/html") do
115 |> Formatter.html_escape("text/html")
116 |> String.replace(~r/\r?\n/, "<br>")
118 |> Formatter.add_user_links(mentions)
119 |> Formatter.finalize()
122 def format_input(text, mentions, tags, "text/markdown") do
124 |> Earmark.as_html!()
125 |> Formatter.html_escape("text/html")
126 |> String.replace(~r/\r?\n/, "")
128 |> Formatter.add_user_links(mentions)
129 |> Formatter.finalize()
132 def add_tag_links(text, tags) do
135 |> Enum.sort_by(fn {tag, _} -> -String.length(tag) end)
137 Enum.reduce(tags, text, fn {full, tag}, text ->
138 url = "<a href='#{Pleroma.Web.base_url()}/tag/#{tag}' rel='tag'>##{tag}</a>"
139 String.replace(text, full, url)
158 "content" => content_html,
160 "context" => context,
161 "attachment" => attachments,
163 "tag" => tags |> Enum.map(fn {_, tag} -> tag end) |> Enum.uniq()
168 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
169 |> Map.put("inReplyToStatusId", inReplyTo.id)
175 def format_naive_asctime(date) do
176 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
179 def format_asctime(date) do
180 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
183 def date_to_asctime(date) do
184 with {:ok, date, _offset} <- date |> DateTime.from_iso8601() do
192 def to_masto_date(%NaiveDateTime{} = date) do
194 |> NaiveDateTime.to_iso8601()
195 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
198 def to_masto_date(date) do
201 |> NaiveDateTime.from_iso8601!()
202 |> NaiveDateTime.to_iso8601()
203 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
209 defp shortname(name) do
210 if String.length(name) < 30 do
213 String.slice(name, 0..30) <> "…"
217 def confirm_current_password(user, password) do
218 with %User{local: true} = db_user <- Repo.get(User, user.id),
219 true <- Pbkdf2.checkpw(password, db_user.password_hash) do
222 _ -> {:error, "Invalid password."}
226 def emoji_from_profile(%{info: info} = user) do
227 (Formatter.get_emoji(user.bio) ++ Formatter.get_emoji(user.name))
228 |> Enum.map(fn {shortcode, url} ->
231 "icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}#{url}"},
232 "name" => ":#{shortcode}:"