generated from tailored/router-template
feat: 自己的笔记也需要at才能评论了,优化一下
This commit is contained in:
parent
71359fba88
commit
530a4f21f5
@ -33,6 +33,10 @@
|
|||||||
},
|
},
|
||||||
"./index": {
|
"./index": {
|
||||||
"import": "./src/index.ts"
|
"import": "./src/index.ts"
|
||||||
|
},
|
||||||
|
"./index.ts": {
|
||||||
|
"import": "./src/index.ts",
|
||||||
|
"types": "./src/index.ts"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { QueryRouterServer } from '@kevisual/router/browser';
|
import { QueryRouterServer } from '@kevisual/router/browser';
|
||||||
import { XhsServices, XhsClient } from '@kevisual/xhs/services/xhs-services.ts';
|
import { XhsServices, XhsClient } from '@kevisual/xhs/services/xhs-services.ts';
|
||||||
|
export { XhsServices };
|
||||||
export const app = new QueryRouterServer();
|
export const app = new QueryRouterServer();
|
||||||
export const xhsServices = new XhsServices(); // Semicolon separated Cookie File
|
export const xhsServices = new XhsServices(); // Semicolon separated Cookie File
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { XhsClient } from './libs/xhs.ts';
|
import { XhsClient } from './libs/xhs.ts';
|
||||||
import { app, xhsServices, xhsRootClient } from './app.ts';
|
import { app, xhsServices, xhsRootClient, XhsServices } from './app.ts';
|
||||||
import './routes/index.ts';
|
import './routes/index.ts';
|
||||||
|
|
||||||
export { XhsClient, app, xhsServices, xhsRootClient };
|
export { XhsClient, app, xhsServices, xhsRootClient, XhsServices };
|
||||||
|
@ -125,22 +125,26 @@ app
|
|||||||
const handleMention: any[] = [];
|
const handleMention: any[] = [];
|
||||||
for (const mention of mentionList) {
|
for (const mention of mentionList) {
|
||||||
const mention_id = mention.id;
|
const mention_id = mention.id;
|
||||||
const note_id = mention.item_info.id;
|
const note_id = mention.item_info.id; // item_info 是笔记信息
|
||||||
|
const note_userid = mention.item_info.user_info?.userid || '';
|
||||||
|
const note_username = mention.item_info.user_info?.nickname || '';
|
||||||
const xsec_token = mention.item_info.xsec_token;
|
const xsec_token = mention.item_info.xsec_token;
|
||||||
let comment: any = Parse.getComment(mention);
|
let comment: any = Parse.getComment(mention);
|
||||||
// console.log('note_id', note_id, 'xsec_token', xsec_token, comment);
|
// console.log('note_id', note_id, 'xsec_token', xsec_token, comment);
|
||||||
handleMention.push({
|
handleMention.push({
|
||||||
mention_id,
|
mention_id,
|
||||||
note_id,
|
note_id,
|
||||||
|
note_userid,
|
||||||
|
note_username,
|
||||||
xsec_token,
|
xsec_token,
|
||||||
comment,
|
comment,
|
||||||
mention,
|
mention,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
console.log('获取提及列表成功', res.code, res.data?.message_list?.length);
|
console.log('获取提及列表成功', '[小红书code]', res.code, '提及数量', res.data?.message_list?.length);
|
||||||
ctx.body = handleMention;
|
ctx.body = handleMention;
|
||||||
} else {
|
} else {
|
||||||
console.log('获取提及列表失败', res.code);
|
console.log('获取提及列表失败', '[小红书code]', res.code);
|
||||||
ctx.throw(res.code, '获取提及列表失败');
|
ctx.throw(res.code, '获取提及列表失败');
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -7,6 +7,8 @@ export { XhsClient };
|
|||||||
type XhsClientOptions = {
|
type XhsClientOptions = {
|
||||||
key: string;
|
key: string;
|
||||||
cookie: string;
|
cookie: string;
|
||||||
|
userid?: string;
|
||||||
|
username?: string;
|
||||||
signConfig?: {
|
signConfig?: {
|
||||||
signUrl: string;
|
signUrl: string;
|
||||||
};
|
};
|
||||||
@ -88,4 +90,37 @@ export class XhsServices {
|
|||||||
const xhsClient = this.map.get(this.getKey(key));
|
const xhsClient = this.map.get(this.getKey(key));
|
||||||
return xhsClient;
|
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;
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { agent } from '@/agent/index.ts';
|
import { agent } from '@/agent/index.ts';
|
||||||
import { taskApp, queue, xhsApp } from '../task.ts';
|
import { taskApp, queue, xhsApp, xhsServices } from '../task.ts';
|
||||||
import { random, omit } from 'lodash-es';
|
import { random, omit } from 'lodash-es';
|
||||||
import util from 'node:util';
|
import util from 'node:util';
|
||||||
|
|
||||||
@ -111,14 +111,29 @@ taskApp
|
|||||||
.define(async (ctx) => {
|
.define(async (ctx) => {
|
||||||
const data = ctx.query.data; // 为提及的相关信息
|
const data = ctx.query.data; // 为提及的相关信息
|
||||||
const note_id = data.note_id;
|
const note_id = data.note_id;
|
||||||
|
const note_userid = data.note_userid;
|
||||||
|
const note_username = data.note_username;
|
||||||
|
|
||||||
|
// 检测是这个用户的username的笔记,如果是的话,需要有at的用户信息才继续。
|
||||||
|
const isOwner = xhsServices.isOwner({ username: note_username, userid: note_userid });
|
||||||
const xsec_token = data.xsec_token;
|
const xsec_token = data.xsec_token;
|
||||||
const comment_id = data.comment.comment_id;
|
const comment_id = data.comment.comment_id;
|
||||||
const content = data.comment?.content || 'test';
|
let content: string = data.comment?.content || 'test';
|
||||||
const postData = {
|
const postData = {
|
||||||
note_id,
|
note_id,
|
||||||
content,
|
content,
|
||||||
comment_id,
|
comment_id,
|
||||||
};
|
};
|
||||||
|
if (isOwner) {
|
||||||
|
// 如果是自己的笔记,且笔记不包含 @信息 则不需要AI回复,
|
||||||
|
const hasAt = content.includes('@' + note_username) || content.includes('@' + note_userid);
|
||||||
|
if (!hasAt) {
|
||||||
|
// console.log('不需要AI回复自己的笔记', note_username, note_id, content);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
content = content.replace('@' + note_username, '');
|
||||||
|
|
||||||
const resAgent = await agent.call({
|
const resAgent = await agent.call({
|
||||||
path: 'xhs',
|
path: 'xhs',
|
||||||
payload: {
|
payload: {
|
||||||
|
@ -3,16 +3,23 @@
|
|||||||
import { QueryRouterServer } from '@kevisual/router';
|
import { QueryRouterServer } from '@kevisual/router';
|
||||||
import { redis } from '@/modules/redis.ts';
|
import { redis } from '@/modules/redis.ts';
|
||||||
import { Queue } from 'bullmq';
|
import { Queue } from 'bullmq';
|
||||||
import { app as xhsApp, xhsServices, xhsRootClient, XhsClient } from '@kevisual/xhs/index';
|
import { app as xhsApp, xhsServices as xhs, xhsRootClient, XhsServices } from '@kevisual/xhs/index.ts';
|
||||||
import { nanoid } from 'nanoid';
|
import { nanoid } from 'nanoid';
|
||||||
export const XHS_GET_UNREAD = 'unread_count';
|
export const XHS_GET_UNREAD = 'unread_count';
|
||||||
export const XHS_QUEUE_NAME = 'XHS_QUEUE';
|
export const XHS_QUEUE_NAME = 'XHS_QUEUE';
|
||||||
import { config } from '../modules/config.ts';
|
import { config } from '../modules/config.ts';
|
||||||
const xhsClient: XhsClient = xhsRootClient;
|
|
||||||
xhsClient.setCookie(config.XHS_ROOT_COOKIE || '');
|
const server: XhsServices = xhs;
|
||||||
console.log('XHS_ROOT_COOKIE', xhsClient.cookie);
|
server.setCookie(config.XHS_ROOT_COOKIE || '');
|
||||||
|
server.setUserInfo({
|
||||||
|
userid: config.XHS_USER_ID || '',
|
||||||
|
username: config.XHS_USER_NAME || '',
|
||||||
|
});
|
||||||
|
console.log('XHS_USER_INFO', config.XHS_USER_ID, config.XHS_USER_NAME, 'XHS_ROOT_COOKIE', config.XHS_ROOT_COOKIE);
|
||||||
export const taskApp = new QueryRouterServer();
|
export const taskApp = new QueryRouterServer();
|
||||||
export { xhsApp, xhsServices, xhsRootClient };
|
export { xhsApp };
|
||||||
|
export const xhsServices = server;
|
||||||
|
|
||||||
export const queue = new Queue(XHS_QUEUE_NAME, {
|
export const queue = new Queue(XHS_QUEUE_NAME, {
|
||||||
connection: redis,
|
connection: redis,
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user