Merge branch 'develop' into feature/tag_feed
[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.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) do
26 object = activity_object(activity)
27
28 %{
29 activity: activity,
30 data: Map.get(object, :data),
31 object: object
32 }
33 end
34
35 def most_recent_update(activities, user) do
36 (List.first(activities) || user).updated_at
37 |> NaiveDateTime.to_iso8601()
38 end
39
40 def feed_logo do
41 case Pleroma.Config.get([:feed, :logo]) do
42 nil ->
43 "#{Pleroma.Web.base_url()}/static/logo.png"
44
45 logo ->
46 "#{Pleroma.Web.base_url()}#{logo}"
47 end
48 |> MediaProxy.url()
49 end
50
51 def logo(user) do
52 user
53 |> User.avatar_url()
54 |> MediaProxy.url()
55 end
56
57 def last_activity(activities), do: List.last(activities)
58
59 def activity_object(activity), do: Object.normalize(activity)
60
61 def activity_title(%{data: %{"content" => content}}, opts \\ %{}) do
62 content
63 |> Pleroma.Web.Metadata.Utils.scrub_html()
64 |> Pleroma.Emoji.Formatter.demojify()
65 |> Formatter.truncate(opts[:max_length], opts[:omission])
66 |> escape()
67 end
68
69 def activity_content(%{data: %{"content" => content}}) do
70 content
71 |> String.replace(~r/[\n\r]/, "")
72 |> escape()
73 end
74
75 def activity_content(_), do: ""
76
77 def activity_context(activity), do: activity.data["context"]
78
79 def attachment_href(attachment) do
80 attachment["url"]
81 |> hd()
82 |> Map.get("href")
83 end
84
85 def attachment_type(attachment) do
86 attachment["url"]
87 |> hd()
88 |> Map.get("mediaType")
89 end
90
91 def get_href(id) do
92 with %Object{data: %{"external_url" => external_url}} <- Object.get_cached_by_ap_id(id) do
93 external_url
94 else
95 _e -> id
96 end
97 end
98
99 def escape(html) do
100 html
101 |> html_escape()
102 |> safe_to_string()
103 end
104 end