Add embeddable posts
[akkoma] / lib / pleroma / web / views / embed_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.EmbedView do
6 use Pleroma.Web, :view
7
8 alias Calendar.Strftime
9 alias Pleroma.Activity
10 alias Pleroma.Emoji.Formatter
11 alias Pleroma.Object
12 alias Pleroma.User
13 alias Pleroma.Web.Gettext
14 alias Pleroma.Web.MediaProxy
15 alias Pleroma.Web.Metadata.Utils
16 alias Pleroma.Web.Router.Helpers
17
18 use Phoenix.HTML
19
20 @media_types ["image", "audio", "video"]
21
22 defp emoji_for_user(%User{} = user) do
23 user.source_data
24 |> Map.get("tag", [])
25 |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
26 |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} ->
27 {String.trim(name, ":"), url}
28 end)
29 end
30
31 defp fetch_media_type(%{"mediaType" => mediaType}) do
32 Utils.fetch_media_type(@media_types, mediaType)
33 end
34
35 defp open_content? do
36 Pleroma.Config.get(
37 [:frontend_configurations, :collapse_message_with_subjects],
38 true
39 )
40 end
41
42 defp full_nickname(user) do
43 %{host: host} = URI.parse(user.ap_id)
44 "@" <> user.nickname <> "@" <> host
45 end
46
47 defp status_title(%Activity{object: %Object{data: %{"name" => name}}}) when is_binary(name),
48 do: name
49
50 defp status_title(%Activity{object: %Object{data: %{"summary" => summary}}})
51 when is_binary(summary),
52 do: summary
53
54 defp status_title(_), do: nil
55
56 defp activity_content(%Activity{object: %Object{data: %{"content" => content}}}) do
57 content |> Pleroma.HTML.filter_tags() |> raw()
58 end
59
60 defp activity_content(_), do: nil
61
62 defp activity_url(%User{local: true}, activity) do
63 Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity)
64 end
65
66 defp activity_url(%User{local: false}, %Activity{object: %Object{data: data}}) do
67 data["url"] || data["external_url"] || data["id"]
68 end
69
70 defp attachments(%Activity{object: %Object{data: %{"attachment" => attachments}}}) do
71 attachments
72 end
73
74 defp sensitive?(%Activity{object: %Object{data: %{"sensitive" => sensitive}}}) do
75 sensitive
76 end
77
78 defp published(%Activity{object: %Object{data: %{"published" => published}}}) do
79 published
80 |> NaiveDateTime.from_iso8601!()
81 |> Strftime.strftime!("%B %d, %Y, %l:%M %p")
82 end
83 end