Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(reminder): disallow reminder in the past #51

Merged
merged 2 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
33 changes: 16 additions & 17 deletions reminder/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
)

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 @@
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 @@
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 Expand Up @@ -136,7 +135,7 @@
template := fmt.Sprintf(
"Hi %s! I'm reminding you to %s. Have a great day!",
strings.Join(reminder.Subject, ", "),
reminder.Object,
utils.SanitizeInput(reminder.Object),

Check warning on line 138 in reminder/handler.go

View check run for this annotation

Codecov / codecov/patch

reminder/handler.go#L138

Added line #L138 was not covered by tests
)
_, err := c.Bot().Send(c.Chat(), template, &tb.SendOptions{ParseMode: tb.ModeHTML, AllowWithoutReply: true})
if err != nil {
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 @@

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 @@

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 @@

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 @@
)

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