feat: incr sync version.

This commit is contained in:
Gordon
2024-06-24 17:48:33 +08:00
parent e8ccae6349
commit 88b8043224
308 changed files with 55952 additions and 59 deletions

View File

@@ -0,0 +1,148 @@
// Copyright © 2023 OpenIM SDK. 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 common
import (
"encoding/json"
"github.com/openimsdk/openim-sdk-core/v3/open_im_sdk_callback"
"github.com/openimsdk/openim-sdk-core/v3/pkg/sdkerrs"
"github.com/openimsdk/openim-sdk-core/v3/pkg/utils"
"runtime"
)
//var validate *validator.Validate
//funcation init() {
// validate = validator.New()
//}
func CheckAnyErrCallback(callback open_im_sdk_callback.Base, errCode int32, err error, operationID string) {
if err != nil {
errInfo := "operationID[" + operationID + "], " + "info[" + err.Error() + "]"
//log.NewError(operationID, "checkErr ", errInfo)
callback.OnError(errCode, errInfo)
runtime.Goexit()
}
}
func CheckConfigErrCallback(callback open_im_sdk_callback.Base, err error, operationID string) {
CheckAnyErrCallback(callback, sdkerrs.ArgsError, err, operationID)
}
//func CheckTokenErrCallback(callback open_im_sdk_callback.Base, err error, operationID string) {
// CheckAnyErrCallback(callback, sdkerrs.TokenInvalidError, err, operationID)
//}
func CheckDBErrCallback(callback open_im_sdk_callback.Base, err error, operationID string) {
CheckAnyErrCallback(callback, sdkerrs.SdkInternalError, err, operationID)
}
func CheckDataErrCallback(callback open_im_sdk_callback.Base, err error, operationID string) {
CheckAnyErrCallback(callback, sdkerrs.SdkInternalError, err, operationID)
}
func CheckArgsErrCallback(callback open_im_sdk_callback.Base, err error, operationID string) {
CheckAnyErrCallback(callback, sdkerrs.ArgsError, err, operationID)
}
//
//funcation CheckErrAndResp2(err error, resp []byte, output interface{}) error {
// if err != nil {
// return utils.Wrap(err, "api resp failed")
// }
// var c server_api_params.CommDataResp
// err = json.Unmarshal(resp, &c)
// if err == nil {
// if c.ErrCode != 0 {
// return utils.Wrap(errors.New(c.ErrMsg), "")
// }
// if output != nil {
// err = mapstructure.Decode(c.Data, output)
// if err != nil {
// goto one
// }
// return nil
// }
// return nil
// }
//
// unMarshaler := jsonpb.Unmarshaler{}
// unMarshaler.Unmarshal()
// s, _ := marshaler.MarshalToString(pb)
// out := make(map[string]interface{})
// json.Unmarshal([]byte(s), &out)
// if idFix {
// if _, ok := out["id"]; ok {
// out["_id"] = out["id"]
// delete(out, "id")
// }
// }
// return out
//
//one:
// var c2 server_api_params.CommDataRespOne
//
// err = json.Unmarshal(resp, &c2)
// if err != nil {
// return utils.Wrap(err, "")
// }
// if c2.ErrCode != 0 {
// return utils.Wrap(errors.New(c2.ErrMsg), "")
// }
// if output != nil {
// err = mapstructure.Decode(c2.Data, output)
// if err != nil {
// return utils.Wrap(err, "")
// }
// return nil
// }
// return nil
//}
func JsonUnmarshalAndArgsValidate(s string, args interface{}, callback open_im_sdk_callback.Base, operationID string) error {
err := json.Unmarshal([]byte(s), args)
if err != nil {
if callback != nil {
//log.NewError(operationID, "Unmarshal failed ", err.Error(), s)
callback.OnError(sdkerrs.ArgsError, err.Error())
runtime.Goexit()
} else {
return utils.Wrap(err, "json Unmarshal failed")
}
}
//err = validate.Struct(args)
//if err != nil {
// if callback != nil {
// log.NewError(operationID, "validate failed ", err.Error(), s)
// callback.OnError(constant.ErrArgs.ErrCode, constant.ErrArgs.ErrMsg)
// runtime.Goexit()
// }
//}
//return utils.Wrap(err, "args check failed")
return nil
}
func JsonUnmarshalCallback(s string, args interface{}, callback open_im_sdk_callback.Base, operationID string) error {
err := json.Unmarshal([]byte(s), args)
if err != nil {
if callback != nil {
//log.NewError(operationID, "Unmarshal failed ", err.Error(), s)
callback.OnError(sdkerrs.ArgsError, err.Error())
runtime.Goexit()
} else {
return utils.Wrap(err, "json Unmarshal failed")
}
}
return nil
}

