1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.CommonAPI.Utils do
7 alias Calendar.Strftime
9 alias Pleroma.{Activity, Formatter, Object, Repo}
12 alias Pleroma.Web.ActivityPub.Utils
13 alias Pleroma.Web.Endpoint
14 alias Pleroma.Web.MediaProxy
16 # This is a hack for twidere.
17 def get_by_id_or_ap_id(id) do
18 activity = Repo.get(Activity, id) || Activity.get_create_activity_by_object_ap_id(id)
21 if activity.data["type"] == "Create" do
24 Activity.get_create_activity_by_object_ap_id(activity.data["object"])
28 def get_replied_to_activity(""), do: nil
30 def get_replied_to_activity(id) when not is_nil(id) do
31 Repo.get(Activity, id)
34 def get_replied_to_activity(_), do: nil
36 def attachments_from_ids(ids, descs) do
37 Enum.map(ids || [], fn media_id -> do
38 Logger.warn(descs[media_id])
39 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, _no_links = true), 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>")
123 Formatting text to plain text.
125 def format_input(text, mentions, tags, "text/plain") do
127 |> Formatter.html_escape("text/plain")
128 |> String.replace(~r/\r?\n/, "<br>")
130 |> Formatter.add_links()
131 |> Formatter.add_user_links(mentions)
132 |> Formatter.add_hashtag_links(tags)
133 |> Formatter.finalize()
137 Formatting text to html.
139 def format_input(text, mentions, _tags, "text/html") do
141 |> Formatter.html_escape("text/html")
142 |> String.replace(~r/\r?\n/, "<br>")
144 |> Formatter.add_user_links(mentions)
145 |> Formatter.finalize()
149 Formatting text to markdown.
151 def format_input(text, mentions, tags, "text/markdown") do
153 |> Formatter.mentions_escape(mentions)
154 |> Earmark.as_html!()
155 |> Formatter.html_escape("text/html")
156 |> String.replace(~r/\r?\n/, "")
158 |> Formatter.add_user_links(mentions)
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}:"