Skip to content

Commit

Permalink
fix: PUSH Stream Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammeds1992 committed Sep 23, 2023
1 parent 421307d commit a702e7b
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 57 deletions.
6 changes: 2 additions & 4 deletions packages/examples/sdk-backend-node/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { runPushAPICases } from './pushAPI';

import { config } from './config';
import { ENV } from './types';
import { runPushStreamCases } from './pushStream';

// CONFIGS
const { env } = config;
Expand All @@ -16,13 +15,12 @@ const start = async (): Promise<void> => {
console.log(`${returnHeadingLog()}`);
console.log(`${returnENVLog()}`);

await runPushStreamCases();
/*await runPushAPICases();
await runPushAPICases();
await runNotificaitonsUseCases();
await runChatUseCases();
await runNFTChatUseCases();
await runVideoUseCases();
await runSpacesUseCases();*/
await runSpacesUseCases();
};

start();
Expand Down
2 changes: 0 additions & 2 deletions packages/restapi/src/lib/chat/getGroupMemberStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ export const getGroupMemberStatus = async (
const API_BASE_URL = getAPIBaseUrls(env);
const requestUrl = `${API_BASE_URL}/v1/chat/groups/${chatId}/members/${user}/status`;

console.log(requestUrl)

return axios
.get(requestUrl)
.then((response) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/restapi/src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { ENV } = Constants;
export const API_BASE_URL = {
[ENV.PROD]: 'https://backend.epns.io/apis',
[ENV.STAGING]: 'https://backend-staging.epns.io/apis',
[ENV.DEV]: 'http://localhost:4000/apis',
[ENV.DEV]: 'https://backend-dev.epns.io/apis',

/**
* **This is for local development only**
Expand Down
28 changes: 23 additions & 5 deletions packages/restapi/src/lib/pushapi/PushAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ export class PushAPI {
const status = await PUSH_CHAT.getGroupMemberStatus({
chatId: target,
did: this.account,
env: this.env,
});

if (status.isPending) {
Expand Down Expand Up @@ -536,14 +537,31 @@ export class PushAPI {
},

leave: async (target: string): Promise<GroupDTO> => {
return await PUSH_CHAT.removeMembers({
const status = await PUSH_CHAT.getGroupMemberStatus({
chatId: target,
members: [this.account],
did: this.account,
env: this.env,
account: this.account,
signer: this.signer,
pgpPrivateKey: this.decryptedPgpPvtKey,
});

if (status.isAdmin) {
return await PUSH_CHAT.removeAdmins({
chatId: target,
admins: [this.account],
env: this.env,
account: this.account,
signer: this.signer,
pgpPrivateKey: this.decryptedPgpPvtKey,
});
} else {
return await PUSH_CHAT.removeMembers({
chatId: target,
members: [this.account],
env: this.env,
account: this.account,
signer: this.signer,
pgpPrivateKey: this.decryptedPgpPvtKey,
});
}
},

reject: async (target: string): Promise<void> => {
Expand Down
56 changes: 47 additions & 9 deletions packages/restapi/src/lib/pushstream/DataModifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
MessageEventType,
Member,
GroupEventType,
LeaveGroupEvent,
JoinGroupEvent,
} from './pushStreamTypes';

export class DataModifier {
Expand All @@ -16,6 +18,13 @@ export class DataModifier {
return this.mapToCreateGroupEvent(data, includeRaw);
} else if (data.eventType === 'update') {
return this.mapToUpdateGroupEvent(data, includeRaw);
} else if (
data.eventType === GroupEventType.JoinGroup ||
data.eventType === GroupEventType.LeaveGroup ||
data.eventType === MessageEventType.Request ||
data.eventType === GroupEventType.Remove
) {
return this.mapToGroupMemberEvent(data, includeRaw, data.eventType);
} else {
console.warn('Unknown eventType:', data.eventType);
return data;
Expand Down Expand Up @@ -113,7 +122,7 @@ export class DataModifier {
includeRaw: boolean
): CreateGroupEvent {
return this.mapToGroupEvent(
GroupEventType.createGroup,
GroupEventType.CreateGroup,
incomingData,
includeRaw
) as CreateGroupEvent;
Expand All @@ -124,7 +133,7 @@ export class DataModifier {
includeRaw: boolean
): UpdateGroupEvent {
return this.mapToGroupEvent(
GroupEventType.updateGroup,
GroupEventType.UpdateGroup,
incomingData,
includeRaw
) as UpdateGroupEvent;
Expand Down Expand Up @@ -173,20 +182,49 @@ export class DataModifier {

public static handleChatEvent(data: any, includeRaw = false): any {
const eventTypeMap: { [key: string]: MessageEventType } = {
Chat: 'message',
Request: 'request',
Approve: 'accept',
Reject: 'reject',
Chat: MessageEventType.Message,
Request: MessageEventType.Request,
Approve: MessageEventType.Accept,
Reject: MessageEventType.Reject,
};

const eventType: MessageEventType | undefined =
eventTypeMap[data.messageCategory];
eventTypeMap[data.eventType || data.messageCategory];

if (eventType) {
return this.mapToMessageEvent(data, includeRaw, eventType);
return this.mapToMessageEvent(
data,
includeRaw,
eventType as MessageEventType
);
} else {
console.warn('Unknown messageCategory:', data.messageCategory);
console.warn(
'Unknown eventType:',
data.eventType || data.messageCategory
);
return data;
}
}

private static mapToGroupMemberEvent(
data: any,
includeRaw: boolean,
eventType: GroupEventType
): JoinGroupEvent | LeaveGroupEvent {
const baseEventData = {
origin: data.messageOrigin,
timestamp: data.timestamp,
chatId: data.chatId,
from: data.from,
to: data.to,
event: eventType as GroupEventType.JoinGroup | GroupEventType.LeaveGroup,
};

return includeRaw
? {
...baseEventData,
raw: { verificationProof: data.verificationProof },
}
: baseEventData;
}
}
20 changes: 15 additions & 5 deletions packages/restapi/src/lib/pushstream/PushStream.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EventEmitter } from 'events';
import { createSocketConnection, EVENTS } from '@pushprotocol/socket';
import { ENV } from '../constants';
import { PushStreamInitializeProps, STREAM } from './pushStreamTypes';
import { GroupEventType, MessageEventType, PushStreamInitializeProps, STREAM } from './pushStreamTypes';
import { DataModifier } from './DataModifier';

export class PushStream extends EventEmitter {
Expand Down Expand Up @@ -57,10 +57,20 @@ export class PushStream extends EventEmitter {
}

public async init(): Promise<void> {
this.pushChatSocket.on(EVENTS.CHAT_GROUPS, (data: any) => {
const modifiedData = DataModifier.handleChatGroupEvent(data, this.raw);
this.emit(STREAM.CHAT_OPS, modifiedData);
});
this.pushChatSocket.on(EVENTS.CHAT_GROUPS, (data: any) => {
const modifiedData = DataModifier.handleChatGroupEvent(data, this.raw);
if (
data.eventType === GroupEventType.JoinGroup ||
data.eventType === GroupEventType.LeaveGroup ||
data.eventType === MessageEventType.Request ||
data.eventType === GroupEventType.Remove
) {
this.emit(STREAM.CHAT, modifiedData);
} else {
this.emit(STREAM.CHAT_OPS, modifiedData);
}
});


this.pushChatSocket.on(EVENTS.CHAT_RECEIVED_MESSAGE, (data: any) => {
const modifiedData = DataModifier.handleChatEvent(data, this.raw);
Expand Down
43 changes: 37 additions & 6 deletions packages/restapi/src/lib/pushstream/pushStreamTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,24 @@ export enum STREAM {
DISCONNECT = 'STREAM.DISCONNECT',
}

export type MessageOrigin = 'other' | 'self';
export type MessageEventType = 'message' | 'request' | 'accept' | 'reject';
export enum MessageOrigin {
Other = 'other',
Self = 'self',
}

export enum MessageEventType {
Message = 'message',
Request = 'request',
Accept = 'accept',
Reject = 'reject',
}

export enum GroupEventType {
createGroup = 'createGroup',
updateGroup = 'updateGroup',
CreateGroup = 'createGroup',
UpdateGroup = 'updateGroup',
JoinGroup = 'joinGroup',
LeaveGroup = 'leaveGroup',
Remove = 'remove',
}

export interface Profile {
Expand Down Expand Up @@ -75,11 +88,29 @@ export interface GroupEventBase {
}

export interface CreateGroupEvent extends GroupEventBase {
event: GroupEventType.createGroup;
event: GroupEventType.CreateGroup;
}

export interface UpdateGroupEvent extends GroupEventBase {
event: GroupEventType.updateGroup;
event: GroupEventType.UpdateGroup;
}

export interface GroupMemberEventBase {
event: GroupEventType;
origin: MessageOrigin;
timestamp: string;
chatId: string;
from: string;
to: string[];
raw?: GroupEventRawData;
}

export interface JoinGroupEvent extends GroupMemberEventBase {
event: GroupEventType.JoinGroup;
}

export interface LeaveGroupEvent extends GroupMemberEventBase {
event: GroupEventType.LeaveGroup;
}

export interface MessageEvent {
Expand Down
Loading

0 comments on commit a702e7b

Please sign in to comment.