From 500137c031a10f94955e803a50fdf85de31c0025 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 2 Apr 2020 09:47:07 +0200 Subject: [PATCH] Optimize request --- src/controllers/CommentsController.ts | 17 +++++++++++++---- src/controllers/UserWebSocketActions.ts | 2 +- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/controllers/CommentsController.ts b/src/controllers/CommentsController.ts index f70432c..0706b9a 100644 --- a/src/controllers/CommentsController.ts +++ b/src/controllers/CommentsController.ts @@ -145,9 +145,10 @@ export class CommentsController { * * @param h Request handler * @param c Comment + * @param assumeNoLike Assume there are not any likes on this comment (false by default) */ - public static async CommentToAPI(h: AbstractUserConnectionContainer, c: Comment) : Promise { - return { + public static async CommentToAPI(h: AbstractUserConnectionContainer, c: Comment, assumeNoLike: boolean = false) : Promise { + const data = { ID: c.id, userID: c.userID, postID: c.postID, @@ -155,9 +156,17 @@ export class CommentsController { content: c.content, img_path: c.hasImage ? c.imagePath : null, img_url: c.hasImage ? c.imageURL : null, - likes: await LikesHelper.Count(c.id, LikesType.COMMENT), - userlike: h.signedIn ? await LikesHelper.IsLiking(h.getUserId(), c.id, LikesType.COMMENT) : false + likes: 0, + userlike: false } + + // Check if we have to load like information + if(!assumeNoLike) { + data.likes = await LikesHelper.Count(c.id, LikesType.COMMENT); + data.userlike = h.signedIn ? await LikesHelper.IsLiking(h.getUserId(), c.id, LikesType.COMMENT) : false; + } + + return data; } } \ No newline at end of file diff --git a/src/controllers/UserWebSocketActions.ts b/src/controllers/UserWebSocketActions.ts index 10311c7..9e5b472 100644 --- a/src/controllers/UserWebSocketActions.ts +++ b/src/controllers/UserWebSocketActions.ts @@ -148,7 +148,7 @@ export class UserWebSocketActions { UserWebSocketController.SendToClient(client, new WsMessage({ id: "", title: "new_comment", - data: await CommentsController.CommentToAPI(new AbritraryUserConnection(client.userID), c) + data: await CommentsController.CommentToAPI(new AbritraryUserConnection(client.userID), c, true) })) }