Extract RSS Feed functionality from OStatus
[akkoma] / lib / pleroma / web / feed / feed_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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.Object
10 alias Pleroma.User
11 alias Pleroma.Web.MediaProxy
12
13 require Pleroma.Constants
14
15 def most_recent_update(activities, user) do
16 (List.first(activities) || user).updated_at
17 |> NaiveDateTime.to_iso8601()
18 end
19
20 def logo(user) do
21 user
22 |> User.avatar_url()
23 |> MediaProxy.url()
24 end
25
26 def last_activity(activities) do
27 List.last(activities)
28 end
29
30 def activity_object(activity) do
31 Object.normalize(activity)
32 end
33
34 def activity_object_data(activity) do
35 activity
36 |> activity_object()
37 |> Map.get(:data)
38 end
39
40 def activity_content(activity) do
41 content = activity_object_data(activity)["content"]
42
43 content
44 |> String.replace(~r/[\n\r]/, "")
45 |> escape()
46 end
47
48 def activity_context(activity) do
49 activity.data["context"]
50 end
51
52 def attachment_href(attachment) do
53 attachment["url"]
54 |> hd()
55 |> Map.get("href")
56 end
57
58 def attachment_type(attachment) do
59 attachment["url"]
60 |> hd()
61 |> Map.get("mediaType")
62 end
63
64 def get_href(id) do
65 with %Object{data: %{"external_url" => external_url}} <- Object.get_cached_by_ap_id(id) do
66 external_url
67 else
68 _e -> id
69 end
70 end
71
72 def escape(html) do
73 html
74 |> html_escape()
75 |> safe_to_string()
76 end
77 end