SideEffects: Ensure a chat is present before streaming something out.
[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.Chat
10 alias Pleroma.Conversation.Participation
11 alias Pleroma.Notification
12 alias Pleroma.User
13 alias Pleroma.Web.MastodonAPI.NotificationView
14
15 def render("chat_update.json", object, user, recipients) do
16 chat = Chat.get(user.id, hd(recipients -- [user.ap_id]))
17
18 representation =
19 Pleroma.Web.PleromaAPI.ChatView.render(
20 "show.json",
21 %{message: object, chat: chat}
22 )
23
24 %{
25 event: "pleroma:chat_update",
26 payload:
27 representation
28 |> Jason.encode!()
29 }
30 |> Jason.encode!()
31 end
32
33 def render("update.json", %Activity{} = activity, %User{} = user) do
34 %{
35 event: "update",
36 payload:
37 Pleroma.Web.MastodonAPI.StatusView.render(
38 "show.json",
39 activity: activity,
40 for: user
41 )
42 |> Jason.encode!()
43 }
44 |> Jason.encode!()
45 end
46
47 def render("notification.json", %Notification{} = notify, %User{} = user) do
48 %{
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) do
61 %{
62 event: "update",
63 payload:
64 Pleroma.Web.MastodonAPI.StatusView.render(
65 "show.json",
66 activity: activity
67 )
68 |> Jason.encode!()
69 }
70 |> Jason.encode!()
71 end
72
73 def render("conversation.json", %Participation{} = participation) do
74 %{
75 event: "conversation",
76 payload:
77 Pleroma.Web.MastodonAPI.ConversationView.render("participation.json", %{
78 participation: participation,
79 for: participation.user
80 })
81 |> Jason.encode!()
82 }
83 |> Jason.encode!()
84 end
85 end