refactor(app_search): refactor app search to use new getChats API
Fixes #5184
This commit is contained in:
parent
72d726f250
commit
254bda51ed
|
@ -84,15 +84,12 @@ proc setSearchLocation*(self: Controller, location: string, subLocation: string)
|
||||||
self.searchLocation = location
|
self.searchLocation = location
|
||||||
self.searchSubLocation = subLocation
|
self.searchSubLocation = subLocation
|
||||||
|
|
||||||
proc getJoinedCommunities*(self: Controller): seq[CommunityDto] =
|
proc getChannelGroups*(self: Controller): seq[ChannelGroupDto] =
|
||||||
return self.communityService.getJoinedCommunities()
|
return self.chatService.getChannelGroups()
|
||||||
|
|
||||||
proc getCommunityById*(self: Controller, communityId: string): CommunityDto =
|
proc getCommunityById*(self: Controller, communityId: string): CommunityDto =
|
||||||
return self.communityService.getCommunityById(communityId)
|
return self.communityService.getCommunityById(communityId)
|
||||||
|
|
||||||
proc getAllChatsForCommunity*(self: Controller, communityId: string): seq[ChatDto] =
|
|
||||||
return self.communityService.getAllChats(communityId)
|
|
||||||
|
|
||||||
proc getChatDetailsForChatTypes*(self: Controller, types: seq[ChatType]): seq[ChatDto] =
|
proc getChatDetailsForChatTypes*(self: Controller, types: seq[ChatType]): seq[ChatDto] =
|
||||||
return self.chatService.getChatsOfChatTypes(types)
|
return self.chatService.getChatsOfChatTypes(types)
|
||||||
|
|
||||||
|
|
|
@ -66,34 +66,28 @@ method viewDidLoad*(self: Module) =
|
||||||
method getModuleAsVariant*(self: Module): QVariant =
|
method getModuleAsVariant*(self: Module): QVariant =
|
||||||
return self.viewVariant
|
return self.viewVariant
|
||||||
|
|
||||||
proc buildLocationMenuForChat(self: Module): location_menu_item.Item =
|
proc buildLocationMenuForChannelGroup(self: Module, channelGroup: ChannelGroupDto): location_menu_item.Item =
|
||||||
var item = location_menu_item.initItem(singletonInstance.userProfile.getPubKey(),
|
let isCommunity = channelGroup.channelGroupType == ChannelGroupType.Community
|
||||||
SEARCH_MENU_LOCATION_CHAT_SECTION_NAME, "", "chat", "")
|
|
||||||
|
|
||||||
let types = @[ChatType.OneToOne, ChatType.Public, ChatType.PrivateGroupChat]
|
var item = location_menu_item.initItem(
|
||||||
let displayedChats = self.controller.getChatDetailsForChatTypes(types)
|
channelGroup.id,
|
||||||
|
if (isCommunity): channelGroup.name else: SEARCH_MENU_LOCATION_CHAT_SECTION_NAME,
|
||||||
|
channelGroup.images.thumbnail,
|
||||||
|
icon=if (isCommunity): "" else: "chat",
|
||||||
|
channelGroup.color)
|
||||||
|
|
||||||
var subItems: seq[location_menu_sub_item.SubItem]
|
var subItems: seq[location_menu_sub_item.SubItem]
|
||||||
for c in displayedChats:
|
for chatDto in channelGroup.chats:
|
||||||
var chatName = c.name
|
var chatName = chatDto.name
|
||||||
var chatImage = c.icon
|
var chatImage = chatDto.icon
|
||||||
if(c.chatType == ChatType.OneToOne):
|
if(chatDto.chatType == ChatType.OneToOne):
|
||||||
(chatName, chatImage) = self.controller.getOneToOneChatNameAndImage(c.id)
|
(chatName, chatImage) = self.controller.getOneToOneChatNameAndImage(chatDto.id)
|
||||||
|
let subItem = location_menu_sub_item.initSubItem(
|
||||||
let subItem = location_menu_sub_item.initSubItem(c.id, chatName, chatImage, "", c.color)
|
chatDto.id,
|
||||||
subItems.add(subItem)
|
chatName,
|
||||||
|
chatImage,
|
||||||
item.setSubItems(subItems)
|
"",
|
||||||
return item
|
chatDto.color)
|
||||||
|
|
||||||
proc buildLocationMenuForCommunity(self: Module, community: CommunityDto): location_menu_item.Item =
|
|
||||||
var item = location_menu_item.initItem(community.id, community.name, community.images.thumbnail, "", community.color)
|
|
||||||
|
|
||||||
var subItems: seq[location_menu_sub_item.SubItem]
|
|
||||||
let chats = self.controller.getAllChatsForCommunity(community.id)
|
|
||||||
for c in chats:
|
|
||||||
let chatDto = self.controller.getChatDetails(community.id, c.id)
|
|
||||||
let subItem = location_menu_sub_item.initSubItem(chatDto.id, chatDto.name, chatDto.icon, "", chatDto.color)
|
|
||||||
subItems.add(subItem)
|
subItems.add(subItem)
|
||||||
|
|
||||||
item.setSubItems(subItems)
|
item.setSubItems(subItems)
|
||||||
|
@ -101,11 +95,10 @@ proc buildLocationMenuForCommunity(self: Module, community: CommunityDto): locat
|
||||||
|
|
||||||
method prepareLocationMenuModel*(self: Module) =
|
method prepareLocationMenuModel*(self: Module) =
|
||||||
var items: seq[location_menu_item.Item]
|
var items: seq[location_menu_item.Item]
|
||||||
items.add(self.buildLocationMenuForChat())
|
let channelGroups = self.controller.getChannelGroups()
|
||||||
|
|
||||||
let communities = self.controller.getJoinedCommunities()
|
for c in channelGroups:
|
||||||
for c in communities:
|
items.add(self.buildLocationMenuForChannelGroup(c))
|
||||||
items.add(self.buildLocationMenuForCommunity(c))
|
|
||||||
|
|
||||||
self.view.locationMenuModel().setItems(items)
|
self.view.locationMenuModel().setItems(items)
|
||||||
|
|
||||||
|
@ -149,53 +142,69 @@ method onSearchMessagesDone*(self: Module, messages: seq[MessageDto]) =
|
||||||
var items: seq[result_item.Item]
|
var items: seq[result_item.Item]
|
||||||
var channels: seq[result_item.Item]
|
var channels: seq[result_item.Item]
|
||||||
|
|
||||||
# Add communities
|
# Add Channel groups
|
||||||
let communities = self.controller.getJoinedCommunities()
|
let channelGroups = self.controller.getChannelGroups()
|
||||||
for co in communities:
|
let searchTerm = self.controller.searchTerm().toLower
|
||||||
if(self.controller.searchLocation().len == 0 and co.name.toLower.startsWith(self.controller.searchTerm().toLower)):
|
for channelGroup in channelGroups:
|
||||||
let item = result_item.initItem(co.id, "", "", co.id, co.name, SEARCH_RESULT_COMMUNITIES_SECTION_NAME,
|
let isCommunity = channelGroup.channelGroupType == ChannelGroupType.Community
|
||||||
co.images.thumbnail, co.color, "", "", co.images.thumbnail, co.color, false)
|
if(self.controller.searchLocation().len == 0 and
|
||||||
|
channelGroup.name.toLower.startsWith(searchTerm)):
|
||||||
|
let item = result_item.initItem(
|
||||||
|
channelGroup.id,
|
||||||
|
content="",
|
||||||
|
time="",
|
||||||
|
titleId=channelGroup.id,
|
||||||
|
title=channelGroup.name,
|
||||||
|
if (isCommunity):
|
||||||
|
SEARCH_RESULT_COMMUNITIES_SECTION_NAME
|
||||||
|
else:
|
||||||
|
SEARCH_RESULT_CHATS_SECTION_NAME,
|
||||||
|
channelGroup.images.thumbnail,
|
||||||
|
channelGroup.color,
|
||||||
|
badgePrimaryText="",
|
||||||
|
badgeSecondaryText="",
|
||||||
|
channelGroup.images.thumbnail,
|
||||||
|
channelGroup.color,
|
||||||
|
badgeIsLetterIdenticon=false)
|
||||||
|
|
||||||
self.controller.addResultItemDetails(co.id, co.id)
|
self.controller.addResultItemDetails(channelGroup.id, channelGroup.id)
|
||||||
items.add(item)
|
items.add(item)
|
||||||
|
|
||||||
# Add channels
|
# Add channels
|
||||||
if(self.controller.searchSubLocation().len == 0 and self.controller.searchLocation().len == 0 or
|
if(self.controller.searchSubLocation().len == 0 and self.controller.searchLocation().len == 0 or
|
||||||
self.controller.searchLocation() == co.id):
|
self.controller.searchLocation() == channelGroup.id):
|
||||||
for c in co.chats:
|
for chatDto in channelGroup.chats:
|
||||||
let chatDto = self.controller.getChatDetails(co.id, c.id)
|
var chatName = chatDto.name
|
||||||
if(c.name.toLower.startsWith(self.controller.searchTerm().toLower)):
|
var chatImage = chatDto.icon
|
||||||
let item = result_item.initItem(chatDto.id, "", "", chatDto.id, chatDto.name,
|
if(chatDto.chatType == ChatType.OneToOne):
|
||||||
SEARCH_RESULT_CHANNELS_SECTION_NAME, chatDto.icon, chatDto.color, "", "", chatDto.icon, chatDto.color,
|
(chatName, chatImage) = self.controller.getOneToOneChatNameAndImage(chatDto.id)
|
||||||
false)
|
|
||||||
|
|
||||||
self.controller.addResultItemDetails(chatDto.id, co.id, chatDto.id)
|
var rawChatName = chatName
|
||||||
|
if(chatName.startsWith("@")):
|
||||||
|
rawChatName = chatName[1 ..^ 1]
|
||||||
|
|
||||||
|
if(rawChatName.toLower.startsWith(searchTerm)):
|
||||||
|
let item = result_item.initItem(
|
||||||
|
chatDto.id,
|
||||||
|
content="",
|
||||||
|
time="",
|
||||||
|
titleId=chatDto.id,
|
||||||
|
title=chatName,
|
||||||
|
if isCommunity:
|
||||||
|
SEARCH_RESULT_CHANNELS_SECTION_NAME
|
||||||
|
else:
|
||||||
|
SEARCH_RESULT_CHATS_SECTION_NAME,
|
||||||
|
chatImage,
|
||||||
|
chatDto.color,
|
||||||
|
badgePrimaryText="",
|
||||||
|
badgeSecondaryText="",
|
||||||
|
chatImage,
|
||||||
|
chatDto.color,
|
||||||
|
false)
|
||||||
|
|
||||||
|
self.controller.addResultItemDetails(chatDto.id, channelGroup.id, chatDto.id)
|
||||||
channels.add(item)
|
channels.add(item)
|
||||||
|
|
||||||
# Add chats
|
|
||||||
if(self.controller.searchLocation().len == 0 or
|
|
||||||
self.controller.searchLocation() == singletonInstance.userProfile.getPubKey() and
|
|
||||||
self.controller.searchSubLocation().len == 0):
|
|
||||||
let types = @[ChatType.OneToOne, ChatType.Public, ChatType.PrivateGroupChat]
|
|
||||||
let displayedChats = self.controller.getChatDetailsForChatTypes(types)
|
|
||||||
|
|
||||||
for c in displayedChats:
|
|
||||||
var chatName = c.name
|
|
||||||
var chatImage = c.icon
|
|
||||||
if(c.chatType == ChatType.OneToOne):
|
|
||||||
(chatName, chatImage) = self.controller.getOneToOneChatNameAndImage(c.id)
|
|
||||||
|
|
||||||
var rawChatName = chatName
|
|
||||||
if(chatName.startsWith("@")):
|
|
||||||
rawChatName = chatName[1 ..^ 1]
|
|
||||||
|
|
||||||
if(rawChatName.toLower.startsWith(self.controller.searchTerm().toLower)):
|
|
||||||
let item = result_item.initItem(c.id, "", "", c.id, chatName, SEARCH_RESULT_CHATS_SECTION_NAME, chatImage,
|
|
||||||
c.color, "", "", chatImage, c.color, false)
|
|
||||||
|
|
||||||
self.controller.addResultItemDetails(c.id, singletonInstance.userProfile.getPubKey(), c.id)
|
|
||||||
items.add(item)
|
|
||||||
|
|
||||||
# Add channels in order as requested by the design
|
# Add channels in order as requested by the design
|
||||||
items.add(channels)
|
items.add(channels)
|
||||||
|
|
||||||
|
@ -216,9 +225,9 @@ method onSearchMessagesDone*(self: Module, messages: seq[MessageDto]) =
|
||||||
var chatImage = chatDto.icon
|
var chatImage = chatDto.icon
|
||||||
if(chatDto.chatType == ChatType.OneToOne):
|
if(chatDto.chatType == ChatType.OneToOne):
|
||||||
(chatName, chatImage) = self.controller.getOneToOneChatNameAndImage(chatDto.id)
|
(chatName, chatImage) = self.controller.getOneToOneChatNameAndImage(chatDto.id)
|
||||||
|
|
||||||
let item = result_item.initItem(m.id, renderedMessageText, $m.timestamp, m.`from`, senderName,
|
let item = result_item.initItem(m.id, renderedMessageText, $m.timestamp, m.`from`, senderName,
|
||||||
SEARCH_RESULT_MESSAGES_SECTION_NAME, senderImage, "", chatName, "", chatImage, chatDto.color, false)
|
SEARCH_RESULT_MESSAGES_SECTION_NAME, senderImage, chatDto.color, chatName, "", chatImage,
|
||||||
|
chatDto.color, false)
|
||||||
|
|
||||||
self.controller.addResultItemDetails(m.id, singletonInstance.userProfile.getPubKey(),
|
self.controller.addResultItemDetails(m.id, singletonInstance.userProfile.getPubKey(),
|
||||||
chatDto.id, m.id)
|
chatDto.id, m.id)
|
||||||
|
@ -228,8 +237,8 @@ method onSearchMessagesDone*(self: Module, messages: seq[MessageDto]) =
|
||||||
let channelName = "#" & chatDto.name
|
let channelName = "#" & chatDto.name
|
||||||
|
|
||||||
let item = result_item.initItem(m.id, renderedMessageText, $m.timestamp, m.`from`, senderName,
|
let item = result_item.initItem(m.id, renderedMessageText, $m.timestamp, m.`from`, senderName,
|
||||||
SEARCH_RESULT_MESSAGES_SECTION_NAME, senderImage, "", community.name, channelName, community.images.thumbnail,
|
SEARCH_RESULT_MESSAGES_SECTION_NAME, senderImage, chatDto.color, community.name,
|
||||||
community.color, false)
|
channelName, community.images.thumbnail, community.color, false)
|
||||||
|
|
||||||
self.controller.addResultItemDetails(m.id, chatDto.communityId, chatDto.id, m.id)
|
self.controller.addResultItemDetails(m.id, chatDto.communityId, chatDto.id, m.id)
|
||||||
items.add(item)
|
items.add(item)
|
||||||
|
|
|
@ -15,8 +15,9 @@ type Item* = object
|
||||||
badgeIconColor: string
|
badgeIconColor: string
|
||||||
badgeIsLetterIdenticon: bool
|
badgeIsLetterIdenticon: bool
|
||||||
|
|
||||||
proc initItem*(itemId, content, time, titleId, title, sectionName: string, image, color, badgePrimaryText,
|
proc initItem*(itemId, content, time, titleId, title, sectionName: string, image, color,
|
||||||
badgeSecondaryText, badgeImage, badgeIconColor: string, badgeIsLetterIdenticon: bool):
|
badgePrimaryText, badgeSecondaryText, badgeImage, badgeIconColor: string,
|
||||||
|
badgeIsLetterIdenticon: bool):
|
||||||
Item =
|
Item =
|
||||||
|
|
||||||
result.itemId = itemId
|
result.itemId = itemId
|
||||||
|
|
|
@ -205,9 +205,6 @@ proc getMySectionId*(self: Controller): string =
|
||||||
proc isCommunity*(self: Controller): bool =
|
proc isCommunity*(self: Controller): bool =
|
||||||
return self.isCommunitySection
|
return self.isCommunitySection
|
||||||
|
|
||||||
proc getJoinedCommunities*(self: Controller): seq[CommunityDto] =
|
|
||||||
return self.communityService.getJoinedCommunities()
|
|
||||||
|
|
||||||
proc getMyCommunity*(self: Controller): CommunityDto =
|
proc getMyCommunity*(self: Controller): CommunityDto =
|
||||||
return self.communityService.getCommunityById(self.sectionId)
|
return self.communityService.getCommunityById(self.sectionId)
|
||||||
|
|
||||||
|
|
|
@ -154,9 +154,33 @@ QtObject:
|
||||||
proc hasChannel*(self: Service, chatId: string): bool =
|
proc hasChannel*(self: Service, chatId: string): bool =
|
||||||
self.chats.hasKey(chatId)
|
self.chats.hasKey(chatId)
|
||||||
|
|
||||||
|
|
||||||
|
proc getChatIndex*(self: Service, channelGroupId, chatId: string): int =
|
||||||
|
var i = 0
|
||||||
|
for chat in self.channelGroups[channelGroupId].chats:
|
||||||
|
if (chat.id == chatId):
|
||||||
|
return i
|
||||||
|
i.inc()
|
||||||
|
return -1
|
||||||
|
|
||||||
|
|
||||||
proc updateOrAddChat*(self: Service, chat: ChatDto) =
|
proc updateOrAddChat*(self: Service, chat: ChatDto) =
|
||||||
self.chats[chat.id] = chat
|
self.chats[chat.id] = chat
|
||||||
|
|
||||||
|
var channelGroupId = chat.communityId
|
||||||
|
if (channelGroupId == ""):
|
||||||
|
channelGroupId = singletonInstance.userProfile.getPubKey()
|
||||||
|
if (not self.channelGroups.contains(channelGroupId)):
|
||||||
|
warn "Unknown community for new channel update", channelGroupId
|
||||||
|
return
|
||||||
|
|
||||||
|
let index = self.getChatIndex(channelGroupId, chat.id)
|
||||||
|
if (index == -1):
|
||||||
|
self.channelGroups[channelGroupId].chats.add(chat)
|
||||||
|
else:
|
||||||
|
self.channelGroups[channelGroupId].chats[index] = chat
|
||||||
|
|
||||||
|
|
||||||
proc parseChatResponse*(self: Service, response: RpcResponse[JsonNode]): (seq[ChatDto], seq[MessageDto]) =
|
proc parseChatResponse*(self: Service, response: RpcResponse[JsonNode]): (seq[ChatDto], seq[MessageDto]) =
|
||||||
var chats: seq[ChatDto] = @[]
|
var chats: seq[ChatDto] = @[]
|
||||||
var messages: seq[MessageDto] = @[]
|
var messages: seq[MessageDto] = @[]
|
||||||
|
|
Loading…
Reference in New Issue