Fix tagpolicy to also work with Update
[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.Gettext
13 alias Pleroma.Web.MediaProxy
14
15 require Pleroma.Constants
16
17 @spec pub_date(String.t() | DateTime.t()) :: String.t()
18 def pub_date(date) when is_binary(date) do
19 date
20 |> Timex.parse!("{ISO:Extended}")
21 |> pub_date
22 end
23
24 def pub_date(%DateTime{} = date), do: Timex.format!(date, "{RFC822}")
25
26 def prepare_activity(activity, opts \\ []) do
27 object = Object.normalize(activity, fetch: false)
28
29 actor =
30 if opts[:actor] do
31 Pleroma.User.get_cached_by_ap_id(activity.actor)
32 end
33
34 %{
35 activity: activity,
36 object: object,
37 data: Map.get(object, :data),
38 actor: actor
39 }
40 end
41
42 def most_recent_update(activities) do
43 with %{updated_at: updated_at} <- List.first(activities) do
44 NaiveDateTime.to_iso8601(updated_at)
45 end
46 end
47
48 def most_recent_update(activities, user) do
49 (List.first(activities) || user).updated_at
50 |> NaiveDateTime.to_iso8601()
51 end
52
53 def feed_logo do
54 case Pleroma.Config.get([:feed, :logo]) do
55 nil ->
56 "#{Pleroma.Web.Endpoint.url()}/static/logo.svg"
57
58 logo ->
59 "#{Pleroma.Web.Endpoint.url()}#{logo}"
60 end
61 |> MediaProxy.url()
62 end
63
64 def logo(user) do
65 user
66 |> User.avatar_url()
67 |> MediaProxy.url()
68 end
69
70 def last_activity(activities), do: List.last(activities)
71
72 def activity_title(%{"content" => content}, opts \\ %{}) do
73 content
74 |> Pleroma.Web.Metadata.Utils.scrub_html()
75 |> Pleroma.Emoji.Formatter.demojify()
76 |> Formatter.truncate(opts[:max_length], opts[:omission])
77 |> escape()
78 end
79
80 def activity_content(%{"content" => content}) do
81 content
82 |> String.replace(~r/[\n\r]/, "")
83 |> escape()
84 end
85
86 def activity_content(_), do: ""
87
88 def activity_context(activity), do: escape(activity.data["context"])
89
90 def attachment_href(attachment) do
91 attachment["url"]
92 |> hd()
93 |> Map.get("href")
94 end
95
96 def attachment_type(attachment) do
97 attachment["url"]
98 |> hd()
99 |> Map.get("mediaType")
100 end
101
102 def get_href(id) do
103 with %Object{data: %{"external_url" => external_url}} <- Object.get_cached_by_ap_id(id) do
104 external_url
105 else
106 _e -> id
107 end
108 end
109
110 def escape(html) do
111 html
112 |> html_escape()
113 |> safe_to_string()
114 end
115 end