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:
12 files changed
+447
-25
No files matched your search
@@ -27,6 +27,16 @@ export interface MessageElement {
|
|||||||
flash?: boolean;
|
flash?: boolean;
|
||||||
resId?: string;
|
resId?: string;
|
||||||
fileHash?: string;
|
fileHash?: string;
|
||||||
|
// Server-side fingerprints carried from receive side so a forward can do a
|
||||||
|
// pure md5/sha1 fast-upload without re-downloading the original bytes.
|
||||||
|
// Set together with `noByteFallback: true` to make the upload modules throw
|
||||||
|
// instead of falling back to fetch(url).
|
||||||
|
md5Hex?: string;
|
||||||
|
sha1Hex?: string;
|
||||||
|
picFormat?: number;
|
||||||
|
videoFormat?: number;
|
||||||
|
voiceFormat?: number;
|
||||||
|
noByteFallback?: boolean;
|
||||||
mediaNode?: {
|
mediaNode?: {
|
||||||
fileUuid?: string;
|
fileUuid?: string;
|
||||||
storeId?: number;
|
storeId?: number;
|
||||||
|
|||||||
@@ -217,6 +217,7 @@ function convertElements(elems: ElemDecoded[]): MessageElement[] {
|
|||||||
height: img.picHeight ?? 0,
|
height: img.picHeight ?? 0,
|
||||||
subType: img.pbRes?.subType ?? 0,
|
subType: img.pbRes?.subType ?? 0,
|
||||||
summary: img.pbRes?.summary ?? '[image]',
|
summary: img.pbRes?.summary ?? '[image]',
|
||||||
|
md5Hex: bytesToHex(img.picMd5),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -234,6 +235,7 @@ function convertElements(elems: ElemDecoded[]): MessageElement[] {
|
|||||||
height: img.height ?? 0,
|
height: img.height ?? 0,
|
||||||
subType: img.pbRes?.subType ?? 0,
|
subType: img.pbRes?.subType ?? 0,
|
||||||
summary: img.pbRes?.summary ?? '[image]',
|
summary: img.pbRes?.summary ?? '[image]',
|
||||||
|
md5Hex: bytesToHex(img.md5),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -397,6 +399,9 @@ function convertElements(elems: ElemDecoded[]): MessageElement[] {
|
|||||||
fileSize: fi.fileSize ?? 0, width: fi.width ?? 0,
|
fileSize: fi.fileSize ?? 0, width: fi.width ?? 0,
|
||||||
height: fi.height ?? 0, imageUrl: url,
|
height: fi.height ?? 0, imageUrl: url,
|
||||||
};
|
};
|
||||||
|
if (fi.fileHash) me.md5Hex = fi.fileHash;
|
||||||
|
if (fi.fileSha1) me.sha1Hex = fi.fileSha1;
|
||||||
|
if (fi.type?.picFormat) me.picFormat = fi.type.picFormat;
|
||||||
if (info.extBizInfo?.pic) {
|
if (info.extBizInfo?.pic) {
|
||||||
me.subType = info.extBizInfo.pic.bizType ?? 0;
|
me.subType = info.extBizInfo.pic.bizType ?? 0;
|
||||||
me.summary = info.extBizInfo.pic.textSummary || '[image]';
|
me.summary = info.extBizInfo.pic.textSummary || '[image]';
|
||||||
@@ -408,6 +413,10 @@ function convertElements(elems: ElemDecoded[]): MessageElement[] {
|
|||||||
type: 'record', fileName: fi.fileName ?? '',
|
type: 'record', fileName: fi.fileName ?? '',
|
||||||
fileId: idx.fileUuid ?? '', duration: fi.time ?? 0,
|
fileId: idx.fileUuid ?? '', duration: fi.time ?? 0,
|
||||||
fileHash: fi.fileHash ?? '',
|
fileHash: fi.fileHash ?? '',
|
||||||
|
fileSize: fi.fileSize ?? 0,
|
||||||
|
md5Hex: fi.fileHash ?? '',
|
||||||
|
sha1Hex: fi.fileSha1 ?? '',
|
||||||
|
voiceFormat: fi.type?.voiceFormat ?? 0,
|
||||||
mediaNode: {
|
mediaNode: {
|
||||||
fileUuid: idx.fileUuid,
|
fileUuid: idx.fileUuid,
|
||||||
storeId: idx.storeId,
|
storeId: idx.storeId,
|
||||||
@@ -439,6 +448,11 @@ function convertElements(elems: ElemDecoded[]): MessageElement[] {
|
|||||||
fileId: idx.fileUuid ?? '', fileSize: fi.fileSize ?? 0,
|
fileId: idx.fileUuid ?? '', fileSize: fi.fileSize ?? 0,
|
||||||
duration: fi.time ?? 0,
|
duration: fi.time ?? 0,
|
||||||
fileHash: fi.fileHash ?? '',
|
fileHash: fi.fileHash ?? '',
|
||||||
|
width: fi.width ?? 0,
|
||||||
|
height: fi.height ?? 0,
|
||||||
|
md5Hex: fi.fileHash ?? '',
|
||||||
|
sha1Hex: fi.fileSha1 ?? '',
|
||||||
|
videoFormat: fi.type?.videoFormat ?? 0,
|
||||||
mediaNode: {
|
mediaNode: {
|
||||||
fileUuid: idx.fileUuid,
|
fileUuid: idx.fileUuid,
|
||||||
storeId: idx.storeId,
|
storeId: idx.storeId,
|
||||||
@@ -490,10 +504,13 @@ function extractRichtextExtras(
|
|||||||
// Ptt (voice)
|
// Ptt (voice)
|
||||||
if (rt.ptt) {
|
if (rt.ptt) {
|
||||||
const p = rt.ptt;
|
const p = rt.ptt;
|
||||||
|
const md5Hex = p.fileMd5 && p.fileMd5.length > 0 ? bytesToHex(p.fileMd5) : '';
|
||||||
const me: MessageElement = {
|
const me: MessageElement = {
|
||||||
type: 'record', fileName: p.fileName ?? '',
|
type: 'record', fileName: p.fileName ?? '',
|
||||||
fileSize: p.fileSize ?? 0, duration: p.time ?? 0,
|
fileSize: p.fileSize ?? 0, duration: p.time ?? 0,
|
||||||
fileHash: p.fileMd5 && p.fileMd5.length > 0 ? bytesToHex(p.fileMd5) : '',
|
fileHash: md5Hex,
|
||||||
|
md5Hex,
|
||||||
|
voiceFormat: p.format ?? 0,
|
||||||
};
|
};
|
||||||
if (isGroup && (p.fileId ?? 0n) !== 0n) {
|
if (isGroup && (p.fileId ?? 0n) !== 0n) {
|
||||||
me.fileId = p.groupFileKey ?? '';
|
me.fileId = p.groupFileKey ?? '';
|
||||||
|
|||||||
@@ -19,20 +19,57 @@ import {
|
|||||||
} from './highway-client';
|
} from './highway-client';
|
||||||
|
|
||||||
interface ImageData {
|
interface ImageData {
|
||||||
|
/** Empty when the caller is forwarding from cached fingerprints (no bytes path). */
|
||||||
bytes: Uint8Array;
|
bytes: Uint8Array;
|
||||||
md5: Uint8Array;
|
md5: Uint8Array;
|
||||||
sha1: Uint8Array;
|
sha1: Uint8Array;
|
||||||
md5Hex: string;
|
md5Hex: string;
|
||||||
sha1Hex: string;
|
sha1Hex: string;
|
||||||
fileName: string;
|
fileName: string;
|
||||||
|
fileSize: number;
|
||||||
summary: string;
|
summary: string;
|
||||||
subType: number;
|
subType: number;
|
||||||
width: number;
|
width: number;
|
||||||
height: number;
|
height: number;
|
||||||
picFormat: number;
|
picFormat: number;
|
||||||
|
/** When true, `bytes` is empty and the upload must fast-path or throw. */
|
||||||
|
fastOnly: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
function hexToBytes(hex: string): Uint8Array {
|
||||||
|
const clean = hex.length % 2 === 0 ? hex : '0' + hex;
|
||||||
|
const out = new Uint8Array(clean.length / 2);
|
||||||
|
for (let i = 0; i < out.length; i++) {
|
||||||
|
out[i] = parseInt(clean.substring(i * 2, i * 2 + 2), 16);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
function imageDataFromFingerprint(element: MessageElement): ImageData {
|
||||||
|
return {
|
||||||
|
bytes: new Uint8Array(0),
|
||||||
|
md5: hexToBytes(element.md5Hex ?? ''),
|
||||||
|
sha1: hexToBytes(element.sha1Hex ?? ''),
|
||||||
|
md5Hex: element.md5Hex ?? '',
|
||||||
|
sha1Hex: element.sha1Hex ?? '',
|
||||||
|
fileName: element.fileName || `${element.md5Hex ?? 'image'}.jpg`,
|
||||||
|
fileSize: element.fileSize ?? 0,
|
||||||
|
summary: element.summary || (element.subType === 0 ? '[image]' : '[sticker]'),
|
||||||
|
subType: element.subType ?? 0,
|
||||||
|
width: element.width ?? 0,
|
||||||
|
height: element.height ?? 0,
|
||||||
|
picFormat: element.picFormat ?? 1000,
|
||||||
|
fastOnly: true,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadImage(element: MessageElement): Promise<ImageData> {
|
function loadImage(element: MessageElement): Promise<ImageData> {
|
||||||
|
if (element.noByteFallback) {
|
||||||
|
if (!element.md5Hex || !element.sha1Hex) {
|
||||||
|
return Promise.reject(new Error('image fast-upload requires md5Hex + sha1Hex'));
|
||||||
|
}
|
||||||
|
return Promise.resolve(imageDataFromFingerprint(element));
|
||||||
|
}
|
||||||
return loadImageFromSource(
|
return loadImageFromSource(
|
||||||
element.url || element.fileId || '',
|
element.url || element.fileId || '',
|
||||||
element.fileName ?? '',
|
element.fileName ?? '',
|
||||||
@@ -59,11 +96,13 @@ async function loadImageFromSource(source: string, fileName: string, subType: nu
|
|||||||
md5Hex: hashes.md5Hex,
|
md5Hex: hashes.md5Hex,
|
||||||
sha1Hex: hashes.sha1Hex,
|
sha1Hex: hashes.sha1Hex,
|
||||||
fileName: finalName,
|
fileName: finalName,
|
||||||
|
fileSize: loaded.bytes.length,
|
||||||
summary: summary || (subType === 0 ? '[image]' : '[sticker]'),
|
summary: summary || (subType === 0 ? '[image]' : '[sticker]'),
|
||||||
subType,
|
subType,
|
||||||
width: fmt.width,
|
width: fmt.width,
|
||||||
height: fmt.height,
|
height: fmt.height,
|
||||||
picFormat: fmt.format,
|
picFormat: fmt.format,
|
||||||
|
fastOnly: false,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +123,7 @@ async function startImageUpload(bridge: Bridge, isGroup: boolean, targetIdOrUid:
|
|||||||
upload: {
|
upload: {
|
||||||
uploadInfo: [{
|
uploadInfo: [{
|
||||||
fileInfo: {
|
fileInfo: {
|
||||||
fileSize: image.bytes.length,
|
fileSize: image.fileSize,
|
||||||
fileHash: image.md5Hex,
|
fileHash: image.md5Hex,
|
||||||
fileSha1: image.sha1Hex,
|
fileSha1: image.sha1Hex,
|
||||||
fileName: image.fileName,
|
fileName: image.fileName,
|
||||||
@@ -185,6 +224,9 @@ export async function uploadImageMsgInfo(
|
|||||||
// Upload binary if uKey is present (not fast-uploaded)
|
// Upload binary if uKey is present (not fast-uploaded)
|
||||||
const uKey = upload?.uKey ?? '';
|
const uKey = upload?.uKey ?? '';
|
||||||
if (uKey && upload?.msgInfo) {
|
if (uKey && upload?.msgInfo) {
|
||||||
|
if (image.fastOnly) {
|
||||||
|
throw new Error('image fast-upload not available (server requires bytes)');
|
||||||
|
}
|
||||||
const session = await fetchHighwaySession(bridge);
|
const session = await fetchHighwaySession(bridge);
|
||||||
const extend = buildHighwayExtend(uKey, upload.msgInfo, upload.ipv4s ?? [], image.sha1);
|
const extend = buildHighwayExtend(uKey, upload.msgInfo, upload.ipv4s ?? [], image.sha1);
|
||||||
const commandId = isGroup ? GROUP_IMAGE_CMD_ID : PRIVATE_IMAGE_CMD_ID;
|
const commandId = isGroup ? GROUP_IMAGE_CMD_ID : PRIVATE_IMAGE_CMD_ID;
|
||||||
|
|||||||
@@ -36,20 +36,58 @@ export const PRIVATE_PTT_CMD_ID = 1007;
|
|||||||
export const GROUP_PTT_CMD_ID = 1008;
|
export const GROUP_PTT_CMD_ID = 1008;
|
||||||
|
|
||||||
interface PttPayload {
|
interface PttPayload {
|
||||||
/** Silk bytes that get uploaded to highway. */
|
/** Silk bytes that get uploaded to highway. Empty when fast-only forward. */
|
||||||
bytes: Uint8Array;
|
bytes: Uint8Array;
|
||||||
md5: Uint8Array;
|
md5: Uint8Array;
|
||||||
sha1: Uint8Array;
|
sha1: Uint8Array;
|
||||||
md5Hex: string;
|
md5Hex: string;
|
||||||
sha1Hex: string;
|
sha1Hex: string;
|
||||||
fileName: string;
|
fileName: string;
|
||||||
|
fileSize: number;
|
||||||
/** Whole seconds, >= 1. */
|
/** Whole seconds, >= 1. */
|
||||||
duration: number;
|
duration: number;
|
||||||
|
voiceFormat: number;
|
||||||
|
/** When true, `bytes` is empty and the upload must fast-path or throw. */
|
||||||
|
fastOnly: boolean;
|
||||||
/** Cleanup hooks invoked once the OIDB request + highway upload finish. */
|
/** Cleanup hooks invoked once the OIDB request + highway upload finish. */
|
||||||
cleanups: Array<() => void>;
|
cleanups: Array<() => void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function hexToBytes(hex: string): Uint8Array {
|
||||||
|
const clean = hex.length % 2 === 0 ? hex : '0' + hex;
|
||||||
|
const out = new Uint8Array(clean.length / 2);
|
||||||
|
for (let i = 0; i < out.length; i++) {
|
||||||
|
out[i] = parseInt(clean.substring(i * 2, i * 2 + 2), 16);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
function pttPayloadFromFingerprint(element: MessageElement): PttPayload {
|
||||||
|
return {
|
||||||
|
bytes: new Uint8Array(0),
|
||||||
|
md5: hexToBytes(element.md5Hex ?? ''),
|
||||||
|
sha1: hexToBytes(element.sha1Hex ?? ''),
|
||||||
|
md5Hex: element.md5Hex ?? '',
|
||||||
|
sha1Hex: element.sha1Hex ?? '',
|
||||||
|
fileName: element.fileName || `${element.md5Hex ?? 'record'}.amr`,
|
||||||
|
fileSize: element.fileSize ?? 0,
|
||||||
|
duration: element.duration ?? 1,
|
||||||
|
// Voice format: 1 = silk (NTV2 standard). If the original carried a
|
||||||
|
// different voiceFormat we honour it; the legacy ptt path on NTQQ uses 1.
|
||||||
|
voiceFormat: element.voiceFormat || 1,
|
||||||
|
fastOnly: true,
|
||||||
|
cleanups: [],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
async function loadPtt(element: MessageElement, tempDir: string): Promise<PttPayload> {
|
async function loadPtt(element: MessageElement, tempDir: string): Promise<PttPayload> {
|
||||||
|
if (element.noByteFallback) {
|
||||||
|
if (!element.md5Hex || !element.sha1Hex) {
|
||||||
|
throw new Error('record fast-upload requires md5Hex + sha1Hex');
|
||||||
|
}
|
||||||
|
return pttPayloadFromFingerprint(element);
|
||||||
|
}
|
||||||
|
|
||||||
const source = element.url || element.fileId || '';
|
const source = element.url || element.fileId || '';
|
||||||
if (!source) throw new Error('record source is empty');
|
if (!source) throw new Error('record source is empty');
|
||||||
|
|
||||||
@@ -97,7 +135,10 @@ async function loadPtt(element: MessageElement, tempDir: string): Promise<PttPay
|
|||||||
md5Hex: hashes.md5Hex,
|
md5Hex: hashes.md5Hex,
|
||||||
sha1Hex: hashes.sha1Hex,
|
sha1Hex: hashes.sha1Hex,
|
||||||
fileName: `${hashes.md5Hex}.amr`,
|
fileName: `${hashes.md5Hex}.amr`,
|
||||||
|
fileSize: silkBytes.length,
|
||||||
duration: silk.duration,
|
duration: silk.duration,
|
||||||
|
voiceFormat: 1,
|
||||||
|
fastOnly: false,
|
||||||
cleanups: [...cleanups],
|
cleanups: [...cleanups],
|
||||||
};
|
};
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -136,12 +177,11 @@ async function startPttUpload(
|
|||||||
upload: {
|
upload: {
|
||||||
uploadInfo: [{
|
uploadInfo: [{
|
||||||
fileInfo: {
|
fileInfo: {
|
||||||
fileSize: ptt.bytes.length,
|
fileSize: ptt.fileSize,
|
||||||
fileHash: ptt.md5Hex,
|
fileHash: ptt.md5Hex,
|
||||||
fileSha1: ptt.sha1Hex,
|
fileSha1: ptt.sha1Hex,
|
||||||
fileName: ptt.fileName,
|
fileName: ptt.fileName,
|
||||||
// type=3 voice, voiceFormat=1 (silk).
|
type: { type: 3, picFormat: 0, videoFormat: 0, voiceFormat: ptt.voiceFormat },
|
||||||
type: { type: 3, picFormat: 0, videoFormat: 0, voiceFormat: 1 },
|
|
||||||
width: 0,
|
width: 0,
|
||||||
height: 0,
|
height: 0,
|
||||||
time: ptt.duration,
|
time: ptt.duration,
|
||||||
@@ -239,6 +279,9 @@ export async function uploadPttMsgInfo(
|
|||||||
// returned MsgInfo and trust the dedupe.
|
// returned MsgInfo and trust the dedupe.
|
||||||
const uKey = upload?.uKey ?? '';
|
const uKey = upload?.uKey ?? '';
|
||||||
if (uKey && upload?.msgInfo) {
|
if (uKey && upload?.msgInfo) {
|
||||||
|
if (ptt.fastOnly) {
|
||||||
|
throw new Error('record fast-upload not available (server requires bytes)');
|
||||||
|
}
|
||||||
log.debug('highway upload: bytes=%d md5=%s scene=%s', ptt.bytes.length, ptt.md5Hex, isGroup ? 'group' : 'c2c');
|
log.debug('highway upload: bytes=%d md5=%s scene=%s', ptt.bytes.length, ptt.md5Hex, isGroup ? 'group' : 'c2c');
|
||||||
const session = await fetchHighwaySession(bridge);
|
const session = await fetchHighwaySession(bridge);
|
||||||
const extend = buildHighwayExtend(uKey, upload.msgInfo, upload.ipv4s ?? [], ptt.sha1);
|
const extend = buildHighwayExtend(uKey, upload.msgInfo, upload.ipv4s ?? [], ptt.sha1);
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ const FALLBACK_THUMB = Buffer.from(
|
|||||||
);
|
);
|
||||||
|
|
||||||
interface VideoPayload {
|
interface VideoPayload {
|
||||||
|
/** Video bytes. Empty when fast-only forward. */
|
||||||
bytes: Uint8Array;
|
bytes: Uint8Array;
|
||||||
md5: Uint8Array;
|
md5: Uint8Array;
|
||||||
sha1: Uint8Array;
|
sha1: Uint8Array;
|
||||||
@@ -60,13 +61,63 @@ interface VideoPayload {
|
|||||||
sha1Hex: string;
|
sha1Hex: string;
|
||||||
fileName: string;
|
fileName: string;
|
||||||
filePath: string;
|
filePath: string;
|
||||||
|
fileSize: number;
|
||||||
width: number;
|
width: number;
|
||||||
height: number;
|
height: number;
|
||||||
duration: number;
|
duration: number;
|
||||||
|
videoFormat: number;
|
||||||
thumb: ThumbPayload;
|
thumb: ThumbPayload;
|
||||||
|
/** When true, video bytes are empty; OIDB must fast-upload the video. The
|
||||||
|
* thumb is always present (computed locally from FALLBACK_THUMB) so the
|
||||||
|
* thumb subFile may still upload normally. */
|
||||||
|
fastOnly: boolean;
|
||||||
cleanups: Array<() => void>;
|
cleanups: Array<() => void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function hexToBytes(hex: string): Uint8Array {
|
||||||
|
const clean = hex.length % 2 === 0 ? hex : '0' + hex;
|
||||||
|
const out = new Uint8Array(clean.length / 2);
|
||||||
|
for (let i = 0; i < out.length; i++) {
|
||||||
|
out[i] = parseInt(clean.substring(i * 2, i * 2 + 2), 16);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
function makeFallbackThumb(): ThumbPayload {
|
||||||
|
const bytes = new Uint8Array(FALLBACK_THUMB);
|
||||||
|
const hashes = computeHashes(bytes);
|
||||||
|
return {
|
||||||
|
bytes,
|
||||||
|
md5: hashes.md5,
|
||||||
|
sha1: hashes.sha1,
|
||||||
|
md5Hex: hashes.md5Hex,
|
||||||
|
sha1Hex: hashes.sha1Hex,
|
||||||
|
width: 1,
|
||||||
|
height: 1,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function videoPayloadFromFingerprint(element: MessageElement): VideoPayload {
|
||||||
|
return {
|
||||||
|
bytes: new Uint8Array(0),
|
||||||
|
md5: hexToBytes(element.md5Hex ?? ''),
|
||||||
|
sha1: hexToBytes(element.sha1Hex ?? ''),
|
||||||
|
sha1Blocks: [],
|
||||||
|
md5Hex: element.md5Hex ?? '',
|
||||||
|
sha1Hex: element.sha1Hex ?? '',
|
||||||
|
fileName: element.fileName || `${element.md5Hex ?? 'video'}.mp4`,
|
||||||
|
filePath: '',
|
||||||
|
fileSize: element.fileSize ?? 0,
|
||||||
|
width: element.width ?? 0,
|
||||||
|
height: element.height ?? 0,
|
||||||
|
duration: element.duration ?? 1,
|
||||||
|
videoFormat: element.videoFormat ?? 0,
|
||||||
|
thumb: makeFallbackThumb(),
|
||||||
|
fastOnly: true,
|
||||||
|
cleanups: [],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
interface ThumbPayload {
|
interface ThumbPayload {
|
||||||
bytes: Uint8Array;
|
bytes: Uint8Array;
|
||||||
md5: Uint8Array;
|
md5: Uint8Array;
|
||||||
@@ -321,6 +372,13 @@ async function loadThumb(element: MessageElement, videoPath: string): Promise<{
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function loadVideo(element: MessageElement): Promise<VideoPayload> {
|
async function loadVideo(element: MessageElement): Promise<VideoPayload> {
|
||||||
|
if (element.noByteFallback) {
|
||||||
|
if (!element.md5Hex || !element.sha1Hex) {
|
||||||
|
throw new Error('video fast-upload requires md5Hex + sha1Hex');
|
||||||
|
}
|
||||||
|
return videoPayloadFromFingerprint(element);
|
||||||
|
}
|
||||||
|
|
||||||
const tempDir = defaultVideoTempDir();
|
const tempDir = defaultVideoTempDir();
|
||||||
const cleanups: Array<() => void> = [];
|
const cleanups: Array<() => void> = [];
|
||||||
fs.mkdirSync(tempDir, { recursive: true });
|
fs.mkdirSync(tempDir, { recursive: true });
|
||||||
@@ -344,10 +402,13 @@ async function loadVideo(element: MessageElement): Promise<VideoPayload> {
|
|||||||
sha1Hex: hashes.sha1Hex,
|
sha1Hex: hashes.sha1Hex,
|
||||||
fileName: staged.fileName || `${hashes.md5Hex}.mp4`,
|
fileName: staged.fileName || `${hashes.md5Hex}.mp4`,
|
||||||
filePath: staged.filePath,
|
filePath: staged.filePath,
|
||||||
|
fileSize: staged.bytes.length,
|
||||||
width,
|
width,
|
||||||
height,
|
height,
|
||||||
duration,
|
duration,
|
||||||
|
videoFormat: 0,
|
||||||
thumb,
|
thumb,
|
||||||
|
fastOnly: false,
|
||||||
cleanups: [...cleanups],
|
cleanups: [...cleanups],
|
||||||
};
|
};
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -381,14 +442,14 @@ async function startVideoUpload(
|
|||||||
uploadInfo: [
|
uploadInfo: [
|
||||||
{
|
{
|
||||||
fileInfo: {
|
fileInfo: {
|
||||||
fileSize: video.bytes.length,
|
fileSize: video.fileSize,
|
||||||
fileHash: video.md5Hex,
|
fileHash: video.md5Hex,
|
||||||
fileSha1: video.sha1Hex,
|
fileSha1: video.sha1Hex,
|
||||||
fileName: 'nya.mp4',
|
fileName: 'nya.mp4',
|
||||||
type: { type: 2, picFormat: 0, videoFormat: 0, voiceFormat: 0 },
|
type: { type: 2, picFormat: 0, videoFormat: video.videoFormat, voiceFormat: 0 },
|
||||||
height: 0,
|
height: video.height,
|
||||||
width: 0,
|
width: video.width,
|
||||||
time: 0,
|
time: video.duration,
|
||||||
original: 0,
|
original: 0,
|
||||||
},
|
},
|
||||||
subFileType: 0,
|
subFileType: 0,
|
||||||
@@ -493,6 +554,9 @@ export async function uploadVideoMsgInfo(
|
|||||||
|
|
||||||
const uKey = upload?.uKey ?? '';
|
const uKey = upload?.uKey ?? '';
|
||||||
if (uKey && upload?.msgInfo) {
|
if (uKey && upload?.msgInfo) {
|
||||||
|
if (video.fastOnly) {
|
||||||
|
throw new Error('video fast-upload not available (server requires bytes)');
|
||||||
|
}
|
||||||
log.debug('highway video upload: bytes=%d md5=%s scene=%s', video.bytes.length, video.md5Hex, isGroup ? 'group' : 'c2c');
|
log.debug('highway video upload: bytes=%d md5=%s scene=%s', video.bytes.length, video.md5Hex, isGroup ? 'group' : 'c2c');
|
||||||
const extend = buildHighwayExtend(uKey, upload.msgInfo, upload.ipv4s ?? [], video.sha1Blocks, 0);
|
const extend = buildHighwayExtend(uKey, upload.msgInfo, upload.ipv4s ?? [], video.sha1Blocks, 0);
|
||||||
const commandId = isGroup ? GROUP_VIDEO_CMD_ID : PRIVATE_VIDEO_CMD_ID;
|
const commandId = isGroup ? GROUP_VIDEO_CMD_ID : PRIVATE_VIDEO_CMD_ID;
|
||||||
|
|||||||
@@ -623,12 +623,32 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
|||||||
return okResponse([]);
|
return okResponse([]);
|
||||||
});
|
});
|
||||||
|
|
||||||
h.registerAction('forward_friend_single_msg', async () => {
|
h.registerAction('forward_friend_single_msg', async (params) => {
|
||||||
return failedResponse(RETCODE.ACTION_FAILED, 'not yet implemented');
|
const messageId = asNumber(params.message_id);
|
||||||
|
const userId = asNumber(params.user_id);
|
||||||
|
if (!messageId) return failedResponse(RETCODE.BAD_REQUEST, 'message_id is required');
|
||||||
|
if (!userId) return failedResponse(RETCODE.BAD_REQUEST, 'user_id is required');
|
||||||
|
if (!ctx.forwardSingleMsg) return failedResponse(RETCODE.ACTION_FAILED, 'not implemented');
|
||||||
|
try {
|
||||||
|
const result = await ctx.forwardSingleMsg(messageId, { userId });
|
||||||
|
return okResponse({ message_id: result.messageId });
|
||||||
|
} catch (err) {
|
||||||
|
return failedResponse(RETCODE.ACTION_FAILED, err instanceof Error ? err.message : String(err));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
h.registerAction('forward_group_single_msg', async () => {
|
h.registerAction('forward_group_single_msg', async (params) => {
|
||||||
return failedResponse(RETCODE.ACTION_FAILED, 'not yet implemented');
|
const messageId = asNumber(params.message_id);
|
||||||
|
const groupId = asNumber(params.group_id);
|
||||||
|
if (!messageId) return failedResponse(RETCODE.BAD_REQUEST, 'message_id is required');
|
||||||
|
if (!groupId) return failedResponse(RETCODE.BAD_REQUEST, 'group_id is required');
|
||||||
|
if (!ctx.forwardSingleMsg) return failedResponse(RETCODE.ACTION_FAILED, 'not implemented');
|
||||||
|
try {
|
||||||
|
const result = await ctx.forwardSingleMsg(messageId, { groupId });
|
||||||
|
return okResponse({ message_id: result.messageId });
|
||||||
|
} catch (err) {
|
||||||
|
return failedResponse(RETCODE.ACTION_FAILED, err instanceof Error ? err.message : String(err));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// todo 我的建议是引入数据库api 纯协议我不知道这种api怎么实现,ntQQ在实现这个方法的时候只进行了数据库查询,完全没碰网络
|
// todo 我的建议是引入数据库api 纯协议我不知道这种api怎么实现,ntQQ在实现这个方法的时候只进行了数据库查询,完全没碰网络
|
||||||
|
|||||||
@@ -101,6 +101,7 @@ export interface ApiActionContext {
|
|||||||
sendPrivateForwardMsg?: (userId: number, messages: JsonValue) => Promise<{ messageId: number; forwardId: string }>;
|
sendPrivateForwardMsg?: (userId: number, messages: JsonValue) => Promise<{ messageId: number; forwardId: string }>;
|
||||||
sendForwardMsg?: (messages: JsonValue) => Promise<{ forwardId: string }>;
|
sendForwardMsg?: (messages: JsonValue) => Promise<{ forwardId: string }>;
|
||||||
getForwardMsg?: (resId: string) => Promise<JsonObject[]>;
|
getForwardMsg?: (resId: string) => Promise<JsonObject[]>;
|
||||||
|
forwardSingleMsg?: (messageId: number, target: { groupId?: number; userId?: number }) => Promise<{ messageId: number }>;
|
||||||
// Extended NapCat-compatible
|
// Extended NapCat-compatible
|
||||||
setFriendRemark?: (userId: number, remark: string) => Promise<void>;
|
setFriendRemark?: (userId: number, remark: string) => Promise<void>;
|
||||||
setGroupRemark?: (groupId: number, remark: string) => Promise<void>;
|
setGroupRemark?: (groupId: number, remark: string) => Promise<void>;
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ export type MessageIdResolver = (isGroup: boolean, sessionId: number, sequence:
|
|||||||
* `get_record`) without having to re-scan the persisted message store.
|
* `get_record`) without having to re-scan the persisted message store.
|
||||||
*/
|
*/
|
||||||
export type MediaSegmentSink = (
|
export type MediaSegmentSink = (
|
||||||
mediaType: 'image' | 'record',
|
mediaType: 'image' | 'record' | 'video',
|
||||||
element: MessageElement,
|
element: MessageElement,
|
||||||
data: JsonObject,
|
data: JsonObject,
|
||||||
isGroup: boolean,
|
isGroup: boolean,
|
||||||
@@ -414,13 +414,12 @@ async function elementToSegment(
|
|||||||
|
|
||||||
if (element.type === 'video') {
|
if (element.type === 'video') {
|
||||||
const url = mediaResolver ? await mediaResolver(element, isGroup, sessionId) : (element.url ?? '');
|
const url = mediaResolver ? await mediaResolver(element, isGroup, sessionId) : (element.url ?? '');
|
||||||
return {
|
const data: JsonObject = {
|
||||||
type: 'video',
|
file: element.fileName ?? element.fileId ?? '',
|
||||||
data: {
|
url,
|
||||||
file: element.fileName ?? element.fileId ?? '',
|
|
||||||
url,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
if (mediaSink) mediaSink('video', element, data, isGroup, sessionId);
|
||||||
|
return { type: 'video', data };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (element.type === 'json') {
|
if (element.type === 'json') {
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import {
|
|||||||
} from './modules/contact-actions';
|
} from './modules/contact-actions';
|
||||||
import {
|
import {
|
||||||
deleteMessage,
|
deleteMessage,
|
||||||
|
forwardSingleMessage,
|
||||||
getForwardMessage,
|
getForwardMessage,
|
||||||
getFriendMsgHistory,
|
getFriendMsgHistory,
|
||||||
getGroupMsgHistory,
|
getGroupMsgHistory,
|
||||||
@@ -166,6 +167,7 @@ export function buildApiContext(ref: OneBotInstanceContext): ApiActionContext {
|
|||||||
sendPrivateForwardMsg: (userId, messages) => sendPrivateForwardMessage(ref, userId, messages),
|
sendPrivateForwardMsg: (userId, messages) => sendPrivateForwardMessage(ref, userId, messages),
|
||||||
sendForwardMsg: (messages) => uploadForwardMessage(ref, messages),
|
sendForwardMsg: (messages) => uploadForwardMessage(ref, messages),
|
||||||
getForwardMsg: (resId) => getForwardMessage(ref, resId),
|
getForwardMsg: (resId) => getForwardMessage(ref, resId),
|
||||||
|
forwardSingleMsg: (messageId, target) => forwardSingleMessage(ref, messageId, target),
|
||||||
forceFetchClientKey: () => bridge.forceFetchClientKey(),
|
forceFetchClientKey: () => bridge.forceFetchClientKey(),
|
||||||
setFriendRemark: (userId, remark) => bridge.setFriendRemark(userId, remark),
|
setFriendRemark: (userId, remark) => bridge.setFriendRemark(userId, remark),
|
||||||
setGroupRemark: (groupId, remark) => bridge.setGroupRemark(groupId, remark),
|
setGroupRemark: (groupId, remark) => bridge.setGroupRemark(groupId, remark),
|
||||||
|
|||||||
@@ -91,10 +91,34 @@ export class OneBotInstance {
|
|||||||
imageUrl: element.imageUrl ?? '',
|
imageUrl: element.imageUrl ?? '',
|
||||||
isGroup,
|
isGroup,
|
||||||
sessionId,
|
sessionId,
|
||||||
|
md5Hex: element.md5Hex,
|
||||||
|
sha1Hex: element.sha1Hex,
|
||||||
|
width: element.width,
|
||||||
|
height: element.height,
|
||||||
|
picFormat: element.picFormat,
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.mediaStore.rememberRecord({
|
if (mediaType === 'record') {
|
||||||
|
this.mediaStore.rememberRecord({
|
||||||
|
file: file || element.fileName || element.fileId || '',
|
||||||
|
fileId: element.fileId ?? '',
|
||||||
|
url,
|
||||||
|
fileSize: element.fileSize ?? 0,
|
||||||
|
fileName: element.fileName ?? '',
|
||||||
|
duration: element.duration ?? 0,
|
||||||
|
fileHash: element.fileHash ?? '',
|
||||||
|
mediaNode: element.mediaNode,
|
||||||
|
isGroup,
|
||||||
|
sessionId,
|
||||||
|
md5Hex: element.md5Hex,
|
||||||
|
sha1Hex: element.sha1Hex,
|
||||||
|
voiceFormat: element.voiceFormat,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// video
|
||||||
|
this.mediaStore.rememberVideo({
|
||||||
file: file || element.fileName || element.fileId || '',
|
file: file || element.fileName || element.fileId || '',
|
||||||
fileId: element.fileId ?? '',
|
fileId: element.fileId ?? '',
|
||||||
url,
|
url,
|
||||||
@@ -105,6 +129,11 @@ export class OneBotInstance {
|
|||||||
mediaNode: element.mediaNode,
|
mediaNode: element.mediaNode,
|
||||||
isGroup,
|
isGroup,
|
||||||
sessionId,
|
sessionId,
|
||||||
|
md5Hex: element.md5Hex,
|
||||||
|
sha1Hex: element.sha1Hex,
|
||||||
|
width: element.width,
|
||||||
|
height: element.height,
|
||||||
|
videoFormat: element.videoFormat,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
this.eventConverter.setMessageIdResolver((isGroup, sessionId, sequence, eventName) =>
|
this.eventConverter.setMessageIdResolver((isGroup, sessionId, sequence, eventName) =>
|
||||||
|
|||||||
@@ -38,6 +38,13 @@ export interface CachedImage {
|
|||||||
sessionId: number;
|
sessionId: number;
|
||||||
/** Original imageUrl from the MessageElement, used for RKey re-appending. */
|
/** Original imageUrl from the MessageElement, used for RKey re-appending. */
|
||||||
imageUrl: string;
|
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 {
|
export interface CachedRecord {
|
||||||
@@ -56,10 +63,34 @@ export interface CachedRecord {
|
|||||||
/** Conversation the media originated from (group vs private path). */
|
/** Conversation the media originated from (group vs private path). */
|
||||||
isGroup: boolean;
|
isGroup: boolean;
|
||||||
sessionId: number;
|
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_IMAGE = 'image';
|
||||||
const TYPE_RECORD = 'record';
|
const TYPE_RECORD = 'record';
|
||||||
|
const TYPE_VIDEO = 'video';
|
||||||
const DEFAULT_KEEP_ENTRIES = 4096;
|
const DEFAULT_KEEP_ENTRIES = 4096;
|
||||||
|
|
||||||
export class MediaStore {
|
export class MediaStore {
|
||||||
@@ -145,6 +176,12 @@ export class MediaStore {
|
|||||||
this.upsertWithAliases(TYPE_RECORD, primaryKey, info, [info.file, info.fileName, info.fileId, info.url]);
|
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 {
|
findImage(key: string): CachedImage | null {
|
||||||
return this.findByAnyKey<CachedImage>(TYPE_IMAGE, key);
|
return this.findByAnyKey<CachedImage>(TYPE_IMAGE, key);
|
||||||
}
|
}
|
||||||
@@ -153,6 +190,10 @@ export class MediaStore {
|
|||||||
return this.findByAnyKey<CachedRecord>(TYPE_RECORD, key);
|
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 {
|
updateImageUrl(key: string, url: string): void {
|
||||||
if (!url) return;
|
if (!url) return;
|
||||||
const cached = this.findImage(key);
|
const cached = this.findImage(key);
|
||||||
@@ -167,11 +208,19 @@ export class MediaStore {
|
|||||||
this.rememberRecord({ ...cached, url });
|
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. */
|
/** 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 img = this.countByType.get(TYPE_IMAGE) as { n: number } | undefined;
|
||||||
const rec = this.countByType.get(TYPE_RECORD) 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 ---
|
// --- internals ---
|
||||||
|
|||||||
@@ -235,6 +235,152 @@ export async function uploadForwardMessage(
|
|||||||
return { forwardId };
|
return { forwardId };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Forward a previously-received message to another peer.
|
||||||
|
*
|
||||||
|
* We look up the cached event + media fingerprints, then re-send via the
|
||||||
|
* normal send pipeline with `noByteFallback` set on media elements so the
|
||||||
|
* upload modules fast-path through OIDB md5/sha1 instead of re-downloading
|
||||||
|
* the original CDN bytes. Fails fast if a media segment has no cached
|
||||||
|
* fingerprints or contains a file segment (file forwarding has its own
|
||||||
|
* separate protocol and is not in scope here).
|
||||||
|
*/
|
||||||
|
export async function forwardSingleMessage(
|
||||||
|
ref: OneBotInstanceContext,
|
||||||
|
messageId: number,
|
||||||
|
target: { groupId?: number; userId?: number },
|
||||||
|
): Promise<{ messageId: number }> {
|
||||||
|
if (!target.groupId && !target.userId) {
|
||||||
|
throw new Error('forward target group_id or user_id is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
const event = ref.messageStore.findEvent(messageId);
|
||||||
|
if (!event) throw new Error(`message not found: ${messageId}`);
|
||||||
|
|
||||||
|
const content = (event.message ?? event.raw_message ?? '') as JsonValue;
|
||||||
|
const parsed = await parseMessage(content, false);
|
||||||
|
if (parsed.length === 0) throw new Error('message has no content');
|
||||||
|
|
||||||
|
const elements = parsed.map((el) => enrichForForward(ref, el));
|
||||||
|
|
||||||
|
let receipt;
|
||||||
|
let messageIdOut: number;
|
||||||
|
if (target.groupId) {
|
||||||
|
receipt = await ref.bridge.sendGroupMessage(target.groupId, elements);
|
||||||
|
messageIdOut = hashMessageIdInt32(receipt.sequence, target.groupId, GROUP_MESSAGE_EVENT);
|
||||||
|
ref.cacheMessageMeta(messageIdOut, {
|
||||||
|
isGroup: true,
|
||||||
|
targetId: target.groupId,
|
||||||
|
sequence: receipt.sequence,
|
||||||
|
eventName: GROUP_MESSAGE_EVENT,
|
||||||
|
clientSequence: receipt.clientSequence,
|
||||||
|
random: receipt.random,
|
||||||
|
timestamp: receipt.timestamp,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
receipt = await ref.bridge.sendPrivateMessage(target.userId!, elements);
|
||||||
|
messageIdOut = hashMessageIdInt32(receipt.sequence, target.userId!, PRIVATE_MESSAGE_EVENT);
|
||||||
|
ref.cacheMessageMeta(messageIdOut, {
|
||||||
|
isGroup: false,
|
||||||
|
targetId: target.userId!,
|
||||||
|
sequence: receipt.sequence,
|
||||||
|
eventName: PRIVATE_MESSAGE_EVENT,
|
||||||
|
clientSequence: receipt.clientSequence,
|
||||||
|
random: receipt.random,
|
||||||
|
timestamp: receipt.timestamp,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return { messageId: messageIdOut };
|
||||||
|
}
|
||||||
|
|
||||||
|
function enrichForForward(ref: OneBotInstanceContext, element: MessageElement): MessageElement {
|
||||||
|
// The send path takes care of these as-is; nothing extra to do.
|
||||||
|
if (element.type === 'text' || element.type === 'face' || element.type === 'at'
|
||||||
|
|| element.type === 'reply' || element.type === 'json' || element.type === 'xml'
|
||||||
|
|| element.type === 'poke' || element.type === 'forward' || element.type === 'mface') {
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The `file` segment is its own upload pipeline (FtnUpload / OfflineFile)
|
||||||
|
// and is not supported by the fast-upload forward path.
|
||||||
|
if (element.type === 'file') {
|
||||||
|
throw new Error('forward of file segment is not supported');
|
||||||
|
}
|
||||||
|
|
||||||
|
// For images/records/videos we look up the cached fingerprints by any of
|
||||||
|
// the keys MediaStore aliases under. After parseMessage, the segment's
|
||||||
|
// `data.file` lands on `element.url` for all three types.
|
||||||
|
const lookupKey = element.url || element.fileName || element.fileId || '';
|
||||||
|
if (!lookupKey) {
|
||||||
|
throw new Error(`forward ${element.type} missing cache key`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (element.type === 'image') {
|
||||||
|
const cached = ref.mediaStore.findImage(lookupKey);
|
||||||
|
if (!cached || !cached.md5Hex || !cached.sha1Hex || !cached.width || !cached.height || !cached.picFormat) {
|
||||||
|
throw new Error('forward image fingerprint not cached (legacy image or expired)');
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
...element,
|
||||||
|
type: 'image',
|
||||||
|
noByteFallback: true,
|
||||||
|
md5Hex: cached.md5Hex,
|
||||||
|
sha1Hex: cached.sha1Hex,
|
||||||
|
fileSize: cached.fileSize,
|
||||||
|
fileName: cached.fileName,
|
||||||
|
subType: cached.subType,
|
||||||
|
summary: cached.summary,
|
||||||
|
width: cached.width,
|
||||||
|
height: cached.height,
|
||||||
|
picFormat: cached.picFormat,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (element.type === 'record') {
|
||||||
|
const cached = ref.mediaStore.findRecord(lookupKey);
|
||||||
|
if (!cached || !cached.md5Hex || !cached.sha1Hex) {
|
||||||
|
throw new Error('forward record fingerprint not cached');
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
...element,
|
||||||
|
type: 'record',
|
||||||
|
noByteFallback: true,
|
||||||
|
md5Hex: cached.md5Hex,
|
||||||
|
sha1Hex: cached.sha1Hex,
|
||||||
|
fileSize: cached.fileSize,
|
||||||
|
fileName: cached.fileName,
|
||||||
|
fileId: cached.fileId,
|
||||||
|
duration: cached.duration,
|
||||||
|
voiceFormat: cached.voiceFormat ?? 1,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (element.type === 'video') {
|
||||||
|
const cached = ref.mediaStore.findVideo(lookupKey);
|
||||||
|
if (!cached || !cached.md5Hex || !cached.sha1Hex) {
|
||||||
|
throw new Error('forward video fingerprint not cached');
|
||||||
|
}
|
||||||
|
log.warn('video forward uses a fallback thumbnail (original thumb not cached)');
|
||||||
|
return {
|
||||||
|
...element,
|
||||||
|
type: 'video',
|
||||||
|
noByteFallback: true,
|
||||||
|
md5Hex: cached.md5Hex,
|
||||||
|
sha1Hex: cached.sha1Hex,
|
||||||
|
fileSize: cached.fileSize,
|
||||||
|
fileName: cached.fileName,
|
||||||
|
fileId: cached.fileId,
|
||||||
|
duration: cached.duration,
|
||||||
|
width: cached.width ?? 0,
|
||||||
|
height: cached.height ?? 0,
|
||||||
|
videoFormat: cached.videoFormat ?? 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
|
||||||
export async function getForwardMessage(
|
export async function getForwardMessage(
|
||||||
ref: OneBotInstanceContext,
|
ref: OneBotInstanceContext,
|
||||||
resId: string,
|
resId: string,
|
||||||
|
|||||||
Reference in new issue
Block a user