1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.StreamerView do
9 alias Pleroma.Conversation.Participation
10 alias Pleroma.Notification
12 alias Pleroma.Web.MastodonAPI.NotificationView
14 require Pleroma.Constants
16 def render("update.json", %Activity{} = activity, %User{} = user, topic) do
18 stream: render("stream.json", %{topic: topic}),
21 Pleroma.Web.MastodonAPI.StatusView.render(
31 def render("status_update.json", %Activity{} = activity, %User{} = user, topic) do
33 stream: render("stream.json", %{topic: topic}),
34 event: "status.update",
36 Pleroma.Web.MastodonAPI.StatusView.render(
46 def render("notification.json", %Notification{} = notify, %User{} = user, topic) do
48 stream: render("stream.json", %{topic: topic}),
49 event: "notification",
51 NotificationView.render(
53 %{notification: notify, for: user}
60 def render("update.json", %Activity{} = activity, topic) do
62 stream: render("stream.json", %{topic: topic}),
65 Pleroma.Web.MastodonAPI.StatusView.render(
74 def render("status_update.json", %Activity{} = activity, topic) do
76 stream: render("stream.json", %{topic: topic}),
77 event: "status.update",
79 Pleroma.Web.MastodonAPI.StatusView.render(
88 def render("chat_update.json", %{chat_message_reference: cm_ref}, topic) do
89 # Explicitly giving the cmr for the object here, so we don't accidentally
90 # send a later 'last_message' that was inserted between inserting this and
93 # It also contains the chat with a cache of the correct unread count
94 Logger.debug("Trying to stream out #{inspect(cm_ref)}")
97 Pleroma.Web.PleromaAPI.ChatView.render(
99 %{last_message: cm_ref, chat: cm_ref.chat}
103 stream: render("stream.json", %{topic: topic}),
104 event: "pleroma:chat_update",
112 def render("follow_relationships_update.json", item, topic) do
114 stream: render("stream.json", %{topic: topic}),
115 event: "pleroma:follow_relationships_update",
120 id: item.follower.id,
121 follower_count: item.follower.follower_count,
122 following_count: item.follower.following_count
125 id: item.following.id,
126 follower_count: item.following.follower_count,
127 following_count: item.following.following_count
135 def render("conversation.json", %Participation{} = participation, topic) do
137 stream: render("stream.json", %{topic: topic}),
138 event: "conversation",
140 Pleroma.Web.MastodonAPI.ConversationView.render("participation.json", %{
141 participation: participation,
142 for: participation.user
149 def render("pleroma_respond.json", %{type: type, result: result} = params) do
151 event: "pleroma:respond",
157 |> Map.merge(maybe_error(params))
163 def render("stream.json", %{topic: "user:pleroma_chat:" <> _}), do: ["user:pleroma_chat"]
164 def render("stream.json", %{topic: "user:notification:" <> _}), do: ["user:notification"]
165 def render("stream.json", %{topic: "user:" <> _}), do: ["user"]
166 def render("stream.json", %{topic: "direct:" <> _}), do: ["direct"]
167 def render("stream.json", %{topic: "list:" <> id}), do: ["list", id]
168 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]
173 def render("stream.json", %{topic: "public:remote:" <> instance}),
174 do: ["public:remote", instance]
176 def render("stream.json", %{topic: stream}) when stream in Pleroma.Constants.public_streams(),
179 defp maybe_error(%{error: :bad_topic}), do: %{error: "bad_topic"}
180 defp maybe_error(%{error: :unauthorized}), do: %{error: "unauthorized"}
181 defp maybe_error(%{error: :already_authenticated}), do: %{error: "already_authenticated"}
182 defp maybe_error(_), do: %{}