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}
11 alias Pleroma.Web.ActivityPub.Utils
12 alias Pleroma.Web.Endpoint
13 alias Pleroma.Web.MediaProxy
15 # This is a hack for twidere.
16 def get_by_id_or_ap_id(id) do
17 activity = Repo.get(Activity, id) || Activity.get_create_activity_by_object_ap_id(id)
20 if activity.data["type"] == "Create" do
23 Activity.get_create_activity_by_object_ap_id(activity.data["object"])
27 def get_replied_to_activity(""), do: nil
29 def get_replied_to_activity(id) when not is_nil(id) do
30 Repo.get(Activity, id)
33 def get_replied_to_activity(_), do: nil
35 def attachments_from_ids(data) do
36 if Map.has_key?(data, "descriptions") do
37 attachments_from_ids_descs(data["media_ids"], data["descriptions"])
39 attachments_from_ids_no_descs(data["media_ids"])
43 def attachments_from_ids_no_descs(ids) do
44 Enum.map(ids || [], fn media_id ->
45 Repo.get(Object, media_id).data
49 def attachments_from_ids_descs(ids, descs_str) do
50 {_, descs} = Jason.decode(descs_str)
52 Enum.map(ids || [], fn media_id ->
53 Map.put(Repo.get(Object, media_id).data, "name", descs[media_id])
57 def to_for_user_and_mentions(user, mentions, inReplyTo, "public") do
58 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
60 to = ["https://www.w3.org/ns/activitystreams#Public" | mentioned_users]
61 cc = [user.follower_address]
64 {Enum.uniq([inReplyTo.data["actor"] | to]), cc}
70 def to_for_user_and_mentions(user, mentions, inReplyTo, "unlisted") do
71 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
73 to = [user.follower_address | mentioned_users]
74 cc = ["https://www.w3.org/ns/activitystreams#Public"]
77 {Enum.uniq([inReplyTo.data["actor"] | to]), cc}
83 def to_for_user_and_mentions(user, mentions, inReplyTo, "private") do
84 {to, cc} = to_for_user_and_mentions(user, mentions, inReplyTo, "direct")
85 {[user.follower_address | to], cc}
88 def to_for_user_and_mentions(_user, mentions, inReplyTo, "direct") do
89 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
92 {Enum.uniq([inReplyTo.data["actor"] | mentioned_users]), []}
98 def make_content_html(
104 no_attachment_links \\ false
107 |> format_input(mentions, tags, content_type)
108 |> maybe_add_attachments(attachments, no_attachment_links)
111 def make_context(%Activity{data: %{"context" => context}}), do: context
112 def make_context(_), do: Utils.generate_context_id()
114 def maybe_add_attachments(text, _attachments, _no_links = true), do: text
116 def maybe_add_attachments(text, attachments, _no_links) do
117 add_attachments(text, attachments)
120 def add_attachments(text, attachments) do
122 Enum.map(attachments, fn
123 %{"url" => [%{"href" => href} | _]} = attachment ->
124 name = attachment["name"] || URI.decode(Path.basename(href))
125 href = MediaProxy.url(href)
126 "<a href=\"#{href}\" class='attachment'>#{shortname(name)}</a>"
132 Enum.join([text | attachment_text], "<br>")
136 Formatting text to plain text.
138 def format_input(text, mentions, tags, "text/plain") do
140 |> Formatter.html_escape("text/plain")
141 |> String.replace(~r/\r?\n/, "<br>")
143 |> Formatter.add_links()
144 |> Formatter.add_user_links(mentions)
145 |> Formatter.add_hashtag_links(tags)
146 |> Formatter.finalize()
150 Formatting text to html.
152 def format_input(text, mentions, _tags, "text/html") do
154 |> Formatter.html_escape("text/html")
155 |> String.replace(~r/\r?\n/, "<br>")
157 |> Formatter.add_user_links(mentions)
158 |> Formatter.finalize()
162 Formatting text to markdown.
164 def format_input(text, mentions, tags, "text/markdown") do
166 |> Formatter.mentions_escape(mentions)
167 |> Earmark.as_html!()
168 |> Formatter.html_escape("text/html")
169 |> String.replace(~r/\r?\n/, "")
171 |> Formatter.add_user_links(mentions)
172 |> Formatter.add_hashtag_links(tags)
173 |> Formatter.finalize()
176 def add_tag_links(text, tags) do
179 |> Enum.sort_by(fn {tag, _} -> -String.length(tag) end)
181 Enum.reduce(tags, text, fn {full, tag}, text ->
182 url = "<a href='#{Web.base_url()}/tag/#{tag}' rel='tag'>##{tag}</a>"
183 String.replace(text, full, url)
202 "content" => content_html,
204 "context" => context,
205 "attachment" => attachments,
207 "tag" => tags |> Enum.map(fn {_, tag} -> tag end) |> Enum.uniq()
212 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
213 |> Map.put("inReplyToStatusId", inReplyTo.id)
219 def format_naive_asctime(date) do
220 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
223 def format_asctime(date) do
224 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
227 def date_to_asctime(date) do
228 with {:ok, date, _offset} <- date |> DateTime.from_iso8601() do
236 def to_masto_date(%NaiveDateTime{} = date) do
238 |> NaiveDateTime.to_iso8601()
239 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
242 def to_masto_date(date) do
245 |> NaiveDateTime.from_iso8601!()
246 |> NaiveDateTime.to_iso8601()
247 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
253 defp shortname(name) do
254 if String.length(name) < 30 do
257 String.slice(name, 0..30) <> "…"
261 def confirm_current_password(user, password) do
262 with %User{local: true} = db_user <- Repo.get(User, user.id),
263 true <- Pbkdf2.checkpw(password, db_user.password_hash) do
266 _ -> {:error, "Invalid password."}
270 def emoji_from_profile(%{info: _info} = user) do
271 (Formatter.get_emoji(user.bio) ++ Formatter.get_emoji(user.name))
272 |> Enum.map(fn {shortcode, url} ->
275 "icon" => %{"type" => "Image", "url" => "#{Endpoint.url()}#{url}"},
276 "name" => ":#{shortcode}:"