Spaces:
Runtime error
Runtime error
File size: 4,092 Bytes
4327358 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
import { WebjsClientCore } from '@waha/core/engines/webjs/WebjsClientCore';
import {
GroupId,
GroupInfo,
GroupParticipant,
GroupParticipantRole,
} from '@waha/structures/groups.dto';
import {
GroupParticipantType,
GroupV2JoinEvent,
GroupV2LeaveEvent,
GroupV2ParticipantsEvent,
GroupV2UpdateEvent,
} from '@waha/structures/groups.events.dto';
import {
GroupChat,
GroupNotification,
GroupNotificationTypes,
GroupParticipant as WEBJSGroupParticipant,
} from 'whatsapp-web.js';
function ToGroupInfo(
group: GroupChat,
invite: string,
participants = [],
): GroupInfo {
// @ts-ignore
const groupMetadata = group.groupMetadata;
const info: GroupInfo = {
// @ts-ignore
id: group.id._serialized,
subject: group.name,
description: group.description,
invite: invite,
membersCanAddNewMember: groupMetadata.restrict,
membersCanSendMessages: groupMetadata.announce,
newMembersApprovalRequired: groupMetadata.membershipApprovalMode,
participants: participants,
};
return info;
}
export async function ToGroupV2JoinEvent(
client: WebjsClientCore,
me: string,
notification: GroupNotification,
): Promise<GroupV2JoinEvent | null> {
if (!notification.recipientIds.includes(me)) {
return null;
}
// @ts-ignore
const group: GroupChat = await client.getChatById(notification.id.remote);
const invite = await group.getInviteCode();
const participants = getParticipants(group.participants);
const info: GroupInfo = ToGroupInfo(group, invite, participants);
return {
timestamp: notification.timestamp,
group: info,
_data: notification,
};
}
function getParticipants(
participants: WEBJSGroupParticipant[],
): GroupParticipant[] {
return participants.map((participant) => {
let role: GroupParticipantRole = GroupParticipantRole.PARTICIPANT;
if (participant.isSuperAdmin) {
role = GroupParticipantRole.SUPERADMIN;
} else if (participant.isAdmin) {
role = GroupParticipantRole.ADMIN;
}
return {
id: participant.id._serialized,
role: role,
};
});
}
export function ToGroupV2LeaveEvent(
me: string,
notification: GroupNotification,
): GroupV2LeaveEvent | null {
if (!notification.recipientIds.includes(me)) {
return null;
}
const group: GroupId = {
// @ts-ignore
id: notification.id.remote,
};
return {
timestamp: notification.timestamp,
group: group,
_data: notification,
};
}
export async function ToGroupV2UpdateEvent(
client: WebjsClientCore,
notification: GroupNotification,
): Promise<GroupV2UpdateEvent> {
// @ts-ignore
const group: GroupChat = await client.getChatById(notification.id.remote);
const invite = await group.getInviteCode();
const info: GroupInfo = ToGroupInfo(group, invite, undefined);
return {
group: info,
timestamp: notification.timestamp,
_data: notification,
};
}
export function ToGroupV2ParticipantsEvent(
notification: GroupNotification,
): GroupV2ParticipantsEvent | null {
let type: GroupParticipantType;
let role: GroupParticipantRole;
if (['add', 'invite', 'linked_group_join'].includes(notification.type)) {
type = GroupParticipantType.JOIN;
role = GroupParticipantRole.PARTICIPANT;
} else if (['remove', 'leave'].includes(notification.type)) {
type = GroupParticipantType.LEAVE;
role = GroupParticipantRole.LEFT;
} else if (['promote'].includes(notification.type)) {
type = GroupParticipantType.PROMOTE;
role = GroupParticipantRole.ADMIN;
} else if (['demote'].includes(notification.type)) {
type = GroupParticipantType.DEMOTE;
role = GroupParticipantRole.PARTICIPANT;
} else {
return null;
}
const participants: GroupParticipant[] = notification.recipientIds.map(
(id) => {
return {
id: id,
role: role,
};
},
);
const group: GroupId = {
// @ts-ignore
id: notification.id.remote,
};
return {
group: group,
type: type,
timestamp: notification.timestamp,
participants: participants,
_data: notification,
};
}
|