/
user_list.go
374 lines (316 loc) · 8.49 KB
/
user_list.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package ui
import (
"github.com/hawklithm/anychatcmd/utils"
"github.com/hawklithm/anychatcmd/wechat"
ui "github.com/hawklithm/termui"
"github.com/hawklithm/termui/widgets"
"log"
"strings"
"time"
)
type UserList struct {
currentId string
curUserIndex int
logger *log.Logger
baseX int
baseY int
width int
height int
recentUserList []UserInfo
recentGroupList []*Group
userList *SortItems
groupList *SortItems
selectEvent chan SelectEvent
we *wechat.Wechat
tabPane *widgets.TabPane
userNickListBox *widgets.List
groupNickListBox *widgets.List
recentListBox *widgets.List
tabWidgets []*widgets.List
tabLists []*SortItems
picked bool
currentTab *widgets.List
recentList *SortItems
currentList *SortItems
}
func (this *UserList) Pick() {
this.picked = true
this.currentTab.BorderStyle = ui.NewStyle(ui.ColorRed)
ui.Render(this.currentTab)
}
func (this *UserList) Unpick() {
this.picked = false
this.currentTab.BorderStyle = ui.NewStyle(ui.ColorMagenta)
ui.Render(this.currentTab)
}
type UserChangeEvent struct {
}
type SelectEvent interface {
GetId() string
GetName() string
GetType() string
GetLastChatTime() time.Time
GetUserList() []*UserInfo
}
type UserInfo struct {
UserId string
Nick string
DisplayName string
LastChatTime time.Time
}
func (l UserInfo) GetUserList() []*UserInfo {
return []*UserInfo{&l}
}
func (l UserInfo) GetLastChatTime() time.Time {
return l.LastChatTime
}
func (l UserInfo) GetId() string {
return l.UserId
}
func (l UserInfo) GetName() string {
return utils.If(l.DisplayName != "", l.DisplayName, l.Nick).(string)
}
func (l UserInfo) GetType() string {
return "user"
}
//用户群组
type Group struct {
UserList []*UserInfo
Name string
GroupId string
LastChatTime time.Time
}
func (l Group) GetUserList() []*UserInfo {
return l.UserList
}
func (l Group) GetLastChatTime() time.Time {
return l.LastChatTime
}
func (l Group) GetId() string {
return l.GroupId
}
func (l Group) GetName() string {
return l.Name
}
func (l Group) GetType() string {
return "group"
}
type SortItems []SelectEvent
func (p SortItems) Len() int { return len(p) }
func (p SortItems) Less(i, j int) bool {
return p[i].GetLastChatTime().After(p[j].GetLastChatTime())
}
func merge(userInfo []UserInfo, groupInfo []*Group) *SortItems {
z := make(SortItems, len(userInfo)+len(groupInfo))
var count = 0
for _, user := range userInfo {
z[count] = user
count++
}
for _, group := range groupInfo {
z[count] = group
count++
}
return &z
}
func (this *UserList) renderTab() {
this.currentTab = this.tabWidgets[this.tabPane.ActiveTabIndex]
this.currentList = this.tabLists[this.tabPane.ActiveTabIndex]
this.refreshCurrentSelect()
ui.Render(this.tabPane, this.currentTab)
}
func InitTalkInfo(w *wechat.Wechat, logger *log.Logger, groups []*Group) {
var groupIds []string
groupMap := make(map[string]*Group)
for _, group := range groups {
groupIds = append(groupIds, group.GroupId)
groupMap[group.GroupId] = group
}
l := len(groupIds)
var end int
for index := 0; index < l; index += 50 {
end = index + 50
if end > l {
end = l
}
if members, err := w.GetContactsInBatch(groupIds[index:end]); err != nil {
logger.Println("init contact error!", err)
} else {
if members != nil && len(members) > 0 {
for _, member := range members {
groupMap[member.UserName].
UserList = convertMemeberToUserInfo(member.MemberList)
}
}
}
}
}
func convertMemeberToUserInfo(users []wechat.User) []*UserInfo {
var userInfos []*UserInfo
if users == nil || len(users) == 0 {
return userInfos
}
for _, u := range users {
userInfos = append(userInfos, &UserInfo{
UserId: u.UserName,
Nick: u.NickName,
DisplayName: u.RemarkName})
}
return userInfos
}
func (this *UserList) refreshCurrentSelect() {
this.curUserIndex = this.currentTab.SelectedRow
if this.selectEvent != nil {
u := (*this.currentList)[this.curUserIndex]
if strings.HasPrefix(u.GetId(), "@@") && len(u.GetUserList()) == 0 {
InitTalkInfo(this.we, this.logger, []*Group{u.(*Group)})
}
this.selectEvent <- u
} else {
this.logger.Println("warning!", "no select event channel set!")
}
}
func (this *UserList) Reset() {
this.userNickListBox = widgets.NewList()
//userNickListBox.Title = "用户列表"
//userNickListBox.BorderStyle = ui.NewStyle(ui.ColorMagenta)
//userNickListBox.Border = true
this.userNickListBox.TextStyle = ui.NewStyle(ui.ColorYellow)
this.userNickListBox.WrapText = false
this.userNickListBox.SelectedRowStyle = ui.NewStyle(ui.ColorWhite,
ui.ColorRed)
this.userNickListBox.SetRect(this.baseX, this.baseY+2,
this.baseX+this.width,
this.baseY+this.height)
groupNickListBox := widgets.NewList()
//userNickListBox.Title = "用户列表"
//userNickListBox.BorderStyle = ui.NewStyle(ui.ColorMagenta)
//userNickListBox.Border = true
groupNickListBox.TextStyle = ui.NewStyle(ui.ColorYellow)
groupNickListBox.WrapText = false
groupNickListBox.SelectedRowStyle = ui.NewStyle(ui.ColorWhite, ui.ColorRed)
groupNickListBox.SetRect(this.baseX, this.baseY+2, this.baseX+this.width,
this.baseY+this.height)
this.groupNickListBox = groupNickListBox
this.recentListBox = widgets.NewList()
this.recentListBox.TextStyle = ui.NewStyle(ui.ColorYellow)
this.recentListBox.WrapText = false
this.recentListBox.SelectedRowStyle = ui.NewStyle(ui.ColorWhite,
ui.ColorRed)
this.recentListBox.SetRect(this.baseX, this.baseY+2,
this.baseX+this.width,
this.baseY+this.height)
nickList := make([]string, len(*this.userList))
groupList := make([]string, len(*this.groupList))
for i, user := range *this.userList {
nickList[i] = user.GetName()
}
this.userNickListBox.Rows = nickList
for i, group := range *this.groupList {
groupList[i] = group.GetName()
}
this.groupNickListBox.Rows = groupList
this.recentList = merge(this.recentUserList, this.recentGroupList)
recentNickList := make([]string, this.recentList.Len())
for i, r := range *this.recentList {
recentNickList[i] = r.GetName()
}
this.recentListBox.Rows = recentNickList
this.tabPane = widgets.NewTabPane("聊天列表", "好友列表", "群列表")
this.tabPane.SetRect(this.baseX, this.baseY, this.baseX+this.width,
this.baseY+3)
this.tabPane.Border = true
tabWidgets := make([]*widgets.List, 3)
tabWidgets[0] = this.recentListBox
tabWidgets[1] = this.userNickListBox
tabWidgets[2] = this.groupNickListBox
this.tabWidgets = tabWidgets
this.tabLists = make([]*SortItems, 3)
this.tabLists[0] = this.recentList
this.tabLists[1] = this.userList
this.tabLists[2] = this.groupList
this.currentTab = this.recentListBox
this.currentList = this.recentList
ui.Render(this.tabPane, this.currentTab)
}
func convertUsersToSortItems(users []UserInfo) *SortItems {
z := make(SortItems, len(users))
for i, user := range users {
z[i] = user
}
return &z
}
func convertGroupsToSortItems(groups []*Group) *SortItems {
z := make(SortItems, len(groups))
for i, user := range groups {
z[i] = user
}
return &z
}
func NewUserList(recentUserList []UserInfo, recentGroupList []*Group,
userList []UserInfo,
groupList []*Group,
selectEvent chan SelectEvent, width,
height,
baseX, baseY int,
logger *log.Logger, we *wechat.Wechat) *UserList {
// chinese := false
convertUsersToSortItems(userList)
l := &UserList{
curUserIndex: 0,
logger: logger,
baseX: baseX,
baseY: baseY,
width: width,
height: height,
recentUserList: recentUserList,
recentGroupList: recentGroupList,
userList: convertUsersToSortItems(userList),
groupList: convertGroupsToSortItems(groupList),
selectEvent: selectEvent,
we: we,
}
l.Reset()
return l
}
func (l *UserList) InvokeSelect() {
l.refreshCurrentSelect()
}
func (l *UserList) nextUser() {
l.currentTab.ScrollDown()
l.refreshCurrentSelect()
ui.Render(l.currentTab)
}
func (l *UserList) prevUser() {
l.currentTab.ScrollUp()
l.refreshCurrentSelect()
ui.Render(l.currentTab)
}
func (l *UserList) focuseLeft() {
l.tabPane.FocusLeft()
l.renderTab()
}
func (l *UserList) focuseRight() {
l.tabPane.FocusRight()
l.renderTab()
}
func (this *UserList) Action(e ui.Event) bool {
if !this.picked {
return false
}
switch e.ID {
case "j":
this.nextUser()
return true
case "k":
this.prevUser()
return true
case "h":
this.focuseLeft()
return true
case "l":
this.focuseRight()
return true
}
return false
}