refactor(zalouser): extract policy and message helpers

This commit is contained in:
Peter Steinberger
2026-03-02 22:16:37 +00:00
parent 7253e91300
commit 19fafed11d
12 changed files with 678 additions and 353 deletions

View File

@@ -1,219 +1,22 @@
declare module "zca-js" {
export enum ThreadType {
User = 0,
Group = 1,
}
export enum Reactions {
HEART = "/-heart",
LIKE = "/-strong",
HAHA = ":>",
WOW = ":o",
CRY = ":-((",
ANGRY = ":-h",
KISS = ":-*",
TEARS_OF_JOY = ":')",
NONE = "",
}
export enum LoginQRCallbackEventType {
QRCodeGenerated = 0,
QRCodeExpired = 1,
QRCodeScanned = 2,
QRCodeDeclined = 3,
GotLoginInfo = 4,
}
export type Credentials = {
imei: string;
cookie: unknown;
userAgent: string;
language?: string;
export const ThreadType: {
User: number;
Group: number;
};
export type User = {
userId: string;
username: string;
displayName: string;
zaloName: string;
avatar: string;
export const LoginQRCallbackEventType: {
QRCodeGenerated: number;
QRCodeExpired: number;
QRCodeScanned: number;
QRCodeDeclined: number;
GotLoginInfo: number;
};
export type GroupInfo = {
groupId: string;
name: string;
totalMember?: number;
memberIds?: unknown[];
currentMems?: Array<{
id?: unknown;
dName?: string;
zaloName?: string;
avatar?: string;
}>;
};
export type Message = {
type: ThreadType;
threadId: string;
isSelf: boolean;
data: Record<string, unknown>;
};
export type LoginQRCallbackEvent =
| {
type: LoginQRCallbackEventType.QRCodeGenerated;
data: {
code: string;
image: string;
};
actions: {
saveToFile: (qrPath?: string) => Promise<unknown>;
retry: () => unknown;
abort: () => unknown;
};
}
| {
type: LoginQRCallbackEventType.QRCodeExpired;
data: null;
actions: {
retry: () => unknown;
abort: () => unknown;
};
}
| {
type: LoginQRCallbackEventType.QRCodeScanned;
data: {
avatar: string;
display_name: string;
};
actions: {
retry: () => unknown;
abort: () => unknown;
};
}
| {
type: LoginQRCallbackEventType.QRCodeDeclined;
data: {
code: string;
};
actions: {
retry: () => unknown;
abort: () => unknown;
};
}
| {
type: LoginQRCallbackEventType.GotLoginInfo;
data: {
cookie: unknown;
imei: string;
userAgent: string;
};
actions: null;
};
export type Listener = {
on(event: "message", callback: (message: Message) => void): void;
on(event: "error", callback: (error: unknown) => void): void;
on(event: "closed", callback: (code: number, reason: string) => void): void;
off(event: "message", callback: (message: Message) => void): void;
off(event: "error", callback: (error: unknown) => void): void;
off(event: "closed", callback: (code: number, reason: string) => void): void;
start(opts?: { retryOnClose?: boolean }): void;
stop(): void;
};
export type ZaloEventMessageParams = {
msgId: string;
cliMsgId: string;
uidFrom: string;
idTo: string;
msgType: string;
st: number;
at: number;
cmd: number;
ts: string | number;
};
export type AddReactionDestination = {
data: {
msgId: string;
cliMsgId: string;
};
threadId: string;
type: ThreadType;
};
export class API {
listener: Listener;
getContext(): {
imei: string;
userAgent: string;
language?: string;
};
getCookie(): {
toJSON(): {
cookies: unknown[];
};
};
fetchAccountInfo(): Promise<{ profile: User } | User>;
getAllFriends(): Promise<User[]>;
getOwnId(): string;
getAllGroups(): Promise<{
gridVerMap: Record<string, string>;
}>;
getGroupInfo(groupId: string | string[]): Promise<{
gridInfoMap: Record<string, GroupInfo & { memVerList?: unknown }>;
}>;
getGroupMembersInfo(memberId: string | string[]): Promise<{
profiles: Record<
string,
{
id?: string;
displayName?: string;
zaloName?: string;
avatar?: string;
}
>;
}>;
sendMessage(
message: string | Record<string, unknown>,
threadId: string,
type?: ThreadType,
): Promise<{
message?: { msgId?: string | number } | null;
attachment?: Array<{ msgId?: string | number }>;
}>;
sendLink(
payload: { link: string; msg?: string },
threadId: string,
type?: ThreadType,
): Promise<{ msgId?: string | number }>;
sendTypingEvent(
threadId: string,
type?: ThreadType,
destType?: number,
): Promise<{ status: number }>;
addReaction(
icon: Reactions | string | { rType: number; source: number; icon: string },
dest: AddReactionDestination,
): Promise<unknown>;
sendDeliveredEvent(
isSeen: boolean,
messages: ZaloEventMessageParams | ZaloEventMessageParams[],
type?: ThreadType,
): Promise<unknown>;
sendSeenEvent(
messages: ZaloEventMessageParams | ZaloEventMessageParams[],
type?: ThreadType,
): Promise<unknown>;
}
export const Reactions: Record<string, string>;
export class Zalo {
constructor(options?: { logging?: boolean; selfListen?: boolean });
login(credentials: Credentials): Promise<API>;
loginQR(
options?: { userAgent?: string; language?: string; qrPath?: string },
callback?: (event: LoginQRCallbackEvent) => unknown,
): Promise<API>;
login(credentials: unknown): Promise<unknown>;
loginQR(options?: unknown, callback?: (event: unknown) => unknown): Promise<unknown>;
}
}