Skip to content

Let's use AI to Earn!

feat: 改回复评论的逻辑,为记录模式

牛文政39d7b8f64dda
diff --git a/electron/db/models/replyCommentRecord.ts b/electron/db/models/replyCommentRecord.ts
new file mode 100644
index 00000000..85ae2711
--- /dev/null
+++ b/electron/db/models/replyCommentRecord.ts
@@ -0,0 +1,39 @@
+/*
+ * @Author: nevin
+ * @Date: 2025-01-20 16:24:16
+ * @LastEditTime: 2025-03-23 09:27:54
+ * @LastEditors: nevin
+ * @Description: 回复评论的记录 replyCommentRecord
+ */
+import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
+import { TempModel } from './temp';
+import { AccountType } from '../../../commont/AccountEnum';
+
+@Entity({ name: 'replyCommentRecord' })
+export class ReplyCommentRecordModel extends TempModel {
+ @PrimaryGeneratedColumn({ type: 'int', comment: 'id' })
+ id!: number;
+
+ @Column({ type: 'varchar', nullable: false, comment: '用户id' })
+ userId!: string;
+
+ @Column({ type: 'int', nullable: false, comment: '账号id,对应account表id' })
+ accountId!: number;
+
+ @Column({
+ type: 'varchar',
+ enum: AccountType,
+ nullable: true,
+ comment: '平台类型',
+ })
+ type!: AccountType;
+
+ @Column({ type: 'varchar', nullable: false, comment: '评论Id' })
+ commentId!: string;
+
+ @Column({ type: 'varchar', nullable: false, comment: '评论内容' })
+ commentContent!: string;
+
+ @Column({ type: 'varchar', nullable: false, comment: '回复的内容' })
+ replyContent!: string;
+}
diff --git a/electron/main/reply/service.ts b/electron/main/reply/service.ts
index f53b8c4a..02318ce5 100644
--- a/electron/main/reply/service.ts
+++ b/electron/main/reply/service.ts
@@ -13,20 +13,65 @@ import { AutoRunService } from '../autoRun/service';
import { AutoRunModel } from '../../db/models/autoRun';
import { sysNotice } from '../../global/notice';
import { AutorReplyCommentScheduleEvent } from '../../../commont/types/reply';
+import { Repository } from 'typeorm';
+import { ReplyCommentRecordModel } from '../../db/models/replyCommentRecord';
+import { AppDataSource } from '../../db';
+import { getUserInfo } from '../user/comment';
@Injectable()
export class ReplyService {
replyQueue: PQueue;
+ private replyCommentRecordRepository: Repository<ReplyCommentRecordModel>;
+
constructor() {
this.replyQueue = new PQueue({ concurrency: 2 });
+ this.replyCommentRecordRepository = AppDataSource.getRepository(
+ ReplyCommentRecordModel,
+ );
}
@Inject(AutoRunService)
private readonly autoRunService!: AutoRunService;
+ // 创建评论记录
+ async create(
+ userId: string,
+ account: AccountModel,
+ comment: {
+ id: string;
+ commentContent: string;
+ replyContent: string;
+ },
+ ) {
+ return await this.replyCommentRecordRepository.save({
+ userId,
+ accountId: account.id,
+ type: account.type,
+ commentId: comment.id + '',
+ commentContent: comment.commentContent,
+ replyContent: comment.replyContent,
+ });
+ }
+
+ // 获取平台的评论记录
+ async getReplyCommentRecord(
+ userId: string,
+ account: AccountModel,
+ commentId: string,
+ ) {
+ return await this.replyCommentRecordRepository.findOne({
+ where: {
+ userId,
+ accountId: account.id,
+ type: account.type,
+ commentId: commentId + '',
+ },
+ });
+ }
+
/**
* 自动一键评论
- * 规则:评论所有的一级评论,已有自己的评论的不评论
+ * 规则:评论所有的一级评论,已经在评论记录的不评论
*/
async autorReplyComment(
account: AccountModel,
@@ -38,6 +83,7 @@ export class ReplyService {
error?: any;
}) => void,
) {
+ const userInfo = getUserInfo();
// 1. 获取作品的评论列表, 如果返回
let theHasMore = true;
let thePcursor = undefined;
@@ -51,7 +97,7 @@ export class ReplyService {
while (theHasMore) {
scheduleEvent({
tag: AutorReplyCommentScheduleEvent.GetCommentListStart,
- status: 1,
+ status: 0,
});
const {
list,
@@ -73,59 +119,74 @@ export class ReplyService {
// 2. 循环AI回复评论
for (const element of list) {
- if (account.uid !== element.userId) {
- let hadReply = false;
- for (const reply of element.subCommentList) {
- if (account.uid === reply.userId) {
- hadReply = true;
- break;
- }
+ const oldRecord = await this.getReplyCommentRecord(
+ userInfo.id,
+ account,
+ element.commentId,
+ );
+ if (oldRecord) continue;
+
+ let hadReply = false;
+ for (const reply of element.subCommentList) {
+ if (account.uid === reply.userId) {
+ hadReply = true;
+ break;
}
+ }
- if (!hadReply) {
- const aiRes = await toolsApi.aiRecoverReview({
- content: element.content,
+ if (!hadReply) {
+ const aiRes = await toolsApi.aiRecoverReview({
+ content: element.content,
+ });
+
+ // AI接口错误
+ if (!aiRes) {
+ scheduleEvent({
+ tag: AutorReplyCommentScheduleEvent.Error,
+ status: -1,
+ error: '未获得AI产出内容',
});
+ return false;
+ }
+ scheduleEvent({
+ tag: AutorReplyCommentScheduleEvent.ReplyCommentStart,
+ data: {
+ content: element.content,
+ aiContent: aiRes,
+ },
+ status: 0,
+ });
+
+ const replyRes = await platController.replyComment(
+ account,
+ element.commentId,
+ aiRes,
+ {
+ dataId,
+ comment: element,
+ },
+ );
+
+ // 错误处理
+ if (!replyRes) {
scheduleEvent({
- tag: AutorReplyCommentScheduleEvent.ReplyCommentStart,
- data: element,
+ tag: AutorReplyCommentScheduleEvent.ReplyCommentEnd,
+ status: -1,
+ error: '回复评论失败',
+ });
+ } else {
+ scheduleEvent({
+ tag: AutorReplyCommentScheduleEvent.ReplyCommentEnd,
status: 0,
});
- // AI接口错误
- if (!aiRes) {
- scheduleEvent({
- tag: AutorReplyCommentScheduleEvent.Error,
- status: -1,
- error: '未获得AI产出内容',
- });
- return false;
- }
-
- const replyRes = await platController.replyComment(
- account,
- element.commentId,
- aiRes,
- {
- dataId,
- comment: element,
- },
- );
-
- // 错误处理
- if (!replyRes) {
- scheduleEvent({
- tag: AutorReplyCommentScheduleEvent.ReplyCommentEnd,
- status: -1,
- error: '回复评论失败',
- });
- } else {
- scheduleEvent({
- tag: AutorReplyCommentScheduleEvent.ReplyCommentEnd,
- status: 0,
- });
- }
+ // 创建评论记录
+ this.create(userInfo.id, account, {
+ id: element.commentId,
+ commentContent: element.content,
+ replyContent: aiRes,
+ });
}
}
}
diff --git a/src/views/reply/components/oneKeyReply.tsx b/src/views/reply/components/oneKeyReply.tsx
index 9ce3fcdb..47f76e0d 100644
--- a/src/views/reply/components/oneKeyReply.tsx
+++ b/src/views/reply/components/oneKeyReply.tsx
@@ -53,8 +53,22 @@ const Com = forwardRef<OneKeyReplyRef>((props: any, ref) => {
error?: any;
},
) {
- const tagStr = AutorReplyCommentScheduleEventTagStrMap.get(args.tag) || '';
- setInfoText(`${tagStr}`);
+ switch (args.tag) {
+ case AutorReplyCommentScheduleEvent.Start:
+ setInfoText('开始评论');
+ break;
+ case AutorReplyCommentScheduleEvent.End:
+ setInfoText('评论结束');
+ break;
+ case AutorReplyCommentScheduleEvent.Error:
+ setErrorText(args.error);
+ break;
+ default:
+ const tagStr =
+ AutorReplyCommentScheduleEventTagStrMap.get(args.tag) || '';
+ setInfoText(`${tagStr}`);
+ break;
+ }
}
useImperativeHandle(ref, () => ({
@@ -75,8 +89,12 @@ const Com = forwardRef<OneKeyReplyRef>((props: any, ref) => {
footer={null}
width={400}
>
- <p>{infoText}</p>
- <p>{errorText}</p>
+ <div className="p-2 mb-4 border border-green-500 rounded-md shadow-md">
+ <p className="text-green-500">{infoText}</p>
+ </div>
+ <div className="p-2 mb-4 border border-red-500 rounded-md shadow-md">
+ <p className="text-red-500">{errorText}</p>
+ </div>
</Modal>
</>
);