Merge branch 'develop' into issue/1383
[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 def prepare_activity(activity) do
17 object = activity_object(activity)
18
19 %{
20 activity: activity,
21 data: Map.get(object, :data),
22 object: object
23 }
24 end
25
26 def most_recent_update(activities, user) do
27 (List.first(activities) || user).updated_at
28 |> NaiveDateTime.to_iso8601()
29 end
30
31 def logo(user) do
32 user
33 |> User.avatar_url()
34 |> MediaProxy.url()
35 end
36
37 def last_activity(activities), do: List.last(activities)
38
39 def activity_object(activity), do: Object.normalize(activity)
40
41 def activity_title(%{data: %{"content" => content}}, opts \\ %{}) do
42 content
43 |> Formatter.truncate(opts[:max_length], opts[:omission])
44 |> escape()
45 end
46
47 def activity_content(%{data: %{"content" => content}}) do
48 content
49 |> String.replace(~r/[\n\r]/, "")
50 |> escape()
51 end
52
53 def activity_context(activity), do: activity.data["context"]
54
55 def attachment_href(attachment) do
56 attachment["url"]
57 |> hd()
58 |> Map.get("href")
59 end
60
61 def attachment_type(attachment) do
62 attachment["url"]
63 |> hd()
64 |> Map.get("mediaType")
65 end
66
67 def get_href(id) do
68 with %Object{data: %{"external_url" => external_url}} <- Object.get_cached_by_ap_id(id) do
69 external_url
70 else
71 _e -> id
72 end
73 end
74
75 def escape(html) do
76 html
77 |> html_escape()
78 |> safe_to_string()
79 end
80 end