Skip to content
This repository was archived by the owner on Feb 16, 2025. It is now read-only.

Commit 02b7bef

Browse files
committed
ban users that ban bots
try better responding
1 parent 5170918 commit 02b7bef

File tree

3 files changed

+64
-11
lines changed

3 files changed

+64
-11
lines changed

app/handler_helpers.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,17 @@ const (
1717
NotSupported = "Not supported yet, sorry"
1818
)
1919

20+
type MethodOfResponding = int
21+
22+
const (
23+
Reply MethodOfResponding = iota
24+
Send
25+
)
26+
2027
func (d DistorterBot) HandleAnimationCommon(c tb.Context) (*tb.Message, string, string, error) {
2128
m := c.Message()
2229
b := c.Bot()
23-
progressMessage, err := d.GetProgressMessage(c, "Downloading...")
30+
progressMessage, err := d.SendMessage(c, "Downloading...", Reply)
2431
if err != nil {
2532
d.logger.Error(err)
2633
return nil, "", "", err
@@ -113,14 +120,27 @@ func (d DistorterBot) DoneMessageWithRepeater(b *tb.Bot, m *tb.Message, failed b
113120
}
114121
}
115122

116-
func (d DistorterBot) GetProgressMessage(c tb.Context, toSend interface{}) (*tb.Message, error) {
123+
func (d DistorterBot) SendMessage(c tb.Context, toSend interface{}, method MethodOfResponding) (*tb.Message, error) {
117124
b := c.Bot()
118125
message := c.Message()
119-
m, err := b.Reply(message, toSend)
126+
127+
var m *tb.Message
128+
var err error
129+
if method == Reply {
130+
m, err = b.Reply(message, toSend)
131+
} else {
132+
m, err = b.Send(message.Chat, toSend)
133+
}
120134
for err != nil {
121-
if strings.Contains(err.Error(), "not enough rights to send") {
135+
switch {
136+
case strings.Contains(err.Error(), "not enough rights to send"):
122137
b.Reply(message, NotEnoughRights)
138+
case strings.Contains(err.Error(), "bot was blocked by the user (403)"):
139+
d.videoWorker.BanUser(message.Chat.ID)
140+
case strings.Contains(err.Error(), "telegram: Bad Request: message to be replied not found (400)"):
141+
return d.SendMessage(c, toSend, Send)
123142
}
143+
124144
var timeout int
125145
timeout, err = tools.ExtractPossibleTimeout(err)
126146
if err != nil {
@@ -138,7 +158,7 @@ func (d DistorterBot) GetProgressMessage(c tb.Context, toSend interface{}) (*tb.
138158
}
139159

140160
func (d DistorterBot) SendMessageWithRepeater(c tb.Context, toSend interface{}) error {
141-
_, err := d.GetProgressMessage(c, toSend)
161+
_, err := d.SendMessage(c, toSend, Reply)
142162
return err
143163
}
144164

app/queue/honest_priority_queue.go

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,31 @@ import (
1010
// Wraps PriorityQueue to make it thread-safe. Manages priorities.
1111
// Extremely inefficient, but works for my use-case (very slow jobs and small queue sizes)
1212
type HonestJobQueue struct {
13-
mu *sync.RWMutex
14-
queue PriorityQueue
15-
users map[int64]int // Tracks the amount of job per-user currently in the queue. Used to calculate priority
13+
mu *sync.RWMutex
14+
queue PriorityQueue
15+
users map[int64]int // Tracks the amount of job per-user currently in the queue. Used to calculate priority
16+
banned map[int64]any // Drop jobs from these users
1617
}
1718

1819
func NewHonestJobQueue(initialCapacity int) *HonestJobQueue {
1920
return &HonestJobQueue{
20-
mu: &sync.RWMutex{},
21-
queue: make(PriorityQueue, 0, initialCapacity),
22-
users: make(map[int64]int),
21+
mu: &sync.RWMutex{},
22+
queue: make(PriorityQueue, 0, initialCapacity),
23+
users: make(map[int64]int),
24+
banned: make(map[int64]any),
2325
}
2426
}
2527

28+
// BanUser This will "ban" the user (if they were impatient and banned the bot first)
29+
// causing their jobs to be dropped when they pop up. Once all the jobs have been popped
30+
// the ban will be lifted
31+
func (hjq *HonestJobQueue) BanUser(userID int64) {
32+
hjq.mu.Lock()
33+
defer hjq.mu.Unlock()
34+
35+
hjq.banned[userID] = nil
36+
}
37+
2638
func (hjq *HonestJobQueue) updatePriorities(userID int64) {
2739
for i, job := range hjq.queue {
2840
if job.userID != userID {
@@ -55,6 +67,23 @@ func (hjq *HonestJobQueue) Pop() *Job {
5567
defer hjq.mu.Unlock()
5668

5769
job := heap.Pop(&hjq.queue).(*Job)
70+
71+
if _, ok := hjq.banned[job.userID]; ok {
72+
allBannedJobsExhausted := true
73+
for _, queued := range hjq.queue {
74+
if queued.userID == job.userID {
75+
allBannedJobsExhausted = false
76+
break
77+
}
78+
}
79+
80+
if allBannedJobsExhausted {
81+
delete(hjq.banned, job.userID)
82+
}
83+
84+
return hjq.Pop()
85+
}
86+
5887
hjq.users[job.userID]--
5988

6089
if hjq.users[job.userID] == 0 {

app/tools/video_worker.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ func NewVideoWorker(workerCount int) *VideoWorker {
2323
return &worker
2424
}
2525

26+
func (vw *VideoWorker) BanUser(userID int64) {
27+
vw.queue.BanUser(userID)
28+
}
29+
2630
func (vw *VideoWorker) run() {
2731
for range vw.messenger {
2832
vw.queue.Pop().Run()

0 commit comments

Comments
 (0)