Stream follow updates
[akkoma] / lib / pleroma / web / views / streamer_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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("notification.json", %Notification{} = notify, %User{} = user) do
29 %{
30 event: "notification",
31 payload:
32 NotificationView.render(
33 "show.json",
34 %{notification: notify, for: user}
35 )
36 |> Jason.encode!()
37 }
38 |> Jason.encode!()
39 end
40
41 def render("update.json", %Activity{} = activity) do
42 %{
43 event: "update",
44 payload:
45 Pleroma.Web.MastodonAPI.StatusView.render(
46 "show.json",
47 activity: activity
48 )
49 |> Jason.encode!()
50 }
51 |> Jason.encode!()
52 end
53
54 def render("chat_update.json", %{chat_message_reference: cm_ref}) do
55 # Explicitly giving the cmr for the object here, so we don't accidentally
56 # send a later 'last_message' that was inserted between inserting this and
57 # streaming it out
58 #
59 # It also contains the chat with a cache of the correct unread count
60 Logger.debug("Trying to stream out #{inspect(cm_ref)}")
61
62 representation =
63 Pleroma.Web.PleromaAPI.ChatView.render(
64 "show.json",
65 %{last_message: cm_ref, chat: cm_ref.chat}
66 )
67
68 %{
69 event: "pleroma:chat_update",
70 payload:
71 representation
72 |> Jason.encode!()
73 }
74 |> Jason.encode!()
75 end
76
77 def render("relationships_update.json", item) do
78 %{
79 event: "pleroma:relationships_update",
80 payload:
81 %{
82 state: item.state,
83 follower: %{
84 id: item.follower.id,
85 follower_count: item.follower.follower_count,
86 following_count: item.follower.following_count
87 },
88 following: %{
89 id: item.following.id,
90 follower_count: item.following.follower_count,
91 following_count: item.following.following_count
92 }
93 }
94 |> Jason.encode!()
95 }
96 |> Jason.encode!()
97 end
98
99 def render("conversation.json", %Participation{} = participation) do
100 %{
101 event: "conversation",
102 payload:
103 Pleroma.Web.MastodonAPI.ConversationView.render("participation.json", %{
104 participation: participation,
105 for: participation.user
106 })
107 |> Jason.encode!()
108 }
109 |> Jason.encode!()
110 end
111 end