Skip to content

Commit 71788b3

Browse files
authored
refactor: remove fetch messages instead of search message clear cache. (#835)
Signed-off-by: Gordon <[email protected]>
1 parent eab602e commit 71788b3

File tree

7 files changed

+30
-66
lines changed

7 files changed

+30
-66
lines changed

internal/conversation_msg/api.go

-54
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"github.com/openimsdk/openim-sdk-core/v3/pkg/content_type"
2121
"github.com/openimsdk/openim-sdk-core/v3/pkg/db/model_struct"
2222
"github.com/openimsdk/openim-sdk-core/v3/pkg/sdk_params_callback"
23-
sdk "github.com/openimsdk/openim-sdk-core/v3/pkg/sdk_params_callback"
2423
"github.com/openimsdk/openim-sdk-core/v3/pkg/sdkerrs"
2524
"github.com/openimsdk/openim-sdk-core/v3/pkg/server_api_params"
2625
"github.com/openimsdk/openim-sdk-core/v3/pkg/utils"
@@ -1006,56 +1005,3 @@ func (c *Conversation) GetInputStates(ctx context.Context, conversationID string
10061005
func (c *Conversation) ChangeInputStates(ctx context.Context, conversationID string, focus bool) error {
10071006
return c.typing.ChangeInputStates(ctx, conversationID, focus)
10081007
}
1009-
1010-
func (c *Conversation) FetchSurroundingMessages(ctx context.Context, req *sdk_params_callback.FetchSurroundingMessagesReq) (*sdk_params_callback.FetchSurroundingMessagesResp, error) {
1011-
conversationID := utils.GetConversationIDByMsg(req.StartMessage)
1012-
var message *model_struct.LocalChatLog
1013-
message, err := c.db.GetMessage(ctx, conversationID, req.StartMessage.ClientMsgID)
1014-
if err == nil {
1015-
if message.Status >= constant.MsgStatusHasDeleted {
1016-
return nil, sdkerrs.ErrMsgHasDeleted
1017-
}
1018-
} else {
1019-
if req.StartMessage.Seq == 0 {
1020-
return nil, sdkerrs.ErrMsgHasDeleted
1021-
}
1022-
var messages []*model_struct.LocalChatLog
1023-
c.fetchAndMergeMissingMessages(ctx, conversationID, []int64{req.StartMessage.Seq}, false, 1, 0, &messages, &sdk.GetAdvancedHistoryMessageListCallback{})
1024-
if len(messages) < 1 {
1025-
return nil, sdkerrs.ErrMsgHasDeleted
1026-
}
1027-
message = messages[0]
1028-
}
1029-
c.messagePullForwardEndSeqMap.Delete(conversationID, req.ViewType)
1030-
c.messagePullReverseEndSeqMap.Delete(conversationID, req.ViewType)
1031-
1032-
result := make([]*sdk_struct.MsgStruct, 0, req.Before+req.After+1)
1033-
if req.Before > 0 {
1034-
req := sdk.GetAdvancedHistoryMessageListParams{
1035-
ConversationID: conversationID,
1036-
Count: req.Before,
1037-
StartClientMsgID: req.StartMessage.ClientMsgID,
1038-
ViewType: req.ViewType,
1039-
}
1040-
val, err := c.getAdvancedHistoryMessageList(ctx, req, false)
1041-
if err != nil {
1042-
return nil, err
1043-
}
1044-
result = append(result, val.MessageList...)
1045-
}
1046-
result = append(result, LocalChatLogToMsgStruct(message))
1047-
if req.After > 0 {
1048-
req := sdk.GetAdvancedHistoryMessageListParams{
1049-
ConversationID: conversationID,
1050-
Count: req.After,
1051-
StartClientMsgID: req.StartMessage.ClientMsgID,
1052-
ViewType: req.ViewType,
1053-
}
1054-
val, err := c.getAdvancedHistoryMessageList(ctx, req, true)
1055-
if err != nil {
1056-
return nil, err
1057-
}
1058-
result = append(result, val.MessageList...)
1059-
}
1060-
return &sdk_params_callback.FetchSurroundingMessagesResp{MessageList: result}, nil
1061-
}

internal/conversation_msg/conversation.go

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"golang.org/x/sync/errgroup"
2626

2727
"github.com/openimsdk/openim-sdk-core/v3/pkg/api"
28+
"github.com/openimsdk/openim-sdk-core/v3/pkg/cache"
2829
"github.com/openimsdk/openim-sdk-core/v3/pkg/constant"
2930
"github.com/openimsdk/openim-sdk-core/v3/pkg/db/model_struct"
3031
sdk "github.com/openimsdk/openim-sdk-core/v3/pkg/sdk_params_callback"
@@ -328,6 +329,11 @@ func (c *Conversation) searchLocalMessages(ctx context.Context, searchParam *sdk
328329
var err error // Variable to store any errors encountered
329330
var conversationID string // Variable to store the current conversation ID
330331

332+
// Clear the sequence cache for pull-up and pull-down operations in the search view,
333+
// to prevent the completion operations from the previous round from affecting the next round
334+
c.messagePullForwardEndSeqMap.DeleteByViewType(cache.ViewSearch)
335+
c.messagePullReverseEndSeqMap.DeleteByViewType(cache.ViewSearch)
336+
331337
// Set the end time for the search; if SearchTimePosition is 0, use the current timestamp
332338
if searchParam.SearchTimePosition == 0 {
333339
endTime = time.Now().Unix()

open_im_sdk/conversation_msg.go

-4
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,3 @@ func ChangeInputStates(callback open_im_sdk_callback.Base, operationID string, c
222222
func GetInputStates(callback open_im_sdk_callback.Base, operationID string, conversationID string, userID string) {
223223
call(callback, operationID, UserForSDK.Conversation().GetInputStates, conversationID, userID)
224224
}
225-
226-
func FetchSurroundingMessages(callback open_im_sdk_callback.Base, operationID string, req string) {
227-
call(callback, operationID, UserForSDK.Conversation().FetchSurroundingMessages, req)
228-
}

pkg/cache/cache.go

+10
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ func (c *Cache[K, V]) DeleteAll() {
5858
})
5959
}
6060

61+
// DeleteCon deletes the value for a key only if the provided condition function returns true.
62+
func (c *Cache[K, V]) DeleteCon(condition func(key K, value V) bool) {
63+
c.m.Range(func(rawKey, rawValue interface{}) bool {
64+
if condition(rawKey.(K), rawValue.(V)) {
65+
c.m.Delete(rawKey)
66+
}
67+
return true // Continue iteration
68+
})
69+
}
70+
6171
// RangeAll returns all values in the map.
6272
func (c *Cache[K, V]) RangeAll() (values []V) {
6373
c.m.Range(func(rawKey, rawValue interface{}) bool {

pkg/cache/conversation_seq_cache.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package cache
22

3-
import "github.com/openimsdk/tools/utils/stringutil"
3+
import (
4+
"strings"
5+
6+
"github.com/openimsdk/tools/utils/stringutil"
7+
)
48

59
const (
610
ViewHistory = iota
@@ -28,10 +32,18 @@ func (c ConversationSeqContextCache) Store(conversationID string, viewType int,
2832
c.Cache.Store(c.getConversationViewTypeKey(conversationID, viewType), thisEndSeq)
2933

3034
}
35+
3136
func (c ConversationSeqContextCache) StoreWithFunc(conversationID string, viewType int, thisEndSeq int64, fn func(key string, value int64) bool) {
3237

3338
c.Cache.StoreWithFunc(c.getConversationViewTypeKey(conversationID, viewType), thisEndSeq, fn)
3439
}
40+
3541
func (c ConversationSeqContextCache) getConversationViewTypeKey(conversationID string, viewType int) string {
36-
return conversationID + "_" + stringutil.IntToString(viewType)
42+
return conversationID + "::viewType::" + stringutil.IntToString(viewType)
43+
}
44+
45+
func (c ConversationSeqContextCache) DeleteByViewType(viewType int) {
46+
c.Cache.DeleteCon(func(key string, value int64) bool {
47+
return strings.Contains(key, "::viewType::"+stringutil.IntToString(viewType))
48+
})
3749
}

wasm/cmd/main.go

-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ func registerFunc() {
121121

122122
js.Global().Set("changeInputStates", js.FuncOf(wrapperConMsg.ChangeInputStates))
123123
js.Global().Set("getInputStates", js.FuncOf(wrapperConMsg.GetInputStates))
124-
js.Global().Set("fetchSurroundingMessages", js.FuncOf(wrapperConMsg.FetchSurroundingMessages))
125124

126125
//register group func
127126
wrapperGroup := wasm_wrapper.NewWrapperGroup(globalFuc)

wasm/wasm_wrapper/wasm_conversation_msg.go

-5
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,3 @@ func (w *WrapperConMsg) GetInputStates(_ js.Value, args []js.Value) interface{}
301301
callback := event_listener.NewBaseCallback(utils.FirstLower(utils.GetSelfFuncName()), w.commonFunc)
302302
return event_listener.NewCaller(open_im_sdk.GetInputStates, callback, &args).AsyncCallWithCallback()
303303
}
304-
305-
func (w *WrapperConMsg) FetchSurroundingMessages(_ js.Value, args []js.Value) interface{} {
306-
callback := event_listener.NewBaseCallback(utils.FirstLower(utils.GetSelfFuncName()), w.commonFunc)
307-
return event_listener.NewCaller(open_im_sdk.FetchSurroundingMessages, callback, &args).AsyncCallWithCallback()
308-
}

0 commit comments

Comments
 (0)