logo

pleroma

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

streamer_view.ex (5460B)


  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.StreamerView do
  5. use Pleroma.Web, :view
  6. alias Pleroma.Activity
  7. alias Pleroma.Conversation.Participation
  8. alias Pleroma.Notification
  9. alias Pleroma.User
  10. alias Pleroma.Web.MastodonAPI.NotificationView
  11. require Pleroma.Constants
  12. def render("update.json", %Activity{} = activity, %User{} = user, topic) do
  13. %{
  14. stream: render("stream.json", %{topic: topic}),
  15. event: "update",
  16. payload:
  17. Pleroma.Web.MastodonAPI.StatusView.render(
  18. "show.json",
  19. activity: activity,
  20. for: user
  21. )
  22. |> Jason.encode!()
  23. }
  24. |> Jason.encode!()
  25. end
  26. def render("status_update.json", %Activity{} = activity, %User{} = user, topic) do
  27. %{
  28. stream: render("stream.json", %{topic: topic}),
  29. event: "status.update",
  30. payload:
  31. Pleroma.Web.MastodonAPI.StatusView.render(
  32. "show.json",
  33. activity: activity,
  34. for: user
  35. )
  36. |> Jason.encode!()
  37. }
  38. |> Jason.encode!()
  39. end
  40. def render("notification.json", %Notification{} = notify, %User{} = user, topic) do
  41. %{
  42. stream: render("stream.json", %{topic: topic}),
  43. event: "notification",
  44. payload:
  45. NotificationView.render(
  46. "show.json",
  47. %{notification: notify, for: user}
  48. )
  49. |> Jason.encode!()
  50. }
  51. |> Jason.encode!()
  52. end
  53. def render("update.json", %Activity{} = activity, topic) do
  54. %{
  55. stream: render("stream.json", %{topic: topic}),
  56. event: "update",
  57. payload:
  58. Pleroma.Web.MastodonAPI.StatusView.render(
  59. "show.json",
  60. activity: activity
  61. )
  62. |> Jason.encode!()
  63. }
  64. |> Jason.encode!()
  65. end
  66. def render("status_update.json", %Activity{} = activity, topic) do
  67. %{
  68. stream: render("stream.json", %{topic: topic}),
  69. event: "status.update",
  70. payload:
  71. Pleroma.Web.MastodonAPI.StatusView.render(
  72. "show.json",
  73. activity: activity
  74. )
  75. |> Jason.encode!()
  76. }
  77. |> Jason.encode!()
  78. end
  79. def render("chat_update.json", %{chat_message_reference: cm_ref}, topic) do
  80. # Explicitly giving the cmr for the object here, so we don't accidentally
  81. # send a later 'last_message' that was inserted between inserting this and
  82. # streaming it out
  83. #
  84. # It also contains the chat with a cache of the correct unread count
  85. Logger.debug("Trying to stream out #{inspect(cm_ref)}")
  86. representation =
  87. Pleroma.Web.PleromaAPI.ChatView.render(
  88. "show.json",
  89. %{last_message: cm_ref, chat: cm_ref.chat}
  90. )
  91. %{
  92. stream: render("stream.json", %{topic: topic}),
  93. event: "pleroma:chat_update",
  94. payload:
  95. representation
  96. |> Jason.encode!()
  97. }
  98. |> Jason.encode!()
  99. end
  100. def render("follow_relationships_update.json", item, topic) do
  101. %{
  102. stream: render("stream.json", %{topic: topic}),
  103. event: "pleroma:follow_relationships_update",
  104. payload:
  105. %{
  106. state: item.state,
  107. follower: %{
  108. id: item.follower.id,
  109. follower_count: item.follower.follower_count,
  110. following_count: item.follower.following_count
  111. },
  112. following: %{
  113. id: item.following.id,
  114. follower_count: item.following.follower_count,
  115. following_count: item.following.following_count
  116. }
  117. }
  118. |> Jason.encode!()
  119. }
  120. |> Jason.encode!()
  121. end
  122. def render("conversation.json", %Participation{} = participation, topic) do
  123. %{
  124. stream: render("stream.json", %{topic: topic}),
  125. event: "conversation",
  126. payload:
  127. Pleroma.Web.MastodonAPI.ConversationView.render("participation.json", %{
  128. participation: participation,
  129. for: participation.user
  130. })
  131. |> Jason.encode!()
  132. }
  133. |> Jason.encode!()
  134. end
  135. def render("pleroma_respond.json", %{type: type, result: result} = params) do
  136. %{
  137. event: "pleroma:respond",
  138. payload:
  139. %{
  140. result: result,
  141. type: type
  142. }
  143. |> Map.merge(maybe_error(params))
  144. |> Jason.encode!()
  145. }
  146. |> Jason.encode!()
  147. end
  148. def render("stream.json", %{topic: "user:pleroma_chat:" <> _}), do: ["user:pleroma_chat"]
  149. def render("stream.json", %{topic: "user:notification:" <> _}), do: ["user:notification"]
  150. def render("stream.json", %{topic: "user:" <> _}), do: ["user"]
  151. def render("stream.json", %{topic: "direct:" <> _}), do: ["direct"]
  152. def render("stream.json", %{topic: "list:" <> id}), do: ["list", id]
  153. def render("stream.json", %{topic: "hashtag:" <> tag}), do: ["hashtag", tag]
  154. def render("stream.json", %{topic: "public:remote:media:" <> instance}),
  155. do: ["public:remote:media", instance]
  156. def render("stream.json", %{topic: "public:remote:" <> instance}),
  157. do: ["public:remote", instance]
  158. def render("stream.json", %{topic: stream}) when stream in Pleroma.Constants.public_streams(),
  159. do: [stream]
  160. defp maybe_error(%{error: :bad_topic}), do: %{error: "bad_topic"}
  161. defp maybe_error(%{error: :unauthorized}), do: %{error: "unauthorized"}
  162. defp maybe_error(%{error: :already_authenticated}), do: %{error: "already_authenticated"}
  163. defp maybe_error(_), do: %{}
  164. end