Skip to content
This repository was archived by the owner on Nov 21, 2020. It is now read-only.

Commit ffffbb4

Browse files
erdenezulBattulga BatAmar
authored andcommitted
Performance improvement facebook (#424)
* Use interface for clarity and indexes on postId,commentId,senderId * Undo removed newline * Add index for recipientId * Add index for senderId
1 parent 63b9f9a commit ffffbb4

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

src/db/models/definitions/conversationMessages.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,13 @@ const facebookSchema = new Schema(
148148
postId: field({
149149
type: String,
150150
optional: true,
151+
index: true,
151152
}),
152153

153154
commentId: field({
154155
type: String,
155156
optional: true,
157+
index: true,
156158
}),
157159

158160
// parent comment id
@@ -216,6 +218,7 @@ const facebookSchema = new Schema(
216218
senderId: field({
217219
type: String,
218220
optional: true,
221+
index: true,
219222
}),
220223

221224
senderName: field({

src/db/models/definitions/conversations.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,11 @@ const facebookSchema = new Schema(
147147
}),
148148
senderId: field({
149149
type: String,
150+
index: true,
150151
}),
151152
recipientId: field({
152153
type: String,
154+
index: true,
153155
}),
154156

155157
// when wall post

src/trackers/facebook.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ interface IReactionParams {
5454
from: IFbUser;
5555
}
5656

57+
interface IFacebookMessageSelector {
58+
facebookData: {
59+
postId?: string;
60+
commentId?: string;
61+
};
62+
}
63+
5764
export interface IFacebookReply {
5865
text?: string;
5966
attachment?: any;
@@ -379,7 +386,7 @@ export class SaveWebhookResponse {
379386
/**
380387
* Increase or decrease like count
381388
*/
382-
public async updateLikeCount(type: string, selector: any) {
389+
public async updateLikeCount(type: string, selector: IFacebookMessageSelector) {
383390
let count = -1;
384391

385392
if (type === 'add') {
@@ -394,7 +401,7 @@ export class SaveWebhookResponse {
394401
/**
395402
* Updates reaction
396403
*/
397-
public async updateReactions(type: string, selector: any, reactionType: string, from: IFbUser) {
404+
public async updateReactions(type: string, selector: IFacebookMessageSelector, reactionType: string, from: IFbUser) {
398405
const reactionField = `facebookData.reactions.${reactionType}`;
399406

400407
if (type === 'add') {
@@ -413,14 +420,14 @@ export class SaveWebhookResponse {
413420
*/
414421
public async handleReactions(reactionParams: IReactionParams) {
415422
const { verb, post_id, comment_id, reaction_type, item, from } = reactionParams;
416-
let selector = {};
423+
let selector: IFacebookMessageSelector = { facebookData: {} };
417424

418425
if (post_id) {
419-
selector = { 'facebookData.postId': post_id };
426+
selector = { facebookData: { postId: post_id } };
420427
}
421428

422429
if (comment_id) {
423-
selector = { 'facebookData.commentId': comment_id };
430+
selector = { facebookData: { commentId: comment_id } };
424431
}
425432

426433
// Receiving like
@@ -526,6 +533,7 @@ export class SaveWebhookResponse {
526533
let msgFacebookData = {};
527534

528535
// sending to comment handler if comment
536+
// TODO: prechecking is too costly.
529537
if (item === 'comment' && comment_id) {
530538
// if already saved then ignore it
531539
const conversationMessage = await ConversationMessages.findOne({
@@ -788,7 +796,6 @@ export const receiveWebhookResponse = async data => {
788796

789797
for (const integration of integrations) {
790798
const { facebookData } = integration;
791-
792799
if (!facebookData) {
793800
throw new Error('Could not find integrations facebookData');
794801
}

0 commit comments

Comments
 (0)