View File

@@ -0,0 +1,217 @@
// Copyright © 2023 OpenIM SDK. 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 common
import (
"context"
"errors"
"github.com/openimsdk/openim-sdk-core/v3/pkg/constant"
"github.com/openimsdk/openim-sdk-core/v3/pkg/utils"
"github.com/openimsdk/openim-sdk-core/v3/sdk_struct"
"time"
"github.com/openimsdk/tools/log"
"github.com/openimsdk/protocol/sdkws"
)
func TriggerCmdJoinedSuperGroup(cmd sdk_struct.CmdJoinedSuperGroup, joinedSuperGroupCh chan Cmd2Value) error {
if joinedSuperGroupCh == nil {
return utils.Wrap(errors.New("ch == nil"), "")
}
c2v := Cmd2Value{Cmd: constant.CmdJoinedSuperGroup, Value: cmd}
return sendCmd(joinedSuperGroupCh, c2v, 100)
}
func TriggerCmdNewMsgCome(ctx context.Context, msg sdk_struct.CmdNewMsgComeToConversation, conversationCh chan Cmd2Value) error {
if conversationCh == nil {
return utils.Wrap(errors.New("ch == nil"), "")
}
c2v := Cmd2Value{Cmd: constant.CmdNewMsgCome, Value: msg, Ctx: ctx}
return sendCmd(conversationCh, c2v, 100)
}
func TriggerCmdSuperGroupMsgCome(msg sdk_struct.CmdNewMsgComeToConversation, conversationCh chan Cmd2Value) error {
if conversationCh == nil {
return utils.Wrap(errors.New("ch == nil"), "")
}
c2v := Cmd2Value{Cmd: constant.CmdSuperGroupMsgCome, Value: msg}
return sendCmd(conversationCh, c2v, 100)
}
func TriggerCmdNotification(ctx context.Context, msg sdk_struct.CmdNewMsgComeToConversation, conversationCh chan Cmd2Value) error {
if conversationCh == nil {
return utils.Wrap(errors.New("ch == nil"), "")
}
c2v := Cmd2Value{Cmd: constant.CmdNotification, Value: msg, Ctx: ctx}
return sendCmd(conversationCh, c2v, 100)
}
func TriggerCmdWakeUp(ch chan Cmd2Value) error {
if ch == nil {
return utils.Wrap(errors.New("ch == nil"), "")
}
c2v := Cmd2Value{Cmd: constant.CmdWakeUp, Value: nil}
return sendCmd(ch, c2v, 100)
}
func TriggerCmdSyncReactionExtensions(node SyncReactionExtensionsNode, conversationCh chan Cmd2Value) error {
if conversationCh == nil {
return utils.Wrap(errors.New("ch == nil"), "")
}
c2v := Cmd2Value{
Cmd: constant.CmSyncReactionExtensions,
Value: node,
}
return sendCmd(conversationCh, c2v, 100)
}
func TriggerCmdUpdateConversation(ctx context.Context, node UpdateConNode, conversationCh chan<- Cmd2Value) error {
c2v := Cmd2Value{
Cmd: constant.CmdUpdateConversation,
Value: node,
Ctx: ctx,
}
return sendCmd(conversationCh, c2v, 100)
}
func TriggerCmdUpdateMessage(ctx context.Context, node UpdateMessageNode, conversationCh chan Cmd2Value) error {
c2v := Cmd2Value{
Cmd: constant.CmdUpdateMessage,
Value: node,
Ctx: ctx,
}
return sendCmd(conversationCh, c2v, 100)
}
// Push message, msg for msgData slice
func TriggerCmdPushMsg(ctx context.Context, msg *sdkws.PushMessages, ch chan Cmd2Value) error {
if ch == nil {
return utils.Wrap(errors.New("ch == nil"), "")
}
c2v := Cmd2Value{Cmd: constant.CmdPushMsg, Value: msg, Ctx: ctx}
return sendCmd(ch, c2v, 100)
}
// seq trigger
func TriggerCmdMaxSeq(ctx context.Context, seq *sdk_struct.CmdMaxSeqToMsgSync, ch chan Cmd2Value) error {
if ch == nil {
return utils.Wrap(errors.New("ch == nil"), "")
}
c2v := Cmd2Value{Cmd: constant.CmdMaxSeq, Value: seq, Ctx: ctx}
return sendCmd(ch, c2v, 100)
}
func TriggerCmdLogOut(ctx context.Context, ch chan Cmd2Value) error {
if ch == nil {
return utils.Wrap(errors.New("ch == nil"), "")
}
c2v := Cmd2Value{Cmd: constant.CmdLogOut, Ctx: ctx}
return sendCmd(ch, c2v, 100)
}
// Connection success trigger
func TriggerCmdConnected(ctx context.Context, ch chan Cmd2Value) error {
if ch == nil {
return utils.Wrap(errors.New("ch == nil"), "")
}
c2v := Cmd2Value{Cmd: constant.CmdConnSuccesss, Value: nil, Ctx: ctx}
return sendCmd(ch, c2v, 100)
}
type DeleteConNode struct {
SourceID string
ConversationID string
SessionType int
}
type SyncReactionExtensionsNode struct {
OperationID string
Action int
Args interface{}
}
type UpdateConNode struct {
ConID string
Action int //1 Delete the conversation; 2 Update the latest news in the conversation or add a conversation; 3 Put a conversation on the top;
// 4 Cancel a conversation on the top, 5 Messages are not read and set to 0, 6 New conversations
Args interface{}
}
type UpdateMessageNode struct {
Action int
Args interface{}
}
type Cmd2Value struct {
Cmd string
Value interface{}
Ctx context.Context
}
type UpdateConInfo struct {
UserID string
GroupID string
}
type UpdateMessageInfo struct {
SessionType int32
UserID string
FaceURL string
Nickname string
GroupID string
}
type SourceIDAndSessionType struct {
SourceID string
SessionType int32
FaceURL string
Nickname string
}
func UnInitAll(conversationCh chan Cmd2Value) error {
c2v := Cmd2Value{Cmd: constant.CmdUnInit}
return sendCmd(conversationCh, c2v, 100)
}
type goroutine interface {
Work(cmd Cmd2Value)
GetCh() chan Cmd2Value
//GetContext() context.Context
}
func DoListener(Li goroutine, ctx context.Context) {
for {
select {
case cmd := <-Li.GetCh():
Li.Work(cmd)
case <-ctx.Done():
log.ZInfo(ctx, "conversation done sdk logout.....")
return
}
}
}
func sendCmd(ch chan<- Cmd2Value, value Cmd2Value, timeout int64) error {
select {
case ch <- value:
return nil
case <-time.After(time.Millisecond * time.Duration(timeout)):
return errors.New("send cmd timeout")
}
}

View File

@@ -0,0 +1,33 @@
// Copyright © 2023 OpenIM SDK. 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 common
//import (
// "github.com/mitchellh/mapstructure"
// "open_im_sdk/open_im_sdk_callback"
// "open_im_sdk/pkg/db"
// "open_im_sdk/pkg/db/model_struct"
//)
//
//funcation GetGroupMemberListByGroupID(callback open_im_sdk_callback.Base, operationID string, db *db.DataBase, groupID string) []*model_struct.LocalGroupMember {
// memberList, err := db.GetGroupMemberListByGroupID(groupID)
// CheckDBErrCallback(callback, err, operationID)
// return memberList
//}
//
//funcation MapstructureDecode(input interface{}, output interface{}, callback open_im_sdk_callback.Base, oprationID string) {
// err := mapstructure.Decode(input, output)
// CheckDataErrCallback(callback, err, oprationID)
//}