diff --git a/go.mod b/go.mod index 98e688259..ef766bf26 100644 --- a/go.mod +++ b/go.mod @@ -16,8 +16,8 @@ require golang.org/x/net v0.22.0 // indirect require ( github.com/google/go-cmp v0.6.0 - github.com/openimsdk/protocol v0.0.69-alpha.37 - github.com/openimsdk/tools v0.0.49-alpha.60 + github.com/openimsdk/protocol v0.0.69 + github.com/openimsdk/tools v0.0.49 github.com/patrickmn/go-cache v2.1.0+incompatible golang.org/x/image v0.15.0 golang.org/x/sync v0.6.0 diff --git a/go.sum b/go.sum index f53c687bd..139579436 100644 --- a/go.sum +++ b/go.sum @@ -25,10 +25,10 @@ github.com/lestrrat-go/strftime v1.0.6 h1:CFGsDEt1pOpFNU+TJB0nhz9jl+K0hZSLE205Ah github.com/lestrrat-go/strftime v1.0.6/go.mod h1:f7jQKgV5nnJpYgdEasS+/y7EsTb8ykN2z68n3TtcTaw= github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= -github.com/openimsdk/protocol v0.0.69-alpha.37 h1:2gtDkippSBfbXIP++BEQsz4e+qjk86ddnh4b7p40/HY= -github.com/openimsdk/protocol v0.0.69-alpha.37/go.mod h1:OZQA9FR55lseYoN2Ql1XAHYKHJGu7OMNkUbuekrKCM8= -github.com/openimsdk/tools v0.0.49-alpha.60 h1:Yvb+sLioDEYUYO3nKv8fWoW7iqMxL60XJmVqaUL1MBQ= -github.com/openimsdk/tools v0.0.49-alpha.60/go.mod h1:oiSQU5Z6fzjxKFjbqDHImD8EmCIwClU1Rkur1sK12Po= +github.com/openimsdk/protocol v0.0.69 h1:dVi8meSg8kmUzSH1XQab4MjihqKkkcCAmt1BYXPJuXo= +github.com/openimsdk/protocol v0.0.69/go.mod h1:OZQA9FR55lseYoN2Ql1XAHYKHJGu7OMNkUbuekrKCM8= +github.com/openimsdk/tools v0.0.49 h1:yILTgOCqxlqJMc889fE99E5ZGa70v/E3hkCSeTnWl3s= +github.com/openimsdk/tools v0.0.49/go.mod h1:oiSQU5Z6fzjxKFjbqDHImD8EmCIwClU1Rkur1sK12Po= github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= diff --git a/internal/group/notification.go b/internal/group/notification.go index 52c632e2d..aed339675 100644 --- a/internal/group/notification.go +++ b/internal/group/notification.go @@ -41,48 +41,7 @@ func (g *Group) DoNotification(ctx context.Context, msg *sdkws.MsgData) { } func (g *Group) doNotification(ctx context.Context, msg *sdkws.MsgData) error { - g.groupSyncMutex.Lock() - defer g.groupSyncMutex.Unlock() switch msg.ContentType { - case constant.GroupCreatedNotification: // 1501 - var detail sdkws.GroupCreatedTips - if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { - return err - } - - if err := g.IncrSyncJoinGroup(ctx); err != nil { - return err - } - return g.IncrSyncGroupAndMember(ctx, detail.Group.GroupID) - - case constant.GroupInfoSetNotification: // 1502 - var detail sdkws.GroupInfoSetTips - if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { - return err - } - return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, nil, - nil, nil, detail.Group, groupSortIDUnchanged, detail.GroupMemberVersion, detail.GroupMemberVersionID) - case constant.JoinGroupApplicationNotification: // 1503 - var detail sdkws.JoinGroupApplicationTips - if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { - return err - } - if detail.Applicant.UserID == g.loginUserID { - return g.SyncSelfGroupApplications(ctx, detail.Group.GroupID) - } else { - return g.SyncAdminGroupApplications(ctx, detail.Group.GroupID) - } - case constant.MemberQuitNotification: // 1504 - var detail sdkws.MemberQuitTips - if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { - return err - } - if detail.QuitUser.UserID == g.loginUserID { - return g.IncrSyncJoinGroup(ctx) - } else { - return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, []*sdkws.GroupMemberFullInfo{detail.QuitUser}, nil, - nil, detail.Group, groupSortIDUnchanged, detail.GroupMemberVersion, detail.GroupMemberVersionID) - } case constant.GroupApplicationAcceptedNotification: // 1505 var detail sdkws.GroupApplicationAcceptedTips if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { @@ -96,6 +55,7 @@ func (g *Group) doNotification(ctx context.Context, msg *sdkws.MsgData) error { default: return errs.New(fmt.Sprintf("GroupApplicationAcceptedNotification ReceiverAs unknown %d", detail.ReceiverAs)).Wrap() } + case constant.GroupApplicationRejectedNotification: // 1506 var detail sdkws.GroupApplicationRejectedTips if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { @@ -109,145 +69,190 @@ func (g *Group) doNotification(ctx context.Context, msg *sdkws.MsgData) error { default: return errs.New(fmt.Sprintf("GroupApplicationRejectedNotification ReceiverAs unknown %d", detail.ReceiverAs)).Wrap() } - case constant.GroupOwnerTransferredNotification: // 1507 - var detail sdkws.GroupOwnerTransferredTips - if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { - return err - } - if detail.Group == nil { - return errs.New(fmt.Sprintf("group is nil, groupID: %s", detail.Group.GroupID)).Wrap() - } - return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, nil, - []*sdkws.GroupMemberFullInfo{detail.NewGroupOwner, detail.OldGroupOwnerInfo}, nil, - detail.Group, groupSortIDChanged, detail.GroupMemberVersion, detail.GroupMemberVersionID) - case constant.MemberKickedNotification: // 1508 - var detail sdkws.MemberKickedTips - if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { - return err - } - var self bool - for _, info := range detail.KickedUserList { - if info.UserID == g.loginUserID { - self = true - break + + default: + g.groupSyncMutex.Lock() + defer g.groupSyncMutex.Unlock() + switch msg.ContentType { + case constant.GroupCreatedNotification: // 1501 + var detail sdkws.GroupCreatedTips + if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { + return err } - } - if self { - return g.IncrSyncJoinGroup(ctx) - } else { - return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, detail.KickedUserList, nil, - nil, detail.Group, groupSortIDUnchanged, detail.GroupMemberVersion, detail.GroupMemberVersionID) - } - case constant.MemberInvitedNotification: // 1509 - var detail sdkws.MemberInvitedTips - if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { - return err - } - userIDMap := datautil.SliceSetAny(detail.InvitedUserList, func(e *sdkws.GroupMemberFullInfo) string { - return e.UserID - }) - //Also invited as a member - if _, ok := userIDMap[g.loginUserID]; ok { + if err := g.IncrSyncJoinGroup(ctx); err != nil { return err } return g.IncrSyncGroupAndMember(ctx, detail.Group.GroupID) - } else { + + case constant.GroupInfoSetNotification: // 1502 + var detail sdkws.GroupInfoSetTips + if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { + return err + } + return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, nil, + nil, nil, detail.Group, groupSortIDUnchanged, detail.GroupMemberVersion, detail.GroupMemberVersionID) + case constant.JoinGroupApplicationNotification: // 1503 + var detail sdkws.JoinGroupApplicationTips + if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { + return err + } + if detail.Applicant.UserID == g.loginUserID { + return g.SyncSelfGroupApplications(ctx, detail.Group.GroupID) + } else { + return g.SyncAdminGroupApplications(ctx, detail.Group.GroupID) + } + case constant.MemberQuitNotification: // 1504 + var detail sdkws.MemberQuitTips + if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { + return err + } + if detail.QuitUser.UserID == g.loginUserID { + return g.IncrSyncJoinGroup(ctx) + } else { + return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, []*sdkws.GroupMemberFullInfo{detail.QuitUser}, nil, + nil, detail.Group, groupSortIDUnchanged, detail.GroupMemberVersion, detail.GroupMemberVersionID) + } + case constant.GroupOwnerTransferredNotification: // 1507 + var detail sdkws.GroupOwnerTransferredTips + if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { + return err + } + if detail.Group == nil { + return errs.New(fmt.Sprintf("group is nil, groupID: %s", detail.Group.GroupID)).Wrap() + } + return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, nil, + []*sdkws.GroupMemberFullInfo{detail.NewGroupOwner, detail.OldGroupOwnerInfo}, nil, + detail.Group, groupSortIDChanged, detail.GroupMemberVersion, detail.GroupMemberVersionID) + case constant.MemberKickedNotification: // 1508 + var detail sdkws.MemberKickedTips + if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { + return err + } + var self bool + for _, info := range detail.KickedUserList { + if info.UserID == g.loginUserID { + self = true + break + } + } + if self { + return g.IncrSyncJoinGroup(ctx) + } else { + return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, detail.KickedUserList, nil, + nil, detail.Group, groupSortIDUnchanged, detail.GroupMemberVersion, detail.GroupMemberVersionID) + } + case constant.MemberInvitedNotification: // 1509 + var detail sdkws.MemberInvitedTips + if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { + return err + } + userIDMap := datautil.SliceSetAny(detail.InvitedUserList, func(e *sdkws.GroupMemberFullInfo) string { + return e.UserID + }) + //Also invited as a member + if _, ok := userIDMap[g.loginUserID]; ok { + if err := g.IncrSyncJoinGroup(ctx); err != nil { + return err + } + return g.IncrSyncGroupAndMember(ctx, detail.Group.GroupID) + } else { + return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, nil, nil, + detail.InvitedUserList, detail.Group, groupSortIDUnchanged, detail.GroupMemberVersion, detail.GroupMemberVersionID) + } + case constant.MemberEnterNotification: // 1510 + var detail sdkws.MemberEnterTips + if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { + return err + } + if detail.EntrantUser.UserID == g.loginUserID { + if err := g.IncrSyncJoinGroup(ctx); err != nil { + return err + } + return g.IncrSyncGroupAndMember(ctx, detail.Group.GroupID) + } else { + return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, nil, nil, + []*sdkws.GroupMemberFullInfo{detail.EntrantUser}, detail.Group, groupSortIDUnchanged, detail.GroupMemberVersion, detail.GroupMemberVersionID) + } + case constant.GroupDismissedNotification: // 1511 + var detail sdkws.GroupDismissedTips + if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { + return err + } + g.listener().OnGroupDismissed(utils.StructToJsonString(detail.Group)) + + return g.IncrSyncJoinGroup(ctx) + case constant.GroupMemberMutedNotification: // 1512 + var detail sdkws.GroupMemberMutedTips + if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { + return err + } + return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, nil, + []*sdkws.GroupMemberFullInfo{detail.MutedUser}, nil, nil, + groupSortIDUnchanged, detail.GroupMemberVersion, detail.GroupMemberVersionID) + case constant.GroupMemberCancelMutedNotification: // 1513 + var detail sdkws.GroupMemberCancelMutedTips + if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { + return err + } + return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, nil, + []*sdkws.GroupMemberFullInfo{detail.MutedUser}, nil, nil, + groupSortIDUnchanged, detail.GroupMemberVersion, detail.GroupMemberVersionID) + case constant.GroupMutedNotification: // 1514 + var detail sdkws.GroupMutedTips + if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { + return err + } return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, nil, nil, - detail.InvitedUserList, detail.Group, groupSortIDUnchanged, detail.GroupMemberVersion, detail.GroupMemberVersionID) - } - case constant.MemberEnterNotification: // 1510 - var detail sdkws.MemberEnterTips - if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { - return err - } - if detail.EntrantUser.UserID == g.loginUserID { - if err := g.IncrSyncJoinGroup(ctx); err != nil { + nil, detail.Group, groupSortIDUnchanged, detail.GroupMemberVersion, detail.GroupMemberVersionID) + case constant.GroupCancelMutedNotification: // 1515 + var detail sdkws.GroupCancelMutedTips + if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { return err } - return g.IncrSyncGroupAndMember(ctx, detail.Group.GroupID) - } else { return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, nil, nil, - []*sdkws.GroupMemberFullInfo{detail.EntrantUser}, detail.Group, groupSortIDUnchanged, detail.GroupMemberVersion, detail.GroupMemberVersionID) - } - case constant.GroupDismissedNotification: // 1511 - var detail sdkws.GroupDismissedTips - if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { - return err - } - g.listener().OnGroupDismissed(utils.StructToJsonString(detail.Group)) - - return g.IncrSyncJoinGroup(ctx) - case constant.GroupMemberMutedNotification: // 1512 - var detail sdkws.GroupMemberMutedTips - if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { - return err - } - return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, nil, - []*sdkws.GroupMemberFullInfo{detail.MutedUser}, nil, nil, - groupSortIDUnchanged, detail.GroupMemberVersion, detail.GroupMemberVersionID) - case constant.GroupMemberCancelMutedNotification: // 1513 - var detail sdkws.GroupMemberCancelMutedTips - if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { - return err - } - return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, nil, - []*sdkws.GroupMemberFullInfo{detail.MutedUser}, nil, nil, - groupSortIDUnchanged, detail.GroupMemberVersion, detail.GroupMemberVersionID) - case constant.GroupMutedNotification: // 1514 - var detail sdkws.GroupMutedTips - if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { - return err - } - return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, nil, nil, - nil, detail.Group, groupSortIDUnchanged, detail.GroupMemberVersion, detail.GroupMemberVersionID) - case constant.GroupCancelMutedNotification: // 1515 - var detail sdkws.GroupCancelMutedTips - if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { - return err - } - return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, nil, nil, - nil, detail.Group, groupSortIDUnchanged, detail.GroupMemberVersion, detail.GroupMemberVersionID) - case constant.GroupMemberInfoSetNotification: // 1516 - var detail sdkws.GroupMemberInfoSetTips - if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { - return err - } + nil, detail.Group, groupSortIDUnchanged, detail.GroupMemberVersion, detail.GroupMemberVersionID) + case constant.GroupMemberInfoSetNotification: // 1516 + var detail sdkws.GroupMemberInfoSetTips + if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { + return err + } - return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, nil, - []*sdkws.GroupMemberFullInfo{detail.ChangedUser}, nil, nil, - detail.GroupSortVersion, detail.GroupMemberVersion, detail.GroupMemberVersionID) - case constant.GroupMemberSetToAdminNotification: // 1517 - var detail sdkws.GroupMemberInfoSetTips - if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { - return err - } - return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, nil, - []*sdkws.GroupMemberFullInfo{detail.ChangedUser}, nil, nil, - detail.GroupSortVersion, detail.GroupMemberVersion, detail.GroupMemberVersionID) - case constant.GroupMemberSetToOrdinaryUserNotification: // 1518 - var detail sdkws.GroupMemberInfoSetTips - if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { - return err - } - return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, nil, - []*sdkws.GroupMemberFullInfo{detail.ChangedUser}, nil, nil, - detail.GroupSortVersion, detail.GroupMemberVersion, detail.GroupMemberVersionID) - case constant.GroupInfoSetAnnouncementNotification: // 1519 - var detail sdkws.GroupInfoSetAnnouncementTips // - if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { - return err - } - return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, nil, nil, - nil, detail.Group, groupSortIDUnchanged, detail.GroupMemberVersion, detail.GroupMemberVersionID) - case constant.GroupInfoSetNameNotification: // 1520 - var detail sdkws.GroupInfoSetNameTips // - if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { - return err + return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, nil, + []*sdkws.GroupMemberFullInfo{detail.ChangedUser}, nil, nil, + detail.GroupSortVersion, detail.GroupMemberVersion, detail.GroupMemberVersionID) + case constant.GroupMemberSetToAdminNotification: // 1517 + var detail sdkws.GroupMemberInfoSetTips + if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { + return err + } + return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, nil, + []*sdkws.GroupMemberFullInfo{detail.ChangedUser}, nil, nil, + detail.GroupSortVersion, detail.GroupMemberVersion, detail.GroupMemberVersionID) + case constant.GroupMemberSetToOrdinaryUserNotification: // 1518 + var detail sdkws.GroupMemberInfoSetTips + if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { + return err + } + return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, nil, + []*sdkws.GroupMemberFullInfo{detail.ChangedUser}, nil, nil, + detail.GroupSortVersion, detail.GroupMemberVersion, detail.GroupMemberVersionID) + case constant.GroupInfoSetAnnouncementNotification: // 1519 + var detail sdkws.GroupInfoSetAnnouncementTips // + if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { + return err + } + return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, nil, nil, + nil, detail.Group, groupSortIDUnchanged, detail.GroupMemberVersion, detail.GroupMemberVersionID) + case constant.GroupInfoSetNameNotification: // 1520 + var detail sdkws.GroupInfoSetNameTips // + if err := utils.UnmarshalNotificationElem(msg.Content, &detail); err != nil { + return err + } + return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, nil, nil, + nil, detail.Group, groupSortIDUnchanged, detail.GroupMemberVersion, detail.GroupMemberVersionID) + default: + return errs.New("unknown tips type", "contentType", msg.ContentType).Wrap() } - return g.onlineSyncGroupAndMember(ctx, detail.Group.GroupID, nil, nil, - nil, detail.Group, groupSortIDUnchanged, detail.GroupMemberVersion, detail.GroupMemberVersionID) - default: - return errs.New("unknown tips type", "contentType", msg.ContentType).Wrap() } } diff --git a/internal/group/sdk.go b/internal/group/sdk.go index 80ca0fcc2..0be4f7e59 100644 --- a/internal/group/sdk.go +++ b/internal/group/sdk.go @@ -493,7 +493,9 @@ func (g *Group) GetGroupMemberList(ctx context.Context, groupID string, filter, case constant.GroupFilterOwnerAndAdmin: return localGroupMembers, false, nil case constant.GroupFilterAll: + fallthrough case constant.GroupFilterOrdinaryUsers: + fallthrough case constant.GroupFilterAdminAndOrdinaryUsers: return localGroupMembers, true, nil } diff --git a/pkg/datafetcher/datafetcher.go b/pkg/datafetcher/datafetcher.go index ddb88d33b..857189970 100644 --- a/pkg/datafetcher/datafetcher.go +++ b/pkg/datafetcher/datafetcher.go @@ -68,6 +68,10 @@ func (ds *DataFetcher[T]) FetchWithPagination(ctx context.Context, offset, limit // FetchMissingAndFillLocal fetches missing data from server and fills local database func (ds *DataFetcher[T]) FetchMissingAndFillLocal(ctx context.Context, uids []string) ([]T, error) { + if len(uids) == 0 { + return nil, nil + } + localData, needServer, err := ds.FetchFromLocal(ctx, uids) if err != nil { return nil, err