Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix role change stream event #1157

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions packages/restapi/src/lib/pushstream/DataModifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
JoinGroupEvent,
RequestEvent,
RemoveEvent,
RoleEvent,
NotificationEvent,
NotificationEventType,
NotificationType,
Expand All @@ -21,7 +22,7 @@ import {
SpaceRemoveEvent,
VideoEventType,
MessageOrigin,
VideoEvent
VideoEvent,
} from './pushStreamTypes';
import { VideoCallStatus, VideoPeerInfo } from '../types';
import { VideoDataType } from '../video';
Expand Down Expand Up @@ -130,14 +131,16 @@ export class DataModifier {

private static mapToRoleChangeEvent(data: any, includeRaw: boolean): any {
// Whatever the structure of your RemoveEvent, modify accordingly
const eventData: RemoveEvent = {
const eventData: RoleEvent = {
origin: data.messageOrigin,
timestamp: data.timestamp,
chatId: data.chatId,
from: data.from,
to: data.to,
event: GroupEventType.Remove,
newRole: data.newRole,
event: GroupEventType.RoleChange,
};

if (includeRaw) {
eventData.raw = { verificationProof: data.verificationProof };
}
Expand Down Expand Up @@ -223,7 +226,6 @@ export class DataModifier {
includeRaw = false,
eventType: MessageEventType
): MessageEvent {

if (data.hasIntent === false && eventType === 'message') {
eventType = MessageEventType.Request;
}
Expand Down Expand Up @@ -396,6 +398,8 @@ export class DataModifier {
return ProposedEventNames.UpdateGroup;
case 'remove':
return ProposedEventNames.Remove;
case 'roleChange':
return ProposedEventNames.RoleChange;
default:
throw new Error(`Unknown current event name: ${currentEventName}`);
}
Expand Down
19 changes: 10 additions & 9 deletions packages/restapi/src/lib/pushstream/PushStream.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { EventEmitter } from 'events';
import { ALPHA_FEATURE_CONFIG } from '../config';
import { createSocketConnection } from './socketClient';
import { ENV, PACKAGE_BUILD } from '../constants';
import { pCAIP10ToWallet, walletToPCAIP10 } from '../helpers';
import { ADDITIONAL_META_TYPE } from '../payloads';
import { Chat } from '../pushapi/chat';
import { ProgressHookType, SignerType } from '../types';
import { DataModifier } from './DataModifier';
import {
GroupEventType,
MessageEventType,
Expand All @@ -16,7 +11,12 @@ import {
STREAM,
EVENTS,
} from './pushStreamTypes';
import { createSocketConnection } from './socketClient';
import { DataModifier } from './DataModifier';
import { pCAIP10ToWallet, walletToPCAIP10 } from '../helpers';
import { Chat } from '../pushapi/chat';
import { ProgressHookType, SignerType } from '../types';
import { ALPHA_FEATURE_CONFIG } from '../config';
import { ADDITIONAL_META_TYPE } from '../payloads';

export class PushStream extends EventEmitter {
private pushChatSocket: any;
Expand Down Expand Up @@ -255,7 +255,8 @@ export class PushStream extends EventEmitter {
data.eventType === GroupEventType.JoinGroup ||
data.eventType === GroupEventType.LeaveGroup ||
data.eventType === MessageEventType.Request ||
data.eventType === GroupEventType.Remove
data.eventType === GroupEventType.Remove ||
data.eventType === GroupEventType.RoleChange
) {
if (shouldEmit(STREAM.CHAT)) {
this.emit(STREAM.CHAT, modifiedData);
Expand Down Expand Up @@ -326,7 +327,7 @@ export class PushStream extends EventEmitter {
data.eventType === SpaceEventType.Join ||
data.eventType === SpaceEventType.Leave ||
data.eventType === MessageEventType.Request ||
data.eventType === SpaceEventType.Remove ||
data.eventType === SpaceEventType.Remove ||
data.eventType === SpaceEventType.Start ||
data.eventType === SpaceEventType.Stop
) {
Expand Down
14 changes: 9 additions & 5 deletions packages/restapi/src/lib/pushstream/pushStreamTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export enum GroupEventType {
JoinGroup = 'joinGroup',
LeaveGroup = 'leaveGroup',
Remove = 'remove',
RoleChange = 'roleChange'
RoleChange = 'roleChange',
}

export enum SpaceEventType {
Expand All @@ -64,7 +64,7 @@ export enum SpaceEventType {
Leave = 'leaveSpace',
Remove = 'remove',
Stop = 'stop',
Start = 'start'
Start = 'start',
}

export enum VideoEventType {
Expand All @@ -75,7 +75,7 @@ export enum VideoEventType {
DISCONNECT = 'video.disconnect',
// retry events
RETRY_REQUEST = 'video.retry.request',
RETRY_APPROVE = 'video.retry.approve'
RETRY_APPROVE = 'video.retry.approve',
}

export enum ProposedEventNames {
Expand All @@ -88,6 +88,7 @@ export enum ProposedEventNames {
CreateGroup = 'chat.group.create',
UpdateGroup = 'chat.group.update',
Remove = 'chat.group.participant.remove',
RoleChange = 'chat.group.participant.role',

CreateSpace = 'space.create',
UpdateSpace = 'space.update',
Expand All @@ -98,7 +99,7 @@ export enum ProposedEventNames {
JoinSpace = 'space.participant.join',
SpaceRemove = 'space.participant.remove',
StartSpace = 'space.start',
StopSpace = 'space.stop'
StopSpace = 'space.stop',
}

export interface Profile {
Expand Down Expand Up @@ -176,7 +177,10 @@ export interface RemoveEvent extends GroupMemberEventBase {
event: GroupEventType.Remove;
}


export interface RoleEvent extends GroupMemberEventBase {
newRole: string;
event: GroupEventType.RoleChange;
}

export interface SpaceMemberEventBase {
event: SpaceEventType | MessageEventType;
Expand Down
115 changes: 115 additions & 0 deletions packages/restapi/tests/lib/stream/group.events.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import * as path from 'path';
import * as dotenv from 'dotenv';
dotenv.config({ path: path.resolve(__dirname, '../../.env') });
import { ethers } from 'ethers';
import { PushAPI } from '../../../src/lib/pushapi/PushAPI';
import CONSTANTS from '../../../src/lib/constantsV2';
import { PushStream } from '../../../src/lib/pushstream/PushStream';

describe('Push Chat Group Events functionality', () => {
const env = CONSTANTS.ENV.DEV;
let userAlice: PushAPI;
let userBob: PushAPI;
let userbobAddress: string;
let userKateAddress: string;
let stream: PushStream;
let group: any;

before(async () => {
const WALLET = ethers.Wallet.createRandom();
const signer = new ethers.Wallet(WALLET.privateKey);
userAlice = await PushAPI.initialize(signer, {
env: CONSTANTS.ENV.DEV,
});

const WALLET2 = ethers.Wallet.createRandom();
userbobAddress = WALLET2.address;
const signer2 = new ethers.Wallet(WALLET2.privateKey);
userBob = await PushAPI.initialize(signer2, {
env: CONSTANTS.ENV.DEV,
});

const WALLET3 = ethers.Wallet.createRandom();
userKateAddress = WALLET3.address;

// Initialize stream to listen for events:
stream = await userAlice.initStream(
[
CONSTANTS.STREAM.CHAT, // Listen for chat messages
CONSTANTS.STREAM.NOTIF, // Listen for notifications
CONSTANTS.STREAM.CONNECT, // Listen for connection events
CONSTANTS.STREAM.DISCONNECT, // Listen for disconnection events
],
{
// Connection options:
connection: {
retries: 3, // Retry connection 3 times if it fails
},
raw: false, // Receive events in structured format
}
);

// Chat event listeners:
// Stream connection established:
stream.on(CONSTANTS.STREAM.CONNECT, async (a) => {
console.log('Stream Connected');
});

// Chat message received:
stream.on(CONSTANTS.STREAM.CHAT, (message) => {
console.log(message); // Log the message payload
});

// Chat operation received:
stream.on(CONSTANTS.STREAM.CHAT_OPS, (data) => {
console.log(data); // Log the chat operation data
});

// Stream disconnection:
stream.on(CONSTANTS.STREAM.DISCONNECT, () => {
console.log('Stream Disconnected');
});

await stream.connect();

group = await userAlice.chat.group.create('Test Grp', {
description: 'Test Desc',
image: 'Test Image',
members: [],
admins: [],
private: false,
});
});

after(async () => {
stream.disconnect();
});

it('Should emit JoinGroup event', async () => {
await userBob.chat.group.join(group.chatId);
// Add a delay to allow the event to be emitted
await new Promise((resolve) => setTimeout(resolve, 3000));
});

it('Should emit ModifyRole event', async () => {
await userAlice.chat.group.modify(group.chatId, {
role: 'ADMIN',
accounts: [userbobAddress],
});
// Add a delay to allow the event to be emitted
await new Promise((resolve) => setTimeout(resolve, 3000));

await userAlice.chat.group.modify(group.chatId, {
role: 'MEMBER',
accounts: [userbobAddress],
});
// Add a delay to allow the event to be emitted
await new Promise((resolve) => setTimeout(resolve, 3000));
});

it('Should emit LeaveGroup event', async () => {
await userBob.chat.group.leave(group.chatId);
// Add a delay to allow the event to be emitted
await new Promise((resolve) => setTimeout(resolve, 3000));
});
});
2 changes: 1 addition & 1 deletion packages/restapi/tests/lib/stream/space.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import CONSTANTS from '../../../src/lib/constantsV2';
import * as util from 'util';
import { PushStream } from '../../../src/lib/pushstream/PushStream';

describe.only('PushStream.initialize functionality', () => {
describe('PushStream.initialize functionality', () => {
it('Should initialize new stream and listen to events', async () => {
const spaceDescription = 'Hey There!!!';
const spaceImage =
Expand Down
Loading