@@ -3,6 +3,16 @@ import Vapor
3
3
4
4
let store = WebSocketManagerStore ( )
5
5
6
+ func broadcastUpdate( _ req: Request , discussionId: UUID ) async throws {
7
+ let discussionDetails = try await Discussion . getDetails ( request: req, discussionId: discussionId)
8
+
9
+ let wsManager = await store. getWebSocket ( for: discussionId)
10
+
11
+ if let wsManager = wsManager {
12
+ wsManager. broadcast ( discussionDetails. getJSONData ( ) )
13
+ }
14
+ }
15
+
6
16
struct DiscussionController : RouteCollection {
7
17
func boot( routes: RoutesBuilder ) throws {
8
18
let api = routes. grouped ( " api " )
@@ -94,11 +104,7 @@ struct DiscussionController: RouteCollection {
94
104
95
105
try await participant. save ( on: req. db)
96
106
97
- let wsManager = try await store. getWebSocket ( for: discussion. requireID ( ) )
98
-
99
- if let wsManager = wsManager {
100
- wsManager. broadcast ( " participant-joined " . data ( using: . utf8) !)
101
- }
107
+ try await broadcastUpdate ( req, discussionId: discussion. requireID ( ) )
102
108
103
109
return Response ( status: . ok, body: . init( string: " Successfully joined discussion " ) )
104
110
}
@@ -107,16 +113,7 @@ struct DiscussionController: RouteCollection {
107
113
func getDetails( _ req: Request ) async throws -> Discussion {
108
114
let discussionId = try req. parameters. require ( " discussionId " )
109
115
110
- let discussion = try await Discussion . query ( on: req. db)
111
- . filter ( \. $id == UUID ( uuidString: discussionId) ?? UUID ( ) )
112
- . with ( \. $author)
113
- . with ( \. $participants)
114
- . with ( \. $comments)
115
- . first ( )
116
-
117
- guard let discussion = discussion else {
118
- throw Abort ( . notFound, reason: " Discussion not found " )
119
- }
116
+ let discussion = try await Discussion . getDetails ( request: req, discussionId: UUID ( uuidString: discussionId) ?? UUID ( ) )
120
117
121
118
let isDiscussionIncludesUser = try await Participant . query ( on: req. db)
122
119
. filter ( \. $discussion. $id == discussion. requireID ( ) )
@@ -151,11 +148,7 @@ struct DiscussionController: RouteCollection {
151
148
152
149
try await participant. delete ( on: req. db)
153
150
154
- let wsManager = try await store. getWebSocket ( for: discussion. requireID ( ) )
155
-
156
- if let wsManager = wsManager {
157
- wsManager. broadcast ( " participant-left " . data ( using: . utf8) !)
158
- }
151
+ try await broadcastUpdate ( req, discussionId: discussion. requireID ( ) )
159
152
160
153
return Response ( status: . ok, body: . init( string: " Successfully left discussion " ) )
161
154
}
@@ -203,11 +196,7 @@ struct DiscussionController: RouteCollection {
203
196
204
197
try await comment. save ( on: req. db)
205
198
206
- let wsManager = try await store. getWebSocket ( for: discussion. requireID ( ) )
207
-
208
- if let wsManager = wsManager {
209
- wsManager. broadcast ( " comment-added " . data ( using: . utf8) !)
210
- }
199
+ try await broadcastUpdate ( req, discussionId: discussion. requireID ( ) )
211
200
212
201
return comment
213
202
}
@@ -229,11 +218,7 @@ struct DiscussionController: RouteCollection {
229
218
230
219
try await comment. delete ( on: req. db)
231
220
232
- let wsManager = try await store. getWebSocket ( for: discussion. requireID ( ) )
233
-
234
- if let wsManager = wsManager {
235
- wsManager. broadcast ( " comment-deleted " . data ( using: . utf8) !)
236
- }
221
+ try await broadcastUpdate ( req, discussionId: discussion. requireID ( ) )
237
222
238
223
return req. redirect ( to: " /api/discussions/ \( discussionId) /comments " )
239
224
}
0 commit comments