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