Skip to content

Commit

Permalink
fix: Notification event mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammeds1992 committed Oct 4, 2023
1 parent 522814d commit ec2aedb
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 57 deletions.
75 changes: 75 additions & 0 deletions packages/restapi/src/lib/pushstream/DataModifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import {
JoinGroupEvent,
RequestEvent,
RemoveEvent,
NotificationEvent,
NotificationEventType,
NotificationType,
NOTIFICATION,
} from './pushStreamTypes';

export class DataModifier {
Expand Down Expand Up @@ -301,4 +305,75 @@ export class DataModifier {
return data;
}
}

public static mapToNotificationEvent(
data: any,
notificationEventType: NotificationEventType,
origin: 'other' | 'self',
includeRaw = false
): NotificationEvent {
const notificationType =
(Object.keys(NOTIFICATION.TYPE) as NotificationType[]).find(
(key) => NOTIFICATION.TYPE[key] === data.payload.data.type
) || 'BROADCAST'; // Assuming 'BROADCAST' as the default


let recipients: string[];

if (Array.isArray(data.payload.recipients)) {
recipients = data.payload.recipients;
} else if (typeof data.payload.recipients === 'string') {
recipients = [data.payload.recipients];
} else {
recipients = Object.keys(data.payload.recipients);
}

const notificationEvent: NotificationEvent = {
event: notificationEventType,
origin: origin,
timestamp: data.epoch,
from: data.sender,
to: recipients,
notifID: data.payload_id.toString(),
channel: {
name: data.payload.data.app,
icon: data.payload.data.icon,
url: data.payload.data.url,
},
meta: {
type: 'NOTIFICATION.' + notificationType,
},
message: {
notification: {
title: data.payload.notification.title,
body: data.payload.notification.body,
},
payload: {
title: data.payload.data.asub,
body: data.payload.data.amsg,
cta: data.payload.data.acta,
embed: data.payload.data.aimg,
meta: {
domain: data.payload.data.additionalMeta?.domain || 'push.org',
type: data.payload.data.additionalMeta?.type,
data: data.payload.data.additionalMeta?.data,
},
},
},
config: {
expiry: data.payload.data.etime,
silent: data.payload.data.silent === '1',
hidden: data.payload.data.hidden === '1',
},
source: data.source,
};

if (includeRaw) {
notificationEvent.raw = {
verificationProof: data.payload.verificationProof,
};
}

return notificationEvent;
}
}
153 changes: 96 additions & 57 deletions packages/restapi/src/lib/pushstream/PushStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ENV } from '../constants';
import {
GroupEventType,
MessageEventType,
NotificationEventType,
ProposedEventNames,
PushStreamInitializeProps,
STREAM,
Expand Down Expand Up @@ -53,11 +54,11 @@ export class PushStream extends EventEmitter {

if (!this.pushNotificationSocket) {
throw new Error('Push notification socket not connected');
}
}

if (!this.pushChatSocket) {
throw new Error('Push chat socket not connected');
}
}

this.raw = options.raw ?? false;
this.options = options;
Expand Down Expand Up @@ -155,6 +156,16 @@ export class PushStream extends EventEmitter {
return this.options.filter.chats.includes(dataChatId);
}

private shouldEmitChannel(dataChannelId: string): boolean {
if (
!this.options.filter?.channels ||
this.options.filter.channels.length === 0
) {
return true;
}
return this.options.filter.channels.includes(dataChannelId);
}

public async init(): Promise<void> {
const shouldEmit = (eventType: STREAM): boolean => {
if (!this.options.listen || this.options.listen.length === 0) {
Expand All @@ -164,78 +175,106 @@ export class PushStream extends EventEmitter {
};

this.pushChatSocket.on(EVENTS.CHAT_GROUPS, (data: any) => {
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) => {
try {
try {
const modifiedData = DataModifier.handleChatGroupEvent(data, this.raw);
modifiedData.event = this.convertToProposedName(modifiedData.event);
this.handleToField(modifiedData);
if (this.shouldEmitChat(data.chatId)) {
if (
data.messageCategory == 'Chat' ||
data.messageCategory == 'Request'
data.eventType === GroupEventType.JoinGroup ||
data.eventType === GroupEventType.LeaveGroup ||
data.eventType === MessageEventType.Request ||
data.eventType === GroupEventType.Remove
) {
data = await this.chatInstance.decrypt([data]);
data = data[0]
}

const modifiedData = DataModifier.handleChatEvent(data, 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);
}
} else {
if (shouldEmit(STREAM.CHAT_OPS)) {
this.emit(STREAM.CHAT_OPS, modifiedData);
}
}
} catch (error) {
console.error(
'Error handling CHAT_RECEIVED_MESSAGE event:',
error,
'Data:',
data
);
}
} catch (error) {
console.error(
'Error handling CHAT_GROUPS event:',
error,
'Data:',
data
);
}
});

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

const modifiedData = DataModifier.handleChatEvent(data, 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) => {
try {
this.emit(STREAM.NOTIF, data);
const modifiedData = DataModifier.mapToNotificationEvent(
data,
NotificationEventType.INBOX,
this.account === data.sender ? 'self' : 'other',
this.raw
);

if (this.shouldEmitChannel(modifiedData.from)) {
if (shouldEmit(STREAM.NOTIF)) {
this.emit(STREAM.NOTIF, modifiedData);
}
}
} catch (error) {
console.error('Error handling USER_FEEDS event:', error, 'Data:', data);
}
});

this.pushNotificationSocket.on(EVENTS.USER_SPAM_FEEDS, (data: any) => {
try {
this.emit(STREAM.NOTIF, data);
} catch (error) {
console.error(
'Error handling USER_SPAM_FEEDS event:',
error,
'Data:',
data
);
}
try {
const modifiedData = DataModifier.mapToNotificationEvent(
data,
NotificationEventType.SPAM,
this.account === data.sender ? 'self' : 'other',
this.raw
);
modifiedData.origin =
this.account === modifiedData.from ? 'self' : 'other';
if (this.shouldEmitChannel(modifiedData.from)) {
if (shouldEmit(STREAM.NOTIF)) {
this.emit(STREAM.NOTIF, modifiedData);
}
}
} catch (error) {
console.error(
'Error handling USER_SPAM_FEEDS event:',
error,
'Data:',
data
);
}
});
}
}
64 changes: 64 additions & 0 deletions packages/restapi/src/lib/pushstream/pushStreamTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export enum STREAM {
CHAT_OPS = 'STREAM.CHAT_OPS',
}

export enum NotificationEventType {
INBOX = 'notification.inbox',
SPAM = 'notification.spam',
}


export enum MessageOrigin {
Other = 'other',
Self = 'self',
Expand Down Expand Up @@ -155,6 +161,64 @@ export interface MessageEvent {
raw?: MessageRawData;
}

export const NOTIFICATION = {
TYPE: {
BROADCAST: 1,
TARGETTED: 3,
SUBSET: 4,
},
} as const;

export type NotificationType = keyof typeof NOTIFICATION.TYPE;



export interface NotificationEvent {
event: NotificationEventType;
origin: 'other' | 'self';
timestamp: string;
from: string;
to: string[];
notifID: string;
channel: {
name: string;
icon: string;
url: string;
};
meta: {
type: string;
};
message: {
notification: {
title: string;
body: string;
};
payload?: {
title?: string;
body?: string;
cta?: string;
embed?: string;
meta?: {
domain?: string;
type: string;
data: string;
};
};
};
config?: {
expiry?: number;
silent?: boolean;
hidden?: boolean;
};
advanced?: {
chatid?: string;
};
source: string;
raw?: {
verificationProof: string;
};
}

export interface MessageRawData {
fromCAIP10: string;
toCAIP10: string;
Expand Down

0 comments on commit ec2aedb

Please sign in to comment.