Merge branch '1851-favorites-pagination' into 'develop'
[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("conversation.json", %Participation{} = participation) do
78 %{
79 event: "conversation",
80 payload:
81 Pleroma.Web.MastodonAPI.ConversationView.render("participation.json", %{
82 participation: participation,
83 for: participation.user
84 })
85 |> Jason.encode!()
86 }
87 |> Jason.encode!()
88 end
89 end