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.
- Loading branch information
Showing
9 changed files
with
158 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package msg | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/model" | ||
"github.com/openimsdk/open-im-server/v3/pkg/msgprocessor" | ||
"github.com/openimsdk/protocol/constant" | ||
"github.com/openimsdk/protocol/msg" | ||
"github.com/openimsdk/protocol/sdkws" | ||
"github.com/openimsdk/tools/errs" | ||
"time" | ||
) | ||
|
||
const StreamDeadlineTime = time.Second * 60 * 10 | ||
|
||
func (m *msgServer) handlerStreamMsg(ctx context.Context, msgData *sdkws.MsgData) error { | ||
now := time.Now() | ||
val := &model.StreamMsg{ | ||
ClientMsgID: msgData.ClientMsgID, | ||
ConversationID: msgprocessor.GetConversationIDByMsg(msgData), | ||
UserID: msgData.SendID, | ||
CreateTime: now, | ||
DeadlineTime: now.Add(StreamDeadlineTime), | ||
} | ||
return m.StreamMsgDatabase.CreateStreamMsg(ctx, val) | ||
} | ||
|
||
func (m *msgServer) getStreamMsg(ctx context.Context, clientMsgID string) (*model.StreamMsg, error) { | ||
res, err := m.StreamMsgDatabase.GetStreamMsg(ctx, clientMsgID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !res.End && res.DeadlineTime.Before(time.Now()) { | ||
res.End = true | ||
} | ||
return res, nil | ||
} | ||
|
||
func (m *msgServer) AppendStreamMsg(ctx context.Context, req *msg.AppendStreamMsgReq) (*msg.AppendStreamMsgResp, error) { | ||
res, err := m.getStreamMsg(ctx, req.ClientMsgID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if res.End { | ||
return nil, errs.ErrNoPermission.WrapMsg("stream msg is end") | ||
} | ||
if len(res.Packets) < int(req.StartIndex) { | ||
return nil, errs.ErrNoPermission.WrapMsg("start index is invalid") | ||
} | ||
if val := len(res.Packets) - int(req.StartIndex); val > 0 { | ||
exist := res.Packets[int(req.StartIndex):] | ||
for i, s := range exist { | ||
if len(req.Packets) == 0 { | ||
break | ||
} | ||
if s != req.Packets[i] { | ||
return nil, errs.ErrNoPermission.WrapMsg(fmt.Sprintf("packet %d has been written and is inconsistent", i)) | ||
} | ||
req.StartIndex++ | ||
req.Packets = req.Packets[1:] | ||
} | ||
} | ||
if len(req.Packets) == 0 && res.End == req.End { | ||
return &msg.AppendStreamMsgResp{}, nil | ||
} | ||
if err := m.StreamMsgDatabase.AppendStreamMsg(ctx, req.ClientMsgID, int(req.StartIndex), req.Packets, req.End); err != nil { | ||
return nil, err | ||
} | ||
conversation, err := m.Conversation.GetConversation(ctx, res.UserID, res.ConversationID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
tips := &sdkws.StreamMsgTips{ | ||
ClientMsgID: res.ClientMsgID, | ||
StartIndex: req.StartIndex, | ||
Packets: req.Packets, | ||
End: req.End, | ||
} | ||
var ( | ||
recvID string | ||
sessionType int32 | ||
) | ||
if conversation.GroupID == "" { | ||
sessionType = constant.SingleChatType | ||
recvID = conversation.UserID | ||
} else { | ||
sessionType = constant.ReadGroupChatType | ||
recvID = conversation.GroupID | ||
} | ||
m.msgNotificationSender.StreamMsgNotification(ctx, res.UserID, recvID, sessionType, tips) | ||
return &msg.AppendStreamMsgResp{}, nil | ||
} | ||
|
||
func (m *msgServer) GetStreamMsg(ctx context.Context, req *msg.GetStreamMsgReq) (*msg.GetStreamMsgResp, error) { | ||
res, err := m.getStreamMsg(ctx, req.ClientMsgID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &msg.GetStreamMsgResp{ | ||
ClientMsgID: res.ClientMsgID, | ||
ConversationID: res.ConversationID, | ||
UserID: res.UserID, | ||
Packets: res.Packets, | ||
End: res.End, | ||
CreateTime: res.CreateTime.UnixMilli(), | ||
DeadlineTime: res.DeadlineTime.UnixMilli(), | ||
}, 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
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,12 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/model" | ||
) | ||
|
||
type StreamMsgDatabase interface { | ||
CreateStreamMsg(ctx context.Context, model *model.StreamMsg) error | ||
AppendStreamMsg(ctx context.Context, clientMsgID string, startIndex int, packets []string, end bool) error | ||
GetStreamMsg(ctx context.Context, clientMsgID string) (*model.StreamMsg, error) | ||
} |
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,21 @@ | ||
package model | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
const ( | ||
StreamMsgStatusWait = 0 | ||
StreamMsgStatusDone = 1 | ||
StreamMsgStatusFail = 2 | ||
) | ||
|
||
type StreamMsg struct { | ||
ClientMsgID string `bson:"client_msg_id"` | ||
ConversationID string `bson:"conversation_id"` | ||
UserID string `bson:"user_id"` | ||
Packets []string `bson:"packets"` | ||
End bool `bson:"end"` | ||
CreateTime time.Time `bson:"create_time"` | ||
DeadlineTime time.Time `bson:"deadline_time"` | ||
} |