mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-05-11 17: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:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user