Files
SnowLuma/packages/onebot/src/media-url-resolver.ts
T
手瓜一十雪 a52a9724cb onebot: clean up comments, imports and whitespace
Remove extraneous comments, redundant explanatory notes and extra blank lines across onebot packages; reorder and normalize import lists in group-admin and group-album; remove unused comment blocks in event-converter. These are non-functional cleanup changes in packages/onebot/src/actions/extended.ts, packages/onebot/src/actions/group-admin.ts, packages/onebot/src/actions/group-album.ts and packages/onebot/src/event-converter/index.ts to improve readability and maintain consistent style.
2026-05-24 13:08:24 +08:00

47 lines
1.8 KiB
TypeScript

import type { BridgeInterface } from '@snowluma/core/bridge-interface';
import type { MessageElement } from '@snowluma/protocol/events';
import type { RKeyCache } from './instance-rkey';
export class MediaUrlResolver {
constructor(
private readonly bridge: BridgeInterface,
private readonly rkeyCache: RKeyCache,
) { }
async resolve(element: MessageElement, isGroup: boolean, sessionId: number): Promise<string> {
if (!element.url) {
try {
await this.populateUrl(element, isGroup, sessionId);
} catch {
// Best-effort: a URL fetch hiccup should never block the
// outgoing segment. The RKey pass below still runs.
}
}
return this.rkeyCache.resolveMediaUrl(this.bridge, element, isGroup);
}
private async populateUrl(element: MessageElement, isGroup: boolean, sessionId: number): Promise<void> {
if (element.type === 'file' && element.fileId) {
if (isGroup) {
element.url = await this.bridge.apis.groupFile.getUrl(sessionId, element.fileId);
} else if (element.fileHash) {
element.url = await this.bridge.apis.groupFile.getPrivateUrl(sessionId, element.fileId, element.fileHash);
} else {
element.url = '';
}
return;
}
if ((element.type === 'record' || element.type === 'video') && element.mediaNode) {
if (isGroup) {
element.url = element.type === 'record'
? await this.bridge.apis.groupFile.getPttUrl(sessionId, element.mediaNode)
: await this.bridge.apis.groupFile.getVideoUrl(sessionId, element.mediaNode);
} else {
element.url = element.type === 'record'
? await this.bridge.apis.groupFile.getPrivatePttUrl(element.mediaNode)
: await this.bridge.apis.groupFile.getPrivateVideoUrl(element.mediaNode);
}
}
}
}