Skip to content

Commit

Permalink
fix: OnParticipantsChanged (#1250)
Browse files Browse the repository at this point in the history
  • Loading branch information
icleitoncosta committed Aug 4, 2022
1 parent 34b7ba9 commit 76b179a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 55 deletions.
54 changes: 27 additions & 27 deletions src/api/layers/listener.layer.ts
Expand Up @@ -239,6 +239,16 @@ export class ListenerLayer extends ProfileLayer {
} catch (error) {
console.error(error);
}
try {
if (!window['onParticipantsChanged'].exposed) {
WPP.on('group.participant_changed', (participantChangedEvent) => {
window['onParticipantsChanged'](participantChangedEvent);
});
window['onParticipantsChanged'].exposed = true;
}
} catch (error) {
console.error(error);
}
})
.catch(() => {});
}
Expand Down Expand Up @@ -377,9 +387,9 @@ export class ListenerLayer extends ProfileLayer {
* @param to callback
* @returns Stream of ParticipantEvent
*/
public onParticipantsChanged(
callback: (participantChangedEvent: ParticipantEvent) => void
): { dispose: () => void };
public onParticipantsChanged(callback: (evData: ParticipantEvent) => void): {
dispose: () => void;
};
/**
* @event Listens to participants changed
* @param to group id: xxxxx-yyyy@us.c
Expand All @@ -388,40 +398,30 @@ export class ListenerLayer extends ProfileLayer {
*/
public onParticipantsChanged(
groupId: string,
callback: (participantChangedEvent: ParticipantEvent) => void
callback: (evData: ParticipantEvent) => void
): { dispose: () => void };
public onParticipantsChanged(
groupId: any,
callback?: (participantChangedEvent: ParticipantEvent) => void
callback?: (evData: 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,
});
return this.registerEvent(ExposedFn.onParticipantsChanged, (evData) => {
if (groupId && groupId !== evData.groupId) {
return;
}
);
callback({
by: evData.author,
byPushName: evData.authorPushName,
groupId: evData.groupId,
action: evData.action,
operation: evData.operation,
who: evData.participants,
});
});
}

/**
Expand Down
23 changes: 0 additions & 23 deletions src/api/model/enum/group-change-event.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/api/model/enum/index.ts
Expand Up @@ -17,7 +17,6 @@

export { AckType } from './ack-type';
export { ChatState } from './chat-state';
export { GroupChangeEvent } from './group-change-event';
export { GroupNotificationType } from './group-notification-type';
export { SocketState, SocketStream } from './socket-state';
export { MessageType, MediaType } from './message-type';
Expand Down
8 changes: 4 additions & 4 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 { GroupChangeEvent } from './enum';

export interface ParticipantEvent {
by: string;
by?: string;
byPushName?: string;
groupId: string;
action: GroupChangeEvent;
action: 'add' | 'remove' | 'demote' | 'promote' | 'leaver' | 'join';
operation: 'add' | 'remove' | 'demote' | 'promote';
who: string[];
}

0 comments on commit 76b179a

Please sign in to comment.