Skip to content

Commit

Permalink
fix(reminder): disallow reminder in the past
Browse files Browse the repository at this point in the history
Signed-off-by: Reinaldy Rafli <[email protected]>
  • Loading branch information
aldy505 committed Dec 26, 2023
1 parent c603e00 commit cb06c98
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 27 deletions.
18 changes: 12 additions & 6 deletions cmd/captcha/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,18 @@ func main() {

// Setup Sentry for error handling.
err = sentry.Init(sentry.ClientOptions{
Dsn: configuration.SentryDSN,
Debug: configuration.Environment == "development",
Environment: configuration.Environment,
SampleRate: 1.0,
EnableTracing: true,
TracesSampleRate: 0.2,
Dsn: configuration.SentryDSN,
Debug: configuration.Environment == "development",
Environment: configuration.Environment,
SampleRate: 1.0,
EnableTracing: true,
TracesSampler: func(ctx sentry.SamplingContext) float64 {
if ctx.Span.Name == "GET /" {
return 0
}

return 0.2
},
ProfilesSampleRate: 0.05,
Release: version,
})
Expand Down
31 changes: 15 additions & 16 deletions reminder/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func (d *Dependency) Handler(ctx context.Context, c tb.Context) error {
input := strings.TrimPrefix(c.Text(), "/remind")
input := strings.TrimPrefix(strings.TrimPrefix(c.Text(), "/remind@TeknumCaptchaBot"), "/remind")

Check warning on line 17 in reminder/handler.go

View check run for this annotation

Codecov / codecov/patch

reminder/handler.go#L17

Added line #L17 was not covered by tests
if input == "" {
err := c.Reply(
"To use /remind properly, you should add with the remaining text including the normal human grammatical that I can understand.\n\n"+
Expand Down Expand Up @@ -64,23 +64,22 @@ func (d *Dependency) Handler(ctx context.Context, c tb.Context) error {
Category: "reminder.handler",
Message: "A reminder input just came in",
Data: map[string]interface{}{
"Reminder Text": input,
"Reminder Text": input,
"Chat ID": c.Chat().ID,
"Chat Username": c.Chat().Username,
"Chat Full Name": c.Chat().FirstName + " " + c.Chat().LastName,
"Chat Title": c.Chat().Title,
"Message ID": c.Message().ID,
"Sender ID": c.Sender().ID,
"Sender Username": c.Sender().Username,
"Sender Full Name": c.Sender().FirstName + " " + c.Sender().LastName,
"From Group": c.Message().FromGroup(),
"From Channel": c.Message().FromChannel(),
"Is Forwarded": c.Message().IsForwarded(),

Check warning on line 78 in reminder/handler.go

View check run for this annotation

Codecov / codecov/patch

reminder/handler.go#L67-L78

Added lines #L67 - L78 were not covered by tests
},
Level: "debug",
Timestamp: time.Now(),
}, &sentry.BreadcrumbHint{
"Chat ID": c.Chat().ID,
"Chat Username": c.Chat().Username,
"Chat Full Name": c.Chat().FirstName + " " + c.Chat().LastName,
"Chat Title": c.Chat().Title,
"Message ID": c.Message().ID,
"Sender ID": c.Sender().ID,
"Sender Username": c.Sender().Username,
"Sender Full Name": c.Sender().FirstName + " " + c.Sender().LastName,
"From Group": c.Message().FromGroup(),
"From Channel": c.Message().FromChannel(),
"Is Forwarded": c.Message().IsForwarded(),
})
}, &sentry.BreadcrumbHint{})

Check warning on line 82 in reminder/handler.go

View check run for this annotation

Codecov / codecov/patch

reminder/handler.go#L82

Added line #L82 was not covered by tests

// Parse text
reminder, err := ParseText(ctx, input)
Expand Down Expand Up @@ -108,7 +107,7 @@ func (d *Dependency) Handler(ctx context.Context, c tb.Context) error {
return nil
}

if reminder.Time.IsZero() || len(reminder.Subject) == 0 || reminder.Object == "" {
if reminder.Time.IsZero() || len(reminder.Subject) == 0 || reminder.Object == "" || reminder.Time.Unix() < time.Now().Unix() {

Check warning on line 110 in reminder/handler.go

View check run for this annotation

Codecov / codecov/patch

reminder/handler.go#L110

Added line #L110 was not covered by tests
err := c.Reply(
"Sorry, I'm unable to parse the reminder text that you just sent. Send /remind and see the guide for this command.",
&tb.SendOptions{ParseMode: tb.ModeHTML, AllowWithoutReply: true},
Expand Down
2 changes: 1 addition & 1 deletion underattack/are_we.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

// AreWe ...on under attack mode?
func (d *Dependency) AreWe(ctx context.Context, chatID int64) (bool, error) {
span := sentry.StartSpan(ctx, "UnderAttack.are_we", sentry.WithTransactionName("Are we under attack?"))
span := sentry.StartSpan(ctx, "underattack.are_we", sentry.WithTransactionName("Are we under attack?"))
defer span.Finish()
ctx = span.Context()

Expand Down
6 changes: 3 additions & 3 deletions underattack/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (d *Dependency) EnableUnderAttackModeHandler(ctx context.Context, c tb.Cont

sentry.GetHubFromContext(ctx).AddBreadcrumb(&sentry.Breadcrumb{
Type: "debug",
Category: "UnderAttack.state",
Category: "underattack.state",

Check warning on line 206 in underattack/handler.go

View check run for this annotation

Codecov / codecov/patch

underattack/handler.go#L206

Added line #L206 was not covered by tests
Message: "Under attack mode is enabled",
Data: map[string]interface{}{
"user": c.Sender(),
Expand All @@ -219,7 +219,7 @@ func (d *Dependency) EnableUnderAttackModeHandler(ctx context.Context, c tb.Cont

sentry.GetHubFromContext(ctx).AddBreadcrumb(&sentry.Breadcrumb{
Type: "debug",
Category: "UnderAttack.state",
Category: "underattack.state",

Check warning on line 222 in underattack/handler.go

View check run for this annotation

Codecov / codecov/patch

underattack/handler.go#L222

Added line #L222 was not covered by tests
Message: "Under attack mode ends",
Data: map[string]interface{}{
"user": c.Sender(),
Expand Down Expand Up @@ -319,7 +319,7 @@ func (d *Dependency) DisableUnderAttackModeHandler(ctx context.Context, c tb.Con

sentry.GetHubFromContext(ctx).AddBreadcrumb(&sentry.Breadcrumb{
Type: "debug",
Category: "UnderAttack.state",
Category: "underattack.state",

Check warning on line 322 in underattack/handler.go

View check run for this annotation

Codecov / codecov/patch

underattack/handler.go#L322

Added line #L322 was not covered by tests
Message: "Under attack mode is disabled",
Data: map[string]interface{}{
"user": c.Sender(),
Expand Down
2 changes: 1 addition & 1 deletion underattack/kicker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func (d *Dependency) Kicker(ctx context.Context, c tb.Context) error {
span := sentry.StartSpan(ctx, "UnderAttack.kicker")
span := sentry.StartSpan(ctx, "underattack.kicker")

Check warning on line 16 in underattack/kicker.go

View check run for this annotation

Codecov / codecov/patch

underattack/kicker.go#L16

Added line #L16 was not covered by tests
defer span.Finish()

for {
Expand Down

0 comments on commit cb06c98

Please sign in to comment.