7a04f298d2
- update to latest telegram layer - remove some references to fields in tg.Entities that don't exist in the schema - originally added here: https://github.com/beeper/td/commit/820929062a2ba0104397bc01235ab58a9cff780e - referenced here - https://github.com/mautrix/telegramgo/commit/124f0967ed195b5a380c9bd02e170ada9710dde3 - https://github.com/mautrix/telegramgo/commit/4205047aab2e0639217148b5d125bfaab668bd8e
94 lines
2.3 KiB
Cheetah
94 lines
2.3 KiB
Cheetah
{{ define "handlers" }}
|
|
{{ $pkg := $.Package }}
|
|
{{ template "header" $ }}
|
|
|
|
type handler = func(context.Context, Entities, UpdateClass) error
|
|
|
|
type UpdateDispatcher struct {
|
|
handlers map[uint32]handler
|
|
}
|
|
|
|
func NewUpdateDispatcher() UpdateDispatcher {
|
|
return UpdateDispatcher{
|
|
handlers: map[uint32]handler{},
|
|
}
|
|
}
|
|
|
|
type Entities struct {
|
|
Short bool
|
|
Users map[int64]*User
|
|
Chats map[int64]*Chat
|
|
Channels map[int64]*Channel
|
|
}
|
|
|
|
func (u *Entities) short() {
|
|
u.Short = true
|
|
u.Users = make(map[int64]*User, 0)
|
|
u.Chats = make(map[int64]*Chat, 0)
|
|
u.Channels = make(map[int64]*Channel, 0)
|
|
}
|
|
|
|
// Handle implements UpdateDispatcher.
|
|
func (u UpdateDispatcher) Handle(ctx context.Context, updates UpdatesClass) error {
|
|
var (
|
|
e Entities
|
|
upds []UpdateClass
|
|
)
|
|
switch u := updates.(type) {
|
|
case *Updates:
|
|
upds = u.Updates
|
|
e.Users = u.MapUsers().NotEmptyToMap()
|
|
chats := u.MapChats()
|
|
e.Chats = chats.ChatToMap()
|
|
e.Channels = chats.ChannelToMap()
|
|
case *UpdatesCombined:
|
|
upds = u.Updates
|
|
e.Users = u.MapUsers().NotEmptyToMap()
|
|
chats := u.MapChats()
|
|
e.Chats = chats.ChatToMap()
|
|
e.Channels = chats.ChannelToMap()
|
|
case *UpdateShort:
|
|
upds = []UpdateClass{u.Update}
|
|
e.short()
|
|
default:
|
|
// *UpdateShortMessage
|
|
// *UpdateShortChatMessage
|
|
// *UpdateShortSentMessage
|
|
// *UpdatesTooLong
|
|
return nil
|
|
}
|
|
|
|
var err error
|
|
for _, update := range upds {
|
|
multierr.AppendInto(&err, u.dispatch(ctx, e, update))
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (u UpdateDispatcher) dispatch(ctx context.Context, e Entities, update UpdateClass) error {
|
|
if update == nil {
|
|
return nil
|
|
}
|
|
typeID := update.TypeID()
|
|
handler, ok := u.handlers[typeID]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
return handler(ctx, e, update)
|
|
}
|
|
|
|
{{- range $s := $.Structs }}{{ if eq $s.Interface "UpdateClass" }}
|
|
{{ $eventName := trimPrefix $s.Name "Update"}}
|
|
// {{ $eventName }}Handler is a {{ $eventName }} event handler.
|
|
type {{ $eventName }}Handler func(ctx context.Context, e Entities, update *{{ $s.Name }}) error
|
|
|
|
// On{{ $eventName }} sets {{ $eventName }} handler.
|
|
func (u UpdateDispatcher) On{{ $eventName }}(handler {{ $eventName }}Handler) {
|
|
u.handlers[{{ $s.Name }}TypeID] = func(ctx context.Context, e Entities, update UpdateClass) error {
|
|
return handler(ctx, e, update.(*{{ $s.Name }}))
|
|
}
|
|
}
|
|
{{- end }}{{ end }}
|
|
|
|
{{ end }}
|