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
9 alias Pleroma.Formatter
14 alias Pleroma.Web.Endpoint
15 alias Pleroma.Web.MediaProxy
16 alias Pleroma.Web.ActivityPub.Utils
18 # This is a hack for twidere.
19 def get_by_id_or_ap_id(id) do
20 activity = Repo.get(Activity, id) || Activity.get_create_by_object_ap_id(id)
23 if activity.data["type"] == "Create" do
26 Activity.get_create_by_object_ap_id(activity.data["object"])
30 def get_replied_to_activity(""), do: nil
32 def get_replied_to_activity(id) when not is_nil(id) do
33 Repo.get(Activity, id)
36 def get_replied_to_activity(_), do: nil
38 def attachments_from_ids(data) do
39 if Map.has_key?(data, "descriptions") do
40 attachments_from_ids_descs(data["media_ids"], data["descriptions"])
42 attachments_from_ids_no_descs(data["media_ids"])
46 def attachments_from_ids_no_descs(ids) do
47 Enum.map(ids || [], fn media_id ->
48 Repo.get(Object, media_id).data
52 def attachments_from_ids_descs(ids, descs_str) do
53 {_, descs} = Jason.decode(descs_str)
55 Enum.map(ids || [], fn media_id ->
56 Map.put(Repo.get(Object, media_id).data, "name", descs[media_id])
60 def to_for_user_and_mentions(user, mentions, inReplyTo, "public") do
61 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
63 to = ["https://www.w3.org/ns/activitystreams#Public" | mentioned_users]
64 cc = [user.follower_address]
67 {Enum.uniq([inReplyTo.data["actor"] | to]), cc}
73 def to_for_user_and_mentions(user, mentions, inReplyTo, "unlisted") do
74 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
76 to = [user.follower_address | mentioned_users]
77 cc = ["https://www.w3.org/ns/activitystreams#Public"]
80 {Enum.uniq([inReplyTo.data["actor"] | to]), cc}
86 def to_for_user_and_mentions(user, mentions, inReplyTo, "private") do
87 {to, cc} = to_for_user_and_mentions(user, mentions, inReplyTo, "direct")
88 {[user.follower_address | to], cc}
91 def to_for_user_and_mentions(_user, mentions, inReplyTo, "direct") do
92 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
95 {Enum.uniq([inReplyTo.data["actor"] | mentioned_users]), []}
101 def make_content_html(
107 no_attachment_links \\ false
110 |> format_input(mentions, tags, content_type)
111 |> maybe_add_attachments(attachments, no_attachment_links)
114 def make_context(%Activity{data: %{"context" => context}}), do: context
115 def make_context(_), do: Utils.generate_context_id()
117 def maybe_add_attachments(text, _attachments, true = _no_links), do: text
119 def maybe_add_attachments(text, attachments, _no_links) do
120 add_attachments(text, attachments)
123 def add_attachments(text, attachments) do
125 Enum.map(attachments, fn
126 %{"url" => [%{"href" => href} | _]} = attachment ->
127 name = attachment["name"] || URI.decode(Path.basename(href))
128 href = MediaProxy.url(href)
129 "<a href=\"#{href}\" class='attachment'>#{shortname(name)}</a>"
135 Enum.join([text | attachment_text], "<br>")
138 def format_input(text, mentions, tags, format, options \\ [])
141 Formatting text to plain text.
143 def format_input(text, mentions, tags, "text/plain", options) do
145 |> Formatter.html_escape("text/plain")
146 |> String.replace(~r/\r?\n/, "<br>")
148 |> Formatter.add_links()
149 |> Formatter.add_user_links(mentions, options[:user_links] || [])
150 |> Formatter.add_hashtag_links(tags)
151 |> Formatter.finalize()
155 Formatting text to html.
157 def format_input(text, mentions, _tags, "text/html", options) do
159 |> Formatter.html_escape("text/html")
161 |> Formatter.add_user_links(mentions, options[:user_links] || [])
162 |> Formatter.finalize()
166 Formatting text to markdown.
168 def format_input(text, mentions, tags, "text/markdown", options) do
170 |> Formatter.mentions_escape(mentions)
171 |> Earmark.as_html!()
172 |> Formatter.html_escape("text/html")
174 |> Formatter.add_user_links(mentions, options[:user_links] || [])
175 |> Formatter.add_hashtag_links(tags)
176 |> Formatter.finalize()
179 def add_tag_links(text, tags) do
182 |> Enum.sort_by(fn {tag, _} -> -String.length(tag) end)
184 Enum.reduce(tags, text, fn {full, tag}, text ->
185 url = "<a href='#{Web.base_url()}/tag/#{tag}' rel='tag'>##{tag}</a>"
186 String.replace(text, full, url)
205 "content" => content_html,
207 "context" => context,
208 "attachment" => attachments,
210 "tag" => tags |> Enum.map(fn {_, tag} -> tag end) |> Enum.uniq()
215 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
216 |> Map.put("inReplyToStatusId", inReplyTo.id)
222 def format_naive_asctime(date) do
223 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
226 def format_asctime(date) do
227 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
230 def date_to_asctime(date) do
231 with {:ok, date, _offset} <- date |> DateTime.from_iso8601() do
239 def to_masto_date(%NaiveDateTime{} = date) do
241 |> NaiveDateTime.to_iso8601()
242 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
245 def to_masto_date(date) do
248 |> NaiveDateTime.from_iso8601!()
249 |> NaiveDateTime.to_iso8601()
250 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
256 defp shortname(name) do
257 if String.length(name) < 30 do
260 String.slice(name, 0..30) <> "…"
264 def confirm_current_password(user, password) do
265 with %User{local: true} = db_user <- Repo.get(User, user.id),
266 true <- Pbkdf2.checkpw(password, db_user.password_hash) do
269 _ -> {:error, "Invalid password."}
273 def emoji_from_profile(%{info: _info} = user) do
274 (Formatter.get_emoji(user.bio) ++ Formatter.get_emoji(user.name))
275 |> Enum.map(fn {shortcode, url} ->
278 "icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}#{url}"},
279 "name" => ":#{shortcode}:"
284 def maybe_notify_to_recipients(
286 %Activity{data: %{"to" => to, "type" => _type}} = _activity
291 def maybe_notify_mentioned_recipients(
293 %Activity{data: %{"to" => _to, "type" => type} = data} = _activity
295 when type == "Create" do
296 object = Object.normalize(data["object"])
303 is_map(data["object"]) ->
310 tagged_mentions = maybe_extract_mentions(object_data)
312 recipients ++ tagged_mentions
315 def maybe_notify_mentioned_recipients(recipients, _), do: recipients
317 def maybe_extract_mentions(%{"tag" => tag}) do
319 |> Enum.filter(fn x -> is_map(x) end)
320 |> Enum.filter(fn x -> x["type"] == "Mention" end)
321 |> Enum.map(fn x -> x["href"] end)
324 def maybe_extract_mentions(_), do: []