logo

pleroma

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

streamer_view.ex (5862B)


  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(
  101. "follow_relationships_update.json",
  102. %{follower: follower, following: following} = item,
  103. topic
  104. ) do
  105. following_follower_count =
  106. if Enum.any?([following.hide_followers_count, following.hide_followers]) do
  107. 0
  108. else
  109. following.follower_count
  110. end
  111. following_following_count =
  112. if Enum.any?([following.hide_follows_count, following.hide_follows]) do
  113. 0
  114. else
  115. following.following_count
  116. end
  117. %{
  118. stream: render("stream.json", %{topic: topic}),
  119. event: "pleroma:follow_relationships_update",
  120. payload:
  121. %{
  122. state: item.state,
  123. follower: %{
  124. id: follower.id,
  125. follower_count: follower.follower_count,
  126. following_count: follower.following_count
  127. },
  128. following: %{
  129. id: following.id,
  130. follower_count: following_follower_count,
  131. following_count: following_following_count
  132. }
  133. }
  134. |> Jason.encode!()
  135. }
  136. |> Jason.encode!()
  137. end
  138. def render("conversation.json", %Participation{} = participation, topic) do
  139. %{
  140. stream: render("stream.json", %{topic: topic}),
  141. event: "conversation",
  142. payload:
  143. Pleroma.Web.MastodonAPI.ConversationView.render("participation.json", %{
  144. participation: participation,
  145. for: participation.user
  146. })
  147. |> Jason.encode!()
  148. }
  149. |> Jason.encode!()
  150. end
  151. def render("pleroma_respond.json", %{type: type, result: result} = params) do
  152. %{
  153. event: "pleroma:respond",
  154. payload:
  155. %{
  156. result: result,
  157. type: type
  158. }
  159. |> Map.merge(maybe_error(params))
  160. |> Jason.encode!()
  161. }
  162. |> Jason.encode!()
  163. end
  164. def render("stream.json", %{topic: "user:pleroma_chat:" <> _}), do: ["user:pleroma_chat"]
  165. def render("stream.json", %{topic: "user:notification:" <> _}), do: ["user:notification"]
  166. def render("stream.json", %{topic: "user:" <> _}), do: ["user"]
  167. def render("stream.json", %{topic: "direct:" <> _}), do: ["direct"]
  168. def render("stream.json", %{topic: "list:" <> id}), do: ["list", id]
  169. def render("stream.json", %{topic: "hashtag:" <> tag}), do: ["hashtag", tag]
  170. def render("stream.json", %{topic: "public:remote:media:" <> instance}),
  171. do: ["public:remote:media", instance]
  172. def render("stream.json", %{topic: "public:remote:" <> instance}),
  173. do: ["public:remote", instance]
  174. def render("stream.json", %{topic: stream}) when stream in Pleroma.Constants.public_streams(),
  175. do: [stream]
  176. defp maybe_error(%{error: :bad_topic}), do: %{error: "bad_topic"}
  177. defp maybe_error(%{error: :unauthorized}), do: %{error: "unauthorized"}
  178. defp maybe_error(%{error: :already_authenticated}), do: %{error: "already_authenticated"}
  179. defp maybe_error(_), do: %{}
  180. end