From 6ff945502d9e78c42a1d05fca79f951e81b946df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BB=BA=E8=BE=89?= Date: Sun, 22 Sep 2024 19:48:17 +0800 Subject: [PATCH] feat: add label feature (#955) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 刘建辉 --- src/Socket/chats.ts | 13 +++++++++++++ src/Types/Chat.ts | 3 +++ src/Types/Label.ts | 12 ++++++++++++ src/Utils/chat-utils.ts | 15 +++++++++++++++ 4 files changed, 43 insertions(+) diff --git a/src/Socket/chats.ts b/src/Socket/chats.ts index 495b86f..38ee669 100644 --- a/src/Socket/chats.ts +++ b/src/Socket/chats.ts @@ -8,6 +8,7 @@ import { makeMutex } from '../Utils/make-mutex' import processMessage from '../Utils/process-message' import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildren, jidNormalizedUser, reduceBinaryNodeToDictionary, S_WHATSAPP_NET } from '../WABinary' import { makeSocket } from './socket' +import { Label, LabelActionBody } from '../Types/Label' const MAX_SYNC_ATTEMPTS = 2 @@ -804,6 +805,17 @@ export const makeChatsSocket = (config: SocketConfig) => { }, jid) } + /** + * Adds label + */ + const addLabel = (jid: string, labels: LabelActionBody) => { + return chatModify({ + addLabel: { + ...labels + } + }, jid) + } + /** * Adds label for the chats */ @@ -1024,6 +1036,7 @@ export const makeChatsSocket = (config: SocketConfig) => { resyncAppState, chatModify, cleanDirtyBits, + addLabel, addChatLabel, removeChatLabel, addMessageLabel, diff --git a/src/Types/Chat.ts b/src/Types/Chat.ts index 09b2cc3..a7ff6bb 100644 --- a/src/Types/Chat.ts +++ b/src/Types/Chat.ts @@ -1,6 +1,7 @@ import type { proto } from '../../WAProto' import type { AccountSettings } from './Auth' import type { BufferedEventData } from './Events' +import type { LabelActionBody } from './Label' import type { ChatLabelAssociationActionBody } from './LabelAssociation' import type { MessageLabelAssociationActionBody } from './LabelAssociation' import type { MinimalMessage } from './Message' @@ -90,6 +91,8 @@ export type ChatModification = lastMessages: LastMessageList } | { delete: true, lastMessages: LastMessageList } + // Label + | { addLabel: LabelActionBody } // Label assosiation | { addChatLabel: ChatLabelAssociationActionBody } | { removeChatLabel: ChatLabelAssociationActionBody } diff --git a/src/Types/Label.ts b/src/Types/Label.ts index 6319b00..d9349e6 100644 --- a/src/Types/Label.ts +++ b/src/Types/Label.ts @@ -11,6 +11,18 @@ export interface Label { predefinedId?: string } +export interface LabelActionBody { + id: string + /** Label name */ + name?: string + /** Label color ID */ + color?: number + /** Is label has been deleted */ + deleted?: boolean + /** WhatsApp has 5 predefined labels (New customer, New order & etc) */ + predefinedId?: number +} + /** WhatsApp has 20 predefined colors */ export enum LabelColor { Color1 = 0, diff --git a/src/Utils/chat-utils.ts b/src/Utils/chat-utils.ts index 22e4243..75e15a2 100644 --- a/src/Utils/chat-utils.ts +++ b/src/Utils/chat-utils.ts @@ -620,6 +620,21 @@ export const chatModificationToAppPatch = ( apiVersion: 1, operation: OP.SET, } + } else if('addLabel' in mod) { + patch = { + syncAction: { + labelEditAction: { + name: mod.addLabel.name, + color: mod.addLabel.color, + predefinedId : mod.addLabel.predefinedId, + deleted: mod.addLabel.deleted + } + }, + index: ['label_edit', mod.addLabel.id], + type: 'regular', + apiVersion: 3, + operation: OP.SET, + } } else if('addChatLabel' in mod) { patch = { syncAction: {