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.ActivityPub.Utils
11 alias Pleroma.Web.Endpoint
12 alias Pleroma.Web.MediaProxy
14 # This is a hack for twidere.
15 def get_by_id_or_ap_id(id) do
16 activity = Repo.get(Activity, id) || Activity.get_create_by_object_ap_id(id)
19 if activity.data["type"] == "Create" do
22 Activity.get_create_by_object_ap_id(activity.data["object"])
26 def get_replied_to_activity(""), do: nil
28 def get_replied_to_activity(id) when not is_nil(id) do
29 Repo.get(Activity, id)
32 def get_replied_to_activity(_), do: nil
34 def attachments_from_ids(ids) do
35 Enum.map(ids || [], fn media_id ->
36 Repo.get(Object, media_id).data
40 def to_for_user_and_mentions(user, mentions, inReplyTo, "public") do
41 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
43 to = ["https://www.w3.org/ns/activitystreams#Public" | mentioned_users]
44 cc = [user.follower_address]
47 {Enum.uniq([inReplyTo.data["actor"] | to]), cc}
53 def to_for_user_and_mentions(user, mentions, inReplyTo, "unlisted") do
54 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
56 to = [user.follower_address | mentioned_users]
57 cc = ["https://www.w3.org/ns/activitystreams#Public"]
60 {Enum.uniq([inReplyTo.data["actor"] | to]), cc}
66 def to_for_user_and_mentions(user, mentions, inReplyTo, "private") do
67 {to, cc} = to_for_user_and_mentions(user, mentions, inReplyTo, "direct")
68 {[user.follower_address | to], cc}
71 def to_for_user_and_mentions(_user, mentions, inReplyTo, "direct") do
72 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
75 {Enum.uniq([inReplyTo.data["actor"] | mentioned_users]), []}
81 def make_content_html(
87 no_attachment_links \\ false
90 |> format_input(mentions, tags, content_type)
91 |> maybe_add_attachments(attachments, no_attachment_links)
94 def make_context(%Activity{data: %{"context" => context}}), do: context
95 def make_context(_), do: Utils.generate_context_id()
97 def maybe_add_attachments(text, _attachments, _no_links = true), do: text
99 def maybe_add_attachments(text, attachments, _no_links) do
100 add_attachments(text, attachments)
103 def add_attachments(text, attachments) do
105 Enum.map(attachments, fn
106 %{"url" => [%{"href" => href} | _]} = attachment ->
107 name = attachment["name"] || URI.decode(Path.basename(href))
108 href = MediaProxy.url(href)
109 "<a href=\"#{href}\" class='attachment'>#{shortname(name)}</a>"
115 Enum.join([text | attachment_text], "<br>")
118 def format_input(text, mentions, tags, format, options \\ [])
121 Formatting text to plain text.
123 def format_input(text, mentions, tags, "text/plain", options) do
125 |> Formatter.html_escape("text/plain")
126 |> String.replace(~r/\r?\n/, "<br>")
128 |> Formatter.add_links()
129 |> Formatter.add_user_links(mentions, options[:user_links] || [])
130 |> Formatter.add_hashtag_links(tags)
131 |> Formatter.finalize()
135 Formatting text to html.
137 def format_input(text, mentions, _tags, "text/html", options) do
139 |> Formatter.html_escape("text/html")
141 |> Formatter.add_user_links(mentions, options[:user_links] || [])
142 |> Formatter.finalize()
146 Formatting text to markdown.
148 def format_input(text, mentions, tags, "text/markdown", options) do
150 |> Formatter.mentions_escape(mentions)
151 |> Earmark.as_html!()
152 |> Formatter.html_escape("text/html")
154 |> Formatter.add_user_links(mentions, options[:user_links] || [])
155 |> Formatter.add_hashtag_links(tags)
156 |> Formatter.finalize()
159 def add_tag_links(text, tags) do
162 |> Enum.sort_by(fn {tag, _} -> -String.length(tag) end)
164 Enum.reduce(tags, text, fn {full, tag}, text ->
165 url = "<a href='#{Web.base_url()}/tag/#{tag}' rel='tag'>##{tag}</a>"
166 String.replace(text, full, url)
185 "content" => content_html,
187 "context" => context,
188 "attachment" => attachments,
190 "tag" => tags |> Enum.map(fn {_, tag} -> tag end) |> Enum.uniq()
195 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
196 |> Map.put("inReplyToStatusId", inReplyTo.id)
202 def format_naive_asctime(date) do
203 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
206 def format_asctime(date) do
207 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
210 def date_to_asctime(date) do
211 with {:ok, date, _offset} <- date |> DateTime.from_iso8601() do
219 def to_masto_date(%NaiveDateTime{} = date) do
221 |> NaiveDateTime.to_iso8601()
222 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
225 def to_masto_date(date) do
228 |> NaiveDateTime.from_iso8601!()
229 |> NaiveDateTime.to_iso8601()
230 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
236 defp shortname(name) do
237 if String.length(name) < 30 do
240 String.slice(name, 0..30) <> "…"
244 def confirm_current_password(user, password) do
245 with %User{local: true} = db_user <- Repo.get(User, user.id),
246 true <- Pbkdf2.checkpw(password, db_user.password_hash) do
249 _ -> {:error, "Invalid password."}
253 def emoji_from_profile(%{info: _info} = user) do
254 (Formatter.get_emoji(user.bio) ++ Formatter.get_emoji(user.name))
255 |> Enum.map(fn {shortcode, url} ->
258 "icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}#{url}"},
259 "name" => ":#{shortcode}:"
264 def maybe_notify_to_recipients(
266 %Activity{data: %{"to" => to, "type" => _type}} = _activity
271 def maybe_notify_mentioned_recipients(
273 %Activity{data: %{"to" => _to, "type" => type} = data} = _activity
275 when type == "Create" do
276 object = Object.normalize(data["object"])
283 is_map(data["object"]) ->
290 tagged_mentions = maybe_extract_mentions(object_data)
292 recipients ++ tagged_mentions
295 def maybe_notify_mentioned_recipients(recipients, _), do: recipients
297 def maybe_extract_mentions(%{"tag" => tag}) do
299 |> Enum.filter(fn x -> is_map(x) end)
300 |> Enum.filter(fn x -> x["type"] == "Mention" end)
301 |> Enum.map(fn x -> x["href"] end)
304 def maybe_extract_mentions(_), do: []