forked from openimsdk/open-im-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'openimsdk:main' into main
- Loading branch information
Showing
39 changed files
with
1,155 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
package jssdk | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/openimsdk/protocol/conversation" | ||
"github.com/openimsdk/protocol/msg" | ||
"github.com/openimsdk/protocol/sdkws" | ||
"github.com/openimsdk/tools/a2r" | ||
"github.com/openimsdk/tools/mcontext" | ||
"github.com/openimsdk/tools/utils/datautil" | ||
"sort" | ||
) | ||
|
||
const ( | ||
maxGetActiveConversation = 500 | ||
defaultGetActiveConversation = 100 | ||
) | ||
|
||
func NewJSSdkApi(msg msg.MsgClient, conv conversation.ConversationClient) *JSSdk { | ||
return &JSSdk{ | ||
msg: msg, | ||
conv: conv, | ||
} | ||
} | ||
|
||
type JSSdk struct { | ||
msg msg.MsgClient | ||
conv conversation.ConversationClient | ||
} | ||
|
||
func (x *JSSdk) GetActiveConversations(c *gin.Context) { | ||
call(c, x.getActiveConversations) | ||
} | ||
|
||
func (x *JSSdk) GetConversations(c *gin.Context) { | ||
call(c, x.getConversations) | ||
} | ||
|
||
func (x *JSSdk) getActiveConversations(ctx *gin.Context) (*ConversationsResp, error) { | ||
req, err := a2r.ParseRequest[ActiveConversationsReq](ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if req.Count <= 0 || req.Count > maxGetActiveConversation { | ||
req.Count = defaultGetActiveConversation | ||
} | ||
opUserID := mcontext.GetOpUserID(ctx) | ||
conversationIDs, err := field(ctx, x.conv.GetConversationIDs, | ||
&conversation.GetConversationIDsReq{UserID: opUserID}, (*conversation.GetConversationIDsResp).GetConversationIDs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(conversationIDs) == 0 { | ||
return &ConversationsResp{}, nil | ||
} | ||
readSeq, err := field(ctx, x.msg.GetHasReadSeqs, | ||
&msg.GetHasReadSeqsReq{UserID: opUserID, ConversationIDs: conversationIDs}, (*msg.SeqsInfoResp).GetMaxSeqs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
activeConversation, err := field(ctx, x.msg.GetActiveConversation, | ||
&msg.GetActiveConversationReq{ConversationIDs: conversationIDs}, (*msg.GetActiveConversationResp).GetConversations) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(activeConversation) == 0 { | ||
return &ConversationsResp{}, nil | ||
} | ||
sortConversations := sortActiveConversations{ | ||
Conversation: activeConversation, | ||
} | ||
if len(activeConversation) > 1 { | ||
pinnedConversationIDs, err := field(ctx, x.conv.GetPinnedConversationIDs, | ||
&conversation.GetPinnedConversationIDsReq{UserID: opUserID}, (*conversation.GetPinnedConversationIDsResp).GetConversationIDs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
sortConversations.PinnedConversationIDs = datautil.SliceSet(pinnedConversationIDs) | ||
} | ||
sort.Sort(&sortConversations) | ||
sortList := sortConversations.Top(req.Count) | ||
conversations, err := field(ctx, x.conv.GetConversations, | ||
&conversation.GetConversationsReq{ | ||
OwnerUserID: opUserID, | ||
ConversationIDs: datautil.Slice(sortList, func(c *msg.ActiveConversation) string { | ||
return c.ConversationID | ||
})}, (*conversation.GetConversationsResp).GetConversations) | ||
if err != nil { | ||
return nil, err | ||
} | ||
msgs, err := field(ctx, x.msg.GetSeqMessage, | ||
&msg.GetSeqMessageReq{ | ||
UserID: opUserID, | ||
Conversations: datautil.Slice(sortList, func(c *msg.ActiveConversation) *msg.ConversationSeqs { | ||
return &msg.ConversationSeqs{ | ||
ConversationID: c.ConversationID, | ||
Seqs: []int64{c.MaxSeq}, | ||
} | ||
}), | ||
}, (*msg.GetSeqMessageResp).GetMsgs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
conversationMap := datautil.SliceToMap(conversations, func(c *conversation.Conversation) string { | ||
return c.ConversationID | ||
}) | ||
resp := make([]ConversationMsg, 0, len(sortList)) | ||
for _, c := range sortList { | ||
conv, ok := conversationMap[c.ConversationID] | ||
if !ok { | ||
continue | ||
} | ||
var lastMsg *sdkws.MsgData | ||
if msgList, ok := msgs[c.ConversationID]; ok && len(msgList.Msgs) > 0 { | ||
lastMsg = msgList.Msgs[0] | ||
} | ||
resp = append(resp, ConversationMsg{ | ||
Conversation: conv, | ||
LastMsg: lastMsg, | ||
MaxSeq: c.MaxSeq, | ||
ReadSeq: readSeq[c.ConversationID], | ||
}) | ||
} | ||
var unreadCount int64 | ||
for _, c := range activeConversation { | ||
count := c.MaxSeq - readSeq[c.ConversationID] | ||
if count > 0 { | ||
unreadCount += count | ||
} | ||
} | ||
return &ConversationsResp{ | ||
Conversations: resp, | ||
UnreadCount: unreadCount, | ||
}, nil | ||
} | ||
|
||
func (x *JSSdk) getConversations(ctx *gin.Context) (*ConversationsResp, error) { | ||
req, err := a2r.ParseRequest[conversation.GetConversationsReq](ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.OwnerUserID = mcontext.GetOpUserID(ctx) | ||
conversations, err := field(ctx, x.conv.GetConversations, req, (*conversation.GetConversationsResp).GetConversations) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(conversations) == 0 { | ||
return &ConversationsResp{}, nil | ||
} | ||
req.ConversationIDs = datautil.Slice(conversations, func(c *conversation.Conversation) string { | ||
return c.ConversationID | ||
}) | ||
maxSeqs, err := field(ctx, x.msg.GetMaxSeqs, | ||
&msg.GetMaxSeqsReq{ConversationIDs: req.ConversationIDs}, (*msg.SeqsInfoResp).GetMaxSeqs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
readSeqs, err := field(ctx, x.msg.GetHasReadSeqs, | ||
&msg.GetHasReadSeqsReq{UserID: req.OwnerUserID, ConversationIDs: req.ConversationIDs}, (*msg.SeqsInfoResp).GetMaxSeqs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
conversationSeqs := make([]*msg.ConversationSeqs, 0, len(conversations)) | ||
for _, c := range conversations { | ||
if seq := maxSeqs[c.ConversationID]; seq > 0 { | ||
conversationSeqs = append(conversationSeqs, &msg.ConversationSeqs{ | ||
ConversationID: c.ConversationID, | ||
Seqs: []int64{seq}, | ||
}) | ||
} | ||
} | ||
var msgs map[string]*sdkws.PullMsgs | ||
if len(conversationSeqs) > 0 { | ||
msgs, err = field(ctx, x.msg.GetSeqMessage, | ||
&msg.GetSeqMessageReq{UserID: req.OwnerUserID, Conversations: conversationSeqs}, (*msg.GetSeqMessageResp).GetMsgs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
resp := make([]ConversationMsg, 0, len(conversations)) | ||
for _, c := range conversations { | ||
var lastMsg *sdkws.MsgData | ||
if msgList, ok := msgs[c.ConversationID]; ok && len(msgList.Msgs) > 0 { | ||
lastMsg = msgList.Msgs[0] | ||
} | ||
resp = append(resp, ConversationMsg{ | ||
Conversation: c, | ||
LastMsg: lastMsg, | ||
MaxSeq: maxSeqs[c.ConversationID], | ||
ReadSeq: readSeqs[c.ConversationID], | ||
}) | ||
} | ||
var unreadCount int64 | ||
for conversationID, maxSeq := range maxSeqs { | ||
count := maxSeq - readSeqs[conversationID] | ||
if count > 0 { | ||
unreadCount += count | ||
} | ||
} | ||
return &ConversationsResp{ | ||
Conversations: resp, | ||
UnreadCount: unreadCount, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package jssdk | ||
|
||
import "github.com/openimsdk/protocol/msg" | ||
|
||
type sortActiveConversations struct { | ||
Conversation []*msg.ActiveConversation | ||
PinnedConversationIDs map[string]struct{} | ||
} | ||
|
||
func (s sortActiveConversations) Top(limit int) []*msg.ActiveConversation { | ||
if limit > 0 && len(s.Conversation) > limit { | ||
return s.Conversation[:limit] | ||
} | ||
return s.Conversation | ||
} | ||
|
||
func (s sortActiveConversations) Len() int { | ||
return len(s.Conversation) | ||
} | ||
|
||
func (s sortActiveConversations) Less(i, j int) bool { | ||
iv, jv := s.Conversation[i], s.Conversation[j] | ||
_, ip := s.PinnedConversationIDs[iv.ConversationID] | ||
_, jp := s.PinnedConversationIDs[jv.ConversationID] | ||
if ip != jp { | ||
return ip | ||
} | ||
return iv.LastTime > jv.LastTime | ||
} | ||
|
||
func (s sortActiveConversations) Swap(i, j int) { | ||
s.Conversation[i], s.Conversation[j] = s.Conversation[j], s.Conversation[i] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package jssdk | ||
|
||
import ( | ||
"github.com/openimsdk/protocol/conversation" | ||
"github.com/openimsdk/protocol/sdkws" | ||
) | ||
|
||
type ActiveConversationsReq struct { | ||
Count int `json:"count"` | ||
} | ||
|
||
type ConversationMsg struct { | ||
Conversation *conversation.Conversation `json:"conversation"` | ||
LastMsg *sdkws.MsgData `json:"lastMsg"` | ||
MaxSeq int64 `json:"maxSeq"` | ||
ReadSeq int64 `json:"readSeq"` | ||
} | ||
|
||
type ConversationsResp struct { | ||
UnreadCount int64 `json:"unreadCount"` | ||
Conversations []ConversationMsg `json:"conversations"` | ||
} |
Oops, something went wrong.