fix(core): restore group album comments (#508)

Restore group album comments accepted by QQ and preserve server error reporting.

Refs #436
This commit is contained in:
辉龙曦继 authored and GitHub committed 2026-10-13 09:47:24 +08:00
1 parent 7c0519f75f
commit ceb8387ea6
3 files changed
+407 -38

No files matched your search

+120 -5
View File
@@ -4,6 +4,7 @@ import { createLogger } from '@snowluma/common/logger';
import type {
AlbumCreator,
CommentReqBodyHeader,
CommentReqBodyUserWrap,
CommentReqPhotoInfo,
CommentRespData,
DeleteMediasRequest,
@@ -404,6 +405,16 @@ export class GroupAlbumApi {
albumId,
batchId,
);
const headerBytes =
feed.rawCellCommon ??
protobuf_encode<CommentReqBodyHeader>(commentReqHeader(feed.cellCommon));
const userBytes =
feed.rawCellUser ??
(ownerUin
? protobuf_encode<CommentReqBodyUserWrap>({ field1: { uin: ownerUin } })
: undefined);
const mediaBytes = feed.rawCellMedia ?? protobuf_encode<CommentReqPhotoInfo>(photoInfo);
const commenterUinNum = numericUin(uin);
const body = protobuf_encode<DoQunCommentRequest>({
field1: DO_QUN_COMMENT_SEQ,
@@ -413,12 +424,15 @@ export class GroupAlbumApi {
groupId: groupId.toString(),
field3: 2,
reqBody: {
field1: commentReqHeader(feed.cellCommon),
...(ownerUin ? { field2: { field1: { uin: ownerUin } } } : {}),
field5: photoInfo,
field1: headerBytes,
...(userBytes ? { field2: userBytes } : {}),
field5: mediaBytes,
},
field5: {
user: { uin },
user: {
uin,
...(commenterUinNum !== undefined ? { uinNum: commenterUinNum } : {}),
},
contents: [{ type: 0, content }],
clientKey,
},
@@ -644,7 +658,14 @@ export class GroupAlbumApi {
albumId: string,
batchId: bigint,
lloc: string,
): Promise<{ cellCommon: QunFeedCellCommon; ownerUin: string; media?: CommentReqPhotoInfo }> {
): Promise<{
cellCommon: QunFeedCellCommon;
ownerUin: string;
media?: CommentReqPhotoInfo;
rawCellCommon?: Uint8Array;
rawCellUser?: Uint8Array;
rawCellMedia?: Uint8Array;
}> {
const traceId = `_${Date.now()}_${Math.floor(Math.random() * 100000)}`;
const body = protobuf_encode<GetQunFeedDetailRequest>({
seq: 0,
@@ -691,10 +712,14 @@ export class GroupAlbumApi {
throw new Error('comment album media error: empty feed');
}
const responseBytes = toBytes(result.responseData);
return {
cellCommon: cell ?? { feedId },
ownerUin: feed?.cellUserInfo?.user?.uin ?? '',
media: feed?.cellMedia,
rawCellCommon: nonemptyBytes(protobufSubmessage(responseBytes, [4, 2, 1, 1])),
rawCellUser: nonemptyBytes(protobufSubmessage(responseBytes, [4, 2, 1, 2])),
rawCellMedia: nonemptyBytes(protobufSubmessage(responseBytes, [4, 2, 1, 5])),
};
}
}
@@ -815,6 +840,96 @@ function qunFeedCellId(
return `${head}^||^421_1_0_${groupId}|${albumId}|${lloc}^||^0`;
}
function toBytes(value: Uint8Array): Uint8Array {
return value instanceof Uint8Array ? value : new Uint8Array(value);
}
function nonemptyBytes(value: Uint8Array | undefined): Uint8Array | undefined {
return value && value.byteLength > 0 ? value : undefined;
}
function numericUin(uin: string): bigint | undefined {
if (!/^\d+$/.test(uin)) {
return undefined;
}
try {
return BigInt(uin);
} catch {
return undefined;
}
}
function readProtobufVarint(buf: Uint8Array, offset: number): { value: number; next: number } {
let value = 0;
let shift = 0;
let next = offset;
while (next < buf.length && shift <= 28) {
const byte = buf[next++];
value += (byte & 0x7f) * 2 ** shift;
if ((byte & 0x80) === 0) {
return { value, next };
}
shift += 7;
}
throw new Error('truncated protobuf varint');
}
function protobufSubmessage(buf: Uint8Array, path: readonly number[]): Uint8Array | undefined {
let current: Uint8Array | undefined = buf;
for (const fieldNo of path) {
if (!current) {
return undefined;
}
current = firstLengthDelimitedField(current, fieldNo);
}
return current;
}
function firstLengthDelimitedField(buf: Uint8Array, fieldNo: number): Uint8Array | undefined {
let offset = 0;
while (offset < buf.length) {
const key = readProtobufVarint(buf, offset);
offset = key.next;
const field = Math.floor(key.value / 8);
const wire = key.value % 8;
if (wire === 0) {
offset = readProtobufVarint(buf, offset).next;
} else if (wire === 1) {
offset += 8;
} else if (wire === 5) {
offset += 4;
} else if (wire === 2) {
const len = readProtobufVarint(buf, offset);
offset = len.next;
const end = offset + len.value;
if (end > buf.length) {
return undefined;
}
if (field === fieldNo) {
return buf.slice(offset, end);
}
offset = end;
} else {
return undefined;
}
}
return undefined;
}
function commentReqHeader(cell: QunFeedCellCommon): CommentReqBodyHeader {
return {
...(cell.time !== undefined ? { time: cell.time } : {}),