feat: implement watch for monitoring chat messages

- Added a new command handler for /watch that allows users to listen to messages from a specified chat and save them according to storage rules.
- Introduced filtering options for messages using regular expressions.
- Implemented functionality to start and stop watching chats, including error handling for invalid inputs and user settings.
- Created a new utility package for message element handling related to the watch feature.
- Updated the user model to manage watched chats, including methods to add, remove, and check if a chat is being watched.
This commit is contained in:
krau
2025-08-03 16:55:56 +08:00
parent 8f9ef07d1c
commit 133453b5d4
14 changed files with 377 additions and 69 deletions

39
database/chat.go Normal file
View File

@@ -0,0 +1,39 @@
package database
import "context"
func (user *User) WatchChat(ctx context.Context, chat WatchChat) error {
if user.WatchChats == nil {
user.WatchChats = make([]WatchChat, 0)
}
user.WatchChats = append(user.WatchChats, chat)
return db.WithContext(ctx).Save(user.WatchChats).Error
}
func (user *User) UnwatchChat(ctx context.Context, chatID int64) error {
var watchChat WatchChat
err := db.WithContext(ctx).Where("chat_id = ? AND user_id = ?", chatID, user.ID).First(&watchChat).Error
if err != nil {
return err
}
return db.WithContext(ctx).Unscoped().Delete(&watchChat).Error
}
func (user *User) WatchingChat(ctx context.Context, chatID int64) (bool, error) {
var count int64
err := db.WithContext(ctx).Model(&WatchChat{}).Where("chat_id = ? AND user_id = ?", chatID, user.ID).Count(&count).Error
if err != nil {
return false, err
}
return count > 0, nil
}
func GetWatchChatsByChatID(ctx context.Context, chatID int64) ([]*WatchChat, error) {
var watchChats []*WatchChat
err := db.WithContext(ctx).Where("chat_id = ?", chatID).Find(&watchChats).Error
if err != nil {
return nil, err
}
return watchChats, nil
}

View File

@@ -37,7 +37,7 @@ func Init(ctx context.Context) {
logger.Fatal("Failed to open database: ", err)
}
logger.Debug("Database connected")
if err := db.AutoMigrate(&User{}, &Dir{}, &Rule{}); err != nil {
if err := db.AutoMigrate(&User{}, &Dir{}, &Rule{}, &WatchChat{}); err != nil {
logger.Fatal("迁移数据库失败, 如果您从旧版本升级, 建议手动删除数据库文件后重试: ", err)
}
if err := syncUsers(ctx); err != nil {

View File

@@ -12,6 +12,14 @@ type User struct {
Dirs []Dir
ApplyRule bool
Rules []Rule
WatchChats []WatchChat
}
type WatchChat struct {
gorm.Model
UserID uint // User's database ID (not chat ID)
ChatID int64
Filter string
}
type Dir struct {

View File

@@ -1,6 +1,10 @@
package database
import "context"
import (
"context"
"gorm.io/gorm/clause"
)
func CreateUser(ctx context.Context, chatID int64) error {
if _, err := GetUserByChatID(ctx, chatID); err == nil {
@@ -11,19 +15,16 @@ func CreateUser(ctx context.Context, chatID int64) error {
func GetAllUsers(ctx context.Context) ([]User, error) {
var users []User
err := db.Preload("Dirs").
WithContext(ctx).
Preload("Rules").
err := db.WithContext(ctx).
Preload(clause.Associations).
Find(&users).Error
return users, err
}
func GetUserByChatID(ctx context.Context, chatID int64) (*User, error) {
var user User
err := db.
Preload("Dirs").
WithContext(ctx).
Preload("Rules").
err := db.WithContext(ctx).
Preload(clause.Associations).
Where("chat_id = ?", chatID).First(&user).Error
return &user, err
}
@@ -36,5 +37,16 @@ func UpdateUser(ctx context.Context, user *User) error {
}
func DeleteUser(ctx context.Context, user *User) error {
return db.WithContext(ctx).Unscoped().Select("Dirs", "Rules").Delete(user).Error
return db.WithContext(ctx).
Unscoped().
Select(clause.Associations).
Delete(user).Error
}
func GetUserByID(ctx context.Context, id uint) (*User, error) {
var user User
err := db.WithContext(ctx).
Preload(clause.Associations).
Where("id = ?", id).First(&user).Error
return &user, err
}