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
83 lines
1.9 KiB
Go
83 lines
1.9 KiB
Go
package tgtest
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/go-faster/errors"
|
|
|
|
"go.mau.fi/mautrix-telegram/pkg/gotd/bin"
|
|
)
|
|
|
|
// Dispatcher is a plain handler to map requests by ID.
|
|
type Dispatcher struct {
|
|
reqs map[uint32]Handler
|
|
mux sync.Mutex
|
|
fallback Handler
|
|
}
|
|
|
|
// NewDispatcher creates new Dispatcher.
|
|
func NewDispatcher() *Dispatcher {
|
|
return &Dispatcher{
|
|
reqs: map[uint32]Handler{},
|
|
}
|
|
}
|
|
|
|
// OnMessage implements Handler
|
|
func (d *Dispatcher) OnMessage(server *Server, req *Request) error {
|
|
id, err := req.Buf.PeekID()
|
|
if err != nil {
|
|
return errors.Wrap(err, "peek id")
|
|
}
|
|
|
|
d.mux.Lock()
|
|
h, ok := d.reqs[id]
|
|
fallback := d.fallback
|
|
d.mux.Unlock()
|
|
if ok {
|
|
return h.OnMessage(server, req)
|
|
}
|
|
|
|
if fallback != nil {
|
|
return fallback.OnMessage(server, req)
|
|
}
|
|
|
|
return errors.Errorf("unexpected type %#x", id)
|
|
}
|
|
|
|
// Handle sets handler for given TypeID.
|
|
func (d *Dispatcher) Handle(id uint32, h Handler) *Dispatcher {
|
|
d.mux.Lock()
|
|
d.reqs[id] = h
|
|
d.mux.Unlock()
|
|
return d
|
|
}
|
|
|
|
// HandleFunc sets handler for given TypeID.
|
|
func (d *Dispatcher) HandleFunc(id uint32, h func(server *Server, req *Request) error) *Dispatcher {
|
|
return d.Handle(id, HandlerFunc(h))
|
|
}
|
|
|
|
// Result sets constant result for given TypeID.
|
|
// NB: it uses rpc_result to pack given encoder.
|
|
func (d *Dispatcher) Result(id uint32, msg bin.Encoder) *Dispatcher {
|
|
return d.HandleFunc(id, func(server *Server, req *Request) error {
|
|
return server.SendResult(req, msg)
|
|
})
|
|
}
|
|
|
|
// Vector sets constant Vector result for given TypeID.
|
|
// NB: it uses rpc_result to pack generic vector with given encoders.
|
|
func (d *Dispatcher) Vector(id uint32, msgs ...bin.Encoder) *Dispatcher {
|
|
return d.HandleFunc(id, func(server *Server, req *Request) error {
|
|
return server.SendVector(req, msgs...)
|
|
})
|
|
}
|
|
|
|
// Fallback sets fallback handler.
|
|
func (d *Dispatcher) Fallback(h Handler) *Dispatcher {
|
|
d.mux.Lock()
|
|
d.fallback = h
|
|
d.mux.Unlock()
|
|
return d
|
|
}
|