From 4c1a12af4c63a82e1ddad274742922878d435a1f Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 21 Oct 2023 18:39:10 +0200 Subject: [PATCH] expiration checkj --- modules/telegram/telegram.go | 44 ++++++++++++++++++++---------------- modules/utils/globals.go | 2 +- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/modules/telegram/telegram.go b/modules/telegram/telegram.go index 5b20901..274c389 100644 --- a/modules/telegram/telegram.go +++ b/modules/telegram/telegram.go @@ -100,30 +100,36 @@ func IncomingMessagesHandler() { replyMessage = "The code you entered is invalid. Please check the code and try again." } else { // code found in cache + // check if code is expired + if tempVerify.ExpiredAt.Before(time.Now()) { + logger.AddSystemLog(rslogger.LogTypeWarning, "Received code which was expired from user: %s %s. Message: %s", update.Message.From.FirstName, update.Message.From.LastName, code) - var foundVerifiedUser structs.VerifiedUser - - database.DB.Where("chat_id = ?", update.Message.From.ID).First(&foundVerifiedUser) - - if foundVerifiedUser.UserId != "" { - // user already verified - - replyMessage = "You have already verified your account. You will continue to receive notifications. Type /unsubscribe to stop receiving notifications." - - logger.AddSystemLog(rslogger.LogTypeWarning, "User: %s %s has tried to verify his account with code %s, but has already verified his account", update.Message.From.FirstName, update.Message.From.LastName, code) + replyMessage = "The code you entered is expired. Please check the code and try again." } else { - // user not verified + var foundVerifiedUser structs.VerifiedUser - replyMessage = "You have successfully verified your account. You will now receive notifications. Type /unsubscribe to stop receiving notifications." + database.DB.Where("chat_id = ?", update.Message.From.ID).First(&foundVerifiedUser) - database.DB.Create(&structs.VerifiedUser{ - UserId: tempVerify.UserId, - ChatId: update.Message.From.ID, - Filter: utils.NotificationTypeError, - CreatedAt: time.Now(), - }) + if foundVerifiedUser.UserId != "" { + // user already verified - logger.AddSystemLog(rslogger.LogTypeInfo, "User: %s %s has subscribed to receive notifications", update.Message.From.FirstName, update.Message.From.LastName) + replyMessage = "You have already verified your account. You will continue to receive notifications. Type /unsubscribe to stop receiving notifications." + + logger.AddSystemLog(rslogger.LogTypeWarning, "User: %s %s has tried to verify his account with code %s, but has already verified his account", update.Message.From.FirstName, update.Message.From.LastName, code) + } else { + // user not verified + + replyMessage = "You have successfully verified your account. You will now receive notifications. Type /unsubscribe to stop receiving notifications." + + database.DB.Create(&structs.VerifiedUser{ + UserId: tempVerify.UserId, + ChatId: update.Message.From.ID, + Filter: utils.NotificationTypeError, + CreatedAt: time.Now(), + }) + + logger.AddSystemLog(rslogger.LogTypeInfo, "User: %s %s has subscribed to receive notifications", update.Message.From.FirstName, update.Message.From.LastName) + } } cache.RemoveTempVerifyCode(tempVerify.UserId) diff --git a/modules/utils/globals.go b/modules/utils/globals.go index 4be2db9..8a88b01 100644 --- a/modules/utils/globals.go +++ b/modules/utils/globals.go @@ -6,7 +6,7 @@ import ( const ( VerifyCodeLength = 6 - TempVerifyCodeExpirationTime = 5 * time.Minute + TempVerifyCodeExpirationTime = 10 * time.Minute // also change on admin dashboard web NotificationIconSuccess = "🟢" NotificationSymbolInfo = "🔵"