/
channel.go
167 lines (138 loc) · 3.8 KB
/
channel.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
package gorevolt
import "fmt"
// Server will provide the server object which the channel belongs to.
// If the server could not be found the result will be nil
func (channel *Channel) Server(c *Client) *Server {
return c.cache.GetServer(channel.ServerID)
}
// SendMessage will send message in the channel
func (channel *Channel) SendMessage(c *Client, content string) (*Message, error) {
return sendMessage(c, channel.ID, &newMessage{
Content: content,
})
}
// SendMessagef will format the message being sent to the channel
func (channel *Channel) SendMessagef(c *Client, format string, a ...interface{}) (*Message, error) {
return sendMessage(c, channel.ID, &newMessage{
Content: fmt.Sprintf(format, a...),
})
}
func updateChannel(c *Client, update *channelUpdate) {
old := c.cache.GetChannel(update.ID)
if old == nil {
// TODO: fetch channel and populate cache
return
}
// clone channel
current := *old
for i := 0; i < len(update.Clear); i++ {
switch update.Clear[i] {
case "Icon":
// TODO: channel updated icons
case "Description":
current.Description = ""
}
}
// update changed fields
for k, v := range update.Data {
switch k {
case "name":
current.Name = v.(string)
case "description":
current.Description = v.(string)
case "nfsw":
current.NFSW = v.(bool)
case "channel_type":
current.ChannelType = v.(string)
case "default_permissions":
current.DefaultPermissions = interfacesToPermissions(v)
case "role_permissions":
current.RolePermissions = v.(map[string]Permissions)
}
}
c.cache.PutChannel(¤t)
// Execute on channel updated handler
for _, handler := range c.handlers.channelUpdate {
go handler(c, old, ¤t)
}
}
// Channels returns the channels which belong to this server
func (s *Server) Channels(c *Client) (channels []*Channel) {
for i := 0; i < len(s.ChannelIDs); i++ {
channels = append(channels, c.cache.GetChannel(s.ChannelIDs[i]))
}
return channels
}
func updateServer(c *Client, update *serverUpdate) {
old := c.cache.GetServer(update.ID)
if old == nil {
// TODO: fetch server and populate cache
return
}
// clone server
current := *old
for i := 0; i < len(update.Clear); i++ {
switch update.Clear[i] {
case "Icon":
// TODO: server updated icons
case "Description":
current.Description = ""
case "Banner":
// TODO: clear server banner
}
}
// update changed fields
for k, v := range update.Data {
switch k {
case "name":
current.Name = v.(string)
case "description":
current.Description = v.(string)
case "owner":
current.OwnerID = v.(string)
case "channels":
current.ChannelIDs = interfacesToStrings(v.([]interface{}))
case "default_permissions":
current.DefaultPermissions = v.(int64)
case "roles":
current.Roles = v.(map[string]Role)
case "categories":
current.Categories = interfacesToCategory(v.([]interface{}))
}
}
c.cache.PutServer(¤t)
// Execute on channel updated handler
for _, handler := range c.handlers.serverUpdate {
go handler(c, old, ¤t)
}
}
func interfacesToStrings(a []interface{}) []string {
output := make([]string, len(a))
for i := 0; i < len(a); i++ {
output[i] = a[i].(string)
}
return output
}
func interfacesToCategory(a []interface{}) []Category {
output := make([]Category, len(a))
// preallocate a category map
x := make(map[string]interface{}, 3)
for i := 0; i < len(a); i++ {
// cast type to map
x = a[i].(map[string]interface{})
// cast and place values into category
output[i] = Category{
ID: x["id"].(string),
Channels: interfacesToStrings(x["channels"].([]interface{})),
Title: x["title"].(string),
}
}
return output
}
func interfacesToPermissions(a interface{}) Permissions {
p := a.(map[string]interface{})
return Permissions{
A: int(p["a"].(float64)),
D: int(p["d"].(float64)),
}
}