move to 2.5.5
[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   def render("update.json", %Activity{} = activity, %User{} = user) do
15     %{
16       event: "update",
17       payload:
18         Pleroma.Web.MastodonAPI.StatusView.render(
19           "show.json",
20           activity: activity,
21           for: user
22         )
23         |> Jason.encode!()
24     }
25     |> Jason.encode!()
26   end
27
28   def render("status_update.json", %Activity{} = activity, %User{} = user) do
29     %{
30       event: "status.update",
31       payload:
32         Pleroma.Web.MastodonAPI.StatusView.render(
33           "show.json",
34           activity: activity,
35           for: user
36         )
37         |> Jason.encode!()
38     }
39     |> Jason.encode!()
40   end
41
42   def render("notification.json", %Notification{} = notify, %User{} = user) do
43     %{
44       event: "notification",
45       payload:
46         NotificationView.render(
47           "show.json",
48           %{notification: notify, for: user}
49         )
50         |> Jason.encode!()
51     }
52     |> Jason.encode!()
53   end
54
55   def render("update.json", %Activity{} = activity) do
56     %{
57       event: "update",
58       payload:
59         Pleroma.Web.MastodonAPI.StatusView.render(
60           "show.json",
61           activity: activity
62         )
63         |> Jason.encode!()
64     }
65     |> Jason.encode!()
66   end
67
68   def render("status_update.json", %Activity{} = activity) do
69     %{
70       event: "status.update",
71       payload:
72         Pleroma.Web.MastodonAPI.StatusView.render(
73           "show.json",
74           activity: activity
75         )
76         |> Jason.encode!()
77     }
78     |> Jason.encode!()
79   end
80
81   def render("chat_update.json", %{chat_message_reference: cm_ref}) do
82     # Explicitly giving the cmr for the object here, so we don't accidentally
83     # send a later 'last_message' that was inserted between inserting this and
84     # streaming it out
85     #
86     # It also contains the chat with a cache of the correct unread count
87     Logger.debug("Trying to stream out #{inspect(cm_ref)}")
88
89     representation =
90       Pleroma.Web.PleromaAPI.ChatView.render(
91         "show.json",
92         %{last_message: cm_ref, chat: cm_ref.chat}
93       )
94
95     %{
96       event: "pleroma:chat_update",
97       payload:
98         representation
99         |> Jason.encode!()
100     }
101     |> Jason.encode!()
102   end
103
104   def render("follow_relationships_update.json", item) do
105     %{
106       event: "pleroma:follow_relationships_update",
107       payload:
108         %{
109           state: item.state,
110           follower: %{
111             id: item.follower.id,
112             follower_count: item.follower.follower_count,
113             following_count: item.follower.following_count
114           },
115           following: %{
116             id: item.following.id,
117             follower_count: item.following.follower_count,
118             following_count: item.following.following_count
119           }
120         }
121         |> Jason.encode!()
122     }
123     |> Jason.encode!()
124   end
125
126   def render("conversation.json", %Participation{} = participation) do
127     %{
128       event: "conversation",
129       payload:
130         Pleroma.Web.MastodonAPI.ConversationView.render("participation.json", %{
131           participation: participation,
132           for: participation.user
133         })
134         |> Jason.encode!()
135     }
136     |> Jason.encode!()
137   end
138 end