Skip to content

Commit

Permalink
fix(deletion): strictier checking
Browse files Browse the repository at this point in the history
  • Loading branch information
aldy505 committed Jan 26, 2024
1 parent 15bbef3 commit 74bbb3f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
12 changes: 11 additions & 1 deletion deletion/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ func (d *Dependency) Handler(ctx context.Context, c tb.Context) error {
if input == "" {
return nil
}

if !c.Message().FromGroup() {
return nil
}
if !c.Message().IsReply() {
return nil
}
if c.Message().ReplyTo.Sender.ID != c.Message().Sender.ID {
return nil
}

// This is an experimental feature, so sending telemetry is a must.
// It eases the debugging process.
Expand Down Expand Up @@ -54,6 +59,11 @@ func (d *Dependency) Handler(ctx context.Context, c tb.Context) error {
return nil
}

// Avoid abuse
if duration <= 0 || duration >= time.Hour*24*7 {
return nil
}

go func(duration2 time.Duration) {
time.Sleep(duration2)
_ = c.Bot().Delete(ctx, c.Message().ReplyTo)
Expand Down
6 changes: 4 additions & 2 deletions deletion/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func ParseDuration(ctx context.Context, s string) (time.Duration, error) {
continue
}

if value, err := strconv.ParseInt(s, 10, 64); err != nil {
if value, err := strconv.ParseInt(part, 10, 64); value > 0 && err == nil {
if duration == 0 {
duration = time.Duration(value)
continue
Expand All @@ -67,7 +67,9 @@ func ParseDuration(ctx context.Context, s string) (time.Duration, error) {
return 0, err
}

duration = (time.Duration(hour) * time.Hour) + (time.Duration(minute) * time.Minute)
n := time.Now().Round(time.Second)
t := time.Date(n.Year(), n.Month(), n.Day(), hour, minute, 0, 0, time.FixedZone("UTC+7", 7*60*60))
duration = t.Sub(n)
break
}

Expand Down
57 changes: 57 additions & 0 deletions deletion/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package deletion_test

import (
"context"
"strconv"
"testing"
"time"

"github.com/teknologi-umum/captcha/deletion"
)

func TestParseDuration(t *testing.T) {
n := time.Now().Round(time.Second)
testCases := []struct {
input string
expect time.Duration
}{
{
input: "in 1 minute",
expect: time.Minute,
},
{
input: "dalam 1 menit",
expect: time.Minute,
},
{
input: "dalam 30 detik",
expect: time.Second * 30,
},
{
input: "in 321 hour",
expect: time.Hour * 321,
},
{
input: "in 1 minute 10 second",
expect: time.Minute,
},
{
input: "in 11:30",
expect: time.Date(n.Year(), n.Month(), n.Day(), 11, 30, 0, 0, time.FixedZone("UTC+7", 7*60*60)).Sub(n),
},
}

for i, testCase := range testCases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
ctx := context.Background()
got, err := deletion.ParseDuration(ctx, testCase.input)
if err != nil {
t.Errorf("unexpected error: %s", err.Error())
}

if got != testCase.expect {
t.Errorf("expecting %v, got %v", testCase.expect, got)
}
})
}
}
2 changes: 1 addition & 1 deletion internal/telebot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -1180,7 +1180,7 @@ func (b *Bot) SetMessageReaction(ctx context.Context, msg Editable, emoji string
"message_id": msgId,
"reaction": map[string]any{
"type": "emoji",
"emoji": emoji,
"emoji": []string{emoji},
},
}

Expand Down

0 comments on commit 74bbb3f

Please sign in to comment.