client,gotd: remove unnecessary dispatcher wrapper
This commit is contained in:
+2
-30
@@ -113,32 +113,6 @@ type TelegramClient struct {
|
|||||||
|
|
||||||
var _ bridgev2.NetworkAPI = (*TelegramClient)(nil)
|
var _ bridgev2.NetworkAPI = (*TelegramClient)(nil)
|
||||||
|
|
||||||
type UpdateDispatcher struct {
|
|
||||||
tg.UpdateDispatcher
|
|
||||||
EntityHandler func(context.Context, tg.Entities) error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u UpdateDispatcher) Handle(ctx context.Context, updates tg.UpdatesClass) error {
|
|
||||||
var e tg.Entities
|
|
||||||
switch u := updates.(type) {
|
|
||||||
case *tg.Updates:
|
|
||||||
e.Users = u.MapUsers().NotEmptyToMap()
|
|
||||||
chats := u.MapChats()
|
|
||||||
e.Chats = chats.ChatToMap()
|
|
||||||
e.Channels = chats.ChannelToMap()
|
|
||||||
case *tg.UpdatesCombined:
|
|
||||||
e.Users = u.MapUsers().NotEmptyToMap()
|
|
||||||
chats := u.MapChats()
|
|
||||||
e.Chats = chats.ChatToMap()
|
|
||||||
e.Channels = chats.ChannelToMap()
|
|
||||||
}
|
|
||||||
if u.EntityHandler != nil {
|
|
||||||
u.EntityHandler(ctx, e)
|
|
||||||
}
|
|
||||||
|
|
||||||
return u.UpdateDispatcher.Handle(ctx, updates)
|
|
||||||
}
|
|
||||||
|
|
||||||
var messageLinkRegex = regexp.MustCompile(`^https?://t(?:elegram)?\.(?:me|dog)/([A-Za-z][A-Za-z0-9_]{3,31}[A-Za-z0-9]|[Cc]/[0-9]{1,20})/([0-9]{1,20})(?:/([0-9]{1,20}))?$`)
|
var messageLinkRegex = regexp.MustCompile(`^https?://t(?:elegram)?\.(?:me|dog)/([A-Za-z][A-Za-z0-9_]{3,31}[A-Za-z0-9]|[Cc]/[0-9]{1,20})/([0-9]{1,20})(?:/([0-9]{1,20}))?$`)
|
||||||
|
|
||||||
func (tg *TelegramConnector) deviceConfig() telegram.DeviceConfig {
|
func (tg *TelegramConnector) deviceConfig() telegram.DeviceConfig {
|
||||||
@@ -198,10 +172,8 @@ func NewTelegramClient(ctx context.Context, tc *TelegramConnector, login *bridge
|
|||||||
return &client, nil
|
return &client, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatcher := UpdateDispatcher{
|
dispatcher := tg.NewUpdateDispatcher()
|
||||||
UpdateDispatcher: tg.NewUpdateDispatcher(),
|
dispatcher.OnFallback(client.onEntityUpdate)
|
||||||
EntityHandler: client.onEntityUpdate,
|
|
||||||
}
|
|
||||||
dispatcher.OnNewMessage(func(ctx context.Context, e tg.Entities, update *tg.UpdateNewMessage) error {
|
dispatcher.OnNewMessage(func(ctx context.Context, e tg.Entities, update *tg.UpdateNewMessage) error {
|
||||||
return client.onUpdateNewMessage(ctx, e, update)
|
return client.onUpdateNewMessage(ctx, e, update)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -817,7 +817,7 @@ func (t *TelegramClient) updateChannel(ctx context.Context, channel *tg.Channel)
|
|||||||
return userInfo, nil
|
return userInfo, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TelegramClient) onEntityUpdate(ctx context.Context, e tg.Entities) error {
|
func (t *TelegramClient) onEntityUpdate(ctx context.Context, e tg.Entities, _ tg.UpdateClass) error {
|
||||||
for userID, user := range e.Users {
|
for userID, user := range e.Users {
|
||||||
if _, err := t.updateGhost(ctx, userID, user); err != nil {
|
if _, err := t.updateGhost(ctx, userID, user); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -5,13 +5,14 @@
|
|||||||
type Handler = func(context.Context, Entities, UpdateClass) error
|
type Handler = func(context.Context, Entities, UpdateClass) error
|
||||||
|
|
||||||
type UpdateDispatcher struct {
|
type UpdateDispatcher struct {
|
||||||
handlers map[uint32]Handler
|
handlers map[uint32]Handler
|
||||||
fallback Handler
|
fallback Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUpdateDispatcher() UpdateDispatcher {
|
func NewUpdateDispatcher() UpdateDispatcher {
|
||||||
return UpdateDispatcher{
|
return UpdateDispatcher{
|
||||||
handlers: map[uint32]Handler{},
|
handlers: map[uint32]Handler{},
|
||||||
|
fallback: func(ctx context.Context, entities Entities, class UpdateClass) error { return nil },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,17 +68,17 @@ func (u UpdateDispatcher) Handle(ctx context.Context, updates UpdatesClass) erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u UpdateDispatcher) dispatch(ctx context.Context, e Entities, update UpdateClass) error {
|
func (u UpdateDispatcher) dispatch(ctx context.Context, e Entities, update UpdateClass) error {
|
||||||
if update == nil {
|
if update == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
typeID := update.TypeID()
|
if err := u.fallback(ctx, e, update); err != nil {
|
||||||
handler, ok := u.handlers[typeID]
|
return err
|
||||||
|
}
|
||||||
|
typeID := update.TypeID()
|
||||||
|
handler, ok := u.handlers[typeID]
|
||||||
if ok {
|
if ok {
|
||||||
return handler(ctx, e, update)
|
return handler(ctx, e, update)
|
||||||
}
|
}
|
||||||
if u.fallback != nil {
|
|
||||||
return u.fallback(ctx, e, update)
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Generated
+4
-3
@@ -41,6 +41,7 @@ type UpdateDispatcher struct {
|
|||||||
func NewUpdateDispatcher() UpdateDispatcher {
|
func NewUpdateDispatcher() UpdateDispatcher {
|
||||||
return UpdateDispatcher{
|
return UpdateDispatcher{
|
||||||
handlers: map[uint32]Handler{},
|
handlers: map[uint32]Handler{},
|
||||||
|
fallback: func(ctx context.Context, entities Entities, class UpdateClass) error { return nil },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,14 +100,14 @@ func (u UpdateDispatcher) dispatch(ctx context.Context, e Entities, update Updat
|
|||||||
if update == nil {
|
if update == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
if err := u.fallback(ctx, e, update); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
typeID := update.TypeID()
|
typeID := update.TypeID()
|
||||||
handler, ok := u.handlers[typeID]
|
handler, ok := u.handlers[typeID]
|
||||||
if ok {
|
if ok {
|
||||||
return handler(ctx, e, update)
|
return handler(ctx, e, update)
|
||||||
}
|
}
|
||||||
if u.fallback != nil {
|
|
||||||
return u.fallback(ctx, e, update)
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user