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