-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
server_event.go
96 lines (85 loc) · 1.96 KB
/
server_event.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
package e2e
import (
"github.com/gotd/td/tg"
)
// EventBuilder struct.
type EventBuilder struct {
updates []tg.UpdateClass
ents *Entities
s *server
date int
}
// SendMessage send a new message.
func (e *EventBuilder) SendMessage(from *tg.PeerUser, peer tg.PeerClass, text string) {
msg := &tg.Message{
Message: text,
PeerID: peer,
FromID: from,
Date: e.date,
}
fromUser, ok := e.s.peers.users[from.UserID]
if !ok {
panic("bad fromID")
}
e.ents.Users[from.UserID] = fromUser
switch peer := peer.(type) {
case *tg.PeerUser:
user, ok := e.s.peers.users[peer.UserID]
if !ok {
panic("peer not found")
}
e.ents.Users[user.ID] = user
e.s.messages.common = append(e.s.messages.common, msg)
e.updates = append(e.updates, &tg.UpdateNewMessage{
Message: msg,
Pts: len(e.s.messages.common),
PtsCount: 1,
})
case *tg.PeerChat:
chat, ok := e.s.peers.chats[peer.ChatID]
if !ok {
panic("peer not found")
}
e.ents.Chats[chat.ID] = chat
e.s.messages.common = append(e.s.messages.common, msg)
e.updates = append(e.updates, &tg.UpdateNewMessage{
Message: msg,
Pts: len(e.s.messages.common),
PtsCount: 1,
})
case *tg.PeerChannel:
channel, ok := e.s.peers.channels[peer.ChannelID]
if !ok {
panic("peer not found")
}
e.ents.Channels[channel.ID] = channel
msgs := append(e.s.messages.channels[peer.ChannelID], msg)
e.s.messages.channels[peer.ChannelID] = msgs
e.updates = append(e.updates, &tg.UpdateNewChannelMessage{
Message: msg,
Pts: len(msgs),
PtsCount: 1,
})
default:
panic("unexpected peer type")
}
}
// CreateEvent creates new event.
func (s *server) CreateEvent(f func(ev *EventBuilder)) *tg.Updates {
s.mux.Lock()
defer s.mux.Unlock()
s.date++
ev := &EventBuilder{
ents: NewEntities(),
s: s,
date: s.date,
}
f(ev)
return &tg.Updates{
Updates: ev.updates,
Users: ev.ents.AsUsers(),
Chats: ev.ents.AsChats(),
Date: s.date,
Seq: 0,
}
}