Skip to content

Commit

Permalink
fix bug - if you don't answer, you can forever get a question
Browse files Browse the repository at this point in the history
  • Loading branch information
kish1n committed Aug 19, 2024
1 parent 6e88284 commit e189d0f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
11 changes: 8 additions & 3 deletions internal/config/daily_question_time_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 15 additions & 1 deletion internal/service/handlers/daily_question_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down
36 changes: 30 additions & 6 deletions internal/service/handlers/daily_question_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand All @@ -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)
}
}()
}

Expand Down

0 comments on commit e189d0f

Please sign in to comment.