-
Notifications
You must be signed in to change notification settings - Fork 31
/
dispatcher.go
225 lines (204 loc) · 6.23 KB
/
dispatcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package dispatcher
import (
"context"
"errors"
"fmt"
"log"
"runtime/debug"
"github.com/celestix/gotgproto/ext"
"github.com/celestix/gotgproto/storage"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/message"
"github.com/gotd/td/tg"
"go.uber.org/multierr"
)
var (
// StopClient cancels the context and stops the client if returned through handler callback function.
StopClient = errors.New("disconnect")
// EndGroups stops iterating over handlers groups if returned through handler callback function.
EndGroups = errors.New("stopped")
// ContinueGroups continues iterating over handlers groups if returned through handler callback function.
ContinueGroups = errors.New("continued")
// SkipCurrentGroup skips current group and continues iterating over handlers groups if returned through handler callback function.
SkipCurrentGroup = errors.New("skipped")
)
type Dispatcher interface {
Initialize(context.Context, context.CancelFunc, *telegram.Client, *tg.User)
Handle(context.Context, tg.UpdatesClass) error
AddHandler(Handler)
AddHandlerToGroup(Handler, int)
}
type NativeDispatcher struct {
cancel context.CancelFunc
client *tg.Client
self *tg.User
sender *message.Sender
setReply bool
setEntireReplyChain bool
// Panic handles all the panics that occur during handler execution.
Panic PanicHandler
// Error handles all the unknown errors which are returned by the handler callback functions.
Error ErrorHandler
// handlerMap is used for internal functionality of NativeDispatcher.
handlerMap map[int][]Handler
// handlerGroups is used for internal functionality of NativeDispatcher.
handlerGroups []int
pStorage *storage.PeerStorage
}
type PanicHandler func(*ext.Context, *ext.Update, string)
type ErrorHandler func(*ext.Context, *ext.Update, string) error
// MakeDispatcher creates new custom dispatcher which process and handles incoming updates.
func NewNativeDispatcher(setReply bool, setEntireReplyChain bool, eHandler ErrorHandler, pHandler PanicHandler, p *storage.PeerStorage) *NativeDispatcher {
if eHandler == nil {
eHandler = defaultErrorHandler
}
return &NativeDispatcher{
pStorage: p,
handlerMap: make(map[int][]Handler),
handlerGroups: make([]int, 0),
setReply: setReply,
setEntireReplyChain: setEntireReplyChain,
Error: eHandler,
Panic: pHandler,
}
}
func defaultErrorHandler(_ *ext.Context, _ *ext.Update, err string) error {
log.Println("An error occured while handling update:", err)
return ContinueGroups
}
type entities tg.Entities
func (u *entities) short() {
u.Short = true
u.Users = make(map[int64]*tg.User, 0)
u.Chats = make(map[int64]*tg.Chat, 0)
u.Channels = make(map[int64]*tg.Channel, 0)
}
func (dp *NativeDispatcher) Initialize(ctx context.Context, cancel context.CancelFunc, client *telegram.Client, self *tg.User) {
dp.client = client.API()
dp.sender = message.NewSender(dp.client)
dp.self = self
dp.cancel = cancel
}
// Handle function handles all the incoming updates, map entities and dispatches updates for further handling.
func (dp *NativeDispatcher) Handle(ctx context.Context, updates tg.UpdatesClass) error {
var (
e entities
upds []tg.UpdateClass
)
switch u := updates.(type) {
case *tg.Updates:
upds = u.Updates
e.Users = u.MapUsers().NotEmptyToMap()
chats := u.MapChats()
e.Chats = chats.ChatToMap()
e.Channels = chats.ChannelToMap()
saveUsersPeers(u.Users, dp.pStorage)
saveChatsPeers(u.Chats, dp.pStorage)
case *tg.UpdatesCombined:
upds = u.Updates
e.Users = u.MapUsers().NotEmptyToMap()
chats := u.MapChats()
e.Chats = chats.ChatToMap()
e.Channels = chats.ChannelToMap()
saveUsersPeers(u.Users, dp.pStorage)
saveChatsPeers(u.Chats, dp.pStorage)
case *tg.UpdateShort:
upds = []tg.UpdateClass{u.Update}
e.short()
default:
return nil
}
var err error
for _, update := range upds {
multierr.AppendInto(&err, dp.dispatch(ctx, tg.Entities(e), update))
}
return err
}
func (dp *NativeDispatcher) dispatch(ctx context.Context, e tg.Entities, update tg.UpdateClass) error {
if update == nil {
return nil
}
return dp.handleUpdate(ctx, e, update)
}
func (dp *NativeDispatcher) handleUpdate(ctx context.Context, e tg.Entities, update tg.UpdateClass) error {
u := ext.GetNewUpdate(ctx, dp.client, dp.pStorage, &e, update)
dp.handleUpdateRepliedToMessage(u, ctx)
c := ext.NewContext(ctx, dp.client, dp.pStorage, dp.self, dp.sender, &e, dp.setReply)
var err error
defer func() {
if r := recover(); r != nil {
errorStack := fmt.Sprintf("%s\n", r) + string(debug.Stack())
if dp.Panic != nil {
dp.Panic(c, u, errorStack)
return
} else {
log.Println(errorStack)
}
}
}()
for _, group := range dp.handlerGroups {
for _, handler := range dp.handlerMap[group] {
err = handler.CheckUpdate(c, u)
if err == nil || errors.Is(err, ContinueGroups) {
continue
} else if errors.Is(err, EndGroups) {
return err
} else if errors.Is(err, SkipCurrentGroup) {
break
} else if errors.Is(err, StopClient) {
dp.cancel()
return nil
} else {
err = dp.Error(c, u, err.Error())
switch err {
case ContinueGroups:
continue
case EndGroups:
return err
case SkipCurrentGroup:
break
}
}
}
}
return err
}
func (dp *NativeDispatcher) handleUpdateRepliedToMessage(u *ext.Update, ctx context.Context) {
msg := u.EffectiveMessage
if msg == nil || !dp.setReply {
return
}
for {
if msg.Message.ReplyTo == nil {
return
}
_ = msg.SetRepliedToMessage(ctx, dp.client, dp.pStorage)
if !dp.setEntireReplyChain {
return
}
msg = msg.ReplyToMessage
}
}
func saveUsersPeers(u tg.UserClassArray, p *storage.PeerStorage) {
for _, user := range u {
c, ok := user.AsNotEmpty()
if !ok {
continue
}
p.AddPeer(c.ID, c.AccessHash, storage.TypeUser, c.Username)
}
}
func saveChatsPeers(u tg.ChatClassArray, p *storage.PeerStorage) {
for _, chat := range u {
channel, ok := chat.(*tg.Channel)
if ok {
p.AddPeer(channel.ID, channel.AccessHash, storage.TypeChannel, channel.Username)
continue
}
chat, ok := chat.(*tg.Chat)
if !ok {
continue
}
p.AddPeer(chat.ID, storage.DefaultAccessHash, storage.TypeChat, storage.DefaultUsername)
}
}