1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.CommonAPI.Utils do
6 alias Calendar.Strftime
8 alias Pleroma.{Activity, Formatter, Object, Repo}
11 alias Pleroma.Web.ActivityPub.Utils
12 alias Pleroma.Web.Endpoint
13 alias Pleroma.Web.MediaProxy
15 # This is a hack for twidere.
16 def get_by_id_or_ap_id(id) do
17 activity = Repo.get(Activity, id) || Activity.get_create_activity_by_object_ap_id(id)
20 if activity.data["type"] == "Create" do
23 Activity.get_create_activity_by_object_ap_id(activity.data["object"])
27 def get_replied_to_activity(""), do: nil
29 def get_replied_to_activity(id) when not is_nil(id) do
30 Repo.get(Activity, id)
33 def get_replied_to_activity(_), do: nil
35 def attachments_from_ids(ids, descs_str) do
36 {_, descs} = Jason.decode(descs_str)
38 Enum.map(ids || [], fn media_id ->
39 Map.put(Repo.get(Object, media_id).data, "name", descs[media_id])
43 def to_for_user_and_mentions(user, mentions, inReplyTo, "public") do
44 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
46 to = ["https://www.w3.org/ns/activitystreams#Public" | mentioned_users]
47 cc = [user.follower_address]
50 {Enum.uniq([inReplyTo.data["actor"] | to]), cc}
56 def to_for_user_and_mentions(user, mentions, inReplyTo, "unlisted") do
57 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
59 to = [user.follower_address | mentioned_users]
60 cc = ["https://www.w3.org/ns/activitystreams#Public"]
63 {Enum.uniq([inReplyTo.data["actor"] | to]), cc}
69 def to_for_user_and_mentions(user, mentions, inReplyTo, "private") do
70 {to, cc} = to_for_user_and_mentions(user, mentions, inReplyTo, "direct")
71 {[user.follower_address | to], cc}
74 def to_for_user_and_mentions(_user, mentions, inReplyTo, "direct") do
75 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
78 {Enum.uniq([inReplyTo.data["actor"] | mentioned_users]), []}
84 def make_content_html(
90 no_attachment_links \\ false
93 |> format_input(mentions, tags, content_type)
94 |> maybe_add_attachments(attachments, no_attachment_links)
97 def make_context(%Activity{data: %{"context" => context}}), do: context
98 def make_context(_), do: Utils.generate_context_id()
100 def maybe_add_attachments(text, _attachments, _no_links = true), do: text
102 def maybe_add_attachments(text, attachments, _no_links) do
103 add_attachments(text, attachments)
106 def add_attachments(text, attachments) do
108 Enum.map(attachments, fn
109 %{"url" => [%{"href" => href} | _]} = attachment ->
110 name = attachment["name"] || URI.decode(Path.basename(href))
111 href = MediaProxy.url(href)
112 "<a href=\"#{href}\" class='attachment'>#{shortname(name)}</a>"
118 Enum.join([text | attachment_text], "<br>")
122 Formatting text to plain text.
124 def format_input(text, mentions, tags, "text/plain") do
126 |> Formatter.html_escape("text/plain")
127 |> String.replace(~r/\r?\n/, "<br>")
129 |> Formatter.add_links()
130 |> Formatter.add_user_links(mentions)
131 |> Formatter.add_hashtag_links(tags)
132 |> Formatter.finalize()
136 Formatting text to html.
138 def format_input(text, mentions, _tags, "text/html") do
140 |> Formatter.html_escape("text/html")
141 |> String.replace(~r/\r?\n/, "<br>")
143 |> Formatter.add_user_links(mentions)
144 |> Formatter.finalize()
148 Formatting text to markdown.
150 def format_input(text, mentions, tags, "text/markdown") do
152 |> Formatter.mentions_escape(mentions)
153 |> Earmark.as_html!()
154 |> Formatter.html_escape("text/html")
155 |> String.replace(~r/\r?\n/, "")
157 |> Formatter.add_user_links(mentions)
158 |> Formatter.add_hashtag_links(tags)
159 |> Formatter.finalize()
162 def add_tag_links(text, tags) do
165 |> Enum.sort_by(fn {tag, _} -> -String.length(tag) end)
167 Enum.reduce(tags, text, fn {full, tag}, text ->
168 url = "<a href='#{Web.base_url()}/tag/#{tag}' rel='tag'>##{tag}</a>"
169 String.replace(text, full, url)
188 "content" => content_html,
190 "context" => context,
191 "attachment" => attachments,
193 "tag" => tags |> Enum.map(fn {_, tag} -> tag end) |> Enum.uniq()
198 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
199 |> Map.put("inReplyToStatusId", inReplyTo.id)
205 def format_naive_asctime(date) do
206 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
209 def format_asctime(date) do
210 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
213 def date_to_asctime(date) do
214 with {:ok, date, _offset} <- date |> DateTime.from_iso8601() do
222 def to_masto_date(%NaiveDateTime{} = date) do
224 |> NaiveDateTime.to_iso8601()
225 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
228 def to_masto_date(date) do
231 |> NaiveDateTime.from_iso8601!()
232 |> NaiveDateTime.to_iso8601()
233 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
239 defp shortname(name) do
240 if String.length(name) < 30 do
243 String.slice(name, 0..30) <> "…"
247 def confirm_current_password(user, password) do
248 with %User{local: true} = db_user <- Repo.get(User, user.id),
249 true <- Pbkdf2.checkpw(password, db_user.password_hash) do
252 _ -> {:error, "Invalid password."}
256 def emoji_from_profile(%{info: _info} = user) do
257 (Formatter.get_emoji(user.bio) ++ Formatter.get_emoji(user.name))
258 |> Enum.map(fn {shortcode, url} ->
261 "icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}#{url}"},
262 "name" => ":#{shortcode}:"