diff --git a/plugin/rsshub/domain/job.go b/plugin/rsshub/domain/job.go index 078523fa..2b3bc34b 100644 --- a/plugin/rsshub/domain/job.go +++ b/plugin/rsshub/domain/job.go @@ -13,7 +13,7 @@ import ( // 1. 获取所有频道 // 2. 遍历所有频道,检查频道是否更新 // 3. 如果更新,获取更新的内容,但是返回的数据 -func (repo *rssDomain) syncRss(ctx context.Context) (updated map[int64]*RssClientView, err error) { +func (repo *RssDomain) syncRss(ctx context.Context) (updated map[int64]*RssClientView, err error) { updated = make(map[int64]*RssClientView) // 获取所有频道 sources, err := repo.storage.GetSources(ctx) @@ -73,7 +73,7 @@ func (repo *rssDomain) syncRss(ctx context.Context) (updated map[int64]*RssClien } // checkSourceNeedUpdate 检查频道是否需要更新 -func (repo *rssDomain) checkSourceNeedUpdate(ctx context.Context, source *RssSource) (needUpdate bool, err error) { +func (repo *RssDomain) checkSourceNeedUpdate(ctx context.Context, source *RssSource) (needUpdate bool, err error) { var sourceInDB *RssSource sourceInDB, err = repo.storage.GetSourceByRssHubFeedLink(ctx, source.RssHubFeedPath) if err != nil { @@ -92,7 +92,7 @@ func (repo *rssDomain) checkSourceNeedUpdate(ctx context.Context, source *RssSou } // processContentsUpdate 处理内容(s)更新 -func (repo *rssDomain) processContentsUpdate(ctx context.Context, cv *RssClientView, updateChannelView *RssClientView) error { +func (repo *RssDomain) processContentsUpdate(ctx context.Context, cv *RssClientView, updateChannelView *RssClientView) error { var err error for _, content := range cv.Contents { if content == nil { @@ -115,7 +115,7 @@ func (repo *rssDomain) processContentsUpdate(ctx context.Context, cv *RssClientV } // processContentItemUpdate 处理单个内容更新 -func (repo *rssDomain) processContentItemUpdate(ctx context.Context, content *RssContent) (existed bool, err error) { +func (repo *RssDomain) processContentItemUpdate(ctx context.Context, content *RssContent) (existed bool, err error) { existed, err = repo.storage.IsContentHashIDExist(ctx, content.HashID) if err != nil { return diff --git a/plugin/rsshub/domain/rssHub.go b/plugin/rsshub/domain/rssHub.go index 1f652032..4efcbe29 100644 --- a/plugin/rsshub/domain/rssHub.go +++ b/plugin/rsshub/domain/rssHub.go @@ -12,31 +12,17 @@ import ( ) // RssDomain RssRepo定义 -type RssDomain interface { - // Subscribe 订阅Rss频道 - Subscribe(ctx context.Context, gid int64, route string) (rv *RssClientView, isChannelExisted, - isSubExisted bool, err error) - // Unsubscribe 取消订阅Rss频道 - Unsubscribe(ctx context.Context, gid int64, route string) (err error) - // GetSubscribedChannelsByGroupID 获取群组订阅的Rss频道 - GetSubscribedChannelsByGroupID(ctx context.Context, gid int64) (rv []*RssClientView, err error) - // Sync 同步Rss频道 - // 返回群组-频道推送视图 map[群组]推送内容数组 - Sync(ctx context.Context) (groupView map[int64][]*RssClientView, err error) -} - -// rssDomain RssRepo定义 -type rssDomain struct { - storage RepoStorage +type RssDomain struct { + storage *repoStorage rssHubClient *RssHubClient } // NewRssDomain 新建RssDomain,调用方保证单例模式 -func NewRssDomain(dbPath string) (RssDomain, error) { +func NewRssDomain(dbPath string) (*RssDomain, error) { return newRssDomain(dbPath) } -func newRssDomain(dbPath string) (*rssDomain, error) { +func newRssDomain(dbPath string) (*RssDomain, error) { if _, err := os.Stat(dbPath); err != nil || os.IsNotExist(err) { // 生成文件 f, err := os.Create(dbPath) @@ -50,7 +36,7 @@ func newRssDomain(dbPath string) (*rssDomain, error) { logrus.Errorf("[rsshub NewRssDomain] open db error: %v", err) panic(err) } - repo := &rssDomain{ + repo := &RssDomain{ storage: &repoStorage{orm: orm}, rssHubClient: &RssHubClient{Client: http.DefaultClient}, } @@ -63,7 +49,7 @@ func newRssDomain(dbPath string) (*rssDomain, error) { } // Subscribe QQ群订阅Rss频道 -func (repo *rssDomain) Subscribe(ctx context.Context, gid int64, feedPath string) ( +func (repo *RssDomain) Subscribe(ctx context.Context, gid int64, feedPath string) ( rv *RssClientView, isChannelExisted, isSubExisted bool, err error) { // 验证 feed, err := repo.rssHubClient.FetchFeed(feedPath) @@ -118,7 +104,7 @@ func (repo *rssDomain) Subscribe(ctx context.Context, gid int64, feedPath string } // Unsubscribe 群组取消订阅 -func (repo *rssDomain) Unsubscribe(ctx context.Context, gid int64, feedPath string) (err error) { +func (repo *RssDomain) Unsubscribe(ctx context.Context, gid int64, feedPath string) (err error) { existedSubscribes, ifExisted, err := repo.storage.GetIfExistedSubscribe(ctx, gid, feedPath) if err != nil { logrus.WithContext(ctx).Errorf("[rsshub Subscribe] query sub by route error: %v", err) @@ -153,7 +139,7 @@ func (repo *rssDomain) Unsubscribe(ctx context.Context, gid int64, feedPath stri } // GetSubscribedChannelsByGroupID 获取群对应的订阅的频道信息 -func (repo *rssDomain) GetSubscribedChannelsByGroupID(ctx context.Context, gid int64) ([]*RssClientView, error) { +func (repo *RssDomain) GetSubscribedChannelsByGroupID(ctx context.Context, gid int64) ([]*RssClientView, error) { channels, err := repo.storage.GetSubscribedChannelsByGroupID(ctx, gid) if err != nil { logrus.WithContext(ctx).Errorf("[rsshub GetSubscribedChannelsByGroupID] GetSubscribedChannelsByGroupID error: %v", err) @@ -170,7 +156,7 @@ func (repo *rssDomain) GetSubscribedChannelsByGroupID(ctx context.Context, gid i } // Sync 同步任务,按照群组订阅情况做好map切片 -func (repo *rssDomain) Sync(ctx context.Context) (groupView map[int64][]*RssClientView, err error) { +func (repo *RssDomain) Sync(ctx context.Context) (groupView map[int64][]*RssClientView, err error) { groupView = make(map[int64][]*RssClientView) // 获取所有Rss频道 // 获取所有频道 diff --git a/plugin/rsshub/domain/storageImpl.go b/plugin/rsshub/domain/storageImpl.go deleted file mode 100644 index 842411de..00000000 --- a/plugin/rsshub/domain/storageImpl.go +++ /dev/null @@ -1,47 +0,0 @@ -package domain - -import "context" - -// RepoContent RSS 推送信息存储接口 -type RepoContent interface { - // UpsertContent 添加一条文章 - UpsertContent(ctx context.Context, content *RssContent) error - // DeleteSourceContents 删除订阅源的所有文章,返回被删除的文章数 - DeleteSourceContents(ctx context.Context, channelID int64) (int64, error) - // IsContentHashIDExist hash id 对应的文章是否已存在 - IsContentHashIDExist(ctx context.Context, hashID string) (bool, error) -} - -// RepoSource RSS 订阅源存储接口 -type RepoSource interface { - // UpsertSource 添加一个订阅源 - UpsertSource(ctx context.Context, rfc *RssSource) error - // GetSources 获取所有订阅源信息 - GetSources(ctx context.Context) ([]RssSource, error) - // GetSourceByRssHubFeedLink 通过 rssHub 的 feed 链接获取订阅源信息 - GetSourceByRssHubFeedLink(ctx context.Context, url string) (*RssSource, error) - // DeleteSource 删除一个订阅源 - DeleteSource(ctx context.Context, fID int64) error -} - -// RepoSubscribe RSS 订阅存储接口 -type RepoSubscribe interface { - // CreateSubscribe 添加一个订阅 - CreateSubscribe(ctx context.Context, gid, rssSourceID int64) error - // DeleteSubscribe 删除一个订阅 - DeleteSubscribe(ctx context.Context, subscribeID int64) error - // GetSubscribeByID 获取一个订阅 - GetSubscribeByID(ctx context.Context, gid int64, subscribeID int64) (*RssSubscribe, error) - // GetSubscribes 获取全部订阅 - GetSubscribes(ctx context.Context) ([]*RssSubscribe, error) -} - -// RepoMultiQuery 多表查询接口 -type RepoMultiQuery interface { - // GetSubscribesBySource 获取一个源对应的所有订阅群组 - GetSubscribesBySource(ctx context.Context, feedPath string) ([]*RssSubscribe, error) - // GetIfExistedSubscribe 判断一个群组是否已订阅了一个源 - GetIfExistedSubscribe(ctx context.Context, gid int64, feedPath string) (*RssSubscribe, bool, error) - // GetSubscribedChannelsByGroupID 获取该群所有的订阅 - GetSubscribedChannelsByGroupID(ctx context.Context, gid int64) ([]*RssSource, error) -} diff --git a/plugin/rsshub/domain/storageRepo.go b/plugin/rsshub/domain/storageRepo.go index 8698a8d9..e4c4904b 100644 --- a/plugin/rsshub/domain/storageRepo.go +++ b/plugin/rsshub/domain/storageRepo.go @@ -10,15 +10,6 @@ import ( "github.com/sirupsen/logrus" ) -// RepoStorage 定义RepoStorage接口 -type RepoStorage interface { - RepoContent - RepoSource - RepoSubscribe - RepoMultiQuery - initDB() error -} - // repoStorage db struct for rss type repoStorage struct { orm *gorm.DB diff --git a/plugin/rsshub/main.go b/plugin/rsshub/main.go index ff4cce10..b2d9aca5 100644 --- a/plugin/rsshub/main.go +++ b/plugin/rsshub/main.go @@ -18,7 +18,7 @@ import ( // 初始化 repo var ( - rssRepo domain.RssDomain + rssRepo *domain.RssDomain initErr error regexpForSQL = regexp.MustCompile(`[\^<>\[\]%&\*\(\)\{\}\|\=]|(union\s+select|update\s+|delete\s+|drop\s+|truncate\s+|insert\s+|exec\s+|declare\s+)`) ) @@ -28,11 +28,11 @@ var ( engine = control.Register("rsshub", &ctrl.Options[*zero.Ctx]{ // 默认不启动 DisableOnDefault: false, - Brief: "RssHub订阅姬", + Brief: "rsshub订阅姬", // 详细帮助 - Help: "RssHub订阅姬desu~ \n" + + Help: "rsshub订阅姬desu~ \n" + "支持的详细订阅列表文档可见:\n" + - "https://rsshub.netlify.app/ \n" + + "https://rsshub.netlify.app/zh/ \n" + "- 添加rsshub订阅-/bookfere/weekly \n" + "- 删除rsshub订阅-/bookfere/weekly \n" + "- 查看rsshub订阅列表 \n" + @@ -43,10 +43,10 @@ var ( // 插件数据存储路径 PrivateDataFolder: "rsshub", OnEnable: func(ctx *zero.Ctx) { - ctx.SendChain(message.Text("RssHub订阅姬现在启动了哦")) + ctx.SendChain(message.Text("rsshub订阅姬现在启动了哦")) }, OnDisable: func(ctx *zero.Ctx) { - ctx.SendChain(message.Text("RssHub订阅姬现在关闭了哦")) + ctx.SendChain(message.Text("rsshub订阅姬现在关闭了哦")) }, }).ApplySingle(zbpCtxExt.DefaultSingle) ) @@ -55,7 +55,7 @@ var ( func init() { rssRepo, initErr = domain.NewRssDomain(engine.DataFolder() + "rsshub.db") if initErr != nil { - logrus.Errorln("RssHub订阅姬:初始化失败", initErr) + logrus.Errorln("rsshub订阅姬:初始化失败", initErr) panic(initErr) } engine.OnFullMatch("rsshub同步", zero.OnlyGroup).SetBlock(true).Handle(func(ctx *zero.Ctx) { @@ -80,18 +80,18 @@ func init() { logrus.Debugf("添加rsshub订阅:raw(%s), replaced(%s)", routeStr, input) rv, _, isSubExisted, err := rssRepo.Subscribe(context.Background(), ctx.Event.GroupID, input) if err != nil { - ctx.SendChain(message.Text("RssHub订阅姬:添加失败", err.Error())) + ctx.SendChain(message.Text("rsshub订阅姬:添加失败", err.Error())) return } if isSubExisted { - ctx.SendChain(message.Text("RssHub订阅姬:已存在,更新成功")) + ctx.SendChain(message.Text("rsshub订阅姬:已存在,更新成功")) } else { - ctx.SendChain(message.Text("RssHub订阅姬:添加成功\n", rv.Source.Title)) + ctx.SendChain(message.Text("rsshub订阅姬:添加成功\n", rv.Source.Title)) } // 添加成功,发送订阅源快照 msg, err := newRssDetailsMsg(ctx, rv) if len(msg) == 0 || err != nil { - ctx.SendPrivateMessage(zero.BotConfig.SuperUsers[0], message.Text("RssHub推送错误", err)) + ctx.SendPrivateMessage(zero.BotConfig.SuperUsers[0], message.Text("rsshub推送错误", err)) return } if id := ctx.Send(msg).ID(); id == 0 { @@ -104,21 +104,21 @@ func init() { logrus.Debugf("删除rsshub订阅:raw(%s), replaced(%s)", routeStr, input) err := rssRepo.Unsubscribe(context.Background(), ctx.Event.GroupID, input) if err != nil { - ctx.SendChain(message.Text("RssHub订阅姬:删除失败 ", err.Error())) + ctx.SendChain(message.Text("rsshub订阅姬:删除失败 ", err.Error())) return } - ctx.SendChain(message.Text(fmt.Sprintf("RssHub订阅姬:删除%s成功", input))) + ctx.SendChain(message.Text(fmt.Sprintf("rsshub订阅姬:删除%s成功", input))) }) engine.OnFullMatch("查看rsshub订阅列表", zero.OnlyGroup).SetBlock(true).Handle(func(ctx *zero.Ctx) { rv, err := rssRepo.GetSubscribedChannelsByGroupID(context.Background(), ctx.Event.GroupID) if err != nil { - ctx.SendChain(message.Text("RssHub订阅姬:查询失败 ", err.Error())) + ctx.SendChain(message.Text("rsshub订阅姬:查询失败 ", err.Error())) return } // 添加成功,发送订阅源信息 msg, err := newRssSourcesMsg(ctx, rv) if err != nil { - ctx.SendChain(message.Text("RssHub订阅姬:查询失败 ", err.Error())) + ctx.SendChain(message.Text("rsshub订阅姬:查询失败 ", err.Error())) return } if len(msg) == 0 { @@ -132,7 +132,7 @@ func init() { // sendRssUpdateMsg 发送Rss更新消息 func sendRssUpdateMsg(ctx *zero.Ctx, groupToFeedsMap map[int64][]*domain.RssClientView) { for groupID, views := range groupToFeedsMap { - logrus.Infof("RssHub插件在群 %d 触发推送检查", groupID) + logrus.Infof("rsshub插件在群 %d 触发推送检查", groupID) for _, view := range views { if view == nil || len(view.Contents) == 0 { continue @@ -142,8 +142,8 @@ func sendRssUpdateMsg(ctx *zero.Ctx, groupToFeedsMap map[int64][]*domain.RssClie ctx.SendPrivateMessage(zero.BotConfig.SuperUsers[0], message.Text(rssHubPushErrMsg, err)) continue } - logrus.Infof("RssHub插件在群 %d 开始推送 %s", groupID, view.Source.Title) - ctx.SendGroupMessage(groupID, message.Text(fmt.Sprintf("%s\n该RssHub频道下有更新了哦~", view.Source.Title))) + logrus.Infof("rsshub插件在群 %d 开始推送 %s", groupID, view.Source.Title) + ctx.SendGroupMessage(groupID, message.Text(fmt.Sprintf("%s\n该rsshub频道下有更新了哦~", view.Source.Title))) if res := ctx.SendGroupForwardMessage(groupID, msg); !res.Exists() { ctx.SendPrivateMessage(zero.BotConfig.SuperUsers[0], message.Text(rssHubPushErrMsg)) }