Files
social-router/packages/xhs/src/services/xhs-services.ts
2025-06-28 02:46:50 +08:00

150 lines
4.1 KiB
TypeScript

import { XhsClient } from '@kevisual/xhs/libs/xhs.ts';
import { Sequelize } from 'sequelize';
// import { createSequelize } from '@kevisual/xhs/services/xhs-db/db.ts';
import path from 'node:path';
import fs from 'node:fs';
export { XhsClient };
type XhsClientOptions = {
key: string;
cookie: string;
userid?: string;
username?: string;
signConfig?: {
signUrl: string;
};
initDB?: boolean;
[key: string]: any;
};
type XhsClientMap = {
client: XhsClient;
key: string;
options: XhsClientOptions;
db?: Sequelize;
};
type XhsServicesOptions = {
root?: string;
};
/**
* @description XhsServices is a singleton class that manages the XhsClient instances.
* It is used to create and manage the XhsClient instances.
*/
export class XhsServices {
map: Map<string, XhsClientMap> = new Map();
root: string = 'root';
constructor(opts?: XhsServicesOptions) {
this.root = opts?.root || this.root;
}
createClient(options: XhsClientOptions) {
const { key, cookie, signConfig } = options;
if (this.map.has(key)) {
return this.map.get(key);
}
const client = new XhsClient({ cookie });
client.signConfig = signConfig;
const storagePath = options.storage || `db-sqlite/xhs-${key}.db`;
const storage = path.resolve(storagePath);
const dir = path.dirname(storage);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
let isNew = options?.initDB ?? false;
if (!fs.existsSync(storage) || !isNew) {
isNew = true;
}
// const db = createSequelize({ storage: storage });
const xhsClientMap = {
client,
key,
options,
// db,
};
// if (isNew) {
// this.initDb(xhsClientMap);
// }
this.map.set(key, xhsClientMap);
return client;
}
async initDb(xhsClientMap: XhsClientMap) {
//
}
createRoot(options: Partial<XhsClientOptions>) {
options.key = options.key || this.root;
return this.createClient(options as XhsClientOptions) as XhsClient;
}
getKey(key?: string) {
if (!key) key = this.root;
return key;
}
/**
* Get the XhsClient instance by key
* @param key
* @returns
*/
getClient(key?: string) {
const xhsClient = this.map.get(this.getKey(key));
return xhsClient.client;
}
getXhsClient(key?: string) {
const xhsClient = this.map.get(this.getKey(key));
return xhsClient;
}
getXhsUserInfo(key?: string) {
const xhsClient = this.map.get(this.getKey(key));
return {
userid: xhsClient?.options?.userid || '',
username: xhsClient?.options?.username || '',
};
}
isOwner(user: { username: string; userid: string }, key?: string) {
const xhsUserInfo = this.getXhsUserInfo(key);
if (!xhsUserInfo.userid || !xhsUserInfo.username) {
return false;
}
return user.userid === xhsUserInfo.userid;
}
isReplayAi(data: any, key?: string) {
const mention = data?.mention || {};
const user_info = mention?.comment_info?.target_comment?.user_info || {};
if (user_info?.userid) {
const xhsUserInfo = this.getXhsUserInfo(key);
// 处理用户信息
return user_info.userid === xhsUserInfo.userid;
}
return false;
}
setCookie(cookie: string, key?: string) {
const xhsClient = this.map.get(this.getKey(key));
if (xhsClient) {
xhsClient.options.cookie = cookie;
xhsClient.client.setCookie(cookie);
}
}
/**
* 设置用户信息
* @param user
* @param key
*/
setUserInfo(user: { userid: string; username: string }, key?: string) {
const xhsClient = this.map.get(this.getKey(key));
if (xhsClient) {
xhsClient.options.userid = user.userid;
xhsClient.options.username = user.username;
}
}
setSignConfig(signConfig: { signUrl: string }, key?: string) {
const xhsClient = this.map.get(this.getKey(key));
if (xhsClient) {
xhsClient.options.signConfig = signConfig;
xhsClient.client.signConfig = signConfig;
}
console.log('setSignConfig', xhsClient?.options?.signConfig);
}
getSignConfig(key?: string) {
const xhsClient = this.map.get(this.getKey(key));
return xhsClient?.options?.signConfig || {};
}
}