-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
conversation.go
199 lines (178 loc) · 5.33 KB
/
conversation.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
// Copyright © 2023 OpenIM. 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 msgprocessor
import (
"sort"
"strings"
"github.com/openimsdk/protocol/constant"
"github.com/openimsdk/protocol/sdkws"
"github.com/openimsdk/tools/errs"
"google.golang.org/protobuf/proto"
)
func GetNotificationConversationIDByMsg(msg *sdkws.MsgData) string {
switch msg.SessionType {
case constant.SingleChatType:
l := []string{msg.SendID, msg.RecvID}
sort.Strings(l)
return "n_" + strings.Join(l, "_")
case constant.WriteGroupChatType:
return "n_" + msg.GroupID
case constant.ReadGroupChatType:
return "n_" + msg.GroupID
case constant.NotificationChatType:
return "n_" + msg.SendID + "_" + msg.RecvID
}
return ""
}
func GetChatConversationIDByMsg(msg *sdkws.MsgData) string {
switch msg.SessionType {
case constant.SingleChatType:
l := []string{msg.SendID, msg.RecvID}
sort.Strings(l)
return "si_" + strings.Join(l, "_")
case constant.WriteGroupChatType:
return "g_" + msg.GroupID
case constant.ReadGroupChatType:
return "sg_" + msg.GroupID
case constant.NotificationChatType:
return "sn_" + msg.SendID + "_" + msg.RecvID
}
return ""
}
func GenConversationUniqueKey(msg *sdkws.MsgData) string {
switch msg.SessionType {
case constant.SingleChatType, constant.NotificationChatType:
l := []string{msg.SendID, msg.RecvID}
sort.Strings(l)
return strings.Join(l, "_")
case constant.ReadGroupChatType:
return msg.GroupID
}
return ""
}
func GetConversationIDByMsg(msg *sdkws.MsgData) string {
options := Options(msg.Options)
switch msg.SessionType {
case constant.SingleChatType:
l := []string{msg.SendID, msg.RecvID}
sort.Strings(l)
if !options.IsNotNotification() {
return "n_" + strings.Join(l, "_")
}
return "si_" + strings.Join(l, "_") // single chat
case constant.WriteGroupChatType:
if !options.IsNotNotification() {
return "n_" + msg.GroupID // group chat
}
return "g_" + msg.GroupID // group chat
case constant.ReadGroupChatType:
if !options.IsNotNotification() {
return "n_" + msg.GroupID // super group chat
}
return "sg_" + msg.GroupID // super group chat
case constant.NotificationChatType:
if !options.IsNotNotification() {
return "n_" + msg.SendID + "_" + msg.RecvID // super group chat
}
return "sn_" + msg.SendID + "_" + msg.RecvID // server notification chat
}
return ""
}
func GetConversationIDBySessionType(sessionType int, ids ...string) string {
sort.Strings(ids)
if len(ids) > 2 || len(ids) < 1 {
return ""
}
switch sessionType {
case constant.SingleChatType:
return "si_" + strings.Join(ids, "_") // single chat
case constant.WriteGroupChatType:
return "g_" + ids[0] // group chat
case constant.ReadGroupChatType:
return "sg_" + ids[0] // super group chat
case constant.NotificationChatType:
return "sn_" + ids[0] // server notification chat
}
return ""
}
func GetNotificationConversationIDByConversationID(conversationID string) string {
l := strings.Split(conversationID, "_")
if len(l) > 1 {
l[0] = "n"
return strings.Join(l, "_")
}
return ""
}
func GetNotificationConversationID(sessionType int, ids ...string) string {
sort.Strings(ids)
if len(ids) > 2 || len(ids) < 1 {
return ""
}
switch sessionType {
case constant.SingleChatType:
return "n_" + strings.Join(ids, "_") // single chat
case constant.ReadGroupChatType:
return "n_" + ids[0] // super group chat
}
return ""
}
func IsNotification(conversationID string) bool {
return strings.HasPrefix(conversationID, "n_")
}
func IsNotificationByMsg(msg *sdkws.MsgData) bool {
return !Options(msg.Options).IsNotNotification()
}
func ParseConversationID(msg *sdkws.MsgData) (isNotification bool, conversationID string) {
options := Options(msg.Options)
switch msg.SessionType {
case constant.SingleChatType:
l := []string{msg.SendID, msg.RecvID}
sort.Strings(l)
if !options.IsNotNotification() {
return true, "n_" + strings.Join(l, "_")
}
return false, "si_" + strings.Join(l, "_") // single chat
case constant.ReadGroupChatType:
if !options.IsNotNotification() {
return true, "n_" + msg.GroupID // super group chat
}
return false, "sg_" + msg.GroupID // super group chat
case constant.NotificationChatType:
if !options.IsNotNotification() {
return true, "n_" + msg.SendID + "_" + msg.RecvID // super group chat
}
return false, "sn_" + msg.SendID + "_" + msg.RecvID // server notification chat
}
return false, ""
}
type MsgBySeq []*sdkws.MsgData
func (s MsgBySeq) Len() int {
return len(s)
}
func (s MsgBySeq) Less(i, j int) bool {
return s[i].Seq < s[j].Seq
}
func (s MsgBySeq) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func Pb2String(pb proto.Message) (string, error) {
s, err := proto.Marshal(pb)
if err != nil {
return "", errs.Wrap(err)
}
return string(s), nil
}
func String2Pb(s string, pb proto.Message) error {
return proto.Unmarshal([]byte(s), pb)
}