From e189d0f811833b32772bba4edb21f1170c640e75 Mon Sep 17 00:00:00 2001 From: trpdjke Date: Tue, 20 Aug 2024 01:26:19 +0300 Subject: [PATCH] fix bug - if you don't answer, you can forever get a question --- internal/config/daily_question_time_hash.go | 11 ++++-- .../service/handlers/daily_question_check.go | 16 ++++++++- .../service/handlers/daily_question_get.go | 36 +++++++++++++++---- 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/internal/config/daily_question_time_hash.go b/internal/config/daily_question_time_hash.go index 4b64974..6075880 100644 --- a/internal/config/daily_question_time_hash.go +++ b/internal/config/daily_question_time_hash.go @@ -5,16 +5,21 @@ type DailyQuestionTimeHash interface { GetDailyQuestionsTimeHash() DailyQuestionsTimeHash } -type DailyQuestionsTimeHash map[string]int64 +type DailyQuestionTimeInfo struct { + MaxDateToAnswer int64 + Answered bool +} + +type DailyQuestionsTimeHash map[string]DailyQuestionTimeInfo -func (c DailyQuestionsTimeHash) SetDailyQuestionsTimeHash(key string, value int64) { +func (c DailyQuestionsTimeHash) SetDailyQuestionsTimeHash(key string, value DailyQuestionTimeInfo) { if c == nil { c = make(DailyQuestionsTimeHash) } (c)[key] = value } -func (c DailyQuestionsTimeHash) GetDailyQuestionsTimeHash(key string) *int64 { +func (c DailyQuestionsTimeHash) GetDailyQuestionsTimeHash(key string) *DailyQuestionTimeInfo { if c == nil { return nil } diff --git a/internal/service/handlers/daily_question_check.go b/internal/service/handlers/daily_question_check.go index 6e530fd..5710726 100644 --- a/internal/service/handlers/daily_question_check.go +++ b/internal/service/handlers/daily_question_check.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "net/http" + "time" "github.com/rarimo/geo-points-svc/internal/data" "github.com/rarimo/geo-points-svc/internal/data/evtypes" @@ -31,11 +32,22 @@ func CheckDailyQuestion(w http.ResponseWriter, r *http.Request) { // return //} - if exits := DailyQuestionTimeHash(r).GetDailyQuestionsTimeHash(req.Nullifier); exits == nil { + cell := DailyQuestionTimeHash(r).GetDailyQuestionsTimeHash(req.Nullifier) + if cell == nil { Log(r).Errorf("The user's nullifier was not found in active requests, it does not exist, or the user has already answered: %s", req.Nullifier) ape.RenderErr(w, problems.NotAllowed()) return } + if cell.MaxDateToAnswer < time.Now().UTC().Unix() { + Log(r).Infof("Time is up :%s", req.Nullifier) + ape.RenderErr(w, problems.Forbidden()) + return + } + if cell.Answered { + Log(r).Infof("User has already answered: %s", req.Nullifier) + ape.RenderErr(w, problems.Forbidden()) + return + } question, err := DailyQuestionsQ(r).FilterTodayQuestions().Get() if err != nil { @@ -103,6 +115,8 @@ func CheckDailyQuestion(w http.ResponseWriter, r *http.Request) { return nil } + cell.Answered = true + if answersMap[req.UserAnswer] != true { answerIsTrue = false Log(r).Infof("Anser wrong") diff --git a/internal/service/handlers/daily_question_get.go b/internal/service/handlers/daily_question_get.go index 5ec9bb0..ab3ff3b 100644 --- a/internal/service/handlers/daily_question_get.go +++ b/internal/service/handlers/daily_question_get.go @@ -8,7 +8,9 @@ import ( "time" "github.com/go-chi/chi" + "github.com/rarimo/geo-points-svc/internal/config" "github.com/rarimo/geo-points-svc/internal/data" + "github.com/rarimo/geo-points-svc/internal/data/evtypes/models" "github.com/rarimo/geo-points-svc/resources" "gitlab.com/distributed_lab/ape" "gitlab.com/distributed_lab/ape/problems" @@ -50,7 +52,24 @@ func GetDailyQuestion(w http.ResponseWriter, r *http.Request) { return } - SetDailyQuestionTimeWithExpiration(r, balance.Nullifier, time.Now().UTC().Unix(), question.TimeForAnswer) + questionEvent, err := EventsQ(r). + FilterTodayEvents(). + FilterByType(models.TypeDailyQuestion). + FilterByNullifier(nullifier). + Get() + + if err != nil { + Log(r).WithError(err).Error("Failed to get active questions") + ape.RenderErr(w, problems.InternalError()) + return + } + if questionEvent != nil { + Log(r).Infof("User already answered %s", nullifier) + ape.RenderErr(w, problems.Forbidden()) + return + } + + SetDailyQuestionTimeWithExpiration(r, balance.Nullifier, time.Now().UTC().Unix(), question.TimeForAnswer, false) ape.Render(w, NewDailyQuestion(*question)) return @@ -78,11 +97,14 @@ func NewDailyQuestion(question data.DailyQuestion) resources.DailyQuestionAttrib } } -func SetDailyQuestionTimeWithExpiration(r *http.Request, nullifier string, timestamp int64, duration int64) { +func SetDailyQuestionTimeWithExpiration(r *http.Request, nullifier string, timestamp int64, duration int64, status bool) { mu.Lock() defer mu.Unlock() - DailyQuestionTimeHash(r).SetDailyQuestionsTimeHash(nullifier, timestamp) + DailyQuestionTimeHash(r).SetDailyQuestionsTimeHash(nullifier, config.DailyQuestionTimeInfo{ + MaxDateToAnswer: timestamp + duration, + Answered: status, + }) Log(r).Infof("add %s %v, length q: %v, mapm %+v", nullifier, duration, len(DailyQuestionTimeHash(r)), DailyQuestionTimeHash(r)) go func() { @@ -91,9 +113,11 @@ func SetDailyQuestionTimeWithExpiration(r *http.Request, nullifier string, times mu.Lock() defer mu.Unlock() - delete(DailyQuestionTimeHash(r), nullifier) - Log(r).Infof("Removed entry for nullifier: %s after expiration", nullifier) - + info := DailyQuestionTimeHash(r).GetDailyQuestionsTimeHash(nullifier) + if info.Answered { + delete(DailyQuestionTimeHash(r), nullifier) + Log(r).Infof("Removed entry for nullifier: %s after expiration", nullifier) + } }() }