add pause
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-04-06 01:18:25 +07:00
parent 9e6681da30
commit b771745c47
5 changed files with 106 additions and 10 deletions
+4 -3
View File
@@ -1,9 +1,10 @@
package db
const (
UserStateNone = UserState("")
UserStateGo = UserState("Go")
UserStateEat = UserState("Eat")
UserStateNone = UserState("")
UserStateGo = UserState("Go")
UserStateEat = UserState("Eat")
UserStatePause = UserState("Pause")
)
type UserState string
+32 -4
View File
@@ -2,12 +2,13 @@ package db
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
"time"
"valera/config"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type DB struct {
@@ -112,6 +113,19 @@ func (db *DB) SetStatusToChat(chatID int64, status UserState) error {
return err
}
func (db *DB) SetPause(chatID int64, duration time.Duration) error {
ctx := context.Background()
loc, _ := time.LoadLocation("Asia/Novosibirsk")
t := time.Now().In(loc).Add(+duration)
_, err := db.chatsColection.UpdateOne(
ctx,
bson.M{"chat_id": chatID},
bson.M{"$set": bson.M{"pause_until": t}},
)
return err
}
func (db *DB) GetStat(chatID int64, cron bool) (map[string]int, error) {
ctx := context.Background()
@@ -178,7 +192,21 @@ func (db *DB) GetStat(chatID int64, cron bool) (map[string]int, error) {
func (db *DB) GetAllChats() ([]int64, error) {
ctx := context.Background()
cursor, err := db.chatsColection.Find(ctx, bson.M{})
loc, _ := time.LoadLocation("Asia/Novosibirsk")
t := time.Now().In(loc)
cursor, err := db.chatsColection.Find(
ctx,
bson.M{
"$or": bson.A{
bson.M{
"pause_until": bson.M{"$lt": t},
},
bson.M{
"pause_until": bson.M{"$exists": false},
},
},
},
)
if err != nil {
return nil, err
}