@theophilusdev/conduit 1.1.4 → 1.2.5
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/dist/index.cjs +130 -52
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +50 -12
- package/dist/index.d.ts +50 -12
- package/dist/index.mjs +130 -52
- package/dist/index.mjs.map +1 -1
- package/docs/DOCS.md +11 -0
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -7,6 +7,10 @@ import { MessengerBotOptions, MessengerBot } from '@dongdev/fca-unofficial';
|
|
|
7
7
|
type Loose = string & {};
|
|
8
8
|
/** Configuration options for the Conduit client. Extends {@link MessengerBotOptions}. */
|
|
9
9
|
interface ConduitClientConfig extends MessengerBotOptions {
|
|
10
|
+
queue?: {
|
|
11
|
+
messageQueue?: ConduitQueueConfig;
|
|
12
|
+
threadQueue?: ConduitQueueConfig;
|
|
13
|
+
};
|
|
10
14
|
}
|
|
11
15
|
/**
|
|
12
16
|
* Credentials used to authenticate the Conduit client with Facebook.
|
|
@@ -238,6 +242,26 @@ interface ConduitMessageBody {
|
|
|
238
242
|
}
|
|
239
243
|
/** A middleware function for a specific Conduit event. */
|
|
240
244
|
type Middleware<K extends keyof ConduitEvents> = (data: Parameters<ConduitEvents[K]> extends [infer First, ...any[]] ? First : never, next?: () => Promise<void>) => Promise<void>;
|
|
245
|
+
type ConduitQueueJob<T> = () => Promise<T>;
|
|
246
|
+
interface ConduitQueueConfig {
|
|
247
|
+
minDelayMs: number;
|
|
248
|
+
maxDelayMs: number;
|
|
249
|
+
switchDelayMinMs?: number;
|
|
250
|
+
switchDelayMaxMs?: number;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
declare class ConduitQueue {
|
|
254
|
+
private minDelayMs;
|
|
255
|
+
private maxDelayMs;
|
|
256
|
+
private switchDelayMinMs;
|
|
257
|
+
private switchDelayMaxMs;
|
|
258
|
+
private queues;
|
|
259
|
+
private running;
|
|
260
|
+
private lastThreadID;
|
|
261
|
+
constructor(options: ConduitQueueConfig);
|
|
262
|
+
enqueue<T>(threadID: string, job: ConduitQueueJob<T>): Promise<T>;
|
|
263
|
+
private _run;
|
|
264
|
+
}
|
|
241
265
|
|
|
242
266
|
/**
|
|
243
267
|
* Provides message-related API methods wrapping the underlying FCA client.
|
|
@@ -245,7 +269,8 @@ type Middleware<K extends keyof ConduitEvents> = (data: Parameters<ConduitEvents
|
|
|
245
269
|
*/
|
|
246
270
|
declare class ConduitMessagesAPI {
|
|
247
271
|
private readonly bot;
|
|
248
|
-
|
|
272
|
+
private readonly queue?;
|
|
273
|
+
constructor(bot: MessengerBot, queue?: ConduitQueue | undefined);
|
|
249
274
|
/**
|
|
250
275
|
* Sends a message to a thread.
|
|
251
276
|
* @param body - The message text.
|
|
@@ -338,7 +363,8 @@ declare class ConduitMessagesAPI {
|
|
|
338
363
|
*/
|
|
339
364
|
declare class ConduitThreadsAPI {
|
|
340
365
|
private readonly bot;
|
|
341
|
-
|
|
366
|
+
private readonly queue?;
|
|
367
|
+
constructor(bot: MessengerBot, queue?: ConduitQueue | undefined);
|
|
342
368
|
/**
|
|
343
369
|
* Fetches detailed info about a thread.
|
|
344
370
|
* @param threadID - The thread ID to query.
|
|
@@ -497,7 +523,7 @@ declare class ConduitAccountAPI {
|
|
|
497
523
|
* ```ts
|
|
498
524
|
* const conduit = new ConduitClient(config);
|
|
499
525
|
* await conduit.login(credentials);
|
|
500
|
-
* conduit.on("message:
|
|
526
|
+
* conduit.on("message:create", async (ctx, next) => {
|
|
501
527
|
* console.log(ctx.body);
|
|
502
528
|
* await next();
|
|
503
529
|
* });
|
|
@@ -514,6 +540,10 @@ declare class ConduitClient {
|
|
|
514
540
|
private _users;
|
|
515
541
|
/** Lazily-initialized account API wrapper. */
|
|
516
542
|
private _account;
|
|
543
|
+
/** Lazily-initialized queue for message operations. */
|
|
544
|
+
private _messageQueue;
|
|
545
|
+
/** Lazily-initialized queue for thread operations. */
|
|
546
|
+
private _threadQueue;
|
|
517
547
|
/** Configuration forwarded to the FCA layer on login. */
|
|
518
548
|
private config;
|
|
519
549
|
/**
|
|
@@ -561,6 +591,16 @@ declare class ConduitClient {
|
|
|
561
591
|
* blocking users, and logging out.
|
|
562
592
|
*/
|
|
563
593
|
get account(): ConduitAccountAPI;
|
|
594
|
+
/**
|
|
595
|
+
* Lazily-initialized queue for message operations.
|
|
596
|
+
* Only created when `config.queue.messageQueue` is defined.
|
|
597
|
+
*/
|
|
598
|
+
private get messageQueue();
|
|
599
|
+
/**
|
|
600
|
+
* Lazily-initialized queue for thread operations.
|
|
601
|
+
* Only created when `config.queue.threadQueue` is defined.
|
|
602
|
+
*/
|
|
603
|
+
private get threadQueue();
|
|
564
604
|
/**
|
|
565
605
|
* Authenticates with Messenger and initialises the underlying FCA bot.
|
|
566
606
|
* Must be called before any events can be received.
|
|
@@ -592,12 +632,10 @@ declare class ConduitClient {
|
|
|
592
632
|
*/
|
|
593
633
|
onFca(event: string, ...middlewares: ((data: any, next?: () => Promise<void>) => Promise<void>)[]): this;
|
|
594
634
|
/**
|
|
595
|
-
*
|
|
596
|
-
* have any type safety and auto-completion features. Use
|
|
597
|
-
* at your own risk.
|
|
635
|
+
* Direct access to the raw FCA API. No type safety — use as a last resort.
|
|
598
636
|
*
|
|
599
|
-
* @returns The raw
|
|
600
|
-
|
|
637
|
+
* @returns The raw FCA API context.
|
|
638
|
+
*/
|
|
601
639
|
get api(): any;
|
|
602
640
|
/**
|
|
603
641
|
* Translates a single Conduit event to its FCA equivalent and attaches a
|
|
@@ -612,7 +650,7 @@ declare class ConduitClient {
|
|
|
612
650
|
* This getter enforces the client lifecycle by ensuring that the underlying
|
|
613
651
|
* FCA bot has been created via {@link login} before any access is allowed.
|
|
614
652
|
*
|
|
615
|
-
* @throws {ConduitError} If the client has not been initialized yet
|
|
653
|
+
* @throws {ConduitError} If the client has not been initialized yet.
|
|
616
654
|
* @returns {MessengerBot} The active Messenger bot instance.
|
|
617
655
|
*/
|
|
618
656
|
private get client();
|
|
@@ -621,8 +659,8 @@ declare class ConduitClient {
|
|
|
621
659
|
* relevant Conduit events based on the incoming `logMessageType`.
|
|
622
660
|
*
|
|
623
661
|
* `thread:update` always fires with the raw payload when its stack is
|
|
624
|
-
* non-empty. Specific sub-events
|
|
625
|
-
*
|
|
662
|
+
* non-empty. Specific sub-events fire only when their stack is also
|
|
663
|
+
* non-empty and a matching `logMessageType` exists.
|
|
626
664
|
*
|
|
627
665
|
* Calling this method more than once is a no-op.
|
|
628
666
|
*/
|
|
@@ -675,4 +713,4 @@ declare class ConduitError extends Error {
|
|
|
675
713
|
static uninitializedClient(): ConduitError;
|
|
676
714
|
}
|
|
677
715
|
|
|
678
|
-
export { type AddedParticipant, ConduitAccountAPI, ConduitClient, type ConduitClientConfig, type ConduitCredentials, ConduitError, type ConduitEvents, type ConduitMessageBody, ConduitMessagesAPI, ConduitThreadsAPI, ConduitUsersAPI, type Loose, type Message, type MessageCreatePayload, type MessageReactPayload, type MessageReadPayload, type MessageRemovePayload, type MessageRespondPayload, type MessageWritingPayload, type Middleware, type Replyable, type Sendable, type ThreadAdminChangedPayload, type ThreadEventBase, type ThreadNicknameChangedPayload, type ThreadPhotoReplacedPayload, type ThreadThemeChangedPayload, type ThreadTitleChangePayload, type ThreadUpdatePayload, type UserCreatePayload, type UserRemovePayload, toFcaEvent };
|
|
716
|
+
export { type AddedParticipant, ConduitAccountAPI, ConduitClient, type ConduitClientConfig, type ConduitCredentials, ConduitError, type ConduitEvents, type ConduitMessageBody, ConduitMessagesAPI, type ConduitQueueConfig, type ConduitQueueJob, ConduitThreadsAPI, ConduitUsersAPI, type Loose, type Message, type MessageCreatePayload, type MessageReactPayload, type MessageReadPayload, type MessageRemovePayload, type MessageRespondPayload, type MessageWritingPayload, type Middleware, type Replyable, type Sendable, type ThreadAdminChangedPayload, type ThreadEventBase, type ThreadNicknameChangedPayload, type ThreadPhotoReplacedPayload, type ThreadThemeChangedPayload, type ThreadTitleChangePayload, type ThreadUpdatePayload, type UserCreatePayload, type UserRemovePayload, toFcaEvent };
|
package/dist/index.d.ts
CHANGED
|
@@ -7,6 +7,10 @@ import { MessengerBotOptions, MessengerBot } from '@dongdev/fca-unofficial';
|
|
|
7
7
|
type Loose = string & {};
|
|
8
8
|
/** Configuration options for the Conduit client. Extends {@link MessengerBotOptions}. */
|
|
9
9
|
interface ConduitClientConfig extends MessengerBotOptions {
|
|
10
|
+
queue?: {
|
|
11
|
+
messageQueue?: ConduitQueueConfig;
|
|
12
|
+
threadQueue?: ConduitQueueConfig;
|
|
13
|
+
};
|
|
10
14
|
}
|
|
11
15
|
/**
|
|
12
16
|
* Credentials used to authenticate the Conduit client with Facebook.
|
|
@@ -238,6 +242,26 @@ interface ConduitMessageBody {
|
|
|
238
242
|
}
|
|
239
243
|
/** A middleware function for a specific Conduit event. */
|
|
240
244
|
type Middleware<K extends keyof ConduitEvents> = (data: Parameters<ConduitEvents[K]> extends [infer First, ...any[]] ? First : never, next?: () => Promise<void>) => Promise<void>;
|
|
245
|
+
type ConduitQueueJob<T> = () => Promise<T>;
|
|
246
|
+
interface ConduitQueueConfig {
|
|
247
|
+
minDelayMs: number;
|
|
248
|
+
maxDelayMs: number;
|
|
249
|
+
switchDelayMinMs?: number;
|
|
250
|
+
switchDelayMaxMs?: number;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
declare class ConduitQueue {
|
|
254
|
+
private minDelayMs;
|
|
255
|
+
private maxDelayMs;
|
|
256
|
+
private switchDelayMinMs;
|
|
257
|
+
private switchDelayMaxMs;
|
|
258
|
+
private queues;
|
|
259
|
+
private running;
|
|
260
|
+
private lastThreadID;
|
|
261
|
+
constructor(options: ConduitQueueConfig);
|
|
262
|
+
enqueue<T>(threadID: string, job: ConduitQueueJob<T>): Promise<T>;
|
|
263
|
+
private _run;
|
|
264
|
+
}
|
|
241
265
|
|
|
242
266
|
/**
|
|
243
267
|
* Provides message-related API methods wrapping the underlying FCA client.
|
|
@@ -245,7 +269,8 @@ type Middleware<K extends keyof ConduitEvents> = (data: Parameters<ConduitEvents
|
|
|
245
269
|
*/
|
|
246
270
|
declare class ConduitMessagesAPI {
|
|
247
271
|
private readonly bot;
|
|
248
|
-
|
|
272
|
+
private readonly queue?;
|
|
273
|
+
constructor(bot: MessengerBot, queue?: ConduitQueue | undefined);
|
|
249
274
|
/**
|
|
250
275
|
* Sends a message to a thread.
|
|
251
276
|
* @param body - The message text.
|
|
@@ -338,7 +363,8 @@ declare class ConduitMessagesAPI {
|
|
|
338
363
|
*/
|
|
339
364
|
declare class ConduitThreadsAPI {
|
|
340
365
|
private readonly bot;
|
|
341
|
-
|
|
366
|
+
private readonly queue?;
|
|
367
|
+
constructor(bot: MessengerBot, queue?: ConduitQueue | undefined);
|
|
342
368
|
/**
|
|
343
369
|
* Fetches detailed info about a thread.
|
|
344
370
|
* @param threadID - The thread ID to query.
|
|
@@ -497,7 +523,7 @@ declare class ConduitAccountAPI {
|
|
|
497
523
|
* ```ts
|
|
498
524
|
* const conduit = new ConduitClient(config);
|
|
499
525
|
* await conduit.login(credentials);
|
|
500
|
-
* conduit.on("message:
|
|
526
|
+
* conduit.on("message:create", async (ctx, next) => {
|
|
501
527
|
* console.log(ctx.body);
|
|
502
528
|
* await next();
|
|
503
529
|
* });
|
|
@@ -514,6 +540,10 @@ declare class ConduitClient {
|
|
|
514
540
|
private _users;
|
|
515
541
|
/** Lazily-initialized account API wrapper. */
|
|
516
542
|
private _account;
|
|
543
|
+
/** Lazily-initialized queue for message operations. */
|
|
544
|
+
private _messageQueue;
|
|
545
|
+
/** Lazily-initialized queue for thread operations. */
|
|
546
|
+
private _threadQueue;
|
|
517
547
|
/** Configuration forwarded to the FCA layer on login. */
|
|
518
548
|
private config;
|
|
519
549
|
/**
|
|
@@ -561,6 +591,16 @@ declare class ConduitClient {
|
|
|
561
591
|
* blocking users, and logging out.
|
|
562
592
|
*/
|
|
563
593
|
get account(): ConduitAccountAPI;
|
|
594
|
+
/**
|
|
595
|
+
* Lazily-initialized queue for message operations.
|
|
596
|
+
* Only created when `config.queue.messageQueue` is defined.
|
|
597
|
+
*/
|
|
598
|
+
private get messageQueue();
|
|
599
|
+
/**
|
|
600
|
+
* Lazily-initialized queue for thread operations.
|
|
601
|
+
* Only created when `config.queue.threadQueue` is defined.
|
|
602
|
+
*/
|
|
603
|
+
private get threadQueue();
|
|
564
604
|
/**
|
|
565
605
|
* Authenticates with Messenger and initialises the underlying FCA bot.
|
|
566
606
|
* Must be called before any events can be received.
|
|
@@ -592,12 +632,10 @@ declare class ConduitClient {
|
|
|
592
632
|
*/
|
|
593
633
|
onFca(event: string, ...middlewares: ((data: any, next?: () => Promise<void>) => Promise<void>)[]): this;
|
|
594
634
|
/**
|
|
595
|
-
*
|
|
596
|
-
* have any type safety and auto-completion features. Use
|
|
597
|
-
* at your own risk.
|
|
635
|
+
* Direct access to the raw FCA API. No type safety — use as a last resort.
|
|
598
636
|
*
|
|
599
|
-
* @returns The raw
|
|
600
|
-
|
|
637
|
+
* @returns The raw FCA API context.
|
|
638
|
+
*/
|
|
601
639
|
get api(): any;
|
|
602
640
|
/**
|
|
603
641
|
* Translates a single Conduit event to its FCA equivalent and attaches a
|
|
@@ -612,7 +650,7 @@ declare class ConduitClient {
|
|
|
612
650
|
* This getter enforces the client lifecycle by ensuring that the underlying
|
|
613
651
|
* FCA bot has been created via {@link login} before any access is allowed.
|
|
614
652
|
*
|
|
615
|
-
* @throws {ConduitError} If the client has not been initialized yet
|
|
653
|
+
* @throws {ConduitError} If the client has not been initialized yet.
|
|
616
654
|
* @returns {MessengerBot} The active Messenger bot instance.
|
|
617
655
|
*/
|
|
618
656
|
private get client();
|
|
@@ -621,8 +659,8 @@ declare class ConduitClient {
|
|
|
621
659
|
* relevant Conduit events based on the incoming `logMessageType`.
|
|
622
660
|
*
|
|
623
661
|
* `thread:update` always fires with the raw payload when its stack is
|
|
624
|
-
* non-empty. Specific sub-events
|
|
625
|
-
*
|
|
662
|
+
* non-empty. Specific sub-events fire only when their stack is also
|
|
663
|
+
* non-empty and a matching `logMessageType` exists.
|
|
626
664
|
*
|
|
627
665
|
* Calling this method more than once is a no-op.
|
|
628
666
|
*/
|
|
@@ -675,4 +713,4 @@ declare class ConduitError extends Error {
|
|
|
675
713
|
static uninitializedClient(): ConduitError;
|
|
676
714
|
}
|
|
677
715
|
|
|
678
|
-
export { type AddedParticipant, ConduitAccountAPI, ConduitClient, type ConduitClientConfig, type ConduitCredentials, ConduitError, type ConduitEvents, type ConduitMessageBody, ConduitMessagesAPI, ConduitThreadsAPI, ConduitUsersAPI, type Loose, type Message, type MessageCreatePayload, type MessageReactPayload, type MessageReadPayload, type MessageRemovePayload, type MessageRespondPayload, type MessageWritingPayload, type Middleware, type Replyable, type Sendable, type ThreadAdminChangedPayload, type ThreadEventBase, type ThreadNicknameChangedPayload, type ThreadPhotoReplacedPayload, type ThreadThemeChangedPayload, type ThreadTitleChangePayload, type ThreadUpdatePayload, type UserCreatePayload, type UserRemovePayload, toFcaEvent };
|
|
716
|
+
export { type AddedParticipant, ConduitAccountAPI, ConduitClient, type ConduitClientConfig, type ConduitCredentials, ConduitError, type ConduitEvents, type ConduitMessageBody, ConduitMessagesAPI, type ConduitQueueConfig, type ConduitQueueJob, ConduitThreadsAPI, ConduitUsersAPI, type Loose, type Message, type MessageCreatePayload, type MessageReactPayload, type MessageReadPayload, type MessageRemovePayload, type MessageRespondPayload, type MessageWritingPayload, type Middleware, type Replyable, type Sendable, type ThreadAdminChangedPayload, type ThreadEventBase, type ThreadNicknameChangedPayload, type ThreadPhotoReplacedPayload, type ThreadThemeChangedPayload, type ThreadTitleChangePayload, type ThreadUpdatePayload, type UserCreatePayload, type UserRemovePayload, toFcaEvent };
|
package/dist/index.mjs
CHANGED
|
@@ -46,36 +46,42 @@ var ConduitError = class _ConduitError extends Error {
|
|
|
46
46
|
}
|
|
47
47
|
};
|
|
48
48
|
|
|
49
|
+
// src/utils/sleep.ts
|
|
50
|
+
function sleep(min, max) {
|
|
51
|
+
const delay = Math.floor(Math.random() * (max - min + 1)) + min;
|
|
52
|
+
return new Promise((resolve) => {
|
|
53
|
+
setTimeout(resolve, delay);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
49
57
|
// src/api/ConduitMessagesAPI.ts
|
|
50
58
|
var ConduitMessagesAPI = class {
|
|
51
|
-
constructor(bot) {
|
|
59
|
+
constructor(bot, queue) {
|
|
52
60
|
this.bot = bot;
|
|
61
|
+
this.queue = queue;
|
|
53
62
|
}
|
|
54
63
|
bot;
|
|
64
|
+
queue;
|
|
55
65
|
/**
|
|
56
66
|
* Sends a message to a thread.
|
|
57
67
|
* @param body - The message text.
|
|
58
68
|
* @param threadID - The target thread ID.
|
|
59
69
|
*/
|
|
60
70
|
send(body, threadID) {
|
|
61
|
-
|
|
62
|
-
|
|
71
|
+
const fn = () => new Promise(async (resolve, reject) => {
|
|
72
|
+
await this.bot.ctx.api.sendTypingIndicator(threadID);
|
|
73
|
+
setTimeout(() => {
|
|
63
74
|
this.bot.ctx.api.sendMessage(
|
|
64
|
-
{ body },
|
|
75
|
+
typeof body === "string" ? { body } : body,
|
|
65
76
|
threadID,
|
|
66
77
|
(err, data) => {
|
|
67
78
|
if (err) reject(err);
|
|
68
79
|
else resolve(data);
|
|
69
80
|
}
|
|
70
81
|
);
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
return new Promise((resolve, reject) => {
|
|
74
|
-
this.bot.ctx.api.sendMessage(body, threadID, (err, data) => {
|
|
75
|
-
if (err) reject(err);
|
|
76
|
-
else resolve(data);
|
|
77
|
-
});
|
|
82
|
+
}, 700);
|
|
78
83
|
});
|
|
84
|
+
return this.queue ? this.queue.enqueue(threadID, fn) : fn();
|
|
79
85
|
}
|
|
80
86
|
/**
|
|
81
87
|
* Sends a quoted reply to a specific message.
|
|
@@ -84,10 +90,11 @@ var ConduitMessagesAPI = class {
|
|
|
84
90
|
* @param messageID - The message ID to reply to.
|
|
85
91
|
*/
|
|
86
92
|
reply(body, threadID, messageID) {
|
|
87
|
-
|
|
88
|
-
|
|
93
|
+
const fn = () => new Promise(async (resolve, reject) => {
|
|
94
|
+
await this.bot.ctx.api.sendTypingIndicator(threadID);
|
|
95
|
+
setTimeout(() => {
|
|
89
96
|
this.bot.ctx.api.sendMessage(
|
|
90
|
-
{ body },
|
|
97
|
+
typeof body === "string" ? { body } : body,
|
|
91
98
|
threadID,
|
|
92
99
|
(err, data) => {
|
|
93
100
|
if (err) reject(err);
|
|
@@ -95,19 +102,9 @@ var ConduitMessagesAPI = class {
|
|
|
95
102
|
},
|
|
96
103
|
messageID
|
|
97
104
|
);
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
return new Promise((resolve, reject) => {
|
|
101
|
-
this.bot.ctx.api.sendMessage(
|
|
102
|
-
body,
|
|
103
|
-
threadID,
|
|
104
|
-
(err, data) => {
|
|
105
|
-
if (err) reject(err);
|
|
106
|
-
else resolve(data);
|
|
107
|
-
},
|
|
108
|
-
messageID
|
|
109
|
-
);
|
|
105
|
+
}, 700);
|
|
110
106
|
});
|
|
107
|
+
return this.queue ? this.queue.enqueue(threadID, fn) : fn();
|
|
111
108
|
}
|
|
112
109
|
/**
|
|
113
110
|
* Edits an existing message.
|
|
@@ -153,15 +150,16 @@ var ConduitMessagesAPI = class {
|
|
|
153
150
|
* @param threadID - The thread the message belongs to.
|
|
154
151
|
*/
|
|
155
152
|
react(emoji, messageID, threadID) {
|
|
156
|
-
return new Promise((resolve, reject) => {
|
|
153
|
+
return new Promise(async (resolve, reject) => {
|
|
154
|
+
await sleep(500, 700);
|
|
157
155
|
this.bot.ctx.api.setMessageReaction(
|
|
158
156
|
emoji,
|
|
159
157
|
messageID,
|
|
158
|
+
threadID,
|
|
160
159
|
(err) => {
|
|
161
160
|
if (err) reject(err);
|
|
162
161
|
else resolve(null);
|
|
163
|
-
}
|
|
164
|
-
threadID
|
|
162
|
+
}
|
|
165
163
|
);
|
|
166
164
|
});
|
|
167
165
|
}
|
|
@@ -171,7 +169,7 @@ var ConduitMessagesAPI = class {
|
|
|
171
169
|
*/
|
|
172
170
|
sendTypingIndicator(threadID) {
|
|
173
171
|
return new Promise((resolve, reject) => {
|
|
174
|
-
this.bot.ctx.api.sendTypingIndicator(threadID, (err) => {
|
|
172
|
+
this.bot.ctx.api.sendTypingIndicator(threadID, async (err) => {
|
|
175
173
|
if (err) reject(err);
|
|
176
174
|
else resolve(null);
|
|
177
175
|
});
|
|
@@ -280,10 +278,12 @@ var ConduitMessagesAPI = class {
|
|
|
280
278
|
|
|
281
279
|
// src/api/ConduitThreadsAPI.ts
|
|
282
280
|
var ConduitThreadsAPI = class {
|
|
283
|
-
constructor(bot) {
|
|
281
|
+
constructor(bot, queue) {
|
|
284
282
|
this.bot = bot;
|
|
283
|
+
this.queue = queue;
|
|
285
284
|
}
|
|
286
285
|
bot;
|
|
286
|
+
queue;
|
|
287
287
|
/**
|
|
288
288
|
* Fetches detailed info about a thread.
|
|
289
289
|
* @param threadID - The thread ID to query.
|
|
@@ -423,7 +423,7 @@ var ConduitThreadsAPI = class {
|
|
|
423
423
|
* @param userID - The target user ID.
|
|
424
424
|
*/
|
|
425
425
|
changeNickname(nickname, threadID, userID) {
|
|
426
|
-
|
|
426
|
+
const fn = () => new Promise((resolve, reject) => {
|
|
427
427
|
this.bot.ctx.api.changeNickname(
|
|
428
428
|
nickname,
|
|
429
429
|
threadID,
|
|
@@ -434,6 +434,7 @@ var ConduitThreadsAPI = class {
|
|
|
434
434
|
}
|
|
435
435
|
);
|
|
436
436
|
});
|
|
437
|
+
return this.queue ? this.queue.enqueue(threadID, fn) : fn();
|
|
437
438
|
}
|
|
438
439
|
/**
|
|
439
440
|
* Changes the title of a group thread.
|
|
@@ -441,12 +442,13 @@ var ConduitThreadsAPI = class {
|
|
|
441
442
|
* @param threadID - The target group thread ID.
|
|
442
443
|
*/
|
|
443
444
|
setTitle(title, threadID) {
|
|
444
|
-
|
|
445
|
+
const fn = () => new Promise((resolve, reject) => {
|
|
445
446
|
this.bot.ctx.api.setTitle(title, threadID, (err) => {
|
|
446
447
|
if (err) reject(err);
|
|
447
448
|
else resolve(null);
|
|
448
449
|
});
|
|
449
450
|
});
|
|
451
|
+
return this.queue ? this.queue.enqueue(threadID, fn) : fn();
|
|
450
452
|
}
|
|
451
453
|
/**
|
|
452
454
|
* Creates a poll in a thread.
|
|
@@ -455,7 +457,7 @@ var ConduitThreadsAPI = class {
|
|
|
455
457
|
* @param options - Array of answer options.
|
|
456
458
|
*/
|
|
457
459
|
createPoll(title, threadID, options) {
|
|
458
|
-
|
|
460
|
+
const fn = () => new Promise((resolve, reject) => {
|
|
459
461
|
this.bot.ctx.api.createPoll(
|
|
460
462
|
title,
|
|
461
463
|
threadID,
|
|
@@ -466,6 +468,7 @@ var ConduitThreadsAPI = class {
|
|
|
466
468
|
}
|
|
467
469
|
);
|
|
468
470
|
});
|
|
471
|
+
return this.queue ? this.queue.enqueue(threadID, fn) : fn();
|
|
469
472
|
}
|
|
470
473
|
/**
|
|
471
474
|
* Deletes a thread.
|
|
@@ -616,6 +619,55 @@ var ConduitAccountAPI = class {
|
|
|
616
619
|
}
|
|
617
620
|
};
|
|
618
621
|
|
|
622
|
+
// src/utils/ConduitQueue.ts
|
|
623
|
+
var ConduitQueue = class {
|
|
624
|
+
minDelayMs;
|
|
625
|
+
maxDelayMs;
|
|
626
|
+
switchDelayMinMs;
|
|
627
|
+
switchDelayMaxMs;
|
|
628
|
+
queues;
|
|
629
|
+
running;
|
|
630
|
+
lastThreadID = null;
|
|
631
|
+
constructor(options) {
|
|
632
|
+
this.minDelayMs = options.minDelayMs;
|
|
633
|
+
this.maxDelayMs = options.maxDelayMs;
|
|
634
|
+
this.switchDelayMinMs = options.switchDelayMinMs ?? 500;
|
|
635
|
+
this.switchDelayMaxMs = options.switchDelayMaxMs ?? 700;
|
|
636
|
+
this.queues = /* @__PURE__ */ new Map();
|
|
637
|
+
this.running = /* @__PURE__ */ new Map();
|
|
638
|
+
}
|
|
639
|
+
enqueue(threadID, job) {
|
|
640
|
+
return new Promise((resolve, reject) => {
|
|
641
|
+
if (!this.queues.has(threadID)) this.queues.set(threadID, []);
|
|
642
|
+
this.queues.get(threadID).push(async () => {
|
|
643
|
+
try {
|
|
644
|
+
resolve(await job());
|
|
645
|
+
} catch (e) {
|
|
646
|
+
reject(e);
|
|
647
|
+
}
|
|
648
|
+
});
|
|
649
|
+
if (!this.running.get(threadID)) this._run(threadID);
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
async _run(threadID) {
|
|
653
|
+
this.running.set(threadID, true);
|
|
654
|
+
const queue = this.queues.get(threadID);
|
|
655
|
+
while (queue.length > 0) {
|
|
656
|
+
if (this.lastThreadID !== null && this.lastThreadID !== threadID) {
|
|
657
|
+
await sleep(this.switchDelayMinMs, this.switchDelayMaxMs);
|
|
658
|
+
}
|
|
659
|
+
this.lastThreadID = threadID;
|
|
660
|
+
const job = queue.shift();
|
|
661
|
+
await job().catch(console.error);
|
|
662
|
+
if (queue.length > 0) {
|
|
663
|
+
await sleep(this.minDelayMs, this.maxDelayMs);
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
this.queues.delete(threadID);
|
|
667
|
+
this.running.delete(threadID);
|
|
668
|
+
}
|
|
669
|
+
};
|
|
670
|
+
|
|
619
671
|
// src/client/ConduitClient.ts
|
|
620
672
|
var FANOUT_EVENTS = /* @__PURE__ */ new Set([
|
|
621
673
|
"user:create",
|
|
@@ -627,6 +679,10 @@ var FANOUT_EVENTS = /* @__PURE__ */ new Set([
|
|
|
627
679
|
"thread:nickname_changed",
|
|
628
680
|
"thread:admin_changed"
|
|
629
681
|
]);
|
|
682
|
+
var MESSAGE_REPLYABLE = /* @__PURE__ */ new Set([
|
|
683
|
+
"message:create",
|
|
684
|
+
"message:respond"
|
|
685
|
+
]);
|
|
630
686
|
var LOG_MESSAGE_TYPE_MAP = {
|
|
631
687
|
"log:subscribe": "user:create",
|
|
632
688
|
"log:unsubscribe": "user:remove",
|
|
@@ -647,6 +703,10 @@ var ConduitClient = class {
|
|
|
647
703
|
_users = null;
|
|
648
704
|
/** Lazily-initialized account API wrapper. */
|
|
649
705
|
_account = null;
|
|
706
|
+
/** Lazily-initialized queue for message operations. */
|
|
707
|
+
_messageQueue = null;
|
|
708
|
+
/** Lazily-initialized queue for thread operations. */
|
|
709
|
+
_threadQueue = null;
|
|
650
710
|
/** Configuration forwarded to the FCA layer on login. */
|
|
651
711
|
config;
|
|
652
712
|
/**
|
|
@@ -679,7 +739,10 @@ var ConduitClient = class {
|
|
|
679
739
|
* and other message-level interactions.
|
|
680
740
|
*/
|
|
681
741
|
get messages() {
|
|
682
|
-
return this._messages ??= new ConduitMessagesAPI(
|
|
742
|
+
return this._messages ??= new ConduitMessagesAPI(
|
|
743
|
+
this.client,
|
|
744
|
+
this.config.queue?.messageQueue ? this.messageQueue : void 0
|
|
745
|
+
);
|
|
683
746
|
}
|
|
684
747
|
/**
|
|
685
748
|
* API for thread management operations.
|
|
@@ -689,7 +752,10 @@ var ConduitClient = class {
|
|
|
689
752
|
* and handling thread-level features like polls.
|
|
690
753
|
*/
|
|
691
754
|
get threads() {
|
|
692
|
-
return this._threads ??= new ConduitThreadsAPI(
|
|
755
|
+
return this._threads ??= new ConduitThreadsAPI(
|
|
756
|
+
this.client,
|
|
757
|
+
this.config.queue?.threadQueue ? this.threadQueue : void 0
|
|
758
|
+
);
|
|
693
759
|
}
|
|
694
760
|
/**
|
|
695
761
|
* API for user-related operations.
|
|
@@ -710,6 +776,24 @@ var ConduitClient = class {
|
|
|
710
776
|
get account() {
|
|
711
777
|
return this._account ??= new ConduitAccountAPI(this.client);
|
|
712
778
|
}
|
|
779
|
+
/**
|
|
780
|
+
* Lazily-initialized queue for message operations.
|
|
781
|
+
* Only created when `config.queue.messageQueue` is defined.
|
|
782
|
+
*/
|
|
783
|
+
get messageQueue() {
|
|
784
|
+
return this._messageQueue ??= new ConduitQueue(
|
|
785
|
+
this.config.queue?.messageQueue ?? {}
|
|
786
|
+
);
|
|
787
|
+
}
|
|
788
|
+
/**
|
|
789
|
+
* Lazily-initialized queue for thread operations.
|
|
790
|
+
* Only created when `config.queue.threadQueue` is defined.
|
|
791
|
+
*/
|
|
792
|
+
get threadQueue() {
|
|
793
|
+
return this._threadQueue ??= new ConduitQueue(
|
|
794
|
+
this.config.queue?.threadQueue ?? {}
|
|
795
|
+
);
|
|
796
|
+
}
|
|
713
797
|
/**
|
|
714
798
|
* Authenticates with Messenger and initialises the underlying FCA bot.
|
|
715
799
|
* Must be called before any events can be received.
|
|
@@ -771,12 +855,10 @@ var ConduitClient = class {
|
|
|
771
855
|
return this;
|
|
772
856
|
}
|
|
773
857
|
/**
|
|
774
|
-
*
|
|
775
|
-
* have any type safety and auto-completion features. Use
|
|
776
|
-
* at your own risk.
|
|
858
|
+
* Direct access to the raw FCA API. No type safety — use as a last resort.
|
|
777
859
|
*
|
|
778
|
-
* @returns The raw
|
|
779
|
-
|
|
860
|
+
* @returns The raw FCA API context.
|
|
861
|
+
*/
|
|
780
862
|
get api() {
|
|
781
863
|
return this.client.ctx.api;
|
|
782
864
|
}
|
|
@@ -799,7 +881,7 @@ var ConduitClient = class {
|
|
|
799
881
|
* This getter enforces the client lifecycle by ensuring that the underlying
|
|
800
882
|
* FCA bot has been created via {@link login} before any access is allowed.
|
|
801
883
|
*
|
|
802
|
-
* @throws {ConduitError} If the client has not been initialized yet
|
|
884
|
+
* @throws {ConduitError} If the client has not been initialized yet.
|
|
803
885
|
* @returns {MessengerBot} The active Messenger bot instance.
|
|
804
886
|
*/
|
|
805
887
|
get client() {
|
|
@@ -811,8 +893,8 @@ var ConduitClient = class {
|
|
|
811
893
|
* relevant Conduit events based on the incoming `logMessageType`.
|
|
812
894
|
*
|
|
813
895
|
* `thread:update` always fires with the raw payload when its stack is
|
|
814
|
-
* non-empty. Specific sub-events
|
|
815
|
-
*
|
|
896
|
+
* non-empty. Specific sub-events fire only when their stack is also
|
|
897
|
+
* non-empty and a matching `logMessageType` exists.
|
|
816
898
|
*
|
|
817
899
|
* Calling this method more than once is a no-op.
|
|
818
900
|
*/
|
|
@@ -850,7 +932,7 @@ var ConduitClient = class {
|
|
|
850
932
|
const sendable = {
|
|
851
933
|
send: (body) => this.messages.send(body, threadID)
|
|
852
934
|
};
|
|
853
|
-
if (
|
|
935
|
+
if (MESSAGE_REPLYABLE.has(event)) {
|
|
854
936
|
return {
|
|
855
937
|
...raw,
|
|
856
938
|
...sendable,
|
|
@@ -858,16 +940,12 @@ var ConduitClient = class {
|
|
|
858
940
|
react: (emoji) => this.messages.react(emoji, messageID, threadID)
|
|
859
941
|
};
|
|
860
942
|
}
|
|
861
|
-
if (event.startsWith("thread:")) {
|
|
943
|
+
if (event.startsWith("thread:") || event === "user:create" || event === "user:remove") {
|
|
862
944
|
return {
|
|
863
945
|
...raw,
|
|
864
946
|
...sendable,
|
|
865
|
-
changeNickname: (nickname,
|
|
866
|
-
|
|
867
|
-
},
|
|
868
|
-
changeAdminStatus: (userID, isAdmin) => {
|
|
869
|
-
this.threads.changeAdminStatus(userID, threadID, isAdmin);
|
|
870
|
-
}
|
|
947
|
+
changeNickname: (nickname, userID) => this.threads.changeNickname(nickname, threadID, userID),
|
|
948
|
+
changeAdminStatus: (userID, isAdmin) => this.threads.changeAdminStatus(userID, threadID, isAdmin)
|
|
871
949
|
};
|
|
872
950
|
}
|
|
873
951
|
return { ...raw, ...sendable };
|