feat(onebot): implement forward_{group,friend}_single_msg via md5 fast-upload

Captures protocol-level media fingerprints (md5/sha1/dims/format) on the
receive side and persists them alongside existing MediaStore entries, so a
forward can re-send the original message via QQ's md5 fast-upload path
without re-downloading the source CDN bytes. Avoids the stale-rkey trap
where the cached imageUrl 404s a few hours after receipt.

- MessageElement: add md5Hex / sha1Hex / pic|video|voiceFormat /
  noByteFallback optional fields.
- MediaStore: extend CachedImage / CachedRecord with fingerprints, add
  CachedVideo + rememberVideo / findVideo / updateVideoUrl. Per-row growth
  ~80-90 bytes; LRU cap stays at 4096 per type.
- msg-push-handler: extract fingerprints from NTV2 image/record/video and
  legacy NotOnlineImage/CustomFace/Ptt paths.
- event-converter: invoke mediaSegmentSink for video too.
- highway image/ptt/video upload: add fast-only branch that skips
  loadBinarySource/encodeSilk/stageVideoSource when noByteFallback is set,
  and fails fast if the server still returns uKey. Video forward uses the
  100-byte FALLBACK_THUMB as its subFile (original thumb not preserved in
  onebot semantics).
- message-actions: add forwardSingleMessage which looks up the cached
  event, enriches each media element with cached fingerprints, and
  dispatches via the existing sendGroup/sendPrivateMessage path. Files
  reject explicitly (separate upload protocol, out of scope).
- extended actions: wire forward_friend_single_msg / forward_group_single_msg
  to the new context method, replacing the not-implemented stubs.
This commit is contained in:
DrSmoothl committed 2026-05-12 16:22:16 +08:00
1 parent ef4a321175
commit e91a506bf5
12 files changed
+447 -25

No files matched your search

+51 -2
View File
@@ -38,6 +38,13 @@ export interface CachedImage {
sessionId: number;
/** Original imageUrl from the MessageElement, used for RKey re-appending. */
imageUrl: string;
/** Fingerprints used for md5/sha1 fast-upload on forward. Optional because
* legacy CustomFace / NotOnlineImage only carry md5. */
md5Hex?: string;
sha1Hex?: string;
width?: number;
height?: number;
picFormat?: number;
}
export interface CachedRecord {
@@ -56,10 +63,34 @@ export interface CachedRecord {
/** Conversation the media originated from (group vs private path). */
isGroup: boolean;
sessionId: number;
/** Fingerprints used for md5/sha1 fast-upload on forward. */
md5Hex?: string;
sha1Hex?: string;
voiceFormat?: number;
}
export interface CachedVideo {
file: string;
fileId: string;
url: string;
fileSize: number;
fileName: string;
duration: number;
fileHash: string;
mediaNode?: MessageElement['mediaNode'];
isGroup: boolean;
sessionId: number;
/** Fingerprints used for md5/sha1 fast-upload on forward. */
md5Hex?: string;
sha1Hex?: string;
width?: number;
height?: number;
videoFormat?: number;
}
const TYPE_IMAGE = 'image';
const TYPE_RECORD = 'record';
const TYPE_VIDEO = 'video';
const DEFAULT_KEEP_ENTRIES = 4096;
export class MediaStore {
@@ -145,6 +176,12 @@ export class MediaStore {
this.upsertWithAliases(TYPE_RECORD, primaryKey, info, [info.file, info.fileName, info.fileId, info.url]);
}
rememberVideo(info: CachedVideo): void {
const primaryKey = pickPrimary([info.file, info.fileName, info.fileId]);
if (!primaryKey) return;
this.upsertWithAliases(TYPE_VIDEO, primaryKey, info, [info.file, info.fileName, info.fileId, info.url]);
}
findImage(key: string): CachedImage | null {
return this.findByAnyKey<CachedImage>(TYPE_IMAGE, key);
}
@@ -153,6 +190,10 @@ export class MediaStore {
return this.findByAnyKey<CachedRecord>(TYPE_RECORD, key);
}
findVideo(key: string): CachedVideo | null {
return this.findByAnyKey<CachedVideo>(TYPE_VIDEO, key);
}
updateImageUrl(key: string, url: string): void {
if (!url) return;
const cached = this.findImage(key);
@@ -167,11 +208,19 @@ export class MediaStore {
this.rememberRecord({ ...cached, url });
}
updateVideoUrl(key: string, url: string): void {
if (!url) return;
const cached = this.findVideo(key);
if (!cached || cached.url === url) return;
this.rememberVideo({ ...cached, url });
}
/** Snapshot count of distinct entries per type; mostly used by tests. */
size(): { images: number; records: number } {
size(): { images: number; records: number; videos: number } {
const img = this.countByType.get(TYPE_IMAGE) as { n: number } | undefined;
const rec = this.countByType.get(TYPE_RECORD) as { n: number } | undefined;
return { images: img?.n ?? 0, records: rec?.n ?? 0 };
const vid = this.countByType.get(TYPE_VIDEO) as { n: number } | undefined;
return { images: img?.n ?? 0, records: rec?.n ?? 0, videos: vid?.n ?? 0 };
}
// --- internals ---