logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

feed_view.ex (4500B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 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.Object
  8. alias Pleroma.User
  9. alias Pleroma.Web.Gettext
  10. alias Pleroma.Web.MediaProxy
  11. require Pleroma.Constants
  12. @days ~w(Mon Tue Wed Thu Fri Sat Sun)
  13. @months ~w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
  14. def prepare_activity(activity, opts \\ []) do
  15. object = Object.normalize(activity, fetch: false)
  16. actor =
  17. if opts[:actor] do
  18. Pleroma.User.get_cached_by_ap_id(activity.actor)
  19. end
  20. %{
  21. activity: activity,
  22. object: object,
  23. data: Map.get(object, :data),
  24. actor: actor
  25. }
  26. end
  27. def most_recent_update(activities) do
  28. with %{updated_at: updated_at} <- List.first(activities) do
  29. to_rfc3339(updated_at)
  30. end
  31. end
  32. def most_recent_update(activities, user, :atom) do
  33. (List.first(activities) || user).updated_at
  34. |> to_rfc3339()
  35. end
  36. def most_recent_update(activities, user, :rss) do
  37. (List.first(activities) || user).updated_at
  38. |> to_rfc2822()
  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 email(user) do
  50. user.nickname <> "@" <> Pleroma.Web.Endpoint.host()
  51. end
  52. def logo(user) do
  53. user
  54. |> User.avatar_url()
  55. |> MediaProxy.url()
  56. end
  57. def last_activity(activities), do: List.last(activities)
  58. def activity_title(%{"content" => content} = data, opts \\ %{}) do
  59. summary = Map.get(data, "summary", "")
  60. title =
  61. cond do
  62. summary != "" -> summary
  63. content != "" -> activity_content(data)
  64. true -> "a post"
  65. end
  66. title
  67. |> Pleroma.Web.Metadata.Utils.scrub_html_and_truncate(opts[:max_length], opts[:omission])
  68. |> HtmlEntities.encode()
  69. end
  70. def activity_description(data) do
  71. content = activity_content(data)
  72. summary = data["summary"]
  73. cond do
  74. content != "" -> escape(content)
  75. summary != "" -> escape(summary)
  76. true -> escape(data["type"])
  77. end
  78. end
  79. def activity_content(%{"content" => content}) do
  80. content
  81. |> String.replace(~r/[\n\r]/, "")
  82. end
  83. def activity_content(_), do: ""
  84. def activity_context(activity), do: escape(activity.data["context"])
  85. def attachment_href(attachment) do
  86. attachment["url"]
  87. |> hd()
  88. |> Map.get("href")
  89. end
  90. def attachment_type(attachment) do
  91. attachment["url"]
  92. |> hd()
  93. |> Map.get("mediaType")
  94. end
  95. def get_href(id) do
  96. with %Object{data: %{"external_url" => external_url}} <- Object.get_cached_by_ap_id(id) do
  97. external_url
  98. else
  99. _e -> id
  100. end
  101. end
  102. def escape(html) do
  103. html
  104. |> html_escape()
  105. |> safe_to_string()
  106. end
  107. @spec to_rfc3339(String.t() | NaiveDateTime.t()) :: String.t()
  108. def to_rfc3339(date) when is_binary(date) do
  109. date
  110. |> Timex.parse!("{ISO:Extended}")
  111. |> to_rfc3339()
  112. end
  113. def to_rfc3339(nd) do
  114. nd
  115. |> Timex.to_datetime()
  116. |> Timex.format!("{RFC3339}")
  117. end
  118. @spec to_rfc2822(String.t() | DateTime.t() | NaiveDateTime.t()) :: String.t()
  119. def to_rfc2822(datestr) when is_binary(datestr) do
  120. datestr
  121. |> Timex.parse!("{ISO:Extended}")
  122. |> to_rfc2822()
  123. end
  124. def to_rfc2822(%DateTime{} = date) do
  125. date
  126. |> DateTime.to_naive()
  127. |> NaiveDateTime.to_erl()
  128. |> rfc2822_from_erl()
  129. end
  130. def to_rfc2822(nd) do
  131. nd
  132. |> Timex.to_datetime()
  133. |> DateTime.to_naive()
  134. |> NaiveDateTime.to_erl()
  135. |> rfc2822_from_erl()
  136. end
  137. @doc """
  138. Builds a RFC2822 timestamp from an Erlang timestamp
  139. [RFC2822 3.3 - Date and Time Specification](https://tools.ietf.org/html/rfc2822#section-3.3)
  140. This function always assumes the Erlang timestamp is in Universal time, not Local time
  141. """
  142. def rfc2822_from_erl({{year, month, day} = date, {hour, minute, second}}) do
  143. day_name = Enum.at(@days, :calendar.day_of_the_week(date) - 1)
  144. month_name = Enum.at(@months, month - 1)
  145. date_part = "#{day_name}, #{day} #{month_name} #{year}"
  146. time_part = "#{pad(hour)}:#{pad(minute)}:#{pad(second)}"
  147. date_part <> " " <> time_part <> " +0000"
  148. end
  149. defp pad(num) do
  150. num
  151. |> Integer.to_string()
  152. |> String.pad_leading(2, "0")
  153. end
  154. end