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}
9 alias Pleroma.{User, Web}
10 alias Pleroma.Web.{Endpoint, MediaProxy}
11 alias Pleroma.Web.ActivityPub.Utils
13 # This is a hack for twidere.
14 def get_by_id_or_ap_id(id) do
15 activity = Repo.get(Activity, id) || Activity.get_create_by_object_ap_id(id)
18 if activity.data["type"] == "Create" do
21 Activity.get_create_by_object_ap_id(activity.data["object"])
25 def get_replied_to_activity(""), do: nil
27 def get_replied_to_activity(id) when not is_nil(id) do
28 Repo.get(Activity, id)
31 def get_replied_to_activity(_), do: nil
33 def attachments_from_ids(ids) do
34 Enum.map(ids || [], fn media_id ->
35 Repo.get(Object, media_id).data
39 def to_for_user_and_mentions(user, mentions, inReplyTo, "public") do
40 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
42 to = ["https://www.w3.org/ns/activitystreams#Public" | mentioned_users]
43 cc = [user.follower_address]
46 {Enum.uniq([inReplyTo.data["actor"] | to]), cc}
52 def to_for_user_and_mentions(user, mentions, inReplyTo, "unlisted") do
53 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
55 to = [user.follower_address | mentioned_users]
56 cc = ["https://www.w3.org/ns/activitystreams#Public"]
59 {Enum.uniq([inReplyTo.data["actor"] | to]), cc}
65 def to_for_user_and_mentions(user, mentions, inReplyTo, "private") do
66 {to, cc} = to_for_user_and_mentions(user, mentions, inReplyTo, "direct")
67 {[user.follower_address | to], cc}
70 def to_for_user_and_mentions(_user, mentions, inReplyTo, "direct") do
71 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
74 {Enum.uniq([inReplyTo.data["actor"] | mentioned_users]), []}
80 def make_content_html(
86 no_attachment_links \\ false
89 |> format_input(mentions, tags, content_type)
90 |> maybe_add_attachments(attachments, no_attachment_links)
93 def make_context(%Activity{data: %{"context" => context}}), do: context
94 def make_context(_), do: Utils.generate_context_id()
96 def maybe_add_attachments(text, _attachments, true = _no_links), do: text
98 def maybe_add_attachments(text, attachments, _no_links) do
99 add_attachments(text, attachments)
102 def add_attachments(text, attachments) do
104 Enum.map(attachments, fn
105 %{"url" => [%{"href" => href} | _]} = attachment ->
106 name = attachment["name"] || URI.decode(Path.basename(href))
107 href = MediaProxy.url(href)
108 "<a href=\"#{href}\" class='attachment'>#{shortname(name)}</a>"
114 Enum.join([text | attachment_text], "<br>")
117 def format_input(text, mentions, tags, format, options \\ [])
120 Formatting text to plain text.
122 def format_input(text, mentions, tags, "text/plain", options) do
124 |> Formatter.html_escape("text/plain")
125 |> String.replace(~r/\r?\n/, "<br>")
127 |> Formatter.add_links()
128 |> Formatter.add_user_links(mentions, options[:user_links] || [])
129 |> Formatter.add_hashtag_links(tags)
130 |> Formatter.finalize()
134 Formatting text to html.
136 def format_input(text, mentions, _tags, "text/html", options) do
138 |> Formatter.html_escape("text/html")
140 |> Formatter.add_user_links(mentions, options[:user_links] || [])
141 |> Formatter.finalize()
145 Formatting text to markdown.
147 def format_input(text, mentions, tags, "text/markdown", options) do
149 |> Formatter.mentions_escape(mentions)
150 |> Earmark.as_html!()
151 |> Formatter.html_escape("text/html")
153 |> Formatter.add_user_links(mentions, options[:user_links] || [])
154 |> Formatter.add_hashtag_links(tags)
155 |> Formatter.finalize()
158 def add_tag_links(text, tags) do
161 |> Enum.sort_by(fn {tag, _} -> -String.length(tag) end)
163 Enum.reduce(tags, text, fn {full, tag}, text ->
164 url = "<a href='#{Web.base_url()}/tag/#{tag}' rel='tag'>##{tag}</a>"
165 String.replace(text, full, url)
184 "content" => content_html,
186 "context" => context,
187 "attachment" => attachments,
189 "tag" => tags |> Enum.map(fn {_, tag} -> tag end) |> Enum.uniq()
194 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
195 |> Map.put("inReplyToStatusId", inReplyTo.id)
201 def format_naive_asctime(date) do
202 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
205 def format_asctime(date) do
206 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
209 def date_to_asctime(date) do
210 with {:ok, date, _offset} <- date |> DateTime.from_iso8601() do
218 def to_masto_date(%NaiveDateTime{} = date) do
220 |> NaiveDateTime.to_iso8601()
221 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
224 def to_masto_date(date) do
227 |> NaiveDateTime.from_iso8601!()
228 |> NaiveDateTime.to_iso8601()
229 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
235 defp shortname(name) do
236 if String.length(name) < 30 do
239 String.slice(name, 0..30) <> "…"
243 def confirm_current_password(user, password) do
244 with %User{local: true} = db_user <- Repo.get(User, user.id),
245 true <- Pbkdf2.checkpw(password, db_user.password_hash) do
248 _ -> {:error, "Invalid password."}
252 def emoji_from_profile(%{info: _info} = user) do
253 (Formatter.get_emoji(user.bio) ++ Formatter.get_emoji(user.name))
254 |> Enum.map(fn {shortcode, url} ->
257 "icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}#{url}"},
258 "name" => ":#{shortcode}:"
263 def maybe_notify_to_recipients(
265 %Activity{data: %{"to" => to, "type" => _type}} = _activity
270 def maybe_notify_mentioned_recipients(
272 %Activity{data: %{"to" => _to, "type" => type} = data} = _activity
274 when type == "Create" do
275 object = Object.normalize(data["object"])
282 is_map(data["object"]) ->
289 tagged_mentions = maybe_extract_mentions(object_data)
291 recipients ++ tagged_mentions
294 def maybe_notify_mentioned_recipients(recipients, _), do: recipients
296 def maybe_extract_mentions(%{"tag" => tag}) do
298 |> Enum.filter(fn x -> is_map(x) end)
299 |> Enum.filter(fn x -> x["type"] == "Mention" end)
300 |> Enum.map(fn x -> x["href"] end)
303 def maybe_extract_mentions(_), do: []