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.

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