@@ -6,6 +6,7 @@ import * as Storage from '@/lib/storage';
66import * as Service from './posts.service' ;
77import * as Middlewares from '@/middlewares' ;
88import { Router , Request , Response } from 'express' ;
9+ import io from '@/lib/io' ;
910
1011export const postsRouter = Router ( ) ;
1112
@@ -143,26 +144,43 @@ postsRouter.post(
143144 } else {
144145 createdPost = await Service . createPost ( postData , user ) ;
145146 }
146- res . status ( 201 ) . json ( createdPost ) ;
147+ res
148+ . status ( 201 )
149+ . json ( createdPost )
150+ . on ( 'finish' , ( ) => {
151+ io . except ( createdPost . author . profile ?. id ?? [ ] ) . emit ( 'post:created' , createdPost . id ) ;
152+ } ) ;
147153 } ,
148154) ;
149155
150156postsRouter . post ( '/:id/upvote' , Middlewares . authValidator , async ( req , res ) => {
151157 const user = req . user as Types . PublicUser ;
152158 const upvotedPost = await Service . upvotePost ( req . params . id , user . id ) ;
153- res . json ( upvotedPost ) ;
159+ res . json ( upvotedPost ) . on ( 'finish' , ( ) => {
160+ io . except ( user . profile ?. id ?? [ ] ) . emit ( 'post:upvoted' , upvotedPost . id ) ;
161+ if ( upvotedPost . author . profile ) {
162+ io . to ( upvotedPost . author . profile . id ) . emit ( 'notifications:updated' ) ;
163+ }
164+ } ) ;
154165} ) ;
155166
156167postsRouter . post ( '/:id/downvote' , Middlewares . authValidator , async ( req , res ) => {
157168 const user = req . user as Types . PublicUser ;
158169 const downvotedPost = await Service . downvotePost ( req . params . id , user . id ) ;
159- res . json ( downvotedPost ) ;
170+ res . json ( downvotedPost ) . on ( 'finish' , ( ) => {
171+ io . except ( user . profile ?. id ?? [ ] ) . emit ( 'post:downvoted' , downvotedPost . id ) ;
172+ if ( downvotedPost . author . profile ) {
173+ io . to ( downvotedPost . author . profile . id ) . emit ( 'notifications:updated' ) ;
174+ }
175+ } ) ;
160176} ) ;
161177
162178postsRouter . post ( '/:id/unvote' , Middlewares . authValidator , async ( req , res ) => {
163179 const user = req . user as Types . PublicUser ;
164180 const unvotedPost = await Service . unvotePost ( req . params . id , user . id ) ;
165- res . json ( unvotedPost ) ;
181+ res . json ( unvotedPost ) . on ( 'finish' , ( ) => {
182+ io . except ( user . profile ?. id ?? [ ] ) . emit ( 'post:unvoted' , unvotedPost . id ) ;
183+ } ) ;
166184} ) ;
167185
168186postsRouter . post (
@@ -171,12 +189,22 @@ postsRouter.post(
171189 async ( req : Request < { id : string } , unknown , { content : string } > , res ) => {
172190 const user = req . user as Types . PublicUser ;
173191 const commentData = Schema . commentSchema . parse ( req . body ) ;
174- const newComment = await Service . findPostByIdAndCreateComment (
192+ const { createdComment , post } = await Service . findPostByIdAndCreateComment (
175193 req . params . id ,
176194 user . id ,
177195 commentData ,
178196 ) ;
179- res . status ( 200 ) . json ( newComment ) ;
197+ res
198+ . status ( 200 )
199+ . json ( createdComment )
200+ . on ( 'finish' , ( ) => {
201+ if ( post . author . profile ) {
202+ io . to ( post . author . profile . id ) . emit ( 'notifications:updated' ) ;
203+ }
204+ const { postId } = createdComment ;
205+ const commentProfileId = createdComment . author . profile ?. id ;
206+ io . except ( commentProfileId ?? [ ] ) . emit ( 'post:comment:created' , postId , createdComment . id ) ;
207+ } ) ;
180208 } ,
181209) ;
182210
@@ -207,7 +235,9 @@ postsRouter.put(
207235 } else {
208236 updatedPost = await Service . updatePost ( post , user , postData , imagedata ) ;
209237 }
210- res . json ( updatedPost ) ;
238+ res . json ( updatedPost ) . on ( 'finish' , ( ) => {
239+ io . except ( updatedPost . author . profile ?. id ?? [ ] ) . emit ( 'post:updated' , updatedPost . id ) ;
240+ } ) ;
211241 } ,
212242) ;
213243
@@ -218,8 +248,20 @@ postsRouter.put(
218248 async ( req , res ) => {
219249 const user = req . user as Types . PublicUser ;
220250 const commentData = Schema . commentSchema . parse ( req . body ) ;
221- const updatedComment = await Service . findCommentAndUpdate ( req . params . cId , user . id , commentData ) ;
222- res . json ( updatedComment ) ;
251+ const { updatedComment, post } = await Service . findCommentAndUpdate (
252+ req . params . pId ,
253+ req . params . cId ,
254+ user . id ,
255+ commentData ,
256+ ) ;
257+ res . json ( updatedComment ) . on ( 'finish' , ( ) => {
258+ if ( post . author . profile ) {
259+ io . to ( post . author . profile . id ) . emit ( 'notifications:updated' ) ;
260+ }
261+ const { postId } = updatedComment ;
262+ const commentProfileId = updatedComment . author . profile ?. id ;
263+ io . except ( commentProfileId ?? [ ] ) . emit ( 'post:comment:updated' , postId , updatedComment . id ) ;
264+ } ) ;
223265 } ,
224266) ;
225267
@@ -230,9 +272,15 @@ postsRouter.delete(
230272 async ( req , res ) => await getPostAuthorIdAndInjectPostInResLocals ( req , res ) ,
231273 ) ,
232274 async ( req , res : Response < unknown , { post : Service . _PostFullData } > ) => {
275+ const deletedPost = res . locals . post ;
233276 const userId = Utils . getCurrentUserIdFromReq ( req ) ;
234- await Service . deletePost ( res . locals . post , userId ) ;
235- res . status ( 204 ) . end ( ) ;
277+ await Service . deletePost ( deletedPost , userId ) ;
278+ res
279+ . status ( 204 )
280+ . end ( )
281+ . on ( 'finish' , ( ) => {
282+ io . except ( deletedPost . author . profile ?. id ?? [ ] ) . emit ( 'post:deleted' , deletedPost . id ) ;
283+ } ) ;
236284 } ,
237285) ;
238286
@@ -246,8 +294,15 @@ postsRouter.delete(
246294 return postAuthorId === userId ? postAuthorId : await getCommentAuthorId ( req ) ;
247295 } ) ,
248296 async ( req , res ) => {
249- const userId = Utils . getCurrentUserIdFromReq ( req ) ;
250- await Service . findCommentAndDelete ( req . params . cId , userId ) ;
251- res . status ( 204 ) . end ( ) ;
297+ const user = req . user as Types . PublicUser ;
298+ await Service . findCommentAndDelete ( req . params . cId , user . id ) ;
299+ res
300+ . status ( 204 )
301+ . end ( )
302+ . on ( 'finish' , ( ) => {
303+ const postId = req . params . pId ;
304+ const commentId = req . params . cId ;
305+ io . except ( user . profile ?. id ?? [ ] ) . emit ( 'post:comment:deleted' , postId , commentId ) ;
306+ } ) ;
252307 } ,
253308) ;
0 commit comments