@kodelyth/zalouser 2026.5.39 → 2026.5.42
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/README.md +120 -0
- package/api.ts +9 -0
- package/channel-plugin-api.ts +3 -0
- package/contract-api.ts +2 -0
- package/dist/accounts-DOefD_if.js +66 -0
- package/dist/accounts.runtime-KT101uuu.js +2 -0
- package/dist/api-DSWT4Dh_.js +133 -0
- package/dist/api.js +7 -0
- package/dist/channel-pby_3Sur.js +602 -0
- package/dist/channel-plugin-api.js +2 -0
- package/dist/channel.runtime-0aJ2O7Y8.js +25 -0
- package/dist/channel.setup-CqyWwqcQ.js +9 -0
- package/dist/contract-api.js +3 -0
- package/dist/doctor-contract-B9EvrW0j.js +128 -0
- package/dist/doctor-contract-api.js +2 -0
- package/dist/index.js +27 -0
- package/dist/monitor-CVtrUqyW.js +708 -0
- package/dist/runtime-api.js +19 -0
- package/dist/secret-contract-api.js +5 -0
- package/dist/security-audit-D_rftvs-.js +34 -0
- package/dist/send-uRjUB8mG.js +542 -0
- package/dist/session-route-CalHiv1d.js +92 -0
- package/dist/setup-entry.js +11 -0
- package/dist/setup-plugin-api.js +2 -0
- package/dist/setup-surface-Cfj4GQlB.js +360 -0
- package/dist/shared-DjK0e2FC.js +160 -0
- package/dist/test-api.js +5 -0
- package/dist/zalo-js-B80cRyDF.js +1285 -0
- package/doctor-contract-api.ts +1 -0
- package/index.ts +34 -0
- package/klaw.plugin.json +3 -286
- package/package.json +4 -4
- package/runtime-api.ts +62 -0
- package/secret-contract-api.ts +4 -0
- package/setup-entry.ts +9 -0
- package/setup-plugin-api.ts +2 -0
- package/src/accounts.runtime.ts +1 -0
- package/src/accounts.test-mocks.ts +14 -0
- package/src/accounts.test.ts +298 -0
- package/src/accounts.ts +136 -0
- package/src/channel-api.ts +16 -0
- package/src/channel.adapters.ts +432 -0
- package/src/channel.directory.test.ts +59 -0
- package/src/channel.runtime.ts +12 -0
- package/src/channel.sendpayload.test.ts +311 -0
- package/src/channel.setup.test.ts +30 -0
- package/src/channel.setup.ts +12 -0
- package/src/channel.test.ts +424 -0
- package/src/channel.ts +221 -0
- package/src/config-schema.ts +33 -0
- package/src/directory.ts +54 -0
- package/src/doctor-contract.ts +156 -0
- package/src/doctor.test.ts +87 -0
- package/src/doctor.ts +37 -0
- package/src/group-policy.test.ts +61 -0
- package/src/group-policy.ts +83 -0
- package/src/message-sid.test.ts +66 -0
- package/src/message-sid.ts +80 -0
- package/src/monitor.account-scope.test.ts +122 -0
- package/src/monitor.group-gating.test.ts +967 -0
- package/src/monitor.send-mocks.ts +20 -0
- package/src/monitor.ts +1057 -0
- package/src/probe.test.ts +60 -0
- package/src/probe.ts +35 -0
- package/src/qr-temp-file.ts +19 -0
- package/src/reaction.test.ts +19 -0
- package/src/reaction.ts +32 -0
- package/src/runtime.ts +9 -0
- package/src/security-audit.test.ts +83 -0
- package/src/security-audit.ts +71 -0
- package/src/send-receipt.ts +31 -0
- package/src/send.test.ts +424 -0
- package/src/send.ts +280 -0
- package/src/session-route.ts +121 -0
- package/src/setup-core.ts +36 -0
- package/src/setup-surface.test.ts +367 -0
- package/src/setup-surface.ts +481 -0
- package/src/setup-test-helpers.ts +42 -0
- package/src/shared.ts +92 -0
- package/src/status-issues.test.ts +31 -0
- package/src/status-issues.ts +55 -0
- package/src/test-helpers.ts +26 -0
- package/src/text-styles.test.ts +203 -0
- package/src/text-styles.ts +540 -0
- package/src/tool.test.ts +212 -0
- package/src/tool.ts +200 -0
- package/src/types.ts +127 -0
- package/src/zalo-js.credentials.test.ts +465 -0
- package/src/zalo-js.test-mocks.ts +89 -0
- package/src/zalo-js.ts +1889 -0
- package/src/zca-client.test.ts +27 -0
- package/src/zca-client.ts +259 -0
- package/src/zca-constants.ts +55 -0
- package/src/zca-js-exports.d.ts +22 -0
- package/test-api.ts +21 -0
- package/tsconfig.json +16 -0
- package/api.js +0 -7
- package/channel-plugin-api.js +0 -7
- package/contract-api.js +0 -7
- package/doctor-contract-api.js +0 -7
- package/index.js +0 -7
- package/runtime-api.js +0 -7
- package/secret-contract-api.js +0 -7
- package/setup-entry.js +0 -7
- package/setup-plugin-api.js +0 -7
- package/test-api.js +0 -7
package/src/directory.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { resolveZalouserAccountSync } from "./accounts.js";
|
|
2
|
+
import type { ChannelDirectoryEntry, KlawConfig } from "./channel-api.js";
|
|
3
|
+
import { parseZalouserDirectoryGroupId } from "./session-route.js";
|
|
4
|
+
|
|
5
|
+
type ZalouserDirectoryDeps = {
|
|
6
|
+
listZaloGroupMembers: (
|
|
7
|
+
profile: string,
|
|
8
|
+
groupId: string,
|
|
9
|
+
) => Promise<
|
|
10
|
+
Array<{
|
|
11
|
+
userId: string;
|
|
12
|
+
displayName?: string | null;
|
|
13
|
+
avatar?: string | null;
|
|
14
|
+
}>
|
|
15
|
+
>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
function mapUser(params: {
|
|
19
|
+
id: string;
|
|
20
|
+
name?: string | null;
|
|
21
|
+
avatarUrl?: string | null;
|
|
22
|
+
raw?: unknown;
|
|
23
|
+
}): ChannelDirectoryEntry {
|
|
24
|
+
return {
|
|
25
|
+
kind: "user",
|
|
26
|
+
id: params.id,
|
|
27
|
+
name: params.name ?? undefined,
|
|
28
|
+
avatarUrl: params.avatarUrl ?? undefined,
|
|
29
|
+
raw: params.raw,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export async function listZalouserDirectoryGroupMembers(
|
|
34
|
+
params: {
|
|
35
|
+
cfg: KlawConfig;
|
|
36
|
+
accountId?: string;
|
|
37
|
+
groupId: string;
|
|
38
|
+
limit?: number;
|
|
39
|
+
},
|
|
40
|
+
deps: ZalouserDirectoryDeps,
|
|
41
|
+
) {
|
|
42
|
+
const account = resolveZalouserAccountSync({ cfg: params.cfg, accountId: params.accountId });
|
|
43
|
+
const normalizedGroupId = parseZalouserDirectoryGroupId(params.groupId);
|
|
44
|
+
const members = await deps.listZaloGroupMembers(account.profile, normalizedGroupId);
|
|
45
|
+
const rows = members.map((member) =>
|
|
46
|
+
mapUser({
|
|
47
|
+
id: member.userId,
|
|
48
|
+
name: member.displayName,
|
|
49
|
+
avatarUrl: member.avatar ?? null,
|
|
50
|
+
raw: member,
|
|
51
|
+
}),
|
|
52
|
+
);
|
|
53
|
+
return typeof params.limit === "number" && params.limit > 0 ? rows.slice(0, params.limit) : rows;
|
|
54
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ChannelDoctorConfigMutation,
|
|
3
|
+
ChannelDoctorLegacyConfigRule,
|
|
4
|
+
} from "klaw/plugin-sdk/channel-contract";
|
|
5
|
+
import type { KlawConfig } from "klaw/plugin-sdk/config-contracts";
|
|
6
|
+
|
|
7
|
+
type ZalouserChannelsConfig = NonNullable<KlawConfig["channels"]>;
|
|
8
|
+
|
|
9
|
+
function asObjectRecord(value: unknown): Record<string, unknown> | null {
|
|
10
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
11
|
+
? (value as Record<string, unknown>)
|
|
12
|
+
: null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function hasLegacyZalouserGroupAllowAlias(value: unknown): boolean {
|
|
16
|
+
const group = asObjectRecord(value);
|
|
17
|
+
return Boolean(group && typeof group.allow === "boolean");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function hasLegacyZalouserGroupAllowAliases(value: unknown): boolean {
|
|
21
|
+
const groups = asObjectRecord(value);
|
|
22
|
+
return Boolean(
|
|
23
|
+
groups && Object.values(groups).some((group) => hasLegacyZalouserGroupAllowAlias(group)),
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function hasLegacyZalouserAccountGroupAllowAliases(value: unknown): boolean {
|
|
28
|
+
const accounts = asObjectRecord(value);
|
|
29
|
+
if (!accounts) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
return Object.values(accounts).some((account) => {
|
|
33
|
+
const accountRecord = asObjectRecord(account);
|
|
34
|
+
return Boolean(accountRecord && hasLegacyZalouserGroupAllowAliases(accountRecord.groups));
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function normalizeZalouserGroupAllowAliases(params: {
|
|
39
|
+
groups: Record<string, unknown>;
|
|
40
|
+
pathPrefix: string;
|
|
41
|
+
changes: string[];
|
|
42
|
+
}): { groups: Record<string, unknown>; changed: boolean } {
|
|
43
|
+
let changed = false;
|
|
44
|
+
const nextGroups: Record<string, unknown> = { ...params.groups };
|
|
45
|
+
for (const [groupId, groupValue] of Object.entries(params.groups)) {
|
|
46
|
+
const group = asObjectRecord(groupValue);
|
|
47
|
+
if (!group || typeof group.allow !== "boolean") {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
const nextGroup = { ...group };
|
|
51
|
+
if (typeof nextGroup.enabled !== "boolean") {
|
|
52
|
+
nextGroup.enabled = group.allow;
|
|
53
|
+
}
|
|
54
|
+
delete nextGroup.allow;
|
|
55
|
+
nextGroups[groupId] = nextGroup;
|
|
56
|
+
changed = true;
|
|
57
|
+
params.changes.push(
|
|
58
|
+
`Moved ${params.pathPrefix}.${groupId}.allow → ${params.pathPrefix}.${groupId}.enabled (${String(nextGroup.enabled)}).`,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
return { groups: nextGroups, changed };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function normalizeZalouserCompatibilityConfig(cfg: KlawConfig): ChannelDoctorConfigMutation {
|
|
65
|
+
const channels = asObjectRecord(cfg.channels);
|
|
66
|
+
const zalouser = asObjectRecord(channels?.zalouser);
|
|
67
|
+
if (!zalouser) {
|
|
68
|
+
return { config: cfg, changes: [] };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const changes: string[] = [];
|
|
72
|
+
let updatedZalouser: Record<string, unknown> = zalouser;
|
|
73
|
+
let changed = false;
|
|
74
|
+
|
|
75
|
+
const groups = asObjectRecord(updatedZalouser.groups);
|
|
76
|
+
if (groups) {
|
|
77
|
+
const normalized = normalizeZalouserGroupAllowAliases({
|
|
78
|
+
groups,
|
|
79
|
+
pathPrefix: "channels.zalouser.groups",
|
|
80
|
+
changes,
|
|
81
|
+
});
|
|
82
|
+
if (normalized.changed) {
|
|
83
|
+
updatedZalouser = { ...updatedZalouser, groups: normalized.groups };
|
|
84
|
+
changed = true;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const accounts = asObjectRecord(updatedZalouser.accounts);
|
|
89
|
+
if (accounts) {
|
|
90
|
+
let accountsChanged = false;
|
|
91
|
+
const nextAccounts: Record<string, unknown> = { ...accounts };
|
|
92
|
+
for (const [accountId, accountValue] of Object.entries(accounts)) {
|
|
93
|
+
const account = asObjectRecord(accountValue);
|
|
94
|
+
if (!account) {
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
const accountGroups = asObjectRecord(account.groups);
|
|
98
|
+
if (!accountGroups) {
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
const normalized = normalizeZalouserGroupAllowAliases({
|
|
102
|
+
groups: accountGroups,
|
|
103
|
+
pathPrefix: `channels.zalouser.accounts.${accountId}.groups`,
|
|
104
|
+
changes,
|
|
105
|
+
});
|
|
106
|
+
if (!normalized.changed) {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
nextAccounts[accountId] = {
|
|
110
|
+
...account,
|
|
111
|
+
groups: normalized.groups,
|
|
112
|
+
};
|
|
113
|
+
accountsChanged = true;
|
|
114
|
+
}
|
|
115
|
+
if (accountsChanged) {
|
|
116
|
+
updatedZalouser = { ...updatedZalouser, accounts: nextAccounts };
|
|
117
|
+
changed = true;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (!changed) {
|
|
122
|
+
return { config: cfg, changes: [] };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
config: {
|
|
127
|
+
...cfg,
|
|
128
|
+
channels: {
|
|
129
|
+
...cfg.channels,
|
|
130
|
+
zalouser: updatedZalouser as ZalouserChannelsConfig["zalouser"],
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
changes,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export const legacyConfigRules: ChannelDoctorLegacyConfigRule[] = [
|
|
138
|
+
{
|
|
139
|
+
path: ["channels", "zalouser", "groups"],
|
|
140
|
+
message:
|
|
141
|
+
'channels.zalouser.groups.<id>.allow is legacy; use channels.zalouser.groups.<id>.enabled instead. Run "klaw doctor --fix".',
|
|
142
|
+
match: hasLegacyZalouserGroupAllowAliases,
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
path: ["channels", "zalouser", "accounts"],
|
|
146
|
+
message:
|
|
147
|
+
'channels.zalouser.accounts.<id>.groups.<id>.allow is legacy; use channels.zalouser.accounts.<id>.groups.<id>.enabled instead. Run "klaw doctor --fix".',
|
|
148
|
+
match: hasLegacyZalouserAccountGroupAllowAliases,
|
|
149
|
+
},
|
|
150
|
+
];
|
|
151
|
+
|
|
152
|
+
export function normalizeCompatibilityConfig(params: {
|
|
153
|
+
cfg: KlawConfig;
|
|
154
|
+
}): ChannelDoctorConfigMutation {
|
|
155
|
+
return normalizeZalouserCompatibilityConfig(params.cfg);
|
|
156
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { zalouserDoctor } from "./doctor.js";
|
|
3
|
+
|
|
4
|
+
function getZaloUserCompatibilityNormalizer(): NonNullable<
|
|
5
|
+
typeof zalouserDoctor.normalizeCompatibilityConfig
|
|
6
|
+
> {
|
|
7
|
+
const normalize = zalouserDoctor.normalizeCompatibilityConfig;
|
|
8
|
+
if (!normalize) {
|
|
9
|
+
throw new Error("Expected zalouser doctor to expose normalizeCompatibilityConfig");
|
|
10
|
+
}
|
|
11
|
+
return normalize;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
describe("zalouser doctor", () => {
|
|
15
|
+
it("warns when mutable group names rely on disabled name matching", async () => {
|
|
16
|
+
const warnings = await Promise.resolve(
|
|
17
|
+
zalouserDoctor.collectMutableAllowlistWarnings?.({
|
|
18
|
+
cfg: {
|
|
19
|
+
channels: {
|
|
20
|
+
zalouser: {
|
|
21
|
+
groups: {
|
|
22
|
+
"group:trusted": {
|
|
23
|
+
enabled: true,
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
} as never,
|
|
29
|
+
}) ?? [],
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
expect(
|
|
33
|
+
warnings.some((warning: string) =>
|
|
34
|
+
warning.includes("mutable allowlist entry across zalouser"),
|
|
35
|
+
),
|
|
36
|
+
).toBe(true);
|
|
37
|
+
expect(
|
|
38
|
+
warnings.some((warning: string) =>
|
|
39
|
+
warning.includes("channels.zalouser.groups: group:trusted"),
|
|
40
|
+
),
|
|
41
|
+
).toBe(true);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("normalizes legacy group allow aliases to enabled", () => {
|
|
45
|
+
const normalize = getZaloUserCompatibilityNormalizer();
|
|
46
|
+
|
|
47
|
+
const result = normalize({
|
|
48
|
+
cfg: {
|
|
49
|
+
channels: {
|
|
50
|
+
zalouser: {
|
|
51
|
+
groups: {
|
|
52
|
+
"group:trusted": {
|
|
53
|
+
allow: true,
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
accounts: {
|
|
57
|
+
work: {
|
|
58
|
+
groups: {
|
|
59
|
+
"group:legacy": {
|
|
60
|
+
allow: false,
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
} as never,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
expect(result.config.channels?.zalouser?.groups?.["group:trusted"]).toEqual({
|
|
71
|
+
enabled: true,
|
|
72
|
+
});
|
|
73
|
+
expect(
|
|
74
|
+
(
|
|
75
|
+
result.config.channels?.zalouser?.accounts?.work as
|
|
76
|
+
| { groups?: Record<string, unknown> }
|
|
77
|
+
| undefined
|
|
78
|
+
)?.groups?.["group:legacy"],
|
|
79
|
+
).toEqual({
|
|
80
|
+
enabled: false,
|
|
81
|
+
});
|
|
82
|
+
expect(result.changes).toEqual([
|
|
83
|
+
"Moved channels.zalouser.groups.group:trusted.allow → channels.zalouser.groups.group:trusted.enabled (true).",
|
|
84
|
+
"Moved channels.zalouser.accounts.work.groups.group:legacy.allow → channels.zalouser.accounts.work.groups.group:legacy.enabled (false).",
|
|
85
|
+
]);
|
|
86
|
+
});
|
|
87
|
+
});
|
package/src/doctor.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { ChannelDoctorAdapter } from "klaw/plugin-sdk/channel-contract";
|
|
2
|
+
import { createDangerousNameMatchingMutableAllowlistWarningCollector } from "klaw/plugin-sdk/channel-policy";
|
|
3
|
+
import { legacyConfigRules, normalizeCompatibilityConfig } from "./doctor-contract.js";
|
|
4
|
+
import { isZalouserMutableGroupEntry } from "./security-audit.js";
|
|
5
|
+
|
|
6
|
+
function asObjectRecord(value: unknown): Record<string, unknown> | null {
|
|
7
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
8
|
+
? (value as Record<string, unknown>)
|
|
9
|
+
: null;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const collectZalouserMutableAllowlistWarnings =
|
|
13
|
+
createDangerousNameMatchingMutableAllowlistWarningCollector({
|
|
14
|
+
channel: "zalouser",
|
|
15
|
+
detector: isZalouserMutableGroupEntry,
|
|
16
|
+
collectLists: (scope) => {
|
|
17
|
+
const groups = asObjectRecord(scope.account.groups);
|
|
18
|
+
return groups
|
|
19
|
+
? [
|
|
20
|
+
{
|
|
21
|
+
pathLabel: `${scope.prefix}.groups`,
|
|
22
|
+
list: Object.keys(groups),
|
|
23
|
+
},
|
|
24
|
+
]
|
|
25
|
+
: [];
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
export const zalouserDoctor: ChannelDoctorAdapter = {
|
|
30
|
+
dmAllowFromMode: "topOnly",
|
|
31
|
+
groupModel: "hybrid",
|
|
32
|
+
groupAllowFromFallbackToAllowFrom: false,
|
|
33
|
+
warnOnEmptyGroupSenderAllowlist: false,
|
|
34
|
+
legacyConfigRules,
|
|
35
|
+
normalizeCompatibilityConfig,
|
|
36
|
+
collectMutableAllowlistWarnings: collectZalouserMutableAllowlistWarnings,
|
|
37
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
buildZalouserGroupCandidates,
|
|
4
|
+
findZalouserGroupEntry,
|
|
5
|
+
isZalouserGroupEntryAllowed,
|
|
6
|
+
normalizeZalouserGroupSlug,
|
|
7
|
+
} from "./group-policy.js";
|
|
8
|
+
|
|
9
|
+
describe("zalouser group policy helpers", () => {
|
|
10
|
+
it("normalizes group slug names", () => {
|
|
11
|
+
expect(normalizeZalouserGroupSlug(" Team Alpha ")).toBe("team-alpha");
|
|
12
|
+
expect(normalizeZalouserGroupSlug("#Roadmap Updates")).toBe("roadmap-updates");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("builds ordered candidates with optional aliases", () => {
|
|
16
|
+
expect(
|
|
17
|
+
buildZalouserGroupCandidates({
|
|
18
|
+
groupId: "123",
|
|
19
|
+
groupChannel: "chan-1",
|
|
20
|
+
groupName: "Team Alpha",
|
|
21
|
+
includeGroupIdAlias: true,
|
|
22
|
+
}),
|
|
23
|
+
).toEqual(["123", "group:123", "chan-1", "Team Alpha", "team-alpha", "*"]);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("builds id-only candidates when name matching is disabled", () => {
|
|
27
|
+
expect(
|
|
28
|
+
buildZalouserGroupCandidates({
|
|
29
|
+
groupId: "123",
|
|
30
|
+
groupChannel: "chan-1",
|
|
31
|
+
groupName: "Team Alpha",
|
|
32
|
+
includeGroupIdAlias: true,
|
|
33
|
+
allowNameMatching: false,
|
|
34
|
+
}),
|
|
35
|
+
).toEqual(["123", "group:123", "*"]);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("finds the first matching group entry", () => {
|
|
39
|
+
const groups = {
|
|
40
|
+
"group:123": { enabled: true },
|
|
41
|
+
"team-alpha": { requireMention: false },
|
|
42
|
+
"*": { requireMention: true },
|
|
43
|
+
};
|
|
44
|
+
const entry = findZalouserGroupEntry(
|
|
45
|
+
groups,
|
|
46
|
+
buildZalouserGroupCandidates({
|
|
47
|
+
groupId: "123",
|
|
48
|
+
groupName: "Team Alpha",
|
|
49
|
+
includeGroupIdAlias: true,
|
|
50
|
+
}),
|
|
51
|
+
);
|
|
52
|
+
expect(entry).toEqual({ enabled: true });
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("evaluates allow/enable flags", () => {
|
|
56
|
+
expect(isZalouserGroupEntryAllowed({ enabled: true })).toBe(true);
|
|
57
|
+
expect(isZalouserGroupEntryAllowed({ allow: false } as never)).toBe(false);
|
|
58
|
+
expect(isZalouserGroupEntryAllowed({ enabled: false })).toBe(false);
|
|
59
|
+
expect(isZalouserGroupEntryAllowed(undefined)).toBe(false);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { normalizeOptionalLowercaseString } from "klaw/plugin-sdk/string-coerce-runtime";
|
|
2
|
+
import type { ZalouserGroupConfig } from "./types.js";
|
|
3
|
+
|
|
4
|
+
type ZalouserGroups = Record<string, ZalouserGroupConfig>;
|
|
5
|
+
|
|
6
|
+
function toGroupCandidate(value?: string | null): string {
|
|
7
|
+
return value?.trim() ?? "";
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function normalizeZalouserGroupSlug(raw?: string | null): string {
|
|
11
|
+
const trimmed = normalizeOptionalLowercaseString(raw) ?? "";
|
|
12
|
+
if (!trimmed) {
|
|
13
|
+
return "";
|
|
14
|
+
}
|
|
15
|
+
return trimmed
|
|
16
|
+
.replace(/^#/, "")
|
|
17
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
18
|
+
.replace(/^-+|-+$/g, "");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function buildZalouserGroupCandidates(params: {
|
|
22
|
+
groupId?: string | null;
|
|
23
|
+
groupChannel?: string | null;
|
|
24
|
+
groupName?: string | null;
|
|
25
|
+
includeGroupIdAlias?: boolean;
|
|
26
|
+
includeWildcard?: boolean;
|
|
27
|
+
allowNameMatching?: boolean;
|
|
28
|
+
}): string[] {
|
|
29
|
+
const seen = new Set<string>();
|
|
30
|
+
const out: string[] = [];
|
|
31
|
+
const push = (value?: string | null) => {
|
|
32
|
+
const normalized = toGroupCandidate(value);
|
|
33
|
+
if (!normalized || seen.has(normalized)) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
seen.add(normalized);
|
|
37
|
+
out.push(normalized);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const groupId = toGroupCandidate(params.groupId);
|
|
41
|
+
const groupChannel = toGroupCandidate(params.groupChannel);
|
|
42
|
+
const groupName = toGroupCandidate(params.groupName);
|
|
43
|
+
|
|
44
|
+
push(groupId);
|
|
45
|
+
if (params.includeGroupIdAlias === true && groupId) {
|
|
46
|
+
push(`group:${groupId}`);
|
|
47
|
+
}
|
|
48
|
+
if (params.allowNameMatching !== false) {
|
|
49
|
+
push(groupChannel);
|
|
50
|
+
push(groupName);
|
|
51
|
+
if (groupName) {
|
|
52
|
+
push(normalizeZalouserGroupSlug(groupName));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (params.includeWildcard !== false) {
|
|
56
|
+
push("*");
|
|
57
|
+
}
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function findZalouserGroupEntry(
|
|
62
|
+
groups: ZalouserGroups | undefined,
|
|
63
|
+
candidates: string[],
|
|
64
|
+
): ZalouserGroupConfig | undefined {
|
|
65
|
+
if (!groups) {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
for (const candidate of candidates) {
|
|
69
|
+
const entry = groups[candidate];
|
|
70
|
+
if (entry) {
|
|
71
|
+
return entry;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function isZalouserGroupEntryAllowed(entry: ZalouserGroupConfig | undefined): boolean {
|
|
78
|
+
if (!entry) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
const legacyAllow = (entry as ZalouserGroupConfig & { allow?: unknown }).allow;
|
|
82
|
+
return legacyAllow !== false && entry.enabled !== false;
|
|
83
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
formatZalouserMessageSidFull,
|
|
4
|
+
parseZalouserMessageSidFull,
|
|
5
|
+
resolveZalouserMessageSid,
|
|
6
|
+
resolveZalouserReactionMessageIds,
|
|
7
|
+
} from "./message-sid.js";
|
|
8
|
+
|
|
9
|
+
describe("zalouser message sid helpers", () => {
|
|
10
|
+
it("parses MessageSidFull pairs", () => {
|
|
11
|
+
expect(parseZalouserMessageSidFull("111:222")).toEqual({
|
|
12
|
+
msgId: "111",
|
|
13
|
+
cliMsgId: "222",
|
|
14
|
+
});
|
|
15
|
+
expect(parseZalouserMessageSidFull("111")).toBeNull();
|
|
16
|
+
expect(parseZalouserMessageSidFull(undefined)).toBeNull();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("resolves reaction ids from explicit params first", () => {
|
|
20
|
+
expect(
|
|
21
|
+
resolveZalouserReactionMessageIds({
|
|
22
|
+
messageId: "m-1",
|
|
23
|
+
cliMsgId: "c-1",
|
|
24
|
+
currentMessageId: "x:y",
|
|
25
|
+
}),
|
|
26
|
+
).toEqual({
|
|
27
|
+
msgId: "m-1",
|
|
28
|
+
cliMsgId: "c-1",
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("resolves reaction ids from current message sid full", () => {
|
|
33
|
+
expect(
|
|
34
|
+
resolveZalouserReactionMessageIds({
|
|
35
|
+
currentMessageId: "m-2:c-2",
|
|
36
|
+
}),
|
|
37
|
+
).toEqual({
|
|
38
|
+
msgId: "m-2",
|
|
39
|
+
cliMsgId: "c-2",
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("falls back to duplicated current id when no pair is available", () => {
|
|
44
|
+
expect(
|
|
45
|
+
resolveZalouserReactionMessageIds({
|
|
46
|
+
currentMessageId: "solo",
|
|
47
|
+
}),
|
|
48
|
+
).toEqual({
|
|
49
|
+
msgId: "solo",
|
|
50
|
+
cliMsgId: "solo",
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("formats message sid fields for context payload", () => {
|
|
55
|
+
expect(formatZalouserMessageSidFull({ msgId: "1", cliMsgId: "2" })).toBe("1:2");
|
|
56
|
+
expect(formatZalouserMessageSidFull({ msgId: "1" })).toBe("1");
|
|
57
|
+
expect(formatZalouserMessageSidFull({ cliMsgId: "2" })).toBe("2");
|
|
58
|
+
expect(formatZalouserMessageSidFull({})).toBeUndefined();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("resolves primary message sid with fallback timestamp", () => {
|
|
62
|
+
expect(resolveZalouserMessageSid({ msgId: "1", cliMsgId: "2", fallback: "t" })).toBe("1");
|
|
63
|
+
expect(resolveZalouserMessageSid({ cliMsgId: "2", fallback: "t" })).toBe("2");
|
|
64
|
+
expect(resolveZalouserMessageSid({ fallback: "t" })).toBe("t");
|
|
65
|
+
});
|
|
66
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
function toMessageSidPart(value?: string | number | null): string {
|
|
2
|
+
if (typeof value === "string") {
|
|
3
|
+
return value.trim();
|
|
4
|
+
}
|
|
5
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
6
|
+
return String(Math.trunc(value));
|
|
7
|
+
}
|
|
8
|
+
return "";
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function parseZalouserMessageSidFull(
|
|
12
|
+
value?: string | number | null,
|
|
13
|
+
): { msgId: string; cliMsgId: string } | null {
|
|
14
|
+
const raw = toMessageSidPart(value);
|
|
15
|
+
if (!raw) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
const [msgIdPart, cliMsgIdPart] = raw.split(":").map((entry) => entry.trim());
|
|
19
|
+
if (!msgIdPart || !cliMsgIdPart) {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
return { msgId: msgIdPart, cliMsgId: cliMsgIdPart };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function resolveZalouserReactionMessageIds(params: {
|
|
26
|
+
messageId?: string;
|
|
27
|
+
cliMsgId?: string;
|
|
28
|
+
currentMessageId?: string | number;
|
|
29
|
+
}): { msgId: string; cliMsgId: string } | null {
|
|
30
|
+
const explicitMessageId = toMessageSidPart(params.messageId);
|
|
31
|
+
const explicitCliMsgId = toMessageSidPart(params.cliMsgId);
|
|
32
|
+
if (explicitMessageId && explicitCliMsgId) {
|
|
33
|
+
return { msgId: explicitMessageId, cliMsgId: explicitCliMsgId };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const parsedFromCurrent = parseZalouserMessageSidFull(params.currentMessageId);
|
|
37
|
+
if (parsedFromCurrent) {
|
|
38
|
+
return parsedFromCurrent;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const currentRaw = toMessageSidPart(params.currentMessageId);
|
|
42
|
+
if (!currentRaw) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
if (explicitMessageId && !explicitCliMsgId) {
|
|
46
|
+
return { msgId: explicitMessageId, cliMsgId: currentRaw };
|
|
47
|
+
}
|
|
48
|
+
if (!explicitMessageId && explicitCliMsgId) {
|
|
49
|
+
return { msgId: currentRaw, cliMsgId: explicitCliMsgId };
|
|
50
|
+
}
|
|
51
|
+
return { msgId: currentRaw, cliMsgId: currentRaw };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function formatZalouserMessageSidFull(params: {
|
|
55
|
+
msgId?: string | null;
|
|
56
|
+
cliMsgId?: string | null;
|
|
57
|
+
}): string | undefined {
|
|
58
|
+
const msgId = toMessageSidPart(params.msgId);
|
|
59
|
+
const cliMsgId = toMessageSidPart(params.cliMsgId);
|
|
60
|
+
if (!msgId && !cliMsgId) {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
if (msgId && cliMsgId) {
|
|
64
|
+
return `${msgId}:${cliMsgId}`;
|
|
65
|
+
}
|
|
66
|
+
return msgId || cliMsgId || undefined;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function resolveZalouserMessageSid(params: {
|
|
70
|
+
msgId?: string | null;
|
|
71
|
+
cliMsgId?: string | null;
|
|
72
|
+
fallback?: string | null;
|
|
73
|
+
}): string | undefined {
|
|
74
|
+
const msgId = toMessageSidPart(params.msgId);
|
|
75
|
+
const cliMsgId = toMessageSidPart(params.cliMsgId);
|
|
76
|
+
if (msgId || cliMsgId) {
|
|
77
|
+
return msgId || cliMsgId;
|
|
78
|
+
}
|
|
79
|
+
return toMessageSidPart(params.fallback) || undefined;
|
|
80
|
+
}
|