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:
6 files changed
+183
No files matched your search
@@ -3,6 +3,7 @@ import { ForceFetchClientKey, type ClientKeyInfo as NamespaceClientKeyInfo } fro
|
||||
import { GetPskey } from '@snowluma/protocol/oidb-services/web/get-pskey';
|
||||
import { getGroupEssenceMsg, getGroupEssenceMsgAll, type GroupEssenceMsgRet } from '@snowluma/protocol/web/group-essence';
|
||||
import { getHonorListWebAPI, WebHonorType, type WebHonorItem } from '@snowluma/protocol/web/group-honor';
|
||||
import { getDaySignedListWebAPI, type SignedListMember } from '@snowluma/protocol/web/group-signin';
|
||||
import {
|
||||
deleteGroupNotice as deleteGroupNoticeHttp,
|
||||
getGroupNoticeWebAPI,
|
||||
@@ -227,6 +228,15 @@ export class WebApi {
|
||||
return honorInfo;
|
||||
}
|
||||
|
||||
// ─────────────── group sign-in (打卡) ───────────────
|
||||
|
||||
/** Today's group sign-in list via qun.qq.com. [] when none / on failure. */
|
||||
async getSignedList(groupId: number): Promise<SignedListMember[]> {
|
||||
const bridge = asBridge(this.ctx);
|
||||
const cookieObject = await getCookies(bridge, 'qun.qq.com');
|
||||
return getDaySignedListWebAPI(cookieObject, groupId.toString(), bridge.identity.uin.toString());
|
||||
}
|
||||
|
||||
// ─────────────── group notice ───────────────
|
||||
|
||||
async sendNotice(
|
||||
|
||||
@@ -728,6 +728,23 @@ export const actions = [
|
||||
},
|
||||
}),
|
||||
|
||||
// get_group_signed_list — 群今日打卡名单。NapCat 走 qun.qq.com 的
|
||||
// v2/signin/trpc/GetDaySignedList(HTTP + PSKey),非 OIDB;SnowLuma 复用
|
||||
// 现有 web cookie/bkn 基建(与群公告/精华同套路),故无需 RE。失败/无打卡返回空表。
|
||||
groupAction({
|
||||
name: 'get_group_signed_list',
|
||||
summary: '获取群今日打卡列表',
|
||||
readOnly: true,
|
||||
run: async (p, ctx) => {
|
||||
try {
|
||||
const list = await ctx.bridge.apis.web.getSignedList(p.group_id);
|
||||
return okResponse(list);
|
||||
} catch (e) {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, e instanceof Error ? e.message : String(e));
|
||||
}
|
||||
},
|
||||
}),
|
||||
|
||||
defineAction({
|
||||
name: 'forward_friend_single_msg',
|
||||
summary: '转发单条消息给好友',
|
||||
|
||||
@@ -842,3 +842,25 @@ describe('extended-actions / TierB compat stubs', () => {
|
||||
expect(res).toMatchObject({ status: 'ok', retcode: 0 });
|
||||
});
|
||||
});
|
||||
|
||||
// ─── TierB ①: get_group_signed_list (qun.qq.com HTTP, real) ───
|
||||
// Thin wrapper over WebApi.getSignedList; we pin that the action drives
|
||||
// the web api with the numeric group id and passes the mapped list through.
|
||||
|
||||
describe('extended-actions / get_group_signed_list', () => {
|
||||
it('calls web.getSignedList with the group id and returns the list', async () => {
|
||||
const list = [{ user_id: 10001, nick: 'Alice', time: 1700000000, rank: 1 }];
|
||||
const getSignedList = vi.fn(async () => list);
|
||||
const bridge = fakeBridge({ apis: { web: { getSignedList } } });
|
||||
const res = await makeHandler(fakeCtx(bridge)).handle('get_group_signed_list', { group_id: 12345 });
|
||||
expect(getSignedList).toHaveBeenCalledWith(12345);
|
||||
expect(res).toMatchObject({ status: 'ok', retcode: 0, data: list });
|
||||
});
|
||||
|
||||
it('surfaces a failure as a failed response', async () => {
|
||||
const getSignedList = vi.fn(async () => { throw new Error('no pskey'); });
|
||||
const bridge = fakeBridge({ apis: { web: { getSignedList } } });
|
||||
const res = await makeHandler(fakeCtx(bridge)).handle('get_group_signed_list', { group_id: 12345 });
|
||||
expect(res.status).toBe('failed');
|
||||
});
|
||||
});
|
||||
@@ -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"
|
||||
|
||||
@@ -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([]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user