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(ids) do
39 Enum.map(ids || [], fn media_id ->
40 Repo.get(Object, media_id).data
44 def to_for_user_and_mentions(user, mentions, inReplyTo, "public") do
45 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
47 to = ["https://www.w3.org/ns/activitystreams#Public" | mentioned_users]
48 cc = [user.follower_address]
51 {Enum.uniq([inReplyTo.data["actor"] | to]), cc}
57 def to_for_user_and_mentions(user, mentions, inReplyTo, "unlisted") do
58 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
60 to = [user.follower_address | mentioned_users]
61 cc = ["https://www.w3.org/ns/activitystreams#Public"]
64 {Enum.uniq([inReplyTo.data["actor"] | to]), cc}
70 def to_for_user_and_mentions(user, mentions, inReplyTo, "private") do
71 {to, cc} = to_for_user_and_mentions(user, mentions, inReplyTo, "direct")
72 {[user.follower_address | to], cc}
75 def to_for_user_and_mentions(_user, mentions, inReplyTo, "direct") do
76 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
79 {Enum.uniq([inReplyTo.data["actor"] | mentioned_users]), []}
85 def make_content_html(
91 no_attachment_links \\ false
94 |> format_input(mentions, tags, content_type)
95 |> maybe_add_attachments(attachments, no_attachment_links)
98 def make_context(%Activity{data: %{"context" => context}}), do: context
99 def make_context(_), do: Utils.generate_context_id()
101 def maybe_add_attachments(text, _attachments, true = _no_links), do: text
103 def maybe_add_attachments(text, attachments, _no_links) do
104 add_attachments(text, attachments)
107 def add_attachments(text, attachments) do
109 Enum.map(attachments, fn
110 %{"url" => [%{"href" => href} | _]} = attachment ->
111 name = attachment["name"] || URI.decode(Path.basename(href))
112 href = MediaProxy.url(href)
113 "<a href=\"#{href}\" class='attachment'>#{shortname(name)}</a>"
119 Enum.join([text | attachment_text], "<br>")
122 def format_input(text, mentions, tags, format, options \\ [])
125 Formatting text to plain text.
127 def format_input(text, mentions, tags, "text/plain", options) do
129 |> Formatter.html_escape("text/plain")
130 |> String.replace(~r/\r?\n/, "<br>")
132 |> Formatter.add_links()
133 |> Formatter.add_user_links(mentions, options[:user_links] || [])
134 |> Formatter.add_hashtag_links(tags)
135 |> Formatter.finalize()
139 Formatting text to html.
141 def format_input(text, mentions, _tags, "text/html", options) do
143 |> Formatter.html_escape("text/html")
145 |> Formatter.add_user_links(mentions, options[:user_links] || [])
146 |> Formatter.finalize()
150 Formatting text to markdown.
152 def format_input(text, mentions, tags, "text/markdown", options) do
154 |> Formatter.mentions_escape(mentions)
155 |> Earmark.as_html!()
156 |> Formatter.html_escape("text/html")
158 |> Formatter.add_user_links(mentions, options[:user_links] || [])
159 |> Formatter.add_hashtag_links(tags)
160 |> Formatter.finalize()
163 def add_tag_links(text, tags) do
166 |> Enum.sort_by(fn {tag, _} -> -String.length(tag) end)
168 Enum.reduce(tags, text, fn {full, tag}, text ->
169 url = "<a href='#{Web.base_url()}/tag/#{tag}' rel='tag'>##{tag}</a>"
170 String.replace(text, full, url)
189 "content" => content_html,
191 "context" => context,
192 "attachment" => attachments,
194 "tag" => tags |> Enum.map(fn {_, tag} -> tag end) |> Enum.uniq()
199 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
200 |> Map.put("inReplyToStatusId", inReplyTo.id)
206 def format_naive_asctime(date) do
207 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
210 def format_asctime(date) do
211 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
214 def date_to_asctime(date) do
215 with {:ok, date, _offset} <- date |> DateTime.from_iso8601() do
223 def to_masto_date(%NaiveDateTime{} = date) do
225 |> NaiveDateTime.to_iso8601()
226 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
229 def to_masto_date(date) do
232 |> NaiveDateTime.from_iso8601!()
233 |> NaiveDateTime.to_iso8601()
234 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
240 defp shortname(name) do
241 if String.length(name) < 30 do
244 String.slice(name, 0..30) <> "…"
248 def confirm_current_password(user, password) do
249 with %User{local: true} = db_user <- Repo.get(User, user.id),
250 true <- Pbkdf2.checkpw(password, db_user.password_hash) do
253 _ -> {:error, "Invalid password."}
257 def emoji_from_profile(%{info: _info} = user) do
258 (Formatter.get_emoji(user.bio) ++ Formatter.get_emoji(user.name))
259 |> Enum.map(fn {shortcode, url} ->
262 "icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}#{url}"},
263 "name" => ":#{shortcode}:"
268 def maybe_notify_to_recipients(
270 %Activity{data: %{"to" => to, "type" => _type}} = _activity
275 def maybe_notify_mentioned_recipients(
277 %Activity{data: %{"to" => _to, "type" => type} = data} = _activity
279 when type == "Create" do
280 object = Object.normalize(data["object"])
287 is_map(data["object"]) ->
294 tagged_mentions = maybe_extract_mentions(object_data)
296 recipients ++ tagged_mentions
299 def maybe_notify_mentioned_recipients(recipients, _), do: recipients
301 def maybe_extract_mentions(%{"tag" => tag}) do
303 |> Enum.filter(fn x -> is_map(x) end)
304 |> Enum.filter(fn x -> x["type"] == "Mention" end)
305 |> Enum.map(fn x -> x["href"] end)
308 def maybe_extract_mentions(_), do: []