From 51a6d8ee7cfeb69fa2c3c0a60d74b183cc9e4fe2 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Tue, 30 Nov 2021 10:26:17 -0400 Subject: [PATCH] fix: error reordering chats when destination category position is the same as original chatId position (#2448) --- protocol/communities/community_categories.go | 22 ++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/protocol/communities/community_categories.go b/protocol/communities/community_categories.go index f76090e37..11f75d61d 100644 --- a/protocol/communities/community_categories.go +++ b/protocol/communities/community_categories.go @@ -254,7 +254,7 @@ func (o *Community) ReorderChat(categoryID string, chatID string, newPosition in changes := o.emptyCommunityChanges() o.SortCategoryChats(changes, oldCategoryID) - o.insertAndSort(changes, categoryID, chat, newPosition) + o.insertAndSort(changes, oldCategoryID, categoryID, chatID, chat, newPosition) o.increaseClock() @@ -295,7 +295,7 @@ func (o *Community) SortCategoryChats(changes *CommunityChanges, categoryID stri } } -func (o *Community) insertAndSort(changes *CommunityChanges, categoryID string, chat *protobuf.CommunityChat, newPosition int) { +func (o *Community) insertAndSort(changes *CommunityChanges, oldCategoryID string, categoryID string, chatID string, chat *protobuf.CommunityChat, newPosition int) { // We sort the chats here because maps are not guaranteed to keep order var catChats []string sortedChats := make(sortSlice, 0, len(o.config.CommunityDescription.Chats)) @@ -318,15 +318,25 @@ func (o *Community) insertAndSort(changes *CommunityChanges, categoryID string, newPosition = 0 } - if int32(newPosition) == chat.Position { - return - } - decrease := false if chat.Position > int32(newPosition) { decrease = true } + for k, v := range o.config.CommunityDescription.Chats { + if k != chatID && newPosition == int(v.Position) && v.CategoryId == categoryID { + if oldCategoryID == categoryID { + if decrease { + v.Position++ + } else { + v.Position-- + } + } else { + v.Position++ + } + } + } + idx := -1 currChatID := "" var sortedChatIDs []string