Deprecate Pleroma.Web.base_url/0
[akkoma] / lib / pleroma / web / feed / feed_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Feed.FeedView do
6 use Phoenix.HTML
7 use Pleroma.Web, :view
8
9 alias Pleroma.Formatter
10 alias Pleroma.Object
11 alias Pleroma.User
12 alias Pleroma.Web.MediaProxy
13
14 require Pleroma.Constants
15
16 @spec pub_date(String.t() | DateTime.t()) :: String.t()
17 def pub_date(date) when is_binary(date) do
18 date
19 |> Timex.parse!("{ISO:Extended}")
20 |> pub_date
21 end
22
23 def pub_date(%DateTime{} = date), do: Timex.format!(date, "{RFC822}")
24
25 def prepare_activity(activity, opts \\ []) do
26 object = Object.normalize(activity, fetch: false)
27
28 actor =
29 if opts[:actor] do
30 Pleroma.User.get_cached_by_ap_id(activity.actor)
31 end
32
33 %{
34 activity: activity,
35 data: Map.get(object, :data),
36 actor: actor
37 }
38 end
39
40 def most_recent_update(activities) do
41 with %{updated_at: updated_at} <- List.first(activities) do
42 NaiveDateTime.to_iso8601(updated_at)
43 end
44 end
45
46 def most_recent_update(activities, user) do
47 (List.first(activities) || user).updated_at
48 |> NaiveDateTime.to_iso8601()
49 end
50
51 def feed_logo do
52 case Pleroma.Config.get([:feed, :logo]) do
53 nil ->
54 "#{Pleroma.Web.Endpoint.url()}/static/logo.svg"
55
56 logo ->
57 "#{Pleroma.Web.Endpoint.url()}#{logo}"
58 end
59 |> MediaProxy.url()
60 end
61
62 def logo(user) do
63 user
64 |> User.avatar_url()
65 |> MediaProxy.url()
66 end
67
68 def last_activity(activities), do: List.last(activities)
69
70 def activity_title(%{"content" => content}, opts \\ %{}) do
71 content
72 |> Pleroma.Web.Metadata.Utils.scrub_html()
73 |> Pleroma.Emoji.Formatter.demojify()
74 |> Formatter.truncate(opts[:max_length], opts[:omission])
75 |> escape()
76 end
77
78 def activity_content(%{"content" => content}) do
79 content
80 |> String.replace(~r/[\n\r]/, "")
81 |> escape()
82 end
83
84 def activity_content(_), do: ""
85
86 def activity_context(activity), do: escape(activity.data["context"])
87
88 def attachment_href(attachment) do
89 attachment["url"]
90 |> hd()
91 |> Map.get("href")
92 end
93
94 def attachment_type(attachment) do
95 attachment["url"]
96 |> hd()
97 |> Map.get("mediaType")
98 end
99
100 def get_href(id) do
101 with %Object{data: %{"external_url" => external_url}} <- Object.get_cached_by_ap_id(id) do
102 external_url
103 else
104 _e -> id
105 end
106 end
107
108 def escape(html) do
109 html
110 |> html_escape()
111 |> safe_to_string()
112 end
113 end