Files
SaveAny-Bot/database/chat.go
krau 133453b5d4 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.
2025-08-03 16:55:56 +08:00

40 lines
1.1 KiB
Go

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
}