add last_time
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
d1f2c359a2
commit
ada1a97999
|
@ -19,7 +19,7 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
version = "v1.7.2"
|
||||
version = "v1.7.3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -71,14 +71,14 @@ func main() {
|
|||
text := update.Message.Text
|
||||
chatID := update.Message.Chat.ID
|
||||
username := update.Message.From.UserName
|
||||
userInfoDTO, err := dataBase.GetChatInfo(chatID)
|
||||
userInfoDTO, err := dataBase.GetChatInfo(chatID, username)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, s := range botStates {
|
||||
if err := s.Do(text, userInfoDTO, username); err != nil {
|
||||
if err := s.Do(text, userInfoDTO); err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,8 +14,9 @@ type Chat struct {
|
|||
}
|
||||
|
||||
type ChatInfo struct {
|
||||
ChatID int64 `bson:"chat_id"`
|
||||
Status string `bson:"status"`
|
||||
ChatID int64 `bson:"chat_id"`
|
||||
Status string `bson:"status"`
|
||||
Username string `bson:"username"`
|
||||
}
|
||||
|
||||
func (c *ChatInfo) GetStatus() UserState {
|
||||
|
|
|
@ -48,7 +48,17 @@ func (db *DB) AddChat(chatID int64) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
return result.Err()
|
||||
if result.Err() != nil {
|
||||
return result.Err()
|
||||
}
|
||||
loc, _ := time.LoadLocation("Asia/Novosibirsk")
|
||||
t := time.Now().In(loc)
|
||||
_, err := db.chatsColection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"chat_id": chatID},
|
||||
bson.M{"$set": bson.M{"last_time": t}},
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *DB) AddWorkout(chatID int64, workout *Workout) error {
|
||||
|
@ -87,14 +97,14 @@ func (db *DB) AddCalories(chatID int64, calories *Calories) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (db *DB) GetChatInfo(chatID int64) (*ChatInfo, error) {
|
||||
func (db *DB) GetChatInfo(chatID int64, username string) (*ChatInfo, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
if err := db.AddChat(chatID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
chatInfoDTO := &ChatInfo{}
|
||||
chatInfoDTO := &ChatInfo{Username: username}
|
||||
if err := db.chatsColection.FindOne(ctx, bson.M{"chat_id": chatID}).Decode(chatInfoDTO); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ func NewClearBotState(bot *tgbot.BotAPI, dataBase *db.DB) states.BotState {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *clearBotState) Do(text string, chatInfo *db.ChatInfo, username string) error {
|
||||
func (s *clearBotState) Do(text string, chatInfo *db.ChatInfo) error {
|
||||
if !slices.Contains(names, text) {
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -27,14 +27,14 @@ func NewEatBotState(bot *tgbot.BotAPI, dataBase *db.DB) states.BotState {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *eatBotState) Do(text string, chatInfo *db.ChatInfo, username string) error {
|
||||
func (s *eatBotState) Do(text string, chatInfo *db.ChatInfo) error {
|
||||
if chatInfo.Status == string(db.UserStateEat) {
|
||||
count, err := calcCalories(text)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return nil
|
||||
}
|
||||
if err := s.dataBase.AddCalories(chatInfo.ChatID, db.NewCalories(count, username)); err != nil {
|
||||
if err := s.dataBase.AddCalories(chatInfo.ChatID, db.NewCalories(count, chatInfo.Username)); err != nil {
|
||||
log.Println(err)
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ func NewGoBotState(bot *tgbot.BotAPI, dataBase *db.DB) states.BotState {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *goBotState) Do(text string, chatInfo *db.ChatInfo, username string) error {
|
||||
func (s *goBotState) Do(text string, chatInfo *db.ChatInfo) error {
|
||||
if chatInfo.Status == string(db.UserStateGo) {
|
||||
if err := s.dataBase.SetStatusToChat(chatInfo.ChatID, db.UserState(text)); err != nil {
|
||||
log.Println(err)
|
||||
|
@ -57,7 +57,7 @@ func (s *goBotState) Do(text string, chatInfo *db.ChatInfo, username string) err
|
|||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
if err := s.dataBase.AddWorkout(chatInfo.ChatID, db.NewWorkout(workoutType, count, username)); err != nil {
|
||||
if err := s.dataBase.AddWorkout(chatInfo.ChatID, db.NewWorkout(workoutType, count, chatInfo.Username)); err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ func NewHelpBotState(bot *tgbot.BotAPI, dataBase *db.DB) states.BotState {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *helpBotState) Do(text string, chatInfo *db.ChatInfo, username string) error {
|
||||
func (s *helpBotState) Do(text string, chatInfo *db.ChatInfo) error {
|
||||
if !slices.Contains(names, text) {
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -6,6 +6,6 @@ import (
|
|||
)
|
||||
|
||||
type BotState interface {
|
||||
Do(text string, chatInfo *db.ChatInfo, username string) error
|
||||
Do(text string, chatInfo *db.ChatInfo) error
|
||||
GetHandler() (string, func(http.ResponseWriter, *http.Request))
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ func NewPauseBotState(bot *tgbot.BotAPI, dataBase *db.DB) states.BotState {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *pauseBotState) Do(text string, chatInfo *db.ChatInfo, username string) error {
|
||||
func (s *pauseBotState) Do(text string, chatInfo *db.ChatInfo) error {
|
||||
if chatInfo.Status == string(db.UserStatePause) {
|
||||
duration, err := getDuration(text)
|
||||
if err != nil {
|
||||
|
|
|
@ -25,7 +25,7 @@ func NewPingBotState(bot *tgbot.BotAPI, dataBase *db.DB) states.BotState {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *pingBotState) Do(text string, chatInfo *db.ChatInfo, username string) error {
|
||||
func (s *pingBotState) Do(text string, chatInfo *db.ChatInfo) error {
|
||||
if !slices.Contains(names, text) {
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ func NewStartBotState(bot *tgbot.BotAPI, dataBase *db.DB, version string) states
|
|||
}
|
||||
}
|
||||
|
||||
func (s *startBotState) Do(text string, chatInfo *db.ChatInfo, username string) error {
|
||||
func (s *startBotState) Do(text string, chatInfo *db.ChatInfo) error {
|
||||
if !slices.Contains(names, text) {
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ func NewStatBotState(bot *tgbot.BotAPI, dataBase *db.DB) states.BotState {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *statBotState) Do(text string, chatInfo *db.ChatInfo, username string) error {
|
||||
func (s *statBotState) Do(text string, chatInfo *db.ChatInfo) error {
|
||||
if !slices.Contains(names, text) {
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue