fix: 修复Bilibili卡片绑定缓存
This commit is contained in:
parent
9fc11975c1
commit
832eeeeec7
@ -11,10 +11,6 @@ import { parseBilibiliVideoReference } from '../domain/bilibili-url-parser';
|
|||||||
import { BilibiliVideoClient } from '../infrastructure/integration/bilibili-video-client';
|
import { BilibiliVideoClient } from '../infrastructure/integration/bilibili-video-client';
|
||||||
|
|
||||||
export class BilibiliCardApplication {
|
export class BilibiliCardApplication {
|
||||||
private readonly boundCache = new Map<
|
|
||||||
string,
|
|
||||||
{ expiresAt: number; value: boolean }
|
|
||||||
>();
|
|
||||||
private readonly dedupe = new Map<string, { expiresAt: number }>();
|
private readonly dedupe = new Map<string, { expiresAt: number }>();
|
||||||
private readonly videoClient: BilibiliVideoClient;
|
private readonly videoClient: BilibiliVideoClient;
|
||||||
|
|
||||||
@ -117,20 +113,10 @@ export class BilibiliCardApplication {
|
|||||||
const normalizedSelfId = `${selfId || ''}`.trim();
|
const normalizedSelfId = `${selfId || ''}`.trim();
|
||||||
if (!normalizedSelfId) return false;
|
if (!normalizedSelfId) return false;
|
||||||
|
|
||||||
const current = this.now();
|
|
||||||
const cached = this.boundCache.get(normalizedSelfId);
|
|
||||||
if (cached && cached.expiresAt > current) return cached.value;
|
|
||||||
|
|
||||||
const config = readBilibiliCardRuntimeConfig(this.host);
|
|
||||||
try {
|
try {
|
||||||
const value = (
|
return (
|
||||||
await this.host.getBoundEventPluginKeys(normalizedSelfId)
|
await this.host.getBoundEventPluginKeys(normalizedSelfId)
|
||||||
).includes(this.manifest.pluginKey);
|
).includes(this.manifest.pluginKey);
|
||||||
this.boundCache.set(normalizedSelfId, {
|
|
||||||
expiresAt: current + Math.min(config.dedupeTtlMs, 60000),
|
|
||||||
value,
|
|
||||||
});
|
|
||||||
return value;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.warn(`Bilibili 事件绑定查询失败: ${normalizeError(error)}`);
|
this.warn(`Bilibili 事件绑定查询失败: ${normalizeError(error)}`);
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@ -150,6 +150,33 @@ describe('Bilibili card application', () => {
|
|||||||
expect(host.sendText).toHaveBeenCalledTimes(1);
|
expect(host.sendText).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('rechecks binding after an unbound message so newly bound accounts work immediately', async () => {
|
||||||
|
let current = 1000;
|
||||||
|
const host = createHost({
|
||||||
|
getBoundEventPluginKeys: jest
|
||||||
|
.fn()
|
||||||
|
.mockResolvedValueOnce([])
|
||||||
|
.mockResolvedValueOnce(['bilibili-card']),
|
||||||
|
requestJson: jest.fn().mockResolvedValue(createBilibiliViewResponse()),
|
||||||
|
});
|
||||||
|
const application = new BilibiliCardApplication(
|
||||||
|
host,
|
||||||
|
createManifest(),
|
||||||
|
() => current,
|
||||||
|
);
|
||||||
|
const message = createMessage({
|
||||||
|
messageText: 'https://www.bilibili.com/video/BV1xx411c7mD',
|
||||||
|
});
|
||||||
|
|
||||||
|
await expect(application.handleMessage(message)).resolves.toBe(false);
|
||||||
|
current += 1000;
|
||||||
|
await expect(application.handleMessage(message)).resolves.toBe(true);
|
||||||
|
|
||||||
|
expect(host.getBoundEventPluginKeys).toHaveBeenCalledTimes(2);
|
||||||
|
expect(host.requestJson).toHaveBeenCalledTimes(1);
|
||||||
|
expect(host.sendText).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
it('routes generic worker message events to package handler', async () => {
|
it('routes generic worker message events to package handler', async () => {
|
||||||
const host = createHost({
|
const host = createHost({
|
||||||
getBoundEventPluginKeys: jest.fn().mockResolvedValue(['bilibili-card']),
|
getBoundEventPluginKeys: jest.fn().mockResolvedValue(['bilibili-card']),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user