fix TestGroupChatMembersRemoval sometimes failed (#3037)

This commit is contained in:
frank 2023-01-05 23:37:50 +08:00 committed by GitHub
parent 7d04be3613
commit fe2270540e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 5 deletions

View file

@ -121,8 +121,10 @@ func (m *Messenger) HandleMembershipUpdate(messageState *ReceivedMessageState, c
wasUserAdded = true
newChat := CreateGroupChat(messageState.Timesource)
// We set group chat inactive and create a notification instead
// unless is coming from us or a contact or were waiting for approval
newChat.Active = isActive
// unless is coming from us or a contact or were waiting for approval.
// Also, as message MEMBER_JOINED may come from member(not creator, not our contact)
// reach earlier than CHAT_CREATED from creator, we need check if creator is our contact
newChat.Active = isActive || m.checkIfCreatorIsOurContact(group)
newChat.ReceivedInvitationAdmin = senderID
chat = &newChat
@ -213,6 +215,16 @@ func (m *Messenger) HandleMembershipUpdate(messageState *ReceivedMessageState, c
return nil
}
func (m *Messenger) checkIfCreatorIsOurContact(group *v1protocol.Group) bool {
creator, err := group.Creator()
if err == nil {
contact, _ := m.allContacts.Load(creator)
return contact != nil && contact.Added && contact.HasAddedUs
}
m.logger.Warn("failed to get creator from group", zap.String("group name", group.Name()), zap.String("group chat id", group.ChatID()), zap.Error(err))
return false
}
func (m *Messenger) createMessageNotification(chat *Chat, messageState *ReceivedMessageState) {
var notificationType ActivityCenterType

View file

@ -479,7 +479,7 @@ func (g Group) LastClockValue() uint64 {
return g.events[len(g.events)-1].ClockValue
}
func (g Group) creator() (string, error) {
func (g Group) Creator() (string, error) {
if len(g.events) == 0 {
return "", errors.New("no events in the group")
}
@ -491,7 +491,7 @@ func (g Group) creator() (string, error) {
}
func (g Group) validateChatID(chatID string) bool {
creator, err := g.creator()
creator, err := g.Creator()
if err != nil || creator == "" {
return false
}

View file

@ -67,7 +67,7 @@ func TestGroupCreator(t *testing.T) {
require.NoError(t, err)
g, err := NewGroupWithCreator("abc", "#fa6565", 20, key)
require.NoError(t, err)
creator, err := g.creator()
creator, err := g.Creator()
require.NoError(t, err)
require.Equal(t, publicKeyToString(&key.PublicKey), creator)
}