Skip to content

Commit 5e0255d

Browse files
authored
fix(captcha): handle negative remaining time (#62)
* fix(captcha): handle negative remaining time * chore: bump dependency
1 parent b3f575c commit 5e0255d

File tree

5 files changed

+58
-37
lines changed

5 files changed

+58
-37
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
FROM golang:1.21-bookworm AS builder
1+
FROM golang:1.22-bookworm AS builder
22

33
WORKDIR /app
44

55
COPY . .
66

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

99
FROM debian:bookworm-slim AS runtime
1010

captcha/answer.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ func (d *Dependencies) WaitForAnswer(ctx context.Context, m *tb.Message) {
8989
// Check if the answer is correct or not
9090
if answer != captcha.Answer {
9191
remainingTime := time.Until(captcha.Expiry)
92+
// If the current time is after the expiry time, we should return immediately.
93+
// We don't need to delete any message since sometimes those messages are a valid one,
94+
// and we are having a race due to network issues. This is not something that
95+
// we can easily fix, since network is not always consistent.
96+
if remainingTime < 0 {
97+
return
98+
}
99+
92100
wrongMsg, err := d.Bot.Send(
93101
ctx,
94102
m.Chat,

captcha/non.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ func (d *Dependencies) NonTextListener(ctx context.Context, m *tb.Message) {
7272

7373
// Check if the answer is a media
7474
remainingTime := time.Until(captcha.Expiry)
75+
// If the current time is after the expiry time, we should return immediately.
76+
// We don't need to delete any message since sometimes those messages are a valid one,
77+
// and we are having a race due to network issues. This is not something that
78+
// we can easily fix, since network is not always consistent.
79+
if remainingTime < 0 {
80+
return
81+
}
82+
7583
wrongMsg, err := d.Bot.Send(
7684
ctx,
7785
m.Chat,

go.mod

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
module github.com/teknologi-umum/captcha
22

3-
go 1.20
3+
go 1.22
44

55
require (
66
github.com/aldy505/asciitxt v0.0.2
77
github.com/allegro/bigcache/v3 v3.1.0
88
github.com/dgraph-io/badger/v4 v4.2.0
9-
github.com/getsentry/sentry-go v0.25.0
10-
github.com/go-chi/chi/v5 v5.0.11
9+
github.com/getsentry/sentry-go v0.27.0
10+
github.com/go-chi/chi/v5 v5.0.12
1111
github.com/goccy/go-yaml v1.9.5
1212
github.com/ilyakaznacheev/cleanenv v1.5.0
1313
github.com/jmoiron/sqlx v1.3.5
1414
github.com/joho/godotenv v1.5.1
1515
github.com/lib/pq v1.10.9
1616
github.com/pkg/errors v0.9.1
1717
github.com/rs/cors v1.10.1
18-
github.com/rs/zerolog v1.31.0
18+
github.com/rs/zerolog v1.32.0
1919
github.com/spf13/viper v1.13.0
2020
github.com/stretchr/testify v1.8.2
21-
github.com/unrolled/secure v1.13.0
22-
go.mongodb.org/mongo-driver v1.13.1
21+
github.com/unrolled/secure v1.14.0
22+
go.mongodb.org/mongo-driver v1.14.0
2323
)
2424

2525
require (
@@ -56,10 +56,10 @@ require (
5656
github.com/xdg-go/stringprep v1.0.4 // indirect
5757
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
5858
go.opencensus.io v0.23.0 // indirect
59-
golang.org/x/crypto v0.17.0 // indirect
60-
golang.org/x/net v0.17.0 // indirect
61-
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
62-
golang.org/x/sys v0.15.0 // indirect
59+
golang.org/x/crypto v0.21.0 // indirect
60+
golang.org/x/net v0.22.0 // indirect
61+
golang.org/x/sync v0.1.0 // indirect
62+
golang.org/x/sys v0.18.0 // indirect
6363
golang.org/x/text v0.14.0 // indirect
6464
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df // indirect
6565
google.golang.org/protobuf v1.33.0 // indirect

0 commit comments

Comments
 (0)