mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-09-05 23:56:50 +08:00
fix: debounce send media event and ignore edit or delete updates
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
|||||||
|
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
"github.com/gotd/td/telegram/dcs"
|
"github.com/gotd/td/telegram/dcs"
|
||||||
|
"github.com/gotd/td/tg"
|
||||||
"github.com/krau/SaveAny-Bot/client/middleware"
|
"github.com/krau/SaveAny-Bot/client/middleware"
|
||||||
"github.com/krau/SaveAny-Bot/common/utils/netutil"
|
"github.com/krau/SaveAny-Bot/common/utils/netutil"
|
||||||
"github.com/krau/SaveAny-Bot/config"
|
"github.com/krau/SaveAny-Bot/config"
|
||||||
@@ -111,6 +112,10 @@ func Login(ctx context.Context) (*gotgproto.Client, error) {
|
|||||||
}
|
}
|
||||||
uc = r.client
|
uc = r.client
|
||||||
uc.Dispatcher.AddHandler(handlers.NewMessage(filters.Message.Media, func(ctx *ext.Context, u *ext.Update) error {
|
uc.Dispatcher.AddHandler(handlers.NewMessage(filters.Message.Media, func(ctx *ext.Context, u *ext.Update) error {
|
||||||
|
switch u.UpdateClass.(type) {
|
||||||
|
case *tg.UpdateEditChannelMessage, *tg.UpdateEditMessage, *tg.UpdateDeleteChannelMessages, *tg.UpdateDeleteMessages:
|
||||||
|
return dispatcher.EndGroups
|
||||||
|
}
|
||||||
chatId := u.EffectiveChat().GetID()
|
chatId := u.EffectiveChat().GetID()
|
||||||
watchChats, err := database.GetWatchChatsByChatID(ctx, chatId)
|
watchChats, err := database.GetWatchChatsByChatID(ctx, chatId)
|
||||||
if err != nil || len(watchChats) == 0 {
|
if err != nil || len(watchChats) == 0 {
|
||||||
|
|||||||
+50
-3
@@ -1,6 +1,9 @@
|
|||||||
package user
|
package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/celestix/gotgproto/dispatcher"
|
"github.com/celestix/gotgproto/dispatcher"
|
||||||
"github.com/celestix/gotgproto/ext"
|
"github.com/celestix/gotgproto/ext"
|
||||||
"github.com/gotd/td/tg"
|
"github.com/gotd/td/tg"
|
||||||
@@ -11,15 +14,58 @@ import (
|
|||||||
type MediaMessageEvent struct {
|
type MediaMessageEvent struct {
|
||||||
Ctx *ext.Context
|
Ctx *ext.Context
|
||||||
ChatID int64 // from witch the media message was sent
|
ChatID int64 // from witch the media message was sent
|
||||||
|
MessageID int
|
||||||
File tfile.TGFileMessage
|
File tfile.TGFileMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
var mediaMessageCh = make(chan MediaMessageEvent, 100)
|
type messageKey struct {
|
||||||
|
ChatID int64
|
||||||
|
MessageID int
|
||||||
|
}
|
||||||
|
|
||||||
|
type MediaMessageHandler struct {
|
||||||
|
events map[messageKey]MediaMessageEvent
|
||||||
|
timers map[messageKey]*time.Timer
|
||||||
|
mu sync.Mutex
|
||||||
|
debounce time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
mediaMessageCh = make(chan MediaMessageEvent, 100)
|
||||||
|
mediaMessageHandler = &MediaMessageHandler{
|
||||||
|
events: make(map[messageKey]MediaMessageEvent),
|
||||||
|
timers: make(map[messageKey]*time.Timer),
|
||||||
|
debounce: 5 * time.Second,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
func GetMediaMessageCh() chan MediaMessageEvent {
|
func GetMediaMessageCh() chan MediaMessageEvent {
|
||||||
return mediaMessageCh
|
return mediaMessageCh
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func sendMediaMessageEvent(event MediaMessageEvent) {
|
||||||
|
key := messageKey{ChatID: event.ChatID, MessageID: event.MessageID}
|
||||||
|
|
||||||
|
mediaMessageHandler.mu.Lock()
|
||||||
|
defer mediaMessageHandler.mu.Unlock()
|
||||||
|
|
||||||
|
if timer, exists := mediaMessageHandler.timers[key]; exists {
|
||||||
|
timer.Stop()
|
||||||
|
} else {
|
||||||
|
mediaMessageHandler.events[key] = event
|
||||||
|
}
|
||||||
|
|
||||||
|
mediaMessageHandler.timers[key] = time.AfterFunc(mediaMessageHandler.debounce, func() {
|
||||||
|
mediaMessageHandler.mu.Lock()
|
||||||
|
event := mediaMessageHandler.events[key]
|
||||||
|
delete(mediaMessageHandler.events, key)
|
||||||
|
delete(mediaMessageHandler.timers, key)
|
||||||
|
mediaMessageHandler.mu.Unlock()
|
||||||
|
|
||||||
|
mediaMessageCh <- event
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func handleMediaMessage(ctx *ext.Context, update *ext.Update) error {
|
func handleMediaMessage(ctx *ext.Context, update *ext.Update) error {
|
||||||
message := update.EffectiveMessage
|
message := update.EffectiveMessage
|
||||||
media, ok := message.GetMedia()
|
media, ok := message.GetMedia()
|
||||||
@@ -44,10 +90,11 @@ func handleMediaMessage(ctx *ext.Context, update *ext.Update) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
chatId := update.EffectiveChat().GetID()
|
chatId := update.EffectiveChat().GetID()
|
||||||
mediaMessageCh <- MediaMessageEvent{
|
sendMediaMessageEvent(MediaMessageEvent{
|
||||||
Ctx: ctx,
|
Ctx: ctx,
|
||||||
ChatID: chatId,
|
ChatID: chatId,
|
||||||
|
MessageID: message.ID,
|
||||||
File: file,
|
File: file,
|
||||||
}
|
})
|
||||||
return dispatcher.EndGroups
|
return dispatcher.EndGroups
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user