Skip to content

Commit

Permalink
fix: error handling in socket events
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammeds1992 committed Sep 29, 2023
1 parent f855ce9 commit 399d1fe
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 37 deletions.
2 changes: 1 addition & 1 deletion packages/examples/sdk-backend-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@pushprotocol/restapi": "0.0.1-alpha.36",
"@pushprotocol/restapi": "0.0.1-alpha.37",
"@pushprotocol/socket": "^0.5.2"
}
}
101 changes: 65 additions & 36 deletions packages/restapi/src/lib/pushstream/PushStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,54 +164,83 @@ export class PushStream extends EventEmitter {
};

this.pushChatSocket.on(EVENTS.CHAT_GROUPS, (data: any) => {
const modifiedData = DataModifier.handleChatGroupEvent(data, this.raw);
modifiedData.event = this.convertToProposedName(modifiedData.event);
this.handleToField(modifiedData);
if (this.shouldEmitChat(data.chatId)) {
if (
data.eventType === GroupEventType.JoinGroup ||
data.eventType === GroupEventType.LeaveGroup ||
data.eventType === MessageEventType.Request ||
data.eventType === GroupEventType.Remove
) {
if (shouldEmit(STREAM.CHAT)) {
this.emit(STREAM.CHAT, modifiedData);
}
} else {
if (shouldEmit(STREAM.CHAT_OPS)) {
this.emit(STREAM.CHAT_OPS, modifiedData);
}
}
}
try {
const modifiedData = DataModifier.handleChatGroupEvent(data, this.raw);
modifiedData.event = this.convertToProposedName(modifiedData.event);
this.handleToField(modifiedData);
if (this.shouldEmitChat(data.chatId)) {
if (
data.eventType === GroupEventType.JoinGroup ||
data.eventType === GroupEventType.LeaveGroup ||
data.eventType === MessageEventType.Request ||
data.eventType === GroupEventType.Remove
) {
if (shouldEmit(STREAM.CHAT)) {
this.emit(STREAM.CHAT, modifiedData);
}
} else {
if (shouldEmit(STREAM.CHAT_OPS)) {
this.emit(STREAM.CHAT_OPS, modifiedData);
}
}
}
} catch (error) {
console.error('Error handling CHAT_GROUPS event:', error, 'Data:', data);
}
});

this.pushChatSocket.on(EVENTS.CHAT_RECEIVED_MESSAGE, async (data: any) => {
if (data.messageCategory == 'Chat' || data.messageCategory == 'Request') {
data = await this.chatInstance.decrypt([data]);
}
try {
if (
data.messageCategory == 'Chat' ||
data.messageCategory == 'Request'
) {
data = await this.chatInstance.decrypt([data]);
}

console.log(data);
console.log(data);

const modifiedData = DataModifier.handleChatEvent(data[0], this.raw);
modifiedData.event = this.convertToProposedName(modifiedData.event);
this.handleToField(modifiedData);
if (this.shouldEmitChat(data.chatId)) {
if (shouldEmit(STREAM.CHAT)) {
this.emit(STREAM.CHAT, modifiedData);
const modifiedData = DataModifier.handleChatEvent(data[0], this.raw);
modifiedData.event = this.convertToProposedName(modifiedData.event);
this.handleToField(modifiedData);
if (this.shouldEmitChat(data.chatId)) {
if (shouldEmit(STREAM.CHAT)) {
this.emit(STREAM.CHAT, modifiedData);
}
}
} catch (error) {
console.error(
'Error handling CHAT_RECEIVED_MESSAGE event:',
error,
'Data:',
data
);
}
}
});

this.pushNotificationSocket.on(EVENTS.USER_FEEDS, (data: any) => {
console.log('Incoming Feed from Socket');
console.log(data);
this.emit(STREAM.NOTIF, data);
try {
console.log('Incoming Feed from Socket');
console.log(data);
this.emit(STREAM.NOTIF, data);
} catch (error) {
console.error('Error handling USER_FEEDS event:', error, 'Data:', data);
}
});

this.pushNotificationSocket.on(EVENTS.USER_SPAM_FEEDS, (data: any) => {
console.log('Incoming Spam Feed from Socket');
console.log(data);
this.emit(STREAM.NOTIF, data);
try {
console.log('Incoming Spam Feed from Socket');
console.log(data);
this.emit(STREAM.NOTIF, data);
} catch (error) {
console.error(
'Error handling USER_SPAM_FEEDS event:',
error,
'Data:',
data
);
}
});
}
}

0 comments on commit 399d1fe

Please sign in to comment.