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_by_object_ap_id(id)
20 if activity.data["type"] == "Create" do
23 Activity.get_create_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) do
36 Enum.map(ids || [], fn media_id ->
37 Repo.get(Object, media_id).data
41 def to_for_user_and_mentions(user, mentions, inReplyTo, "public") do
42 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
44 to = ["https://www.w3.org/ns/activitystreams#Public" | mentioned_users]
45 cc = [user.follower_address]
48 {Enum.uniq([inReplyTo.data["actor"] | to]), cc}
54 def to_for_user_and_mentions(user, mentions, inReplyTo, "unlisted") do
55 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
57 to = [user.follower_address | mentioned_users]
58 cc = ["https://www.w3.org/ns/activitystreams#Public"]
61 {Enum.uniq([inReplyTo.data["actor"] | to]), cc}
67 def to_for_user_and_mentions(user, mentions, inReplyTo, "private") do
68 {to, cc} = to_for_user_and_mentions(user, mentions, inReplyTo, "direct")
69 {[user.follower_address | to], cc}
72 def to_for_user_and_mentions(_user, mentions, inReplyTo, "direct") do
73 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
76 {Enum.uniq([inReplyTo.data["actor"] | mentioned_users]), []}
82 def make_content_html(
88 no_attachment_links \\ false
91 |> format_input(mentions, tags, content_type)
92 |> maybe_add_attachments(attachments, no_attachment_links)
95 def make_context(%Activity{data: %{"context" => context}}), do: context
96 def make_context(_), do: Utils.generate_context_id()
98 def maybe_add_attachments(text, _attachments, _no_links = true), do: text
100 def maybe_add_attachments(text, attachments, _no_links) do
101 add_attachments(text, attachments)
104 def add_attachments(text, attachments) do
106 Enum.map(attachments, fn
107 %{"url" => [%{"href" => href} | _]} = attachment ->
108 name = attachment["name"] || URI.decode(Path.basename(href))
109 href = MediaProxy.url(href)
110 "<a href=\"#{href}\" class='attachment'>#{shortname(name)}</a>"
116 Enum.join([text | attachment_text], "<br>")
119 def format_input(text, mentions, tags, format, options \\ [])
122 Formatting text to plain text.
124 def format_input(text, mentions, tags, "text/plain", options) do
126 |> Formatter.html_escape("text/plain")
127 |> String.replace(~r/\r?\n/, "<br>")
129 |> Formatter.add_links()
130 |> Formatter.add_user_links(mentions, options[:user_links] || [])
131 |> Formatter.add_hashtag_links(tags)
132 |> Formatter.finalize()
136 Formatting text to html.
138 def format_input(text, mentions, _tags, "text/html", options) do
140 |> Formatter.html_escape("text/html")
142 |> Formatter.add_user_links(mentions, options[:user_links] || [])
143 |> Formatter.finalize()
147 Formatting text to markdown.
149 def format_input(text, mentions, tags, "text/markdown", options) do
151 |> Formatter.mentions_escape(mentions)
152 |> Earmark.as_html!()
153 |> Formatter.html_escape("text/html")
155 |> Formatter.add_user_links(mentions, options[:user_links] || [])
156 |> Formatter.add_hashtag_links(tags)
157 |> Formatter.finalize()
160 def add_tag_links(text, tags) do
163 |> Enum.sort_by(fn {tag, _} -> -String.length(tag) end)
165 Enum.reduce(tags, text, fn {full, tag}, text ->
166 url = "<a href='#{Web.base_url()}/tag/#{tag}' rel='tag'>##{tag}</a>"
167 String.replace(text, full, url)
186 "content" => content_html,
188 "context" => context,
189 "attachment" => attachments,
191 "tag" => tags |> Enum.map(fn {_, tag} -> tag end) |> Enum.uniq()
196 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
197 |> Map.put("inReplyToStatusId", inReplyTo.id)
203 def format_naive_asctime(date) do
204 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
207 def format_asctime(date) do
208 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
211 def date_to_asctime(date) do
212 with {:ok, date, _offset} <- date |> DateTime.from_iso8601() do
220 def to_masto_date(%NaiveDateTime{} = date) do
222 |> NaiveDateTime.to_iso8601()
223 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
226 def to_masto_date(date) do
229 |> NaiveDateTime.from_iso8601!()
230 |> NaiveDateTime.to_iso8601()
231 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
237 defp shortname(name) do
238 if String.length(name) < 30 do
241 String.slice(name, 0..30) <> "…"
245 def confirm_current_password(user, password) do
246 with %User{local: true} = db_user <- Repo.get(User, user.id),
247 true <- Pbkdf2.checkpw(password, db_user.password_hash) do
250 _ -> {:error, "Invalid password."}
254 def emoji_from_profile(%{info: _info} = user) do
255 (Formatter.get_emoji(user.bio) ++ Formatter.get_emoji(user.name))
256 |> Enum.map(fn {shortcode, url} ->
259 "icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}#{url}"},
260 "name" => ":#{shortcode}:"
265 def maybe_notify_to_recipients(
267 %Activity{data: %{"to" => to, "type" => _type}} = _activity
272 def maybe_notify_mentioned_recipients(
274 %Activity{data: %{"to" => _to, "type" => type} = data} = _activity
276 when type == "Create" do
277 object = Object.normalize(data["object"])
284 is_map(data["object"]) ->
291 tagged_mentions = maybe_extract_mentions(object_data)
293 recipients ++ tagged_mentions
296 def maybe_notify_mentioned_recipients(recipients, _), do: recipients
298 def maybe_extract_mentions(%{"tag" => tag}) do
300 |> Enum.filter(fn x -> is_map(x) end)
301 |> Enum.filter(fn x -> x["type"] == "Mention" end)
302 |> Enum.map(fn x -> x["href"] end)
305 def maybe_extract_mentions(_), do: []