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