feat(onebot): TierB ① — get_group_signed_list via qun.qq.com web API

Group sign-in (打卡) list is NapCat's one kernel-free item here: it's a
plain HTTP POST to qun.qq.com/v2/signin/trpc/GetDaySignedList with g_tk
from p_skey, not OIDB. SnowLuma already has the cookie/bkn infra (same as
group notice/essence/honor), so this is a straight port — no RE.

- protocol/web/group-signin.ts: getDaySignedListWebAPI + pure
  mapDaySignedList (de-skews signInRank via (r-1)/2+1, [] when no page).
- core WebApi.getSignedList(groupId) wires the qun.qq.com cookie jar.
- onebot get_group_signed_list groupAction (readOnly), [] on no sign-ins.
- Tests pin the rank/field transform and the action wiring.
This commit is contained in:
DrSmoothl committed 2026-06-18 19:24:42 +08:00
1 parent dfb6aa54c7
commit 36d557e5ea
6 files changed
+183

No files matched your search

+4
View File
@@ -371,6 +371,10 @@
"types": "./src/web/group-notice.ts",
"default": "./src/web/group-notice.ts"
},
"./web/group-signin": {
"types": "./src/web/group-signin.ts",
"default": "./src/web/group-signin.ts"
},
"./web/request-util": {
"types": "./src/web/request-util.ts",
"default": "./src/web/request-util.ts"
+95
View File
@@ -0,0 +1,95 @@
import { createLogger } from '@snowluma/common/logger';
import { RequestUtil, cookieToString, getBknFromCookie } from './request-util';
const log = createLogger('Bridge.Web');
// Raw shapes from qun.qq.com `v2/signin/trpc/GetDaySignedList`.
export interface DaySignedInfo {
uid: string;
uidGroupNick: string;
signedTimeStamp: string;
signInRank: number;
}
export interface DaySignedListResponse {
retCode: number;
costTime?: number;
funcCode: number;
response: {
ret?: { code: string; msg: string };
page?: Array<{ infos?: DaySignedInfo[]; offset: number; total: number }>;
};
}
/** OneBot-facing signed-in member, matching the napcat surface. */
export interface SignedListMember {
[key: string]: import('@snowluma/common/json').JsonValue;
user_id: number;
nick: string;
time: number;
rank: number;
}
/**
* Pure transform from the qun.qq.com response into the OneBot list.
* The server returns `signInRank` on a doubled-and-offset scale
* (1, 3, 5, …); NapCat de-skews it with `(rank - 1) / 2 + 1` to recover
* the human 1-based rank — we mirror that exactly. Missing page/infos
* yields an empty list rather than throwing, so a group with no sign-ins
* is an empty result, not an error.
*/
export function mapDaySignedList(data: DaySignedListResponse): SignedListMember[] {
const infos = data.response?.page?.[0]?.infos;
if (!infos) return [];
return infos.map((info) => ({
user_id: Number(info.uid),
nick: info.uidGroupNick,
time: Number(info.signedTimeStamp),
rank: (info.signInRank - 1) / 2 + 1,
}));
}
/** YYYYMMDD for the given Date (defaults to today), the form the endpoint wants. */
function yyyymmdd(d: Date = new Date()): string {
return `${d.getFullYear()}${String(d.getMonth() + 1).padStart(2, '0')}${String(d.getDate()).padStart(2, '0')}`;
}
/**
* Fetch today's group sign-in (打卡) list via the qun.qq.com web API.
* Mirrors NapCat's `getDaySignedList`: POST JSON to the signin trpc
* endpoint with `g_tk` derived from p_skey and a minimal cookie jar.
* Returns the mapped list, or [] on any transport / shape failure.
*/
export async function getDaySignedListWebAPI(
cookieObject: Record<string, string>,
groupCode: string,
selfUin: string,
day?: Date,
): Promise<SignedListMember[]> {
try {
const gtk = getBknFromCookie(cookieObject);
const url = `https://qun.qq.com/v2/signin/trpc/GetDaySignedList?g_tk=${gtk}`;
const body = {
dayYmd: yyyymmdd(day),
offset: 0,
limit: 100,
uid: selfUin,
groupId: groupCode,
};
const data = await RequestUtil.HttpGetJson<DaySignedListResponse>(
url,
'POST',
body,
{
Cookie: cookieToString(cookieObject),
'Content-Type': 'application/json',
},
true,
true,
);
return mapDaySignedList(data);
} catch (e) {
log.warn('getDaySignedList failed (group=%s): %s', groupCode, e instanceof Error ? (e.stack ?? e.message) : String(e));
return [];
}
}
@@ -0,0 +1,35 @@
import { describe, it, expect } from 'vitest';
import { mapDaySignedList, type DaySignedInfo, type DaySignedListResponse } from '@snowluma/protocol/web/group-signin';
// The signed-in list comes back from qun.qq.com's GetDaySignedList trpc
// endpoint nested under response.page[0].infos. We pin the field renames
// and the rank de-skewing formula NapCat uses ((signInRank - 1) / 2 + 1),
// because that transform is the only real logic in this otherwise-thin
// HTTP port and a silent off-by-one would be invisible end-to-end.
function resp(infos: DaySignedInfo[]): DaySignedListResponse {
return { retCode: 0, response: { page: [{ infos, offset: 0, total: infos.length }] }, funcCode: 0 };
}
describe('group-signin / mapDaySignedList', () => {
it('renames fields and de-skews signInRank', () => {
const out = mapDaySignedList(resp([
{ uid: '10001', uidGroupNick: 'Alice', signedTimeStamp: '1700000000', signInRank: 1 },
{ uid: '10002', uidGroupNick: 'Bob', signedTimeStamp: '1700000050', signInRank: 3 },
{ uid: '10003', uidGroupNick: 'Carol', signedTimeStamp: '1700000100', signInRank: 5 },
]));
expect(out).toEqual([
{ user_id: 10001, nick: 'Alice', time: 1700000000, rank: 1 },
{ user_id: 10002, nick: 'Bob', time: 1700000050, rank: 2 },
{ user_id: 10003, nick: 'Carol', time: 1700000100, rank: 3 },
]);
});
it('returns [] when infos is empty', () => {
expect(mapDaySignedList(resp([]))).toEqual([]);
});
it('returns [] when page is missing entirely', () => {
expect(mapDaySignedList({ retCode: 0, response: {}, funcCode: 0 })).toEqual([]);
});
});