mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Group participants now derive from WAContact
This commit is contained in:
@@ -374,6 +374,11 @@ export class WAConnection extends Base {
|
|||||||
this.emit ('chat-new', chat)
|
this.emit ('chat-new', chat)
|
||||||
return chat
|
return chat
|
||||||
}
|
}
|
||||||
|
protected contactAddOrGet (jid: string) {
|
||||||
|
jid = whatsappID(jid)
|
||||||
|
if (!this.contacts[jid]) this.contacts[jid] = { jid }
|
||||||
|
return this.contacts[jid]
|
||||||
|
}
|
||||||
/** find a chat or return an error */
|
/** find a chat or return an error */
|
||||||
protected assertChatGet = jid => {
|
protected assertChatGet = jid => {
|
||||||
const chat = this.chats.get (jid)
|
const chat = this.chats.get (jid)
|
||||||
@@ -501,18 +506,18 @@ export class WAConnection extends Base {
|
|||||||
if (meta) {
|
if (meta) {
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case 'add':
|
case 'add':
|
||||||
participants.forEach(id => (
|
participants.forEach(jid => (
|
||||||
meta.participants.push({ id, isAdmin: false, isSuperAdmin: false })
|
meta.participants.push({ ...this.contactAddOrGet(jid), isAdmin: false, isSuperAdmin: false })
|
||||||
))
|
))
|
||||||
break
|
break
|
||||||
case 'remove':
|
case 'remove':
|
||||||
meta.participants = meta.participants.filter(p => !participants.includes(whatsappID(p.id)))
|
meta.participants = meta.participants.filter(p => !participants.includes(p.jid))
|
||||||
break
|
break
|
||||||
case 'promote':
|
case 'promote':
|
||||||
case 'demote':
|
case 'demote':
|
||||||
const isAdmin = action==='promote'
|
const isAdmin = action==='promote'
|
||||||
meta.participants.forEach(p => {
|
meta.participants.forEach(p => {
|
||||||
if (participants.includes(whatsappID(p.id))) p.isAdmin = isAdmin
|
if (participants.includes( p.jid )) p.isAdmin = isAdmin
|
||||||
})
|
})
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import {WAConnection as Base} from './7.MessagesExtra'
|
import {WAConnection as Base} from './7.MessagesExtra'
|
||||||
import { WAMetric, WAFlag, WANode, WAGroupMetadata, WAGroupCreateResponse, WAGroupModification, BaileysError } from '../WAConnection/Constants'
|
import { WAMetric, WAFlag, WANode, WAGroupMetadata, WAGroupCreateResponse, WAGroupModification, BaileysError } from '../WAConnection/Constants'
|
||||||
import { GroupSettingChange } from './Constants'
|
import { GroupSettingChange } from './Constants'
|
||||||
import { generateMessageID } from '../WAConnection/Utils'
|
import { generateMessageID, whatsappID } from '../WAConnection/Utils'
|
||||||
import { Mutex } from './Mutex'
|
import { Mutex } from './Mutex'
|
||||||
|
|
||||||
export class WAConnection extends Base {
|
export class WAConnection extends Base {
|
||||||
@@ -41,7 +41,13 @@ export class WAConnection extends Base {
|
|||||||
return metadata
|
return metadata
|
||||||
}
|
}
|
||||||
/** Get the metadata of the group from WA */
|
/** Get the metadata of the group from WA */
|
||||||
fetchGroupMetadataFromWA = (jid: string) => this.query({json: ['query', 'GroupMetadata', jid], expect200: true}) as Promise<WAGroupMetadata>
|
fetchGroupMetadataFromWA = async (jid: string) => {
|
||||||
|
const metadata = await this.query({json: ['query', 'GroupMetadata', jid], expect200: true})
|
||||||
|
metadata.participants = metadata.participants.map(p => (
|
||||||
|
{ ...this.contactAddOrGet(p.id), ...p }
|
||||||
|
))
|
||||||
|
return metadata as WAGroupMetadata
|
||||||
|
}
|
||||||
/** Get the metadata (works after you've left the group also) */
|
/** Get the metadata (works after you've left the group also) */
|
||||||
groupMetadataMinimal = async (jid: string) => {
|
groupMetadataMinimal = async (jid: string) => {
|
||||||
const query = ['query', {type: 'group', jid: jid, epoch: this.msgCount.toString()}, null]
|
const query = ['query', {type: 'group', jid: jid, epoch: this.msgCount.toString()}, null]
|
||||||
@@ -56,8 +62,10 @@ export class WAConnection extends Base {
|
|||||||
creator: creatorDesc?.creator,
|
creator: creatorDesc?.creator,
|
||||||
creation: parseInt(creatorDesc?.create),
|
creation: parseInt(creatorDesc?.create),
|
||||||
subject: null,
|
subject: null,
|
||||||
desc: description ? description[2].toString('utf-8') : null,
|
desc: description && description[2].toString('utf-8'),
|
||||||
participants: participants.map (item => ({ id: item[1].jid, isAdmin: item[1].type==='admin' }))
|
participants: participants.map (item => (
|
||||||
|
{ ...this.contactAddOrGet(item[1].jid), isAdmin: item[1].type === 'admin' }
|
||||||
|
))
|
||||||
} as WAGroupMetadata
|
} as WAGroupMetadata
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -168,6 +168,7 @@ export interface WAGroupCreateResponse {
|
|||||||
gid?: string
|
gid?: string
|
||||||
participants?: [{ [key: string]: any }]
|
participants?: [{ [key: string]: any }]
|
||||||
}
|
}
|
||||||
|
export type WAGroupParticipant = (WAContact & { isAdmin: boolean; isSuperAdmin: boolean })
|
||||||
export interface WAGroupMetadata {
|
export interface WAGroupMetadata {
|
||||||
id: string
|
id: string
|
||||||
owner: string
|
owner: string
|
||||||
@@ -180,7 +181,8 @@ export interface WAGroupMetadata {
|
|||||||
restrict?: 'true' | 'false'
|
restrict?: 'true' | 'false'
|
||||||
/** is set when the group only allows admins to write messages */
|
/** is set when the group only allows admins to write messages */
|
||||||
announce?: 'true' | 'false'
|
announce?: 'true' | 'false'
|
||||||
participants: { id: string; isAdmin: boolean; isSuperAdmin: boolean }[]
|
// Baileys modified array
|
||||||
|
participants: WAGroupParticipant[]
|
||||||
}
|
}
|
||||||
export interface WAGroupModification {
|
export interface WAGroupModification {
|
||||||
status: number
|
status: number
|
||||||
|
|||||||
Reference in New Issue
Block a user