@yaebal/media-group 0.0.1
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 +21 -0
- package/README.md +13 -0
- package/lib/index.d.ts +14 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +34 -0
- package/lib/index.js.map +1 -0
- package/lib/index.test.d.ts +2 -0
- package/lib/index.test.d.ts.map +1 -0
- package/lib/index.test.js +55 -0
- package/lib/index.test.js.map +1 -0
- package/package.json +48 -0
- package/src/index.test.ts +87 -0
- package/src/index.ts +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 neverlane
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# @yaebal/media-group
|
|
2
|
+
|
|
3
|
+
called once per album, with every message in the group.
|
|
4
|
+
|
|
5
|
+
## install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add @yaebal/media-group
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
part of [**yaebal**](https://github.com/neverlane/yaebal) — a type-safe, runtime-agnostic Telegram Bot API framework. MIT.
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Context, Message, Plugin } from "@yaebal/core";
|
|
2
|
+
/** called once per album, with every message in the group. */
|
|
3
|
+
export type MediaGroupHandler = (ctx: Context, messages: Message[]) => unknown | Promise<unknown>;
|
|
4
|
+
export interface MediaGroupOptions {
|
|
5
|
+
/** how long to wait for more album parts before firing. defaults to 200ms. */
|
|
6
|
+
delayMs?: number;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* telegram delivers an album as separate updates sharing a `media_group_id`.
|
|
10
|
+
* this buffers them and fires `handler(ctx, messages)` once, after a short
|
|
11
|
+
* debounce. album parts are consumed — they don't reach other handlers.
|
|
12
|
+
*/
|
|
13
|
+
export declare function mediaGroup(handler: MediaGroupHandler, options?: MediaGroupOptions): Plugin<Context, Record<never, never>>;
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAE7D,8DAA8D;AAC9D,MAAM,MAAM,iBAAiB,GAAG,CAAC,GAAG,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;AAElG,MAAM,WAAW,iBAAiB;IACjC,8EAA8E;IAC9E,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB;AAQD;;;;GAIG;AACH,wBAAgB,UAAU,CACzB,OAAO,EAAE,iBAAiB,EAC1B,OAAO,GAAE,iBAAsB,GAC7B,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAiCvC"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* telegram delivers an album as separate updates sharing a `media_group_id`.
|
|
3
|
+
* this buffers them and fires `handler(ctx, messages)` once, after a short
|
|
4
|
+
* debounce. album parts are consumed — they don't reach other handlers.
|
|
5
|
+
*/
|
|
6
|
+
export function mediaGroup(handler, options = {}) {
|
|
7
|
+
const delayMs = options.delayMs ?? 200;
|
|
8
|
+
const groups = new Map();
|
|
9
|
+
const flush = (id) => {
|
|
10
|
+
const group = groups.get(id);
|
|
11
|
+
if (!group)
|
|
12
|
+
return;
|
|
13
|
+
groups.delete(id);
|
|
14
|
+
void handler(group.ctx, group.messages);
|
|
15
|
+
};
|
|
16
|
+
const plugin = (composer) => composer.use((ctx, next) => {
|
|
17
|
+
const msg = ctx.message;
|
|
18
|
+
const id = msg?.media_group_id;
|
|
19
|
+
if (!msg || id === undefined)
|
|
20
|
+
return next();
|
|
21
|
+
const group = groups.get(id);
|
|
22
|
+
if (group) {
|
|
23
|
+
group.messages.push(msg);
|
|
24
|
+
clearTimeout(group.timer);
|
|
25
|
+
group.timer = setTimeout(() => flush(id), delayMs);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
groups.set(id, { ctx, messages: [msg], timer: setTimeout(() => flush(id), delayMs) });
|
|
29
|
+
}
|
|
30
|
+
// consumed — the album is handled here
|
|
31
|
+
});
|
|
32
|
+
return plugin;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index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M,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,GAAG,EAAE,QAAQ,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,UAAU,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,CAAC,CAAC;QACvF,CAAC;QAED,uCAAuC;IACxC,CAAC,CAAC,CAAC;IAEJ,OAAO,MAAM,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.test.d.ts","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import { Composer, Context } from "@yaebal/core";
|
|
4
|
+
import { mediaGroup } from "./index.js";
|
|
5
|
+
const noop = async () => { };
|
|
6
|
+
const entry = (c) => c.toMiddleware();
|
|
7
|
+
const api = {};
|
|
8
|
+
const albumCtx = (groupId, text) => new Context({
|
|
9
|
+
api,
|
|
10
|
+
update: {
|
|
11
|
+
update_id: 1,
|
|
12
|
+
message: {
|
|
13
|
+
message_id: 1,
|
|
14
|
+
date: 0,
|
|
15
|
+
chat: { id: 1, type: "private" },
|
|
16
|
+
from: { id: 1, is_bot: false, first_name: "u" },
|
|
17
|
+
media_group_id: groupId,
|
|
18
|
+
text,
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
updateType: "message",
|
|
22
|
+
});
|
|
23
|
+
const wait = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
24
|
+
test("album parts are collected into one handler call", async () => {
|
|
25
|
+
const captured = [];
|
|
26
|
+
const mw = entry(new Composer().install(mediaGroup((_ctx, messages) => {
|
|
27
|
+
captured.push(messages);
|
|
28
|
+
}, { delayMs: 5 })));
|
|
29
|
+
await mw(albumCtx("g1", "a"), noop);
|
|
30
|
+
await mw(albumCtx("g1", "b"), noop);
|
|
31
|
+
await mw(albumCtx("g1", "c"), noop);
|
|
32
|
+
await wait(25); // past the debounce
|
|
33
|
+
assert.equal(captured.length, 1); // one album, one call
|
|
34
|
+
assert.deepEqual(captured[0]?.map((m) => m.text), ["a", "b", "c"]);
|
|
35
|
+
});
|
|
36
|
+
test("messages without a media_group_id pass through", async () => {
|
|
37
|
+
let through = false;
|
|
38
|
+
const mw = entry(new Composer().install(mediaGroup(() => { }, { delayMs: 5 })).on("message:text", () => {
|
|
39
|
+
through = true;
|
|
40
|
+
}));
|
|
41
|
+
await mw(albumCtx(undefined, "plain"), noop);
|
|
42
|
+
assert.equal(through, true);
|
|
43
|
+
});
|
|
44
|
+
test("separate albums fire independently", async () => {
|
|
45
|
+
const sizes = [];
|
|
46
|
+
const mw = entry(new Composer().install(mediaGroup((_ctx, messages) => {
|
|
47
|
+
sizes.push(messages.length);
|
|
48
|
+
}, { delayMs: 5 })));
|
|
49
|
+
await mw(albumCtx("a", "1"), noop);
|
|
50
|
+
await mw(albumCtx("b", "1"), noop);
|
|
51
|
+
await mw(albumCtx("b", "2"), noop);
|
|
52
|
+
await wait(25);
|
|
53
|
+
assert.deepEqual(sizes.sort(), [1, 2]);
|
|
54
|
+
});
|
|
55
|
+
//# sourceMappingURL=index.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,oBAAoB,CAAC;AACxC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAiC,MAAM,cAAc,CAAC;AAChF,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAExC,MAAM,IAAI,GAAG,KAAK,IAAI,EAAE,GAAE,CAAC,CAAC;AAC5B,MAAM,KAAK,GAAG,CAAoB,CAAc,EAAE,EAAE,CACnD,CAAC,CAAC,YAAY,EAAoC,CAAC;AAEpD,MAAM,GAAG,GAAG,EAAW,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GAAG,CAAC,EAAE,IAAI,CAAC,CAAC;IACnC,MAAM,EAAE,CAAC,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC;IACnC,MAAM,EAAE,CAAC,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC;IAEnC,MAAM,IAAI,CAAC,EAAE,CAAC,CAAC;IACf,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AACxC,CAAC,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@yaebal/media-group",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "yaebal media-group plugin — collect album updates into one handler call.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./lib/index.js",
|
|
7
|
+
"types": "./lib/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./lib/index.d.ts",
|
|
11
|
+
"import": "./lib/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"lib",
|
|
16
|
+
"src"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@yaebal/core": "0.0.1"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@types/node": "latest"
|
|
23
|
+
},
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=20"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"telegram",
|
|
29
|
+
"telegram-bot",
|
|
30
|
+
"yaebal",
|
|
31
|
+
"media-group",
|
|
32
|
+
"album"
|
|
33
|
+
],
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "https://github.com/neverlane/yaebal",
|
|
38
|
+
"directory": "packages/media-group"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "tsc -p tsconfig.json",
|
|
45
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
46
|
+
"test": "node --test lib"
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import { Composer, Context, type Message, type Middleware } from "@yaebal/core";
|
|
4
|
+
import { mediaGroup } from "./index.js";
|
|
5
|
+
|
|
6
|
+
const noop = async () => {};
|
|
7
|
+
const entry = <C extends Context>(c: Composer<C>) =>
|
|
8
|
+
c.toMiddleware() as unknown as Middleware<Context>;
|
|
9
|
+
|
|
10
|
+
const api = {} as never;
|
|
11
|
+
const albumCtx = (groupId: string | undefined, text: string) =>
|
|
12
|
+
new Context({
|
|
13
|
+
api,
|
|
14
|
+
update: {
|
|
15
|
+
update_id: 1,
|
|
16
|
+
message: {
|
|
17
|
+
message_id: 1,
|
|
18
|
+
date: 0,
|
|
19
|
+
chat: { id: 1, type: "private" },
|
|
20
|
+
from: { id: 1, is_bot: false, first_name: "u" },
|
|
21
|
+
media_group_id: groupId,
|
|
22
|
+
text,
|
|
23
|
+
},
|
|
24
|
+
} as never,
|
|
25
|
+
updateType: "message",
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const wait = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
|
29
|
+
|
|
30
|
+
test("album parts are collected into one handler call", async () => {
|
|
31
|
+
const captured: Message[][] = [];
|
|
32
|
+
const mw = entry(
|
|
33
|
+
new Composer<Context>().install(
|
|
34
|
+
mediaGroup(
|
|
35
|
+
(_ctx, messages) => {
|
|
36
|
+
captured.push(messages);
|
|
37
|
+
},
|
|
38
|
+
{ delayMs: 5 },
|
|
39
|
+
),
|
|
40
|
+
),
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
await mw(albumCtx("g1", "a"), noop);
|
|
44
|
+
await mw(albumCtx("g1", "b"), noop);
|
|
45
|
+
await mw(albumCtx("g1", "c"), noop);
|
|
46
|
+
await wait(25); // past the debounce
|
|
47
|
+
|
|
48
|
+
assert.equal(captured.length, 1); // one album, one call
|
|
49
|
+
assert.deepEqual(
|
|
50
|
+
captured[0]?.map((m) => m.text),
|
|
51
|
+
["a", "b", "c"],
|
|
52
|
+
);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("messages without a media_group_id pass through", async () => {
|
|
56
|
+
let through = false;
|
|
57
|
+
const mw = entry(
|
|
58
|
+
new Composer<Context>().install(mediaGroup(() => {}, { delayMs: 5 })).on("message:text", () => {
|
|
59
|
+
through = true;
|
|
60
|
+
}),
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
await mw(albumCtx(undefined, "plain"), noop);
|
|
64
|
+
|
|
65
|
+
assert.equal(through, true);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("separate albums fire independently", async () => {
|
|
69
|
+
const sizes: number[] = [];
|
|
70
|
+
const mw = entry(
|
|
71
|
+
new Composer<Context>().install(
|
|
72
|
+
mediaGroup(
|
|
73
|
+
(_ctx, messages) => {
|
|
74
|
+
sizes.push(messages.length);
|
|
75
|
+
},
|
|
76
|
+
{ delayMs: 5 },
|
|
77
|
+
),
|
|
78
|
+
),
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
await mw(albumCtx("a", "1"), noop);
|
|
82
|
+
await mw(albumCtx("b", "1"), noop);
|
|
83
|
+
await mw(albumCtx("b", "2"), noop);
|
|
84
|
+
|
|
85
|
+
await wait(25);
|
|
86
|
+
assert.deepEqual(sizes.sort(), [1, 2]);
|
|
87
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { Context, Message, Plugin } from "@yaebal/core";
|
|
2
|
+
|
|
3
|
+
/** called once per album, with every message in the group. */
|
|
4
|
+
export type MediaGroupHandler = (ctx: Context, messages: Message[]) => unknown | Promise<unknown>;
|
|
5
|
+
|
|
6
|
+
export interface MediaGroupOptions {
|
|
7
|
+
/** how long to wait for more album parts before firing. defaults to 200ms. */
|
|
8
|
+
delayMs?: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface Group {
|
|
12
|
+
ctx: Context;
|
|
13
|
+
messages: Message[];
|
|
14
|
+
timer: ReturnType<typeof setTimeout>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* telegram delivers an album as separate updates sharing a `media_group_id`.
|
|
19
|
+
* this buffers them and fires `handler(ctx, messages)` once, after a short
|
|
20
|
+
* debounce. album parts are consumed — they don't reach other handlers.
|
|
21
|
+
*/
|
|
22
|
+
export function mediaGroup(
|
|
23
|
+
handler: MediaGroupHandler,
|
|
24
|
+
options: MediaGroupOptions = {},
|
|
25
|
+
): Plugin<Context, Record<never, never>> {
|
|
26
|
+
const delayMs = options.delayMs ?? 200;
|
|
27
|
+
const groups = new Map<string, Group>();
|
|
28
|
+
|
|
29
|
+
const flush = (id: string): void => {
|
|
30
|
+
const group = groups.get(id);
|
|
31
|
+
if (!group) return;
|
|
32
|
+
|
|
33
|
+
groups.delete(id);
|
|
34
|
+
void handler(group.ctx, group.messages);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const plugin: Plugin<Context, Record<never, never>> = (composer) =>
|
|
38
|
+
composer.use((ctx, next) => {
|
|
39
|
+
const msg = ctx.message as (Message & { media_group_id?: string }) | undefined;
|
|
40
|
+
const id = msg?.media_group_id;
|
|
41
|
+
|
|
42
|
+
if (!msg || id === undefined) return next();
|
|
43
|
+
|
|
44
|
+
const group = groups.get(id);
|
|
45
|
+
if (group) {
|
|
46
|
+
group.messages.push(msg);
|
|
47
|
+
clearTimeout(group.timer);
|
|
48
|
+
|
|
49
|
+
group.timer = setTimeout(() => flush(id), delayMs);
|
|
50
|
+
} else {
|
|
51
|
+
groups.set(id, { ctx, messages: [msg], timer: setTimeout(() => flush(id), delayMs) });
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// consumed — the album is handled here
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
return plugin;
|
|
58
|
+
}
|