1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.CommonAPI.Utils do
6 import Pleroma.Web.Gettext
7 import Pleroma.Web.ControllerHelper, only: [truthy_param?: 1]
9 alias Calendar.Strftime
10 alias Pleroma.Activity
12 alias Pleroma.Conversation.Participation
13 alias Pleroma.Formatter
15 alias Pleroma.Plugs.AuthenticationPlug
18 alias Pleroma.Web.ActivityPub.Utils
19 alias Pleroma.Web.ActivityPub.Visibility
20 alias Pleroma.Web.MediaProxy
23 require Pleroma.Constants
25 # This is a hack for twidere.
26 def get_by_id_or_ap_id(id) do
28 with true <- FlakeId.flake_id?(id),
29 %Activity{} = activity <- Activity.get_by_id_with_object(id) do
32 _ -> Activity.get_create_by_object_ap_id_with_object(id)
36 if activity.data["type"] == "Create" do
39 Activity.get_create_by_object_ap_id_with_object(activity.data["object"])
43 def attachments_from_ids(%{"media_ids" => ids, "descriptions" => desc} = _) do
44 attachments_from_ids_descs(ids, desc)
47 def attachments_from_ids(%{"media_ids" => ids} = _) do
48 attachments_from_ids_no_descs(ids)
51 def attachments_from_ids(_), do: []
53 def attachments_from_ids_no_descs([]), do: []
55 def attachments_from_ids_no_descs(ids) do
56 Enum.map(ids, fn media_id ->
57 case Repo.get(Object, media_id) do
58 %Object{data: data} = _ -> data
65 def attachments_from_ids_descs([], _), do: []
67 def attachments_from_ids_descs(ids, descs_str) do
68 {_, descs} = Jason.decode(descs_str)
70 Enum.map(ids, fn media_id ->
71 case Repo.get(Object, media_id) do
72 %Object{data: data} = _ ->
73 Map.put(data, "name", descs[media_id])
87 Participation.t() | nil
88 ) :: {list(String.t()), list(String.t())}
90 def get_to_and_cc(_, _, _, _, %Participation{} = participation) do
91 participation = Repo.preload(participation, :recipients)
92 {Enum.map(participation.recipients, & &1.ap_id), []}
95 def get_to_and_cc(user, mentioned_users, inReplyTo, "public", _) do
96 to = [Pleroma.Constants.as_public() | mentioned_users]
97 cc = [user.follower_address]
100 {Enum.uniq([inReplyTo.data["actor"] | to]), cc}
106 def get_to_and_cc(user, mentioned_users, inReplyTo, "unlisted", _) do
107 to = [user.follower_address | mentioned_users]
108 cc = [Pleroma.Constants.as_public()]
111 {Enum.uniq([inReplyTo.data["actor"] | to]), cc}
117 def get_to_and_cc(user, mentioned_users, inReplyTo, "private", _) do
118 {to, cc} = get_to_and_cc(user, mentioned_users, inReplyTo, "direct", nil)
119 {[user.follower_address | to], cc}
122 def get_to_and_cc(_user, mentioned_users, inReplyTo, "direct", _) do
124 {Enum.uniq([inReplyTo.data["actor"] | mentioned_users]), []}
126 {mentioned_users, []}
130 def get_to_and_cc(_user, mentions, _inReplyTo, {:list, _}, _), do: {mentions, []}
132 def get_addressed_users(_, to) when is_list(to) do
133 User.get_ap_ids_by_nicknames(to)
136 def get_addressed_users(mentioned_users, _), do: mentioned_users
138 def maybe_add_list_data(activity_params, user, {:list, list_id}) do
139 case Pleroma.List.get(list_id, user) do
140 %Pleroma.List{} = list ->
142 |> put_in([:additional, "bcc"], [list.ap_id])
143 |> put_in([:additional, "listMessage"], list.ap_id)
144 |> put_in([:object, "listMessage"], list.ap_id)
151 def maybe_add_list_data(activity_params, _, _), do: activity_params
153 def make_poll_data(%{"poll" => %{"expires_in" => expires_in}} = data)
154 when is_binary(expires_in) do
155 # In some cases mastofe sends out strings instead of integers
157 |> put_in(["poll", "expires_in"], String.to_integer(expires_in))
161 def make_poll_data(%{"poll" => %{"options" => options, "expires_in" => expires_in}} = data)
162 when is_list(options) do
163 limits = Pleroma.Config.get([:instance, :poll_limits])
165 with :ok <- validate_poll_expiration(expires_in, limits),
166 :ok <- validate_poll_options_amount(options, limits),
167 :ok <- validate_poll_options_length(options, limits) do
168 {option_notes, emoji} =
169 Enum.map_reduce(options, %{}, fn option, emoji ->
173 "replies" => %{"type" => "Collection", "totalItems" => 0}
176 {note, Map.merge(emoji, Pleroma.Emoji.Formatter.get_emoji_map(option))}
181 |> DateTime.add(expires_in)
182 |> DateTime.to_iso8601()
184 key = if truthy_param?(data["poll"]["multiple"]), do: "anyOf", else: "oneOf"
185 poll = %{"type" => "Question", key => option_notes, "closed" => end_time}
191 def make_poll_data(%{"poll" => poll}) when is_map(poll) do
192 {:error, "Invalid poll"}
195 def make_poll_data(_data) do
199 defp validate_poll_options_amount(options, %{max_options: max_options}) do
200 if Enum.count(options) > max_options do
201 {:error, "Poll can't contain more than #{max_options} options"}
207 defp validate_poll_options_length(options, %{max_option_chars: max_option_chars}) do
208 if Enum.any?(options, &(String.length(&1) > max_option_chars)) do
209 {:error, "Poll options cannot be longer than #{max_option_chars} characters each"}
215 defp validate_poll_expiration(expires_in, %{min_expiration: min, max_expiration: max}) do
217 expires_in > max -> {:error, "Expiration date is too far in the future"}
218 expires_in < min -> {:error, "Expiration date is too soon"}
223 def make_content_html(
231 |> Map.get("attachment_links", Config.get([:instance, :attachment_links]))
234 content_type = get_content_type(data["content_type"])
237 if visibility == "direct" && Config.get([:instance, :safe_dm_mentions]) do
244 |> format_input(content_type, options)
245 |> maybe_add_attachments(attachments, attachment_links)
246 |> maybe_add_nsfw_tag(data)
249 defp get_content_type(content_type) do
250 if Enum.member?(Config.get([:instance, :allowed_post_formats]), content_type) do
257 defp maybe_add_nsfw_tag({text, mentions, tags}, %{"sensitive" => sensitive})
258 when sensitive in [true, "True", "true", "1"] do
259 {text, mentions, [{"#nsfw", "nsfw"} | tags]}
262 defp maybe_add_nsfw_tag(data, _), do: data
264 def make_context(_, %Participation{} = participation) do
265 Repo.preload(participation, :conversation).conversation.ap_id
268 def make_context(%Activity{data: %{"context" => context}}, _), do: context
269 def make_context(_, _), do: Utils.generate_context_id()
271 def maybe_add_attachments(parsed, _attachments, false = _no_links), do: parsed
273 def maybe_add_attachments({text, mentions, tags}, attachments, _no_links) do
274 text = add_attachments(text, attachments)
275 {text, mentions, tags}
278 def add_attachments(text, attachments) do
279 attachment_text = Enum.map(attachments, &build_attachment_link/1)
280 Enum.join([text | attachment_text], "<br>")
283 defp build_attachment_link(%{"url" => [%{"href" => href} | _]} = attachment) do
284 name = attachment["name"] || URI.decode(Path.basename(href))
285 href = MediaProxy.url(href)
286 "<a href=\"#{href}\" class='attachment'>#{shortname(name)}</a>"
289 defp build_attachment_link(_), do: ""
291 def format_input(text, format, options \\ [])
294 Formatting text to plain text.
296 def format_input(text, "text/plain", options) do
298 |> Formatter.html_escape("text/plain")
299 |> Formatter.linkify(options)
300 |> (fn {text, mentions, tags} ->
301 {String.replace(text, ~r/\r?\n/, "<br>"), mentions, tags}
306 Formatting text as BBCode.
308 def format_input(text, "text/bbcode", options) do
310 |> String.replace(~r/\r/, "")
311 |> Formatter.html_escape("text/plain")
313 |> (fn {:ok, html} -> html end).()
314 |> Formatter.linkify(options)
318 Formatting text to html.
320 def format_input(text, "text/html", options) do
322 |> Formatter.html_escape("text/html")
323 |> Formatter.linkify(options)
327 Formatting text to markdown.
329 def format_input(text, "text/markdown", options) do
331 |> Formatter.mentions_escape(options)
332 |> Earmark.as_html!(%Earmark.Options{renderer: Pleroma.EarmarkRenderer})
333 |> Formatter.linkify(options)
334 |> Formatter.html_escape("text/html")
354 "content" => content_html,
355 "summary" => summary,
356 "sensitive" => truthy_param?(sensitive),
357 "context" => context,
358 "attachment" => attachments,
360 "tag" => Keyword.values(tags) |> Enum.uniq()
362 |> add_in_reply_to(in_reply_to)
363 |> Map.merge(extra_params)
366 defp add_in_reply_to(object, nil), do: object
368 defp add_in_reply_to(object, in_reply_to) do
369 with %Object{} = in_reply_to_object <- Object.normalize(in_reply_to) do
370 Map.put(object, "inReplyTo", in_reply_to_object.data["id"])
376 def format_naive_asctime(date) do
377 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
380 def format_asctime(date) do
381 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
384 def date_to_asctime(date) when is_binary(date) do
385 with {:ok, date, _offset} <- DateTime.from_iso8601(date) do
389 Logger.warn("Date #{date} in wrong format, must be ISO 8601")
394 def date_to_asctime(date) do
395 Logger.warn("Date #{date} in wrong format, must be ISO 8601")
399 def to_masto_date(%NaiveDateTime{} = date) do
401 |> NaiveDateTime.to_iso8601()
402 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
405 def to_masto_date(date) when is_binary(date) do
406 with {:ok, date} <- NaiveDateTime.from_iso8601(date) do
413 def to_masto_date(_), do: ""
415 defp shortname(name) do
416 if String.length(name) < 30 do
419 String.slice(name, 0..30) <> "…"
423 def confirm_current_password(user, password) do
424 with %User{local: true} = db_user <- User.get_cached_by_id(user.id),
425 true <- AuthenticationPlug.checkpw(password, db_user.password_hash) do
428 _ -> {:error, dgettext("errors", "Invalid password.")}
432 def maybe_notify_to_recipients(
434 %Activity{data: %{"to" => to, "type" => _type}} = _activity
439 def maybe_notify_to_recipients(recipients, _), do: recipients
441 def maybe_notify_mentioned_recipients(
443 %Activity{data: %{"to" => _to, "type" => type} = data} = activity
445 when type == "Create" do
446 object = Object.normalize(activity)
450 not is_nil(object) ->
453 is_map(data["object"]) ->
460 tagged_mentions = maybe_extract_mentions(object_data)
462 recipients ++ tagged_mentions
465 def maybe_notify_mentioned_recipients(recipients, _), do: recipients
467 # Do not notify subscribers if author is making a reply
468 def maybe_notify_subscribers(recipients, %Activity{
469 object: %Object{data: %{"inReplyTo" => _ap_id}}
474 def maybe_notify_subscribers(
476 %Activity{data: %{"actor" => actor, "type" => type}} = activity
478 when type == "Create" do
479 with %User{} = user <- User.get_cached_by_ap_id(actor) do
482 |> User.subscriber_users()
483 |> Enum.filter(&Visibility.visible_for_user?(activity, &1))
484 |> Enum.map(& &1.ap_id)
486 recipients ++ subscriber_ids
490 def maybe_notify_subscribers(recipients, _), do: recipients
492 def maybe_notify_followers(recipients, %Activity{data: %{"type" => "Move"}} = activity) do
493 with %User{} = user <- User.get_cached_by_ap_id(activity.actor) do
495 |> User.get_followers()
496 |> Enum.map(& &1.ap_id)
497 |> Enum.concat(recipients)
501 def maybe_notify_followers(recipients, _), do: recipients
503 def maybe_extract_mentions(%{"tag" => tag}) do
505 |> Enum.filter(fn x -> is_map(x) && x["type"] == "Mention" end)
506 |> Enum.map(fn x -> x["href"] end)
510 def maybe_extract_mentions(_), do: []
512 def make_report_content_html(nil), do: {:ok, {nil, [], []}}
514 def make_report_content_html(comment) do
515 max_size = Pleroma.Config.get([:instance, :max_report_comment_size], 1000)
517 if String.length(comment) <= max_size do
518 {:ok, format_input(comment, "text/plain")}
521 dgettext("errors", "Comment must be up to %{max_size} characters", max_size: max_size)}
525 def get_report_statuses(%User{ap_id: actor}, %{"status_ids" => status_ids}) do
526 {:ok, Activity.all_by_actor_and_id(actor, status_ids)}
529 def get_report_statuses(_, _), do: {:ok, nil}
531 # DEPRECATED mostly, context objects are now created at insertion time.
532 def context_to_conversation_id(context) do
533 with %Object{id: id} <- Object.get_cached_by_ap_id(context) do
537 changeset = Object.context_mapping(context)
539 case Repo.insert(changeset) do
543 # This should be solved by an upsert, but it seems ecto
544 # has problems accessing the constraint inside the jsonb.
546 Object.get_cached_by_ap_id(context).id
551 def conversation_id_to_context(id) do
552 with %Object{data: %{"id" => context}} <- Repo.get(Object, id) do
556 {:error, dgettext("errors", "No such conversation")}
560 def make_answer_data(%User{ap_id: ap_id}, object, name) do
564 "cc" => [object.data["actor"]],
567 "inReplyTo" => object.data["id"]
571 def validate_character_limit("" = _full_payload, [] = _attachments) do
572 {:error, dgettext("errors", "Cannot post an empty status without attachments")}
575 def validate_character_limit(full_payload, _attachments) do
576 limit = Pleroma.Config.get([:instance, :limit])
577 length = String.length(full_payload)
579 if length <= limit do
582 {:error, dgettext("errors", "The status is over the character limit")}