-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongPollEvent.go
187 lines (176 loc) · 4.35 KB
/
LongPollEvent.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
package VkApi
import (
"errors"
"strconv"
)
const LPMessageReplaceFlag = 1
const LPMessageSetFlag = 2
const LPMessageDropFlag = 3
const LPNewMessage = 4
const LPEditMessage = 5
const LPReadInputMessage = 6
const LPReadOutputMessage = 7
const LPOnline = 8
const LPOffline = 9
const LPDialogDropFlag = 10
const LPDialogReplaceFlang = 11
const LPDialogSetFlag = 12
const LPDeleteMessage = 13
const LPRestoreMessage = 14
const LPChangeGroupDialogSettings = 51
const LPTyping = 61
const LPTypingAtGroupDialog = 62
const LPCall = 70
const LPCounter = 80
const LPNotifications = 114
const FLAG_OUTBOX = 2
const FLAG_UNREAD = 1
type ActorAndUser struct {
Actor int
User int
}
type MessageEvent struct {
MessageId int
Mask int
PeerId int
Timestamp int
Text string
RandomId int
ChatCreate bool
ChatTitleUpdate bool
ChatPhotoUpdate bool
ChatInviteUser *ActorAndUser
ChatKilUser *ActorAndUser
Title string
IsEditMessage bool
Emoji bool
FromAdmin int
Geo string
GeoProvider string
From int
}
func (m *MessageEvent) Fill(data Update) error {
if len(data) >= 6 {
if d, ok := data[1].(float64); ok {
m.MessageId = int(d)
} else {
return errors.New("No message id\n")
}
if d, ok := data[2].(float64); ok {
m.Mask = int(d)
} else {
return errors.New("No message mask\n")
}
if d, ok := data[3].(float64); ok {
m.PeerId = int(d)
} else {
return errors.New("No message peer id \n")
}
if d, ok := data[4].(float64); ok {
m.Timestamp = int(d)
} else {
return errors.New("No message timestamp\n")
}
if d, ok := data[5].(string); ok {
m.Text = d
} else {
return errors.New("No message text\n")
}
if len(data) >= 7 {
if d, ok := data[6].(map[string]interface{}); ok {
if err := m.fillAttachments(d); err != nil {
return err
}
} else {
return errors.New("Bad attachments type\n")
}
}
if len(data) >= 9 {
if d, ok := data[7].(float64); ok {
m.RandomId = int(d)
} else {
return errors.New("No message random id\n")
}
}
return nil
} else {
return errors.New("Bad message size\n")
}
}
func (m *MessageEvent) fillAttachments(attachments map[string]interface{}) error {
if len(attachments) > 0 {
for key, value := range attachments {
if v, ok := value.(string); ok && key == "source_act" {
if v == "chat_create" {
m.ChatCreate = true
} else if v == "chat_title_update" {
m.ChatTitleUpdate = true
} else if v == "chat_photo_update" {
m.ChatPhotoUpdate = true
} else if v == "chat_invite_user" {
user, hasUser := attachments["source_mid"]
actor, hasActor := attachments["from"]
if hasUser && hasActor {
uId, u := user.(string)
aId, a := actor.(string)
if a && u {
uIdInt, errU := strconv.Atoi(uId)
aIdInt, errA := strconv.Atoi(aId)
if errA == nil && errU == nil {
m.ChatInviteUser = &ActorAndUser{int(aIdInt), int(uIdInt)}
} else if errU != nil {
return errU
} else if errA != nil {
return errA
}
}
}
} else if v == "chat_kick_user" {
user, hasUser := attachments["source_mid"]
actor, hasActor := attachments["from"]
if hasUser && hasActor {
uId, u := user.(string)
aId, a := actor.(string)
if a && u {
uIdInt, errU := strconv.Atoi(uId)
aIdInt, errA := strconv.Atoi(aId)
if errA == nil && errU == nil {
m.ChatKilUser = &ActorAndUser{int(aIdInt), int(uIdInt)}
} else if errU != nil {
return errU
} else if errA != nil {
return errA
}
}
}
}
}
if v, ok := value.(string); ok && key == "title" {
m.Title = v
}
if _, ok := value.(float64); ok && key == "emoji" {
m.Emoji = true
}
if userId, ok := value.(float64); ok && key == "from_admin" {
m.FromAdmin = int(userId)
}
if geo, ok := value.(string); ok && key == "geo" {
m.Geo = geo
}
if geoProvider, ok := value.(string); ok && key == "geo_provider" {
m.GeoProvider = geoProvider
}
if from, ok := value.(string); ok && key == "from" {
fromId, err := strconv.Atoi(from)
if err != nil {
return err
}
m.From = int(fromId)
}
}
}
return nil
}
func (m *MessageEvent) IsOutMessage() bool {
return m.Mask&FLAG_OUTBOX == FLAG_OUTBOX
}