-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
entities.go
81 lines (70 loc) · 1.81 KB
/
entities.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
package e2e
import "github.com/gotd/td/tg"
// Entities contains update entities.
type Entities struct {
Users map[int64]*tg.User
Chats map[int64]*tg.Chat
Channels map[int64]*tg.Channel
ChannelsForbidden map[int64]*tg.ChannelForbidden
}
// NewEntities creates new Entities.
func NewEntities() *Entities {
return &Entities{
Users: map[int64]*tg.User{},
Chats: map[int64]*tg.Chat{},
Channels: map[int64]*tg.Channel{},
ChannelsForbidden: map[int64]*tg.ChannelForbidden{},
}
}
// Merge merges entities.
func (e *Entities) Merge(from *Entities) {
if from == nil {
return
}
for userID, user := range from.Users {
e.Users[userID] = user
}
for chanID, chat := range from.Chats {
e.Chats[chanID] = chat
}
for channelID, channel := range from.Channels {
e.Channels[channelID] = channel
}
for channelID, channel := range from.ChannelsForbidden {
e.ChannelsForbidden[channelID] = channel
}
}
// FromUpdates method.
func (e *Entities) FromUpdates(u interface {
tg.UpdatesClass
MapUsers() tg.UserClassArray
MapChats() tg.ChatClassArray
}) *Entities {
u.MapChats().FillChatMap(e.Chats)
u.MapChats().FillChannelMap(e.Channels)
u.MapChats().FillChannelForbiddenMap(e.ChannelsForbidden)
u.MapUsers().FillUserMap(e.Users)
return e
}
// AsUsers returns users as tg.UserClass slice.
func (e *Entities) AsUsers() []tg.UserClass {
var users []tg.UserClass
for _, u := range e.Users {
users = append(users, u)
}
return users
}
// AsChats returns chats as tg.ChatClass slice.
func (e *Entities) AsChats() []tg.ChatClass {
var chats []tg.ChatClass
for _, c := range e.Chats {
chats = append(chats, c)
}
for _, c := range e.Channels {
chats = append(chats, c)
}
for _, c := range e.ChannelsForbidden {
chats = append(chats, c)
}
return chats
}