mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-05-12 04:39:41 +08:00
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:
39
database/chat.go
Normal file
39
database/chat.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user