Skip to content

Commit

Permalink
fix: Fixed onParticipantsChanged function (fix #171)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed May 8, 2021
1 parent 32c395b commit 08975a0
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 23 deletions.
63 changes: 44 additions & 19 deletions src/api/layers/listener.layer.ts
Expand Up @@ -20,7 +20,7 @@ import { Page } from 'puppeteer';
import { CreateConfig } from '../../config/create-config';
import { ExposedFn } from '../helpers/exposed.enum';
import { Ack, Chat, LiveLocation, Message, ParticipantEvent } from '../model';
import { SocketState, SocketStream } from '../model/enum';
import { MessageType, SocketState, SocketStream } from '../model/enum';
import { InterfaceMode } from '../model/enum/interface-mode';
import { InterfaceState } from '../model/enum/interface-state';
import { ProfileLayer } from './profile.layer';
Expand Down Expand Up @@ -227,31 +227,56 @@ export class ListenerLayer extends ProfileLayer {
);
}

/**
* @event Listens to participants changed
* @param to callback
* @returns Stream of ParticipantEvent
*/
public onParticipantsChanged(
callback: (participantChangedEvent: ParticipantEvent) => void
): { dispose: () => void };
/**
* @event Listens to participants changed
* @param to group id: xxxxx-yyyy@us.c
* @param to callback
* @returns Stream of ParticipantEvent
*/
public async onParticipantsChanged(
public onParticipantsChanged(
groupId: string,
fn: (participantChangedEvent: ParticipantEvent) => void
) {
const method =
'onParticipantsChanged_' + groupId.replace('_', '').replace('_', '');
return this.page
.exposeFunction(method, (participantChangedEvent: ParticipantEvent) =>
fn(participantChangedEvent)
)
.then((_) =>
this.page.evaluate(
({ groupId, method }) => {
//@ts-ignore
WAPI.onParticipantsChanged(groupId, window[method]);
},
{ groupId, method }
)
);
callback: (participantChangedEvent: ParticipantEvent) => void
): { dispose: () => void };
public onParticipantsChanged(
groupId: any,
callback?: (participantChangedEvent: ParticipantEvent) => void
): { dispose: () => void } {
if (typeof groupId === 'function') {
callback = groupId;
groupId = null;
}

const subtypeEvents = ['invite', 'add', 'remove', 'leave'];

return this.registerEvent(
ExposedFn.onNotificationMessage,
(message: Message) => {
// Only group events
if (
message.type !== MessageType.GP2 ||
!subtypeEvents.includes(message.subtype)
) {
return;
}
if (groupId && groupId !== message.id) {
return;
}
callback({
by: message.from,
groupId: message.chatId,
action: message.subtype as any,
who: message.recipients,
});
}
);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/api/model/enum/group-change-event.ts
Expand Up @@ -16,6 +16,8 @@
*/

export enum GroupChangeEvent {
Remove = 'remove',
Add = 'add',
Inivite = 'invite',
Leave = 'leave',
Remove = 'remove',
}
1 change: 1 addition & 0 deletions src/api/model/message.ts
Expand Up @@ -67,6 +67,7 @@ export interface Message {
chatId: string;
quotedMsgObj: null;
mediaData: MediaData;
recipients?: string[];
}

export interface MediaData {
Expand Down
6 changes: 3 additions & 3 deletions src/api/model/participant-event.ts
Expand Up @@ -15,11 +15,11 @@
* along with WPPConnect. If not, see <https://www.gnu.org/licenses/>.
*/

import { Id } from './id';
import { GroupChangeEvent } from './enum';

export interface ParticipantEvent {
by: Id;
by: string;
groupId: string;
action: GroupChangeEvent;
who: [Id];
who: string[];
}

0 comments on commit 08975a0

Please sign in to comment.