-
Notifications
You must be signed in to change notification settings - Fork 194
/
dialog_model.go
275 lines (250 loc) · 8.93 KB
/
dialog_model.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
/*
* Copyright (c) 2017, https://github.com/nebulaim
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model
import (
"github.com/nebulaim/telegramd/mtproto"
"github.com/nebulaim/telegramd/biz_model/base"
"sync"
"github.com/nebulaim/telegramd/biz_model/dal/dao"
"github.com/nebulaim/telegramd/biz_model/dal/dataobject"
"github.com/golang/glog"
base2 "github.com/nebulaim/telegramd/base/base"
"time"
)
var (
dialogInstance *dialogModel
dialogInstanceOnce sync.Once
)
type dialogModel struct {
}
func GetDialogModel() *dialogModel {
dialogInstanceOnce.Do(func() {
dialogInstance = &dialogModel{}
})
return dialogInstance
}
func dialogDOToDialog(dialogDO* dataobject.UserDialogsDO) *mtproto.TLDialog {
dialog := &mtproto.TLDialog{}
dialog.Pinned = dialogDO.IsPinned == 1
switch dialogDO.PeerType {
case base.PEER_EMPTY:
case base.PEER_SELF, base.PEER_USER:
peer := &mtproto.TLPeerUser{dialogDO.PeerId}
dialog.Peer = peer.ToPeer()
case base.PEER_CHAT:
peer := &mtproto.TLPeerChat{dialogDO.PeerId}
dialog.Peer = peer.ToPeer()
case base.PEER_CHANNEL:
peer := &mtproto.TLPeerChannel{dialogDO.PeerId}
dialog.Peer = peer.ToPeer()
}
dialog.TopMessage = dialogDO.TopMessage
dialog.ReadInboxMaxId = dialogDO.ReadInboxMaxId
dialog.ReadOutboxMaxId = dialogDO.ReadOutboxMaxId
dialog.UnreadCount = dialogDO.UnreadCount
dialog.UnreadMentionsCount = dialogDO.UnreadMentionsCount
// TODO(@benqi): pts/draft
// NotifySettings
peerNotifySettings := &mtproto.TLPeerNotifySettings{}
peerNotifySettings.ShowPreviews = true
peerNotifySettings.MuteUntil = 0
peerNotifySettings.Sound = "default"
dialog.NotifySettings = peerNotifySettings.ToPeerNotifySettings()
return dialog
}
// dialog#e4def5db flags:# pinned:flags.2?true peer:Peer top_message:int read_inbox_max_id:int read_outbox_max_id:int unread_count:int unread_mentions_count:int notify_settings:PeerNotifySettings pts:flags.0?int draft:flags.1?DraftMessage = Dialog;
//message TL_dialog {
// bool pinned = 1;
// Peer peer = 2;
// int32 top_message = 3;
// int32 read_inbox_max_id = 4;
// int32 read_outbox_max_id = 5;
// int32 unread_count = 6;
// int32 unread_mentions_count = 7;
// PeerNotifySettings notify_settings = 8;
// int32 pts = 9;
// DraftMessage draft = 10;
//}
//func (m *dialogModel) GetDialog(userId int32, offsetPeer *base.PeerUtil) (dialog *mtproto.TLDialog) {
// slave := dao.GetUserDialogsDAO(dao.DB_SLAVE)
//
// dialogDO := slave.SelectByPeer(userId, int8(offsetPeer.PeerType), offsetPeer.PeerId)
// // _ = dialog
// // dialog := slave.SelectByPeer(userId, int8(offsetPeer.PeerType), offsetPeer.PeerId)
//
//}
//
//
///*
// exclude_pinned: YES [ BY BIT 0 IN FIELD flags ],
// offset_date: 0 [INT],
// offset_id: 0 [INT],
// offset_peer: { inputPeerEmpty },
// */
func (m *dialogModel) GetDialogsByOffsetDate(userId int32, excludePinned bool, offsetData int32, limit int32) (dialogs []*mtproto.TLDialog) {
dialogDOList := dao.GetUserDialogsDAO(dao.DB_SLAVE).SelectDialogsByPinnedAndOffsetDate(
userId, base2.BoolToInt8(!excludePinned), offsetData, limit)
for _, dialogDO := range dialogDOList {
dialog := &mtproto.TLDialog{}
dialog.Pinned = dialogDO.IsPinned == 1
switch dialogDO.PeerType {
case base.PEER_EMPTY:
continue
case base.PEER_SELF, base.PEER_USER:
peer := &mtproto.TLPeerUser{dialogDO.PeerId}
dialog.Peer = peer.ToPeer()
case base.PEER_CHAT:
peer := &mtproto.TLPeerChat{dialogDO.PeerId}
dialog.Peer = peer.ToPeer()
case base.PEER_CHANNEL:
peer := &mtproto.TLPeerChannel{dialogDO.PeerId}
dialog.Peer = peer.ToPeer()
}
dialog.TopMessage = dialogDO.TopMessage
dialog.ReadInboxMaxId = dialogDO.ReadInboxMaxId
dialog.ReadOutboxMaxId = dialogDO.ReadOutboxMaxId
dialog.UnreadCount = dialogDO.UnreadCount
dialog.UnreadMentionsCount = dialogDO.UnreadMentionsCount
// TODO(@benqi): pts/draft
// NotifySettings
peerNotifySettings := &mtproto.TLPeerNotifySettings{}
peerNotifySettings.ShowPreviews = true
peerNotifySettings.MuteUntil = 0
peerNotifySettings.Sound = "default"
dialog.NotifySettings = peerNotifySettings.ToPeerNotifySettings()
dialogs = append(dialogs, dialog)
}
return
}
func (m *dialogModel) GetDialogsByUserIDAndType(userId, peerType int32) (dialogs []*mtproto.TLDialog) {
dialogsDAO := dao.GetUserDialogsDAO(dao.DB_SLAVE)
var dialogDOList []dataobject.UserDialogsDO
if peerType == base.PEER_UNKNOWN || peerType == base.PEER_EMPTY {
dialogDOList = dialogsDAO.SelectDialogsByUserID(userId)
glog.Infof("SelectDialogsByUserID(%d) - {%v}", userId, dialogDOList)
} else {
dialogDOList = dialogsDAO.SelectDialogsByPeerType(userId, int8(peerType))
glog.Infof("SelectDialogsByPeerType(%d, %d) - {%v}", userId, int32(peerType), dialogDOList)
}
// []do.UserDialogsDO
// dialogDOList, _ := dialogsDAO.SelectDialogsByUserID(userId)
dialogs = []*mtproto.TLDialog{}
for _, dialogDO := range dialogDOList {
dialog := &mtproto.TLDialog{}
dialog.Pinned = dialogDO.IsPinned == 1
switch dialogDO.PeerType {
case base.PEER_EMPTY:
continue
case base.PEER_SELF, base.PEER_USER:
peer := &mtproto.TLPeerUser{dialogDO.PeerId}
dialog.Peer = peer.ToPeer()
case base.PEER_CHAT:
peer := &mtproto.TLPeerChat{dialogDO.PeerId}
dialog.Peer = peer.ToPeer()
case base.PEER_CHANNEL:
peer := &mtproto.TLPeerChannel{dialogDO.PeerId}
dialog.Peer = peer.ToPeer()
}
dialog.TopMessage = dialogDO.TopMessage
dialog.ReadInboxMaxId = dialogDO.ReadInboxMaxId
dialog.ReadOutboxMaxId = dialogDO.ReadOutboxMaxId
dialog.UnreadCount = dialogDO.UnreadCount
dialog.UnreadMentionsCount = dialogDO.UnreadMentionsCount
// TODO(@benqi): pts/draft
// NotifySettings
peerNotifySettings := &mtproto.TLPeerNotifySettings{}
peerNotifySettings.ShowPreviews = true
peerNotifySettings.MuteUntil = 0
peerNotifySettings.Sound = "default"
dialog.NotifySettings = peerNotifySettings.ToPeerNotifySettings()
dialogs = append(dialogs, dialog)
}
glog.Infof("SelectDialogsByPeerType(%d, %d) - {%v}", userId, int32(peerType), dialogs)
return
}
func (m *dialogModel) GetPinnedDialogs(userId int32) (dialogs []*mtproto.TLDialog) {
dialogDOList := dao.GetUserDialogsDAO(dao.DB_SLAVE).SelectPinnedDialogs(userId)
for _, dialogDO := range dialogDOList {
dialog := &mtproto.TLDialog{}
dialog.Pinned = dialogDO.IsPinned == 1
switch dialogDO.PeerType {
case base.PEER_EMPTY:
continue
case base.PEER_SELF, base.PEER_USER:
peer := &mtproto.TLPeerUser{dialogDO.PeerId}
dialog.Peer = peer.ToPeer()
case base.PEER_CHAT:
peer := &mtproto.TLPeerChat{dialogDO.PeerId}
dialog.Peer = peer.ToPeer()
case base.PEER_CHANNEL:
peer := &mtproto.TLPeerChannel{dialogDO.PeerId}
dialog.Peer = peer.ToPeer()
}
dialog.TopMessage = dialogDO.TopMessage
dialog.ReadInboxMaxId = dialogDO.ReadInboxMaxId
dialog.ReadOutboxMaxId = dialogDO.ReadOutboxMaxId
dialog.UnreadCount = dialogDO.UnreadCount
dialog.UnreadMentionsCount = dialogDO.UnreadMentionsCount
// TODO(@benqi): pts/draft
// NotifySettings
peerNotifySettings := &mtproto.TLPeerNotifySettings{}
peerNotifySettings.ShowPreviews = true
peerNotifySettings.MuteUntil = 0
peerNotifySettings.Sound = "default"
dialog.NotifySettings = peerNotifySettings.ToPeerNotifySettings()
dialogs = append(dialogs, dialog)
}
return
}
//func (m *dialogModel) UpdateTopMessage(dialogId, topMessage int32) {
// dialogsDAO := dao.GetUserDialogsDAO(dao.DB_MASTER)
// dialogsDAO.UpdateTopMessage(topMessage, dialogId)
//}
func (m *dialogModel) CreateOrUpdateByLastMessage(userId int32, peerType int32, peerId int32, topMessage int32, unreadMentions bool) (dialogId int32) {
// TODO(@benqi): 事务
// 创建会话
slave := dao.GetUserDialogsDAO(dao.DB_SLAVE)
master := dao.GetUserDialogsDAO(dao.DB_MASTER)
date := int32(time.Now().Unix())
dialog :=slave.SelectByPeer(userId, int8(peerType), peerId)
if dialog == nil {
dialog = &dataobject.UserDialogsDO{}
dialog.UserId = userId
dialog.PeerType = int8(peerType)
dialog.PeerId = peerId
if unreadMentions {
dialog.UnreadMentionsCount = 1
} else {
dialog.UnreadMentionsCount = 0
}
dialog.UnreadCount = 1
dialog.TopMessage = topMessage
dialog.CreatedAt = base2.NowFormatYMDHMS()
dialog.Date2 = date
dialogId = int32(master.Insert(dialog))
} else {
if unreadMentions {
dialog.UnreadMentionsCount += 1
}
dialog.UnreadCount += 1
dialog.TopMessage = topMessage
dialog.Date2 = date
dialogId = dialog.Id
master.UpdateTopMessage(topMessage, dialog.UnreadCount, dialog.UnreadMentionsCount, date, dialog.Id)
}
return
}