fix: populate channels with all members for existing communities

This commit is contained in:
Patryk Osmaczko 2023-07-26 16:12:53 +02:00 committed by osmaczko
parent 0465ab668d
commit 971fc0c816
2 changed files with 69 additions and 0 deletions

View File

@ -1903,6 +1903,39 @@ func (o *Community) AddMemberToChat(chatID string, publicKey *ecdsa.PublicKey, r
return changes, nil
}
func (o *Community) PopulateChatWithAllMembers(chatID string) (*CommunityChanges, error) {
o.mutex.Lock()
defer o.mutex.Unlock()
if !o.IsControlNode() {
return o.emptyCommunityChanges(), ErrNotControlNode
}
return o.populateChatWithAllMembers(chatID)
}
func (o *Community) populateChatWithAllMembers(chatID string) (*CommunityChanges, error) {
result := o.emptyCommunityChanges()
chat, exists := o.chats()[chatID]
if !exists {
return result, ErrChatNotFound
}
membersAdded := make(map[string]*protobuf.CommunityMember)
for pubKey, member := range o.Members() {
if chat.Members[pubKey] == nil {
membersAdded[pubKey] = member
}
}
result.ChatsModified[chatID] = &CommunityChatChanges{
MembersAdded: membersAdded,
}
chat.Members = o.Members()
return result, nil
}
func (o *Community) ChatIDs() (chatIDs []string) {
for id := range o.config.CommunityDescription.Chats {
chatIDs = append(chatIDs, o.IDString()+id)

View File

@ -243,6 +243,11 @@ func NewManager(identity *ecdsa.PrivateKey, db *sql.DB, encryptor *encryption.Pr
manager.ensVerifier = verifier
}
err = manager.fixupChannelMembers()
if err != nil {
return nil, errors.Wrap(err, "failed to fixup channel members")
}
return manager, nil
}
@ -4113,3 +4118,34 @@ func (m *Manager) saveAndPublish(community *Community) error {
return nil
}
// This populates the member list of channels with all community members, if required.
// Motivation: The member lists of channels were not populated for communities that had already been created.
//
// Ideally, this should be executed through a migration, but it's technically unfeasible
// because `CommunityDescription“ is stored as a signed message blob in the database.
//
// However, it's safe to run this migration/fixup multiple times.
func (m *Manager) fixupChannelMembers() error {
controlledCommunities, err := m.Created()
if err != nil {
return err
}
for _, c := range controlledCommunities {
for channelID := range c.Chats() {
if !c.ChannelHasTokenPermissions(c.IDString() + channelID) {
_, err := c.PopulateChatWithAllMembers(channelID)
if err != nil {
return err
}
err = m.persistence.SaveCommunity(c)
if err != nil {
return err
}
}
}
}
return nil
}