fix(community): ensure chats are reordered when cateories have changed

We were not reordering chats when their category IDs have changed,
this resulted in missing updates in community member's UIs

Closes #9329
This commit is contained in:
Pascal Precht 2023-01-31 10:05:32 +01:00 committed by Jonathan Rainville
parent 81926de731
commit 182a189818
5 changed files with 37 additions and 8 deletions

View File

@ -172,12 +172,12 @@ proc init*(self: Controller) =
self.events.on(SIGNAL_COMMUNITY_CHANNEL_REORDERED) do(e:Args): self.events.on(SIGNAL_COMMUNITY_CHANNEL_REORDERED) do(e:Args):
let args = CommunityChatOrderArgs(e) let args = CommunityChatOrderArgs(e)
if (args.communityId == self.sectionId): if (args.communityId == self.sectionId):
self.delegate.onReorderChat(args.chatId, args.position, args.categoryId) self.delegate.onReorderChat(args.chatId, args.position, args.categoryId, args.prevCategoryId, false)
self.events.on(SIGNAL_COMMUNITY_CHANNEL_CATEGORY_CHANGED) do(e:Args): self.events.on(SIGNAL_COMMUNITY_CHANNEL_CATEGORY_CHANGED) do(e:Args):
let args = CommunityChatOrderArgs(e) let args = CommunityChatOrderArgs(e)
if (args.communityId == self.sectionId): if (args.communityId == self.sectionId):
self.delegate.onCommunityCategoryChannelChanged(args.chatId, args.categoryId) self.delegate.onReorderChat(args.chatId, args.position, args.categoryId, args.prevCategoryId, args.prevCategoryDeleted)
self.events.on(SIGNAL_RELOAD_MESSAGES) do(e: Args): self.events.on(SIGNAL_RELOAD_MESSAGES) do(e: Args):
let args = ReloadMessagesArgs(e) let args = ReloadMessagesArgs(e)

View File

