Skip to content

Commit

Permalink
fix(captcha): handle negative remaining time (#62)
Browse files Browse the repository at this point in the history
* fix(captcha): handle negative remaining time

* chore: bump dependency
  • Loading branch information
aldy505 authored Mar 29, 2024
1 parent b3f575c commit 5e0255d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 37 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
FROM golang:1.21-bookworm AS builder
FROM golang:1.22-bookworm AS builder

WORKDIR /app

COPY . .

RUN go build -o captcha-bot -ldflags="-X main.version=$(git rev-parse HEAD)" ./cmd/captcha
RUN go build -o captcha-bot -ldflags="-X main.version=$(git rev-parse HEAD)" ./cmd/captcha

FROM debian:bookworm-slim AS runtime

Expand Down
8 changes: 8 additions & 0 deletions captcha/answer.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ func (d *Dependencies) WaitForAnswer(ctx context.Context, m *tb.Message) {
// Check if the answer is correct or not
if answer != captcha.Answer {
remainingTime := time.Until(captcha.Expiry)
// If the current time is after the expiry time, we should return immediately.
// We don't need to delete any message since sometimes those messages are a valid one,
// and we are having a race due to network issues. This is not something that
// we can easily fix, since network is not always consistent.
if remainingTime < 0 {
return
}

wrongMsg, err := d.Bot.Send(
ctx,
m.Chat,
Expand Down
8 changes: 8 additions & 0 deletions captcha/non.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ func (d *Dependencies) NonTextListener(ctx context.Context, m *tb.Message) {

// Check if the answer is a media
remainingTime := time.Until(captcha.Expiry)
// If the current time is after the expiry time, we should return immediately.
// We don't need to delete any message since sometimes those messages are a valid one,
// and we are having a race due to network issues. This is not something that
// we can easily fix, since network is not always consistent.
if remainingTime < 0 {
return
}

wrongMsg, err := d.Bot.Send(
ctx,
m.Chat,
Expand Down
20 changes: 10 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
module github.com/teknologi-umum/captcha

go 1.20
go 1.22

require (
github.com/aldy505/asciitxt v0.0.2
github.com/allegro/bigcache/v3 v3.1.0
github.com/dgraph-io/badger/v4 v4.2.0
github.com/getsentry/sentry-go v0.25.0
github.com/go-chi/chi/v5 v5.0.11
github.com/getsentry/sentry-go v0.27.0
github.com/go-chi/chi/v5 v5.0.12
github.com/goccy/go-yaml v1.9.5
github.com/ilyakaznacheev/cleanenv v1.5.0
github.com/jmoiron/sqlx v1.3.5
github.com/joho/godotenv v1.5.1
github.com/lib/pq v1.10.9
github.com/pkg/errors v0.9.1
github.com/rs/cors v1.10.1
github.com/rs/zerolog v1.31.0
github.com/rs/zerolog v1.32.0
github.com/spf13/viper v1.13.0
github.com/stretchr/testify v1.8.2
github.com/unrolled/secure v1.13.0
go.mongodb.org/mongo-driver v1.13.1
github.com/unrolled/secure v1.14.0
go.mongodb.org/mongo-driver v1.14.0
)

require (
Expand Down Expand Up @@ -56,10 +56,10 @@ require (
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df // indirect
google.golang.org/protobuf v1.33.0 // indirect
Expand Down
Loading

0 comments on commit 5e0255d

Please sign in to comment.