zapind 0.1.2 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +165 -165
- package/dist/index.cjs +276 -159
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +17 -16
- package/dist/index.d.ts +17 -16
- package/dist/index.js +276 -159
- package/dist/index.js.map +1 -1
- package/package.json +66 -65
package/dist/index.cjs
CHANGED
|
@@ -96,16 +96,34 @@ var EventBus = class {
|
|
|
96
96
|
set.delete(handler);
|
|
97
97
|
if (set.size === 0) this.listeners.delete(event);
|
|
98
98
|
}
|
|
99
|
+
invokeHandler(event, handler, payload) {
|
|
100
|
+
try {
|
|
101
|
+
const result = handler(payload);
|
|
102
|
+
if (result && typeof result.then === "function") {
|
|
103
|
+
result.catch((err) => {
|
|
104
|
+
this.logger.error("Event handler threw an error", {
|
|
105
|
+
event,
|
|
106
|
+
error: err instanceof Error ? err.message : String(err)
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
} catch (err) {
|
|
111
|
+
this.logger.error("Event handler threw an error", {
|
|
112
|
+
event,
|
|
113
|
+
error: err instanceof Error ? err.message : String(err)
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
99
117
|
emit(event, payload) {
|
|
100
118
|
const set = this.listeners.get(event);
|
|
101
119
|
if (!set || set.size === 0) return;
|
|
120
|
+
if (set.size === 1) {
|
|
121
|
+
const single = set.values().next().value;
|
|
122
|
+
if (single) this.invokeHandler(event, single, payload);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
102
125
|
for (const handler of [...set]) {
|
|
103
|
-
|
|
104
|
-
this.logger.error("Event handler threw an error", {
|
|
105
|
-
event,
|
|
106
|
-
error: err instanceof Error ? err.message : String(err)
|
|
107
|
-
});
|
|
108
|
-
});
|
|
126
|
+
this.invokeHandler(event, handler, payload);
|
|
109
127
|
}
|
|
110
128
|
}
|
|
111
129
|
listenerCount(event) {
|
|
@@ -386,24 +404,24 @@ var PolicyExecutor = class {
|
|
|
386
404
|
async execute(fn, options) {
|
|
387
405
|
const timeoutMs = options.timeoutMs ?? this.defaultTimeoutMs;
|
|
388
406
|
const allowRetry = options.idempotent ?? true;
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
);
|
|
407
|
+
const runOnce = async () => {
|
|
408
|
+
if (this.rateLimiter) await this.rateLimiter.acquire();
|
|
409
|
+
try {
|
|
410
|
+
return await withTimeout(fn, timeoutMs, options.operation);
|
|
411
|
+
} catch (err) {
|
|
412
|
+
const normalized = normalizeError(err, `Failed during ${options.operation}`);
|
|
413
|
+
this.logger.debug("Operation error", {
|
|
414
|
+
operation: options.operation,
|
|
415
|
+
code: normalized.code,
|
|
416
|
+
retryable: normalized.retryable
|
|
417
|
+
});
|
|
418
|
+
throw normalized;
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
if (!allowRetry || this.retryOptions.maxAttempts <= 1) {
|
|
422
|
+
return runOnce();
|
|
423
|
+
}
|
|
424
|
+
return withRetry(runOnce, this.retryOptions, true);
|
|
407
425
|
}
|
|
408
426
|
};
|
|
409
427
|
|
|
@@ -474,6 +492,7 @@ var Reaction = /* @__PURE__ */ ((Reaction2) => {
|
|
|
474
492
|
})(Reaction || {});
|
|
475
493
|
|
|
476
494
|
// src/core/thread-registry.ts
|
|
495
|
+
var MAX_KNOWN_USERS = 1e4;
|
|
477
496
|
var ThreadTypeRegistry = class {
|
|
478
497
|
groupIds = /* @__PURE__ */ new Set();
|
|
479
498
|
userIds = /* @__PURE__ */ new Set();
|
|
@@ -498,13 +517,17 @@ var ThreadTypeRegistry = class {
|
|
|
498
517
|
this.groupsLoadedAt = Date.now();
|
|
499
518
|
}
|
|
500
519
|
registerUser(userId) {
|
|
501
|
-
if (!userId) return;
|
|
502
|
-
if (!this.
|
|
503
|
-
this.userIds.
|
|
520
|
+
if (!userId || this.groupIds.has(userId)) return;
|
|
521
|
+
if (!this.userIds.has(userId) && this.userIds.size >= MAX_KNOWN_USERS) {
|
|
522
|
+
const oldest = this.userIds.values().next().value;
|
|
523
|
+
if (oldest !== void 0) this.userIds.delete(oldest);
|
|
504
524
|
}
|
|
525
|
+
this.userIds.add(userId);
|
|
505
526
|
}
|
|
506
527
|
extractId(target) {
|
|
507
|
-
|
|
528
|
+
if (typeof target === "string") return target.trim();
|
|
529
|
+
const raw = target.threadId ?? target.id ?? "";
|
|
530
|
+
return raw.trim();
|
|
508
531
|
}
|
|
509
532
|
async resolve(target, explicitType, fetchAllGroupIds) {
|
|
510
533
|
const threadId = this.extractId(target);
|
|
@@ -856,8 +879,10 @@ function mapGroupMember(raw) {
|
|
|
856
879
|
accountStatus: typeof raw.accountStatus === "number" ? raw.accountStatus : 0
|
|
857
880
|
};
|
|
858
881
|
}
|
|
882
|
+
var EMPTY_ATTACHMENTS = Object.freeze([]);
|
|
883
|
+
var EMPTY_MENTIONS = Object.freeze([]);
|
|
859
884
|
function inferKind(msgType, rawType) {
|
|
860
|
-
const s = `${msgType} ${rawType
|
|
885
|
+
const s = rawType ? `${msgType} ${rawType}`.toLowerCase() : msgType.toLowerCase();
|
|
861
886
|
if (s.includes("photo") || s.includes("image")) return "image";
|
|
862
887
|
if (s.includes("video")) return "video";
|
|
863
888
|
if (s.includes("voice") || s.includes("audio")) return "audio";
|
|
@@ -867,28 +892,60 @@ function inferKind(msgType, rawType) {
|
|
|
867
892
|
return "other";
|
|
868
893
|
}
|
|
869
894
|
function mapMessageData(raw) {
|
|
870
|
-
const data = raw.data
|
|
871
|
-
|
|
895
|
+
const data = raw.data;
|
|
896
|
+
if (!data) {
|
|
897
|
+
return {
|
|
898
|
+
id: "",
|
|
899
|
+
clientMsgId: "",
|
|
900
|
+
threadId: String(raw.threadId ?? ""),
|
|
901
|
+
threadType: raw.type === 1 /* Group */ ? 1 /* Group */ : 0 /* User */,
|
|
902
|
+
sender: { id: "" },
|
|
903
|
+
recipientId: "",
|
|
904
|
+
isSelf: Boolean(raw.isSelf),
|
|
905
|
+
msgType: "webchat",
|
|
906
|
+
text: "",
|
|
907
|
+
attachments: EMPTY_ATTACHMENTS,
|
|
908
|
+
mentions: EMPTY_MENTIONS,
|
|
909
|
+
replyTo: void 0,
|
|
910
|
+
timestamp: /* @__PURE__ */ new Date(0),
|
|
911
|
+
ttlMs: 0,
|
|
912
|
+
st: 0,
|
|
913
|
+
at: 0,
|
|
914
|
+
cmd: 0
|
|
915
|
+
};
|
|
916
|
+
}
|
|
917
|
+
const msgType = typeof data.msgType === "string" ? data.msgType : "webchat";
|
|
872
918
|
let text = "";
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
919
|
+
let attachments = EMPTY_ATTACHMENTS;
|
|
920
|
+
const content = data.content;
|
|
921
|
+
if (typeof content === "string") {
|
|
922
|
+
text = content;
|
|
923
|
+
} else if (content !== null && typeof content === "object") {
|
|
924
|
+
const obj = content;
|
|
878
925
|
text = typeof obj.title === "string" ? obj.title : "";
|
|
879
|
-
attachments
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
926
|
+
attachments = [
|
|
927
|
+
{
|
|
928
|
+
kind: inferKind(msgType, typeof obj.type === "string" ? obj.type : void 0),
|
|
929
|
+
url: typeof obj.href === "string" ? obj.href : void 0,
|
|
930
|
+
thumbnailUrl: typeof obj.thumb === "string" ? obj.thumb : void 0,
|
|
931
|
+
title: text || void 0,
|
|
932
|
+
description: typeof obj.description === "string" ? obj.description : void 0
|
|
933
|
+
}
|
|
934
|
+
];
|
|
935
|
+
}
|
|
936
|
+
let mentions = EMPTY_MENTIONS;
|
|
937
|
+
if (Array.isArray(data.mentions) && data.mentions.length > 0) {
|
|
938
|
+
const mapped = new Array(data.mentions.length);
|
|
939
|
+
for (let i = 0; i < data.mentions.length; i++) {
|
|
940
|
+
const m = data.mentions[i];
|
|
941
|
+
mapped[i] = {
|
|
942
|
+
userId: String(m.uid ?? ""),
|
|
943
|
+
pos: typeof m.pos === "number" ? m.pos : 0,
|
|
944
|
+
len: typeof m.len === "number" ? m.len : 0
|
|
945
|
+
};
|
|
946
|
+
}
|
|
947
|
+
mentions = mapped;
|
|
886
948
|
}
|
|
887
|
-
const mentions = Array.isArray(data.mentions) ? data.mentions.map((m) => ({
|
|
888
|
-
userId: String(m.uid ?? ""),
|
|
889
|
-
pos: typeof m.pos === "number" ? m.pos : 0,
|
|
890
|
-
len: typeof m.len === "number" ? m.len : 0
|
|
891
|
-
})) : [];
|
|
892
949
|
let replyTo;
|
|
893
950
|
if (data.quote && typeof data.quote === "object") {
|
|
894
951
|
replyTo = {
|
|
@@ -900,15 +957,15 @@ function mapMessageData(raw) {
|
|
|
900
957
|
};
|
|
901
958
|
}
|
|
902
959
|
return {
|
|
903
|
-
id: String(data.msgId
|
|
904
|
-
clientMsgId: String(data.cliMsgId
|
|
960
|
+
id: data.msgId !== void 0 ? String(data.msgId) : "",
|
|
961
|
+
clientMsgId: data.cliMsgId !== void 0 ? String(data.cliMsgId) : "",
|
|
905
962
|
threadId: String(raw.threadId ?? data.idTo ?? ""),
|
|
906
963
|
threadType: raw.type === 1 /* Group */ ? 1 /* Group */ : 0 /* User */,
|
|
907
964
|
sender: {
|
|
908
|
-
id: String(data.uidFrom
|
|
965
|
+
id: data.uidFrom !== void 0 ? String(data.uidFrom) : "",
|
|
909
966
|
name: data.dName || void 0
|
|
910
967
|
},
|
|
911
|
-
recipientId: String(data.idTo
|
|
968
|
+
recipientId: data.idTo !== void 0 ? String(data.idTo) : "",
|
|
912
969
|
isSelf: Boolean(raw.isSelf),
|
|
913
970
|
msgType,
|
|
914
971
|
text,
|
|
@@ -975,8 +1032,50 @@ function mapAccountSettings(raw) {
|
|
|
975
1032
|
// src/services/index.ts
|
|
976
1033
|
function ensureConnected(transport) {
|
|
977
1034
|
if (!transport.isConnected()) {
|
|
978
|
-
throw new AuthenticationError(
|
|
1035
|
+
throw new AuthenticationError(
|
|
1036
|
+
"Zapind client is not authenticated. Call await client.login() first."
|
|
1037
|
+
);
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
function requireNonEmpty(value, fieldName) {
|
|
1041
|
+
const trimmed = typeof value === "string" ? value.trim() : "";
|
|
1042
|
+
if (!trimmed) {
|
|
1043
|
+
throw new ValidationError(`${fieldName} must be a non-empty string`);
|
|
1044
|
+
}
|
|
1045
|
+
return trimmed;
|
|
1046
|
+
}
|
|
1047
|
+
function requireUniqueNonEmptyIds(values, fieldName) {
|
|
1048
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1049
|
+
const out = [];
|
|
1050
|
+
for (const v of values) {
|
|
1051
|
+
const trimmed = typeof v === "string" ? v.trim() : "";
|
|
1052
|
+
if (trimmed && !seen.has(trimmed)) {
|
|
1053
|
+
seen.add(trimmed);
|
|
1054
|
+
out.push(trimmed);
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
if (out.length === 0) {
|
|
1058
|
+
throw new ValidationError(`${fieldName} must contain at least one non-empty ID`);
|
|
1059
|
+
}
|
|
1060
|
+
return out;
|
|
1061
|
+
}
|
|
1062
|
+
function assertOptionalPositiveInt(value, fieldName) {
|
|
1063
|
+
if (value !== void 0 && (!Number.isInteger(value) || value <= 0)) {
|
|
1064
|
+
throw new ValidationError(`${fieldName} must be a positive integer`);
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
async function resolveThreadTarget(deps, target, explicitType) {
|
|
1068
|
+
const resolved = await deps.registry.resolve(target, explicitType, async () => {
|
|
1069
|
+
const map = await deps.executor.execute(() => deps.transport.getAllGroups(), {
|
|
1070
|
+
operation: "registry.fetchGroupIds",
|
|
1071
|
+
idempotent: true
|
|
1072
|
+
});
|
|
1073
|
+
return Object.keys(map);
|
|
1074
|
+
});
|
|
1075
|
+
if (!resolved.threadId) {
|
|
1076
|
+
throw new ValidationError("Target threadId must be a non-empty string");
|
|
979
1077
|
}
|
|
1078
|
+
return resolved;
|
|
980
1079
|
}
|
|
981
1080
|
var AccountService = class {
|
|
982
1081
|
constructor(deps) {
|
|
@@ -999,10 +1098,10 @@ var AccountService = class {
|
|
|
999
1098
|
}
|
|
1000
1099
|
async updateProfile(patch) {
|
|
1001
1100
|
ensureConnected(this.deps.transport);
|
|
1002
|
-
|
|
1101
|
+
const name = requireNonEmpty(patch.name, "name");
|
|
1003
1102
|
await this.deps.executor.execute(
|
|
1004
1103
|
() => this.deps.transport.updateProfile({
|
|
1005
|
-
name
|
|
1104
|
+
name,
|
|
1006
1105
|
dob: patch.dob,
|
|
1007
1106
|
gender: patch.gender === "male" ? 0 : 1
|
|
1008
1107
|
}),
|
|
@@ -1031,8 +1130,7 @@ var UsersService = class {
|
|
|
1031
1130
|
deps;
|
|
1032
1131
|
async get(userId, options) {
|
|
1033
1132
|
ensureConnected(this.deps.transport);
|
|
1034
|
-
const id = userId
|
|
1035
|
-
if (!id) throw new ValidationError("userId must be a non-empty string");
|
|
1133
|
+
const id = requireNonEmpty(userId, "userId");
|
|
1036
1134
|
if (!options?.skipCache) {
|
|
1037
1135
|
const cached = this.deps.userCache.get(id);
|
|
1038
1136
|
if (cached) return cached;
|
|
@@ -1050,15 +1148,26 @@ var UsersService = class {
|
|
|
1050
1148
|
return user;
|
|
1051
1149
|
});
|
|
1052
1150
|
}
|
|
1053
|
-
async getMany(userIds) {
|
|
1151
|
+
async getMany(userIds, options) {
|
|
1054
1152
|
ensureConnected(this.deps.transport);
|
|
1055
|
-
const ids = userIds
|
|
1056
|
-
|
|
1057
|
-
const
|
|
1153
|
+
const ids = requireUniqueNonEmptyIds(userIds, "userIds");
|
|
1154
|
+
const out = {};
|
|
1155
|
+
const missing = [];
|
|
1156
|
+
for (const id of ids) {
|
|
1157
|
+
const cached = options?.skipCache ? void 0 : this.deps.userCache.get(id);
|
|
1158
|
+
if (cached) {
|
|
1159
|
+
out[id] = cached;
|
|
1160
|
+
} else {
|
|
1161
|
+
missing.push(id);
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
if (missing.length === 0) {
|
|
1165
|
+
return out;
|
|
1166
|
+
}
|
|
1167
|
+
const rawMap = await this.deps.executor.execute(() => this.deps.transport.getUserInfo(missing), {
|
|
1058
1168
|
operation: "users.getMany",
|
|
1059
1169
|
idempotent: true
|
|
1060
1170
|
});
|
|
1061
|
-
const out = {};
|
|
1062
1171
|
for (const [id, raw] of Object.entries(rawMap)) {
|
|
1063
1172
|
const user = new User(mapUserData(raw), this.deps.actions);
|
|
1064
1173
|
this.deps.registry.registerUser(user.id);
|
|
@@ -1069,8 +1178,7 @@ var UsersService = class {
|
|
|
1069
1178
|
}
|
|
1070
1179
|
async findByPhone(phoneNumber) {
|
|
1071
1180
|
ensureConnected(this.deps.transport);
|
|
1072
|
-
const phone = phoneNumber
|
|
1073
|
-
if (!phone) throw new ValidationError("phoneNumber must be a non-empty string");
|
|
1181
|
+
const phone = requireNonEmpty(phoneNumber, "phoneNumber");
|
|
1074
1182
|
const raw = await this.deps.executor.execute(() => this.deps.transport.findUserByPhone(phone), {
|
|
1075
1183
|
operation: "users.findByPhone",
|
|
1076
1184
|
idempotent: true
|
|
@@ -1083,8 +1191,7 @@ var UsersService = class {
|
|
|
1083
1191
|
}
|
|
1084
1192
|
async findByUsername(username) {
|
|
1085
1193
|
ensureConnected(this.deps.transport);
|
|
1086
|
-
const cleaned = username
|
|
1087
|
-
if (!cleaned) throw new ValidationError("username must be a non-empty string");
|
|
1194
|
+
const cleaned = requireNonEmpty(username, "username");
|
|
1088
1195
|
const raw = await this.deps.executor.execute(
|
|
1089
1196
|
() => this.deps.transport.findUserByUsername(cleaned),
|
|
1090
1197
|
{ operation: "users.findByUsername", idempotent: true }
|
|
@@ -1097,8 +1204,7 @@ var UsersService = class {
|
|
|
1097
1204
|
}
|
|
1098
1205
|
async getLastOnline(userId) {
|
|
1099
1206
|
ensureConnected(this.deps.transport);
|
|
1100
|
-
const id = userId
|
|
1101
|
-
if (!id) throw new ValidationError("userId must be a non-empty string");
|
|
1207
|
+
const id = requireNonEmpty(userId, "userId");
|
|
1102
1208
|
const raw = await this.deps.executor.execute(() => this.deps.transport.getLastOnline(id), {
|
|
1103
1209
|
operation: "users.getLastOnline",
|
|
1104
1210
|
idempotent: true
|
|
@@ -1113,6 +1219,8 @@ var ContactsService = class {
|
|
|
1113
1219
|
deps;
|
|
1114
1220
|
async list(options = {}) {
|
|
1115
1221
|
ensureConnected(this.deps.transport);
|
|
1222
|
+
assertOptionalPositiveInt(options.count, "count");
|
|
1223
|
+
assertOptionalPositiveInt(options.page, "page");
|
|
1116
1224
|
const rawList = await this.deps.executor.execute(
|
|
1117
1225
|
() => this.deps.transport.getAllFriends(options.count, options.page),
|
|
1118
1226
|
{ operation: "contacts.list", idempotent: true }
|
|
@@ -1126,53 +1234,62 @@ var ContactsService = class {
|
|
|
1126
1234
|
}
|
|
1127
1235
|
async sendRequest(userId, message = "Hello!") {
|
|
1128
1236
|
ensureConnected(this.deps.transport);
|
|
1129
|
-
|
|
1130
|
-
await this.deps.executor.execute(
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
);
|
|
1237
|
+
const id = requireNonEmpty(userId, "userId");
|
|
1238
|
+
await this.deps.executor.execute(() => this.deps.transport.sendFriendRequest(id, message), {
|
|
1239
|
+
operation: "contacts.sendRequest",
|
|
1240
|
+
idempotent: false
|
|
1241
|
+
});
|
|
1134
1242
|
}
|
|
1135
1243
|
async acceptRequest(userId) {
|
|
1136
1244
|
ensureConnected(this.deps.transport);
|
|
1137
|
-
|
|
1245
|
+
const id = requireNonEmpty(userId, "userId");
|
|
1246
|
+
await this.deps.executor.execute(() => this.deps.transport.acceptFriendRequest(id), {
|
|
1138
1247
|
operation: "contacts.acceptRequest",
|
|
1139
1248
|
idempotent: true
|
|
1140
1249
|
});
|
|
1141
1250
|
}
|
|
1142
1251
|
async rejectRequest(userId) {
|
|
1143
1252
|
ensureConnected(this.deps.transport);
|
|
1144
|
-
|
|
1253
|
+
const id = requireNonEmpty(userId, "userId");
|
|
1254
|
+
await this.deps.executor.execute(() => this.deps.transport.rejectFriendRequest(id), {
|
|
1145
1255
|
operation: "contacts.rejectRequest",
|
|
1146
1256
|
idempotent: true
|
|
1147
1257
|
});
|
|
1148
1258
|
}
|
|
1149
1259
|
async remove(userId) {
|
|
1150
1260
|
ensureConnected(this.deps.transport);
|
|
1151
|
-
|
|
1261
|
+
const id = requireNonEmpty(userId, "userId");
|
|
1262
|
+
await this.deps.executor.execute(() => this.deps.transport.removeFriend(id), {
|
|
1152
1263
|
operation: "contacts.remove",
|
|
1153
1264
|
idempotent: true
|
|
1154
1265
|
});
|
|
1266
|
+
this.deps.userCache.delete(id);
|
|
1155
1267
|
}
|
|
1156
1268
|
async block(userId) {
|
|
1157
1269
|
ensureConnected(this.deps.transport);
|
|
1158
|
-
|
|
1270
|
+
const id = requireNonEmpty(userId, "userId");
|
|
1271
|
+
await this.deps.executor.execute(() => this.deps.transport.blockUser(id), {
|
|
1159
1272
|
operation: "contacts.block",
|
|
1160
1273
|
idempotent: true
|
|
1161
1274
|
});
|
|
1275
|
+
this.deps.userCache.delete(id);
|
|
1162
1276
|
}
|
|
1163
1277
|
async unblock(userId) {
|
|
1164
1278
|
ensureConnected(this.deps.transport);
|
|
1165
|
-
|
|
1279
|
+
const id = requireNonEmpty(userId, "userId");
|
|
1280
|
+
await this.deps.executor.execute(() => this.deps.transport.unblockUser(id), {
|
|
1166
1281
|
operation: "contacts.unblock",
|
|
1167
1282
|
idempotent: true
|
|
1168
1283
|
});
|
|
1284
|
+
this.deps.userCache.delete(id);
|
|
1169
1285
|
}
|
|
1170
1286
|
async setAlias(userId, alias) {
|
|
1171
1287
|
ensureConnected(this.deps.transport);
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1288
|
+
const id = requireNonEmpty(userId, "userId");
|
|
1289
|
+
await this.deps.executor.execute(() => this.deps.transport.setFriendAlias(id, alias), {
|
|
1290
|
+
operation: "contacts.setAlias",
|
|
1291
|
+
idempotent: true
|
|
1292
|
+
});
|
|
1176
1293
|
}
|
|
1177
1294
|
async getOnline() {
|
|
1178
1295
|
ensureConnected(this.deps.transport);
|
|
@@ -1200,8 +1317,7 @@ var GroupsService = class {
|
|
|
1200
1317
|
}
|
|
1201
1318
|
async get(groupId, options) {
|
|
1202
1319
|
ensureConnected(this.deps.transport);
|
|
1203
|
-
const id = groupId
|
|
1204
|
-
if (!id) throw new ValidationError("groupId must be a non-empty string");
|
|
1320
|
+
const id = requireNonEmpty(groupId, "groupId");
|
|
1205
1321
|
if (!options?.skipCache) {
|
|
1206
1322
|
const cached = this.deps.groupCache.get(id);
|
|
1207
1323
|
if (cached) return cached;
|
|
@@ -1219,15 +1335,26 @@ var GroupsService = class {
|
|
|
1219
1335
|
return group;
|
|
1220
1336
|
});
|
|
1221
1337
|
}
|
|
1222
|
-
async getMany(groupIds) {
|
|
1338
|
+
async getMany(groupIds, options) {
|
|
1223
1339
|
ensureConnected(this.deps.transport);
|
|
1224
|
-
const ids = groupIds
|
|
1225
|
-
|
|
1226
|
-
const
|
|
1340
|
+
const ids = requireUniqueNonEmptyIds(groupIds, "groupIds");
|
|
1341
|
+
const out = {};
|
|
1342
|
+
const missing = [];
|
|
1343
|
+
for (const id of ids) {
|
|
1344
|
+
const cached = options?.skipCache ? void 0 : this.deps.groupCache.get(id);
|
|
1345
|
+
if (cached) {
|
|
1346
|
+
out[id] = cached;
|
|
1347
|
+
} else {
|
|
1348
|
+
missing.push(id);
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
if (missing.length === 0) {
|
|
1352
|
+
return out;
|
|
1353
|
+
}
|
|
1354
|
+
const map = await this.deps.executor.execute(() => this.deps.transport.getGroupInfo(missing), {
|
|
1227
1355
|
operation: "groups.getMany",
|
|
1228
1356
|
idempotent: true
|
|
1229
1357
|
});
|
|
1230
|
-
const out = {};
|
|
1231
1358
|
for (const [id, raw] of Object.entries(map)) {
|
|
1232
1359
|
const group = new Group(mapGroupData(raw, id), this.deps.actions);
|
|
1233
1360
|
this.deps.registry.registerGroup(group.id);
|
|
@@ -1238,8 +1365,7 @@ var GroupsService = class {
|
|
|
1238
1365
|
}
|
|
1239
1366
|
async create(options) {
|
|
1240
1367
|
ensureConnected(this.deps.transport);
|
|
1241
|
-
const members = options.members
|
|
1242
|
-
if (members.length === 0) throw new ValidationError("members must contain at least one userId");
|
|
1368
|
+
const members = requireUniqueNonEmptyIds(options.members, "members");
|
|
1243
1369
|
const created = await this.deps.executor.execute(
|
|
1244
1370
|
() => this.deps.transport.createGroup({ name: options.name, members }),
|
|
1245
1371
|
{ operation: "groups.create", idempotent: false }
|
|
@@ -1249,8 +1375,7 @@ var GroupsService = class {
|
|
|
1249
1375
|
}
|
|
1250
1376
|
async getMembers(memberIds) {
|
|
1251
1377
|
ensureConnected(this.deps.transport);
|
|
1252
|
-
const ids = memberIds
|
|
1253
|
-
if (ids.length === 0) throw new ValidationError("memberIds must not be empty");
|
|
1378
|
+
const ids = requireUniqueNonEmptyIds(memberIds, "memberIds");
|
|
1254
1379
|
const profiles = await this.deps.executor.execute(
|
|
1255
1380
|
() => this.deps.transport.getGroupMembersInfo(ids),
|
|
1256
1381
|
{ operation: "groups.getMembers", idempotent: true }
|
|
@@ -1259,9 +1384,8 @@ var GroupsService = class {
|
|
|
1259
1384
|
}
|
|
1260
1385
|
async addMembers(groupId, userIds) {
|
|
1261
1386
|
ensureConnected(this.deps.transport);
|
|
1262
|
-
const id = groupId
|
|
1263
|
-
const ids = userIds
|
|
1264
|
-
if (!id || ids.length === 0) throw new ValidationError("groupId and userIds are required");
|
|
1387
|
+
const id = requireNonEmpty(groupId, "groupId");
|
|
1388
|
+
const ids = requireUniqueNonEmptyIds(userIds, "userIds");
|
|
1265
1389
|
await this.deps.executor.execute(() => this.deps.transport.addGroupMembers(id, ids), {
|
|
1266
1390
|
operation: "groups.addMembers",
|
|
1267
1391
|
idempotent: true
|
|
@@ -1270,9 +1394,8 @@ var GroupsService = class {
|
|
|
1270
1394
|
}
|
|
1271
1395
|
async removeMembers(groupId, userIds) {
|
|
1272
1396
|
ensureConnected(this.deps.transport);
|
|
1273
|
-
const id = groupId
|
|
1274
|
-
const ids = userIds
|
|
1275
|
-
if (!id || ids.length === 0) throw new ValidationError("groupId and userIds are required");
|
|
1397
|
+
const id = requireNonEmpty(groupId, "groupId");
|
|
1398
|
+
const ids = requireUniqueNonEmptyIds(userIds, "userIds");
|
|
1276
1399
|
await this.deps.executor.execute(() => this.deps.transport.removeGroupMembers(id, ids), {
|
|
1277
1400
|
operation: "groups.removeMembers",
|
|
1278
1401
|
idempotent: true
|
|
@@ -1281,9 +1404,9 @@ var GroupsService = class {
|
|
|
1281
1404
|
}
|
|
1282
1405
|
async rename(groupId, name) {
|
|
1283
1406
|
ensureConnected(this.deps.transport);
|
|
1284
|
-
const id = groupId
|
|
1285
|
-
|
|
1286
|
-
await this.deps.executor.execute(() => this.deps.transport.renameGroup(id,
|
|
1407
|
+
const id = requireNonEmpty(groupId, "groupId");
|
|
1408
|
+
const trimmedName = requireNonEmpty(name, "name");
|
|
1409
|
+
await this.deps.executor.execute(() => this.deps.transport.renameGroup(id, trimmedName), {
|
|
1287
1410
|
operation: "groups.rename",
|
|
1288
1411
|
idempotent: true
|
|
1289
1412
|
});
|
|
@@ -1291,8 +1414,7 @@ var GroupsService = class {
|
|
|
1291
1414
|
}
|
|
1292
1415
|
async leave(groupId) {
|
|
1293
1416
|
ensureConnected(this.deps.transport);
|
|
1294
|
-
const id = groupId
|
|
1295
|
-
if (!id) throw new ValidationError("groupId is required");
|
|
1417
|
+
const id = requireNonEmpty(groupId, "groupId");
|
|
1296
1418
|
await this.deps.executor.execute(() => this.deps.transport.leaveGroup(id), {
|
|
1297
1419
|
operation: "groups.leave",
|
|
1298
1420
|
idempotent: false
|
|
@@ -1305,20 +1427,16 @@ var ConversationsService = class {
|
|
|
1305
1427
|
this.deps = deps;
|
|
1306
1428
|
}
|
|
1307
1429
|
deps;
|
|
1308
|
-
fetchGroupIds = async () => {
|
|
1309
|
-
const map = await this.deps.transport.getAllGroups();
|
|
1310
|
-
return Object.keys(map);
|
|
1311
|
-
};
|
|
1312
1430
|
async get(id, threadType) {
|
|
1313
1431
|
ensureConnected(this.deps.transport);
|
|
1314
|
-
|
|
1315
|
-
const resolved = await this.deps
|
|
1432
|
+
const cleaned = requireNonEmpty(id, "conversationId");
|
|
1433
|
+
const resolved = await resolveThreadTarget(this.deps, cleaned, threadType);
|
|
1316
1434
|
return new Conversation(resolved.threadId, resolved.threadType, this.deps.actions);
|
|
1317
1435
|
}
|
|
1318
1436
|
async getMessages(conversationId, count) {
|
|
1319
1437
|
ensureConnected(this.deps.transport);
|
|
1320
|
-
const id = conversationId
|
|
1321
|
-
|
|
1438
|
+
const id = requireNonEmpty(conversationId, "conversationId");
|
|
1439
|
+
assertOptionalPositiveInt(count, "count");
|
|
1322
1440
|
const res = await this.deps.executor.execute(
|
|
1323
1441
|
() => this.deps.transport.getGroupChatHistory(id, count),
|
|
1324
1442
|
{ operation: "conversations.getMessages", idempotent: true }
|
|
@@ -1343,37 +1461,25 @@ var ConversationsService = class {
|
|
|
1343
1461
|
idempotent: true
|
|
1344
1462
|
});
|
|
1345
1463
|
}
|
|
1346
|
-
async pin(target, pinned = true) {
|
|
1464
|
+
async pin(target, pinned = true, explicitType) {
|
|
1347
1465
|
ensureConnected(this.deps.transport);
|
|
1348
|
-
const { threadId, threadType } = await this.deps
|
|
1349
|
-
target,
|
|
1350
|
-
void 0,
|
|
1351
|
-
this.fetchGroupIds
|
|
1352
|
-
);
|
|
1466
|
+
const { threadId, threadType } = await resolveThreadTarget(this.deps, target, explicitType);
|
|
1353
1467
|
await this.deps.executor.execute(
|
|
1354
1468
|
() => this.deps.transport.setPinnedConversations([threadId], pinned, threadType),
|
|
1355
1469
|
{ operation: "conversations.pin", idempotent: true }
|
|
1356
1470
|
);
|
|
1357
1471
|
}
|
|
1358
|
-
async mute(target, durationSeconds) {
|
|
1472
|
+
async mute(target, durationSeconds, explicitType) {
|
|
1359
1473
|
ensureConnected(this.deps.transport);
|
|
1360
|
-
const { threadId, threadType } = await this.deps
|
|
1361
|
-
target,
|
|
1362
|
-
void 0,
|
|
1363
|
-
this.fetchGroupIds
|
|
1364
|
-
);
|
|
1474
|
+
const { threadId, threadType } = await resolveThreadTarget(this.deps, target, explicitType);
|
|
1365
1475
|
await this.deps.executor.execute(
|
|
1366
1476
|
() => this.deps.transport.setMute(threadId, durationSeconds, threadType),
|
|
1367
1477
|
{ operation: "conversations.mute", idempotent: true }
|
|
1368
1478
|
);
|
|
1369
1479
|
}
|
|
1370
|
-
async sendTyping(target) {
|
|
1480
|
+
async sendTyping(target, explicitType) {
|
|
1371
1481
|
ensureConnected(this.deps.transport);
|
|
1372
|
-
const { threadId, threadType } = await this.deps
|
|
1373
|
-
target,
|
|
1374
|
-
void 0,
|
|
1375
|
-
this.fetchGroupIds
|
|
1376
|
-
);
|
|
1482
|
+
const { threadId, threadType } = await resolveThreadTarget(this.deps, target, explicitType);
|
|
1377
1483
|
await this.deps.executor.execute(() => this.deps.transport.sendTyping(threadId, threadType), {
|
|
1378
1484
|
operation: "conversations.sendTyping",
|
|
1379
1485
|
idempotent: true
|
|
@@ -1385,10 +1491,6 @@ var MessagesService = class {
|
|
|
1385
1491
|
this.deps = deps;
|
|
1386
1492
|
}
|
|
1387
1493
|
deps;
|
|
1388
|
-
fetchGroupIds = async () => {
|
|
1389
|
-
const map = await this.deps.transport.getAllGroups();
|
|
1390
|
-
return Object.keys(map);
|
|
1391
|
-
};
|
|
1392
1494
|
/**
|
|
1393
1495
|
* Unified message sender. Automatically resolves threadType from target or Group ID cache.
|
|
1394
1496
|
*/
|
|
@@ -1401,15 +1503,11 @@ var MessagesService = class {
|
|
|
1401
1503
|
if (!payload.text && !hasMedia) {
|
|
1402
1504
|
throw new ValidationError("Message payload must contain text or media/link/sticker");
|
|
1403
1505
|
}
|
|
1404
|
-
const
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
explicitType ?? payload.threadType
|
|
1408
|
-
this.fetchGroupIds
|
|
1506
|
+
const resolved = await resolveThreadTarget(
|
|
1507
|
+
this.deps,
|
|
1508
|
+
target,
|
|
1509
|
+
explicitType ?? payload.threadType
|
|
1409
1510
|
);
|
|
1410
|
-
if (!resolved.threadId) {
|
|
1411
|
-
throw new ValidationError("Target threadId must be a non-empty string");
|
|
1412
|
-
}
|
|
1413
1511
|
return this.deps.executor.execute(
|
|
1414
1512
|
() => this.deps.transport.sendMessage(resolved.threadId, payload, resolved.threadType),
|
|
1415
1513
|
{ operation: "messages.send", idempotent: false }
|
|
@@ -1457,20 +1555,11 @@ var MediaService = class {
|
|
|
1457
1555
|
}
|
|
1458
1556
|
deps;
|
|
1459
1557
|
messages;
|
|
1460
|
-
fetchGroupIds = async () => {
|
|
1461
|
-
const map = await this.deps.transport.getAllGroups();
|
|
1462
|
-
return Object.keys(map);
|
|
1463
|
-
};
|
|
1464
1558
|
async upload(target, sources, explicitType) {
|
|
1465
1559
|
ensureConnected(this.deps.transport);
|
|
1466
1560
|
const list = Array.isArray(sources) ? sources : [sources];
|
|
1467
1561
|
if (list.length === 0) throw new ValidationError("upload requires at least one media item");
|
|
1468
|
-
const
|
|
1469
|
-
const { threadId, threadType } = await this.deps.registry.resolve(
|
|
1470
|
-
targetLike,
|
|
1471
|
-
explicitType,
|
|
1472
|
-
this.fetchGroupIds
|
|
1473
|
-
);
|
|
1562
|
+
const { threadId, threadType } = await resolveThreadTarget(this.deps, target, explicitType);
|
|
1474
1563
|
const raw = await this.deps.executor.execute(
|
|
1475
1564
|
() => this.deps.transport.uploadAttachment(list, threadId, threadType),
|
|
1476
1565
|
{ operation: "media.upload", idempotent: false }
|
|
@@ -2197,7 +2286,19 @@ var Zapind = class {
|
|
|
2197
2286
|
if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {
|
|
2198
2287
|
throw new ValidationError("timeoutMs must be a positive number");
|
|
2199
2288
|
}
|
|
2200
|
-
|
|
2289
|
+
const qrTimeoutMs = options.qrTimeoutMs ?? 18e4;
|
|
2290
|
+
if (!Number.isFinite(qrTimeoutMs) || qrTimeoutMs <= 0) {
|
|
2291
|
+
throw new ValidationError("qrTimeoutMs must be a positive number");
|
|
2292
|
+
}
|
|
2293
|
+
const cacheTtlMs = options.cacheTtlMs ?? 6e4;
|
|
2294
|
+
if (!Number.isFinite(cacheTtlMs) || cacheTtlMs <= 0) {
|
|
2295
|
+
throw new ValidationError("cacheTtlMs must be a positive number");
|
|
2296
|
+
}
|
|
2297
|
+
const maxCacheEntries = options.maxCacheEntries ?? 500;
|
|
2298
|
+
if (!Number.isInteger(maxCacheEntries) || maxCacheEntries < 1) {
|
|
2299
|
+
throw new ValidationError("maxCacheEntries must be a positive integer");
|
|
2300
|
+
}
|
|
2301
|
+
this.defaultQrTimeoutMs = qrTimeoutMs;
|
|
2201
2302
|
this.selfListen = options.selfListen ?? false;
|
|
2202
2303
|
this.defaultUserAgent = options.userAgent ?? DEFAULT_USER_AGENT;
|
|
2203
2304
|
this.defaultLanguage = options.language ?? "vi";
|
|
@@ -2213,17 +2314,26 @@ var Zapind = class {
|
|
|
2213
2314
|
baseDelayMs: options.retry?.baseDelayMs ?? 300,
|
|
2214
2315
|
maxDelayMs: options.retry?.maxDelayMs ?? 5e3
|
|
2215
2316
|
};
|
|
2317
|
+
if (!Number.isInteger(retryOptions.maxAttempts) || retryOptions.maxAttempts < 1) {
|
|
2318
|
+
throw new ValidationError("retry.maxAttempts must be an integer >= 1");
|
|
2319
|
+
}
|
|
2320
|
+
if (!Number.isFinite(retryOptions.baseDelayMs) || retryOptions.baseDelayMs < 0) {
|
|
2321
|
+
throw new ValidationError("retry.baseDelayMs must be >= 0");
|
|
2322
|
+
}
|
|
2323
|
+
if (!Number.isFinite(retryOptions.maxDelayMs) || retryOptions.maxDelayMs < retryOptions.baseDelayMs) {
|
|
2324
|
+
throw new ValidationError("retry.maxDelayMs must be >= retry.baseDelayMs");
|
|
2325
|
+
}
|
|
2216
2326
|
const rateLimiter = options.rateLimit === false ? null : new RateLimiter(options.rateLimit ?? { maxRequestsPerSecond: 5, burst: 10 });
|
|
2217
2327
|
this.executor = new PolicyExecutor(timeoutMs, retryOptions, rateLimiter, this.logger);
|
|
2218
2328
|
this.eventBus = new EventBus(this.logger);
|
|
2219
2329
|
this.registry = new ThreadTypeRegistry(options.cacheTtlMs ?? 5 * 6e4);
|
|
2220
2330
|
this.userCache = new TTLCache({
|
|
2221
|
-
maxEntries:
|
|
2222
|
-
defaultTtlMs:
|
|
2331
|
+
maxEntries: maxCacheEntries,
|
|
2332
|
+
defaultTtlMs: cacheTtlMs
|
|
2223
2333
|
});
|
|
2224
2334
|
this.groupCache = new TTLCache({
|
|
2225
2335
|
maxEntries: options.maxCacheEntries ?? 250,
|
|
2226
|
-
defaultTtlMs:
|
|
2336
|
+
defaultTtlMs: cacheTtlMs
|
|
2227
2337
|
});
|
|
2228
2338
|
this.dedup = new InFlightDeduplicator();
|
|
2229
2339
|
const actions = {
|
|
@@ -2234,9 +2344,9 @@ var Zapind = class {
|
|
|
2234
2344
|
deleteMessage: (msg, onlyMe) => this.messages.delete(msg, onlyMe),
|
|
2235
2345
|
markSeen: (msgs) => this.messages.markSeen(msgs),
|
|
2236
2346
|
getGroupMessages: (groupId, count) => this.conversations.getMessages(groupId, count),
|
|
2237
|
-
sendTyping: (id, type) => this.conversations.sendTyping(
|
|
2238
|
-
pinConversation: (id, pinned, type) => this.conversations.pin(
|
|
2239
|
-
muteConversation: (id, duration, type) => this.conversations.mute(
|
|
2347
|
+
sendTyping: (id, type) => this.conversations.sendTyping(id, type),
|
|
2348
|
+
pinConversation: (id, pinned, type) => this.conversations.pin(id, pinned, type),
|
|
2349
|
+
muteConversation: (id, duration, type) => this.conversations.mute(id, duration, type),
|
|
2240
2350
|
sendFriendRequest: (uid, msg) => this.contacts.sendRequest(uid, msg),
|
|
2241
2351
|
blockUser: (uid) => this.contacts.block(uid),
|
|
2242
2352
|
unblockUser: (uid) => this.contacts.unblock(uid),
|
|
@@ -2442,6 +2552,13 @@ var Zapind = class {
|
|
|
2442
2552
|
await this.sessionStore.clear();
|
|
2443
2553
|
}
|
|
2444
2554
|
}
|
|
2555
|
+
/**
|
|
2556
|
+
* Stops realtime listeners, disconnects, clears caches, and removes all registered event listeners.
|
|
2557
|
+
*/
|
|
2558
|
+
async destroy(options = { clearSession: false }) {
|
|
2559
|
+
await this.logout(options);
|
|
2560
|
+
this.eventBus.removeAllListeners();
|
|
2561
|
+
}
|
|
2445
2562
|
// --- Event System ---
|
|
2446
2563
|
on(event, handler) {
|
|
2447
2564
|
return this.eventBus.on(event, handler);
|