@ -124,7 +124,7 @@ method onGroupChatDetailsUpdated*(self: AccessInterface, chatId: string, newName
method onCommunityChannelEdited*(self: AccessInterface, chat: ChatDto) {.base.} = method onCommunityChannelEdited*(self: AccessInterface, chat: ChatDto) {.base.} =
raise newException(ValueError, "No implementation available") raise newException(ValueError, "No implementation available")
method onReorderChat*(self: AccessInterface, chattId: string, position: int, newCategoryIdForChat: string) {.base.} = method onReorderChat*(self: AccessInterface, chattId: string, position: int, newCategoryIdForChat: string, prevCategoryId: string, prevCategoryDeleted: bool) {.base.} =
raise newException(ValueError, "No implementation available") raise newException(ValueError, "No implementation available")
method onReorderCategory*(self: AccessInterface, catId: string, position: int) {.base.} = method onReorderCategory*(self: AccessInterface, catId: string, position: int) {.base.} =
@ -327,4 +327,4 @@ method switchToChannel*(self: AccessInterface, channelName: string) =
raise newException(ValueError, "No implementation available") raise newException(ValueError, "No implementation available")
method joinSpectatedCommunity*(self: AccessInterface) = method joinSpectatedCommunity*(self: AccessInterface) =
raise newException(ValueError, "No implementation available") raise newException(ValueError, "No implementation available")

View File

@ -185,6 +185,12 @@ QtObject:
idx.inc idx.inc
return -1 return -1
proc hasEmptyChatItem*(self: Model, categoryId: string): bool =
for it in self.items:
if it.id == categoryId:
return true
return false
proc getCategoryNameForCategoryId*(self: Model, categoryId: string): string {.slot.} = proc getCategoryNameForCategoryId*(self: Model, categoryId: string): string {.slot.} =
let index = self.getItemIdxByCategory(categoryId) let index = self.getItemIdxByCategory(categoryId)
if index == -1: if index == -1:
@ -391,7 +397,7 @@ QtObject:
continue continue
item.categoryName = newName item.categoryName = newName
let modelIndex = self.createIndex(i, 0, nil) let modelIndex = self.createIndex(i, 0, nil)
self.dataChanged(modelIndex, modelIndex, @[ModelRole.CategoryName.int]) self.dataChanged(modelIndex, modelIndex, @[ModelRole.CategoryId.int, ModelRole.CategoryName.int])
proc renameItemById*(self: Model, id, name: string) = proc renameItemById*(self: Model, id, name: string) =
let index = self.getItemIdxById(id) let index = self.getItemIdxById(id)

View File

@ -29,6 +29,8 @@ import ../../../../app_service/service/contacts/dto/contacts as contacts_dto
export io_interface export io_interface
const CATEGORY_ID_PREFIX = "cat-"
logScope: logScope:
topics = "chat-section-module" topics = "chat-section-module"
@ -130,7 +132,7 @@ proc removeSubmodule(self: Module, chatId: string) =
proc addEmptyChatItemForCategory(self: Module, category: Category) = proc addEmptyChatItemForCategory(self: Module, category: Category) =
# Add an empty chat item that has the category info # Add an empty chat item that has the category info
let emptyChatItem = chat_item.initItem( let emptyChatItem = chat_item.initItem(
id = "cat-" & category.id, id = CATEGORY_ID_PREFIX & category.id,
name = "", name = "",
icon = "", icon = "",
color = "", color = "",
@ -599,6 +601,8 @@ method onCommunityCategoryDeleted*(self: Module, cat: Category, chats: seq[ChatD
newCategoryName = "", newCategoryName = "",
newCategoryPosition = -1, newCategoryPosition = -1,
) )
self.view.chatsModel().removeItemById(CATEGORY_ID_PREFIX & cat.id)
method setFirstChannelAsActive*(self: Module) = method setFirstChannelAsActive*(self: Module) =
if(self.view.chatsModel().getCount() == 0): if(self.view.chatsModel().getCount() == 0):
@ -614,7 +618,7 @@ method onCommunityCategoryChannelChanged*(self: Module, channelId: string, newCa
else: else:
self.setActiveItem(channelId) self.setActiveItem(channelId)
method onReorderChat*(self: Module, chatId: string, position: int, newCategoryIdForChat: string) = method onReorderChat*(self: Module, chatId: string, position: int, newCategoryIdForChat: string, prevCategoryId: string, prevCategoryDeleted: bool) =
var newCategoryName = "" var newCategoryName = ""
var newCategoryPos = -1 var newCategoryPos = -1
if newCategoryIdForChat != "": if newCategoryIdForChat != "":
@ -622,6 +626,10 @@ method onReorderChat*(self: Module, chatId: string, position: int, newCategoryId
newCategoryName = newCategory.name newCategoryName = newCategory.name
newCategoryPos = newCategory.position newCategoryPos = newCategory.position
self.view.chatsModel().reorderChatById(chatId, position, newCategoryIdForChat, newCategoryName, newCategoryPos) self.view.chatsModel().reorderChatById(chatId, position, newCategoryIdForChat, newCategoryName, newCategoryPos)
if prevCategoryId != "" and not prevCategoryDeleted:
if not self.view.chatsModel().hasEmptyChatItem(CATEGORY_ID_PREFIX & prevCategoryId):
let category = self.controller.getCommunityCategoryDetails(self.controller.getMySectionId(), prevCategoryId)
self.addEmptyChatItemForCategory(category)
method onReorderCategory*(self: Module, catId: string, position: int) = method onReorderCategory*(self: Module, catId: string, position: int) =
self.view.chatsModel().reorderCategoryById(catId, position) self.view.chatsModel().reorderCategoryById(catId, position)

View File

@ -52,6 +52,8 @@ type
chatId*: string chatId*: string
categoryId*: string categoryId*: string
position*: int position*: int
prevCategoryId*: string
prevCategoryDeleted*: bool
CommunityCategoryOrderArgs* = ref object of Args CommunityCategoryOrderArgs* = ref object of Args
communityId*: string communityId*: string
@ -371,10 +373,14 @@ QtObject:
if community.settings.id == "": if community.settings.id == "":
community.settings = prev_community.settings community.settings = prev_community.settings
var deletedCategories: seq[string] = @[]
# category was added # category was added
if(community.categories.len > prev_community.categories.len): if(community.categories.len > prev_community.categories.len):
for category in community.categories: for category in community.categories:
if findIndexById(category.id, prev_community.categories) == -1: if findIndexById(category.id, prev_community.categories) == -1:
self.allCommunities[community.id].categories.add(category)
self.joinedCommunities[community.id].categories.add(category)
let chats = self.getChatsInCategory(community, category.id) let chats = self.getChatsInCategory(community, category.id)
self.events.emit(SIGNAL_COMMUNITY_CATEGORY_CREATED, self.events.emit(SIGNAL_COMMUNITY_CATEGORY_CREATED,
@ -384,6 +390,7 @@ QtObject:
elif(community.categories.len < prev_community.categories.len): elif(community.categories.len < prev_community.categories.len):
for prv_category in prev_community.categories: for prv_category in prev_community.categories:
if findIndexById(prv_category.id, community.categories) == -1: if findIndexById(prv_category.id, community.categories) == -1:
deletedCategories.add(prv_category.id)
self.events.emit(SIGNAL_COMMUNITY_CATEGORY_DELETED, self.events.emit(SIGNAL_COMMUNITY_CATEGORY_DELETED,
CommunityCategoryArgs(communityId: community.id, category: Category(id: prv_category.id))) CommunityCategoryArgs(communityId: community.id, category: Category(id: prv_category.id)))
@ -440,8 +447,16 @@ QtObject:
# Handle channel was added/removed to/from category # Handle channel was added/removed to/from category
if chat.categoryId != prev_chat.categoryId: if chat.categoryId != prev_chat.categoryId:
var prevCategoryDeleted = false
if chat.categoryId == "":
for catId in deletedCategories:
if prev_chat.categoryId == catId:
prevCategoryDeleted = true
break
self.events.emit(SIGNAL_COMMUNITY_CHANNEL_CATEGORY_CHANGED, CommunityChatOrderArgs(communityId: community.id, self.events.emit(SIGNAL_COMMUNITY_CHANNEL_CATEGORY_CHANGED, CommunityChatOrderArgs(communityId: community.id,
chatId: chat.id, categoryId: chat.categoryId, position: chat.position)) chatId: chat.id, categoryId: chat.categoryId, position: chat.position, prevCategoryId: prev_chat.categoryId, prevCategoryDeleted: prevCategoryDeleted))
# Handle name/description changes # Handle name/description changes
if chat.name != prev_chat.name or chat.description != prev_chat.description or chat.color != prev_chat.color: if chat.name != prev_chat.name or chat.description != prev_chat.description or chat.color != prev_chat.color: