Let's use AI to Earn!
TypeScript auto-publishdouyindouyin-apielectron-appelectron-reactkuaishoukwaipublishedshipinhaotoolxiaohongshu
feat: 作品评论的回调通知
yikaJasonMic1a95d7e8f5b2
diff --git a/electron/main/reply/comment.ts b/electron/main/reply/comment.tsindex 411e3cec..3eda6d7b 100644--- a/electron/main/reply/comment.ts+++ b/electron/main/reply/comment.ts@@ -1,7 +1,17 @@/** @Author: nevin* @Date: 2025-01-21 21:12:52- * @LastEditTime: 2025-03-18 21:17:03+ * @LastEditTime: 2025-03-23 19:09:01* @LastEditors: nevin* @Description:*/+// 自动任务的进度标识 开始 获取评论列表开始 获取评论列表结束 评论开始 评论结束 结束 错误+enum AutorReplyCommentScheduleEvent {+ Start = 'start',+ GetCommentListStart = 'getCommentListStart',+ GetCommentListEnd = 'getCommentListEnd',+ ReplyCommentStart = 'replyCommentStart',+ ReplyCommentEnd = 'replyCommentEnd',+ End = 'end',+ Error = 'error',+}diff --git a/electron/main/reply/controller.ts b/electron/main/reply/controller.tsindex 5a4a8a01..53199340 100644--- a/electron/main/reply/controller.ts+++ b/electron/main/reply/controller.ts@@ -1,7 +1,7 @@/** @Author: nevin* @Date: 2025-01-20 22:02:54- * @LastEditTime: 2025-03-23 15:03:20+ * @LastEditTime: 2025-03-23 19:17:37* @LastEditors: nevin* @Description: reply Reply*/@@ -91,7 +91,18 @@ export class ReplyController {const account = await this.accountService.getAccountById(accountId);if (!account) return null;- const res = await this.replyService.autorReplyComment(account, dataId);+ const res = await this.replyService.autorReplyComment(+ account,+ dataId,+ (e: {+ tag: AutorReplyCommentScheduleEvent;+ status: -1 | 0 | 1;+ error?: any;+ }) => {+ console.log(e);+ // TODO: 推送消息+ },+ );return res;}diff --git a/electron/main/reply/service.ts b/electron/main/reply/service.tsindex ece1c479..4075cd1d 100644--- a/electron/main/reply/service.ts+++ b/electron/main/reply/service.ts@@ -6,7 +6,6 @@*/import { Injectable } from '../core/decorators';import PQueue from 'p-queue';-import { CommentData } from '../plat/plat.type';import { AccountModel } from '../../db/models/account';import platController from '../plat';import { toolsApi } from '../api/tools';@@ -22,38 +21,91 @@ export class ReplyService {* 自动一键评论* 规则:评论所有的一级评论,已有自己的评论的不评论*/- async autorReplyComment(account: AccountModel, dataId: string) {+ async autorReplyComment(+ account: AccountModel,+ dataId: string,+ scheduleEvent: (data: {+ tag: AutorReplyCommentScheduleEvent;+ status: -1 | 0 | 1;+ error?: any;+ }) => void,+ ) {// 1. 获取作品的评论列表, 如果返回let theHasMore = true;let thePcursor = undefined;- const list: CommentData[] = [];+ try {+ scheduleEvent({+ tag: AutorReplyCommentScheduleEvent.Start,+ status: 0,+ });- while (theHasMore) {- const {- list,- pageInfo: { pcursor, hasMore },- } = await platController.getCommentList(account, dataId, thePcursor);+ while (theHasMore) {+ scheduleEvent({+ tag: AutorReplyCommentScheduleEvent.GetCommentListStart,+ status: 1,+ });+ const {+ list,+ pageInfo: { pcursor, hasMore },+ } = await platController.getCommentList(account, dataId, thePcursor);- for (const item of list) list.push(item);+ scheduleEvent({+ tag: AutorReplyCommentScheduleEvent.GetCommentListEnd,+ status: 0,+ });- thePcursor = pcursor;- theHasMore = !!hasMore;- }+ for (const item of list) list.push(item);- // 2. 循环AI回复评论- for (const element of list) {- const aiRes = await toolsApi.aiRecoverReview({- content: element.content,- });+ if (list.length === 0) {+ scheduleEvent({+ tag: AutorReplyCommentScheduleEvent.End,+ status: 0,+ });+ return true;+ }- platController.replyComment(account, element.commentId, aiRes, {- dataId,- comment: element,+ scheduleEvent({+ tag: AutorReplyCommentScheduleEvent.ReplyCommentStart,+ status: 0,+ });++ // 2. 循环AI回复评论+ for (const element of list) {+ const aiRes = await toolsApi.aiRecoverReview({+ content: element.content,+ });++ platController.replyComment(account, element.commentId, aiRes, {+ dataId,+ comment: element,+ });+ }++ scheduleEvent({+ tag: AutorReplyCommentScheduleEvent.ReplyCommentEnd,+ status: 0,+ });++ thePcursor = pcursor;+ theHasMore = !!hasMore;++ if (!theHasMore) {+ scheduleEvent({+ tag: AutorReplyCommentScheduleEvent.End,+ status: 0,+ });+ return true;+ }+ }+ } catch (error) {+ scheduleEvent({+ tag: AutorReplyCommentScheduleEvent.Error,+ status: -1,+ error,});+ return false;}-- return true;}/**@@ -63,7 +115,17 @@ export class ReplyService {*/addReplyQueue(account: AccountModel, dataId: string) {this.replyQueue.add(() => {- this.autorReplyComment(account, dataId);+ this.autorReplyComment(+ account,+ dataId,+ (e: {+ tag: AutorReplyCommentScheduleEvent;+ status: -1 | 0 | 1;+ error?: any;+ }) => {+ // TODO: 调用自动任务的通知+ },+ );});}}