Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
2.5KB

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