feat(labels): modify chat utils

This commit is contained in:
Rafael Garcia
2023-05-14 12:44:19 -05:00
parent 00a7b48749
commit 38f285760d
4 changed files with 362 additions and 208 deletions

View File

@@ -777,6 +777,52 @@ export const makeChatsSocket = (config: SocketConfig) => {
return appPatch(patch)
}
/**
* Adds label for the chats
*/
const addChatLabel = (jid: string, labelId: string) => {
return chatModify({
addChatLabel: {
labelId
}
}, jid)
}
/**
* Removes label for the chat
*/
const removeChatLabel = (jid: string, labelId: string) => {
return chatModify({
removeChatLabel: {
labelId
}
}, jid)
}
/**
* Adds label for the message
*/
const addMessageLabel = (jid: string, messageId: string, labelId: string) => {
return chatModify({
addMessageLabel: {
messageId,
labelId
}
}, jid)
}
/**
* Removes label for the message
*/
const removeMessageLabel = (jid: string, messageId: string, labelId: string) => {
return chatModify({
removeMessageLabel: {
messageId,
labelId
}
}, jid)
}
/**
* queries need to be fired on connection open
* help ensure parity with WA Web
@@ -945,6 +991,10 @@ export const makeChatsSocket = (config: SocketConfig) => {
updateDefaultDisappearingMode,
getBusinessProfile,
resyncAppState,
chatModify
chatModify,
addChatLabel,
removeChatLabel,
addMessageLabel,
removeMessageLabel
}
}

View File

@@ -1,6 +1,8 @@
import type { proto } from '../../WAProto'
import type { AccountSettings } from './Auth'
import type { BufferedEventData } from './Events'
import type { ChatLabelAssociationActionBody } from './LabelAssociation'
import type { MessageLabelAssociationActionBody } from './LabelAssociation'
import type { MinimalMessage } from './Message'
/** privacy settings in WhatsApp Web */
@@ -90,6 +92,11 @@ export type ChatModification =
lastMessages: LastMessageList
}
| { delete: true, lastMessages: LastMessageList }
// Label assosiation
| { addChatLabel: ChatLabelAssociationActionBody }
| { removeChatLabel: ChatLabelAssociationActionBody }
| { addMessageLabel: MessageLabelAssociationActionBody }
| { removeMessageLabel: MessageLabelAssociationActionBody }
export type InitialReceivedChatsState = {
[jid: string]: {

View File

@@ -5,6 +5,8 @@ import { WACallEvent } from './Call'
import { Chat, ChatUpdate, PresenceData } from './Chat'
import { Contact } from './Contact'
import { GroupMetadata, ParticipantAction } from './GroupMetadata'
import { Label } from './Label'
import { LabelAssociation } from './LabelAssociation'
import { MessageUpsertType, MessageUserReceiptUpdate, WAMessage, WAMessageKey, WAMessageUpdate } from './Message'
import { ConnectionState } from './State'
@@ -54,6 +56,8 @@ export type BaileysEventMap = {
'blocklist.update': { blocklist: string[], type: 'add' | 'remove' }
/** Receive an update on a call, including when the call was received, rejected, accepted */
'call': WACallEvent[]
'labels.edit': Label
'labels.association': { association: LabelAssociation, type: 'add' | 'remove' }
}
export type BufferedEventData = {

View File

@@ -3,6 +3,7 @@ import { AxiosRequestConfig } from 'axios'
import type { Logger } from 'pino'
import { proto } from '../../WAProto'
import { BaileysEventEmitter, Chat, ChatModification, ChatMutation, ChatUpdate, Contact, InitialAppStateSyncOptions, LastMessageList, LTHashState, WAPatchCreate, WAPatchName } from '../Types'
import { ChatLabelAssociation, LabelAssociationType, MessageLabelAssociation } from '../Types/LabelAssociation'
import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildren, isJidGroup, jidNormalizedUser } from '../WABinary'
import { aesDecrypt, aesEncrypt, hkdf, hmacSign } from './crypto'
import { toNumber } from './generics'
@@ -606,6 +607,68 @@ export const chatModificationToAppPatch = (
apiVersion: 1,
operation: OP.SET,
}
} else if ('addChatLabel' in mod) {
patch = {
syncAction: {
labelAssociationAction: {
labeled: true,
}
},
index: [LabelAssociationType.Chat, mod.addChatLabel.labelId, jid],
type: 'regular',
apiVersion: 3,
operation: OP.SET,
}
} else if ('removeChatLabel' in mod) {
patch = {
syncAction: {
labelAssociationAction: {
labeled: false,
}
},
index: [LabelAssociationType.Chat, mod.removeChatLabel.labelId, jid],
type: 'regular',
apiVersion: 3,
operation: OP.SET,
}
} else if ('addMessageLabel' in mod) {
patch = {
syncAction: {
labelAssociationAction: {
labeled: true,
}
},
index: [
LabelAssociationType.Message,
mod.addMessageLabel.labelId,
jid,
mod.addMessageLabel.messageId,
'0',
'0'
],
type: 'regular',
apiVersion: 3,
operation: OP.SET,
}
} else if ('removeMessageLabel' in mod) {
patch = {
syncAction: {
labelAssociationAction: {
labeled: false,
}
},
index: [
LabelAssociationType.Message,
mod.removeMessageLabel.labelId,
jid,
mod.removeMessageLabel.messageId,
'0',
'0'
],
type: 'regular',
apiVersion: 3,
operation: OP.SET,
}
} else {
throw new Boom('not supported')
}
@@ -687,13 +750,15 @@ export const processSyncAction = (
conditional: getChatUpdateConditional(id, markReadAction?.messageRange)
}])
} else if (action?.deleteMessageForMeAction || type === 'deleteMessageForMe') {
ev.emit('messages.delete', { keys: [
ev.emit('messages.delete', {
keys: [
{
remoteJid: id,
id: msgId,
fromMe: fromMe === '1'
}
] })
]
})
} else if (action?.contactAction) {
ev.emit('contacts.upsert', [{ id, name: action.contactAction!.fullName! }])
} else if (action?.pushNameSetting) {
@@ -731,6 +796,34 @@ export const processSyncAction = (
if (!isInitialSync) {
ev.emit('chats.delete', [id])
}
} else if (action?.labelEditAction) {
const { name, color, deleted, predefinedId } = action.labelEditAction!
ev.emit('labels.edit', {
id,
name: name!,
color: color!,
deleted: deleted!,
predefinedId: predefinedId ? String(predefinedId) : undefined
})
} else if (action?.labelAssociationAction) {
ev.emit('labels.association', {
type: action.labelAssociationAction.labeled
? 'add'
: 'remove',
association: type === LabelAssociationType.Chat
? {
type: LabelAssociationType.Chat,
chatId: syncAction.index[2],
labelId: syncAction.index[1]
} as ChatLabelAssociation
: {
type: LabelAssociationType.Message,
chatId: syncAction.index[2],
messageId: syncAction.index[3],
labelId: syncAction.index[1]
} as MessageLabelAssociation
})
} else {
logger?.debug({ syncAction, id }, 'unprocessable update')
}