refactor(general): new convenient methods added to chat and contact services

- `getOneToOneChatNameAndImage` method added to the chat service, we should
use where ever in the app we need to display one to one chat image and name
- `getContactNameAndImage` method added to the contacts service, we should use
when we want to display pretty contact name and correct image/identicon depends
what user set
This commit is contained in:
Sale Djenic 2021-11-26 12:50:07 +01:00
parent 4c42809a68
commit 64c0148bbf
9 changed files with 33 additions and 27 deletions

View File

@ -116,8 +116,8 @@ method searchMessages*(self: Controller, searchTerm: string) =
self.messageService.asyncSearchMessages(communities, chats, self.searchTerm, false)
method getPrettyChatName*(self: Controller, chatId: string): string =
return self.chatService.prettyChatName(chatId)
method getOneToOneChatNameAndImage*(self: Controller, chatId: string): tuple[name: string, image: string] =
return self.chatService.getOneToOneChatNameAndImage(chatId)
method getContactById*(self: Controller, contactId: string): ContactsDto =
return self.contactsService.getContactById(contactId)
method getContactNameAndImage*(self: Controller, contactId: string): tuple[name: string, image: string] =
return self.contactsService.getContactNameAndImage(contactId)

View File

@ -51,8 +51,9 @@ method getChatDetails*(self: AccessInterface, communityId, chatId: string): Chat
method searchMessages*(self: AccessInterface, searchTerm: string) {.base.} =
raise newException(ValueError, "No implementation available")
method getPrettyChatName*(self: AccessInterface, chatId: string): string {.base.} =
method getOneToOneChatNameAndImage*(self: AccessInterface, chatId: string): tuple[name: string, image: string]
{.base.} =
raise newException(ValueError, "No implementation available")
method getContactById*(self: AccessInterface, contactId: string): ContactsDto {.base.} =
method getContactNameAndImage*(self: AccessInterface, contactId: string): tuple[name: string, image: string] {.base.} =
raise newException(ValueError, "No implementation available")

View File

@ -66,13 +66,6 @@ method viewDidLoad*(self: Module) =
method getModuleAsVariant*(self: Module): QVariant =
return self.viewVariant
proc getContactNameAndImage(self: Module, id: string): tuple[name: string, image: string] =
let contactDto = self.controller.getContactById(id)
result.name = contactDto.userNameOrAlias()
result.image = contactDto.identicon
if(contactDto.image.thumbnail.len > 0):
result.image = contactDto.image.thumbnail
proc buildLocationMenuForChat(self: Module): location_menu_item.Item =
var item = location_menu_item.initItem(conf.CHAT_SECTION_ID, SEARCH_MENU_LOCATION_CHAT_SECTION_NAME, "", "chat", "",
false)
@ -85,7 +78,7 @@ proc buildLocationMenuForChat(self: Module): location_menu_item.Item =
var chatName = c.name
var chatImage = c.identicon
if(c.chatType == ChatType.OneToOne):
(chatName, chatImage) = self.getContactNameAndImage(c.id)
(chatName, chatImage) = self.controller.getOneToOneChatNameAndImage(c.id)
let subItem = location_menu_sub_item.initSubItem(c.id, chatName, chatImage, "", c.color, chatImage.len == 0)
subItems.add(subItem)
@ -189,7 +182,7 @@ method onSearchMessagesDone*(self: Module, messages: seq[MessageDto]) =
var chatName = c.name
var chatImage = c.identicon
if(c.chatType == ChatType.OneToOne):
(chatName, chatImage) = self.getContactNameAndImage(c.id)
(chatName, chatImage) = self.controller.getOneToOneChatNameAndImage(c.id)
var rawChatName = chatName
if(chatName.startsWith("@")):
@ -210,12 +203,12 @@ method onSearchMessagesDone*(self: Module, messages: seq[MessageDto]) =
continue
let chatDto = self.controller.getChatDetails("", m.chatId)
let (senderName, senderImage) = self.getContactNameAndImage(m.`from`)
let (senderName, senderImage) = self.controller.getContactNameAndImage(m.`from`)
if(chatDto.communityId.len == 0):
var chatName = chatDto.name
var chatImage = chatDto.identicon
if(chatDto.chatType == ChatType.OneToOne):
(chatName, chatImage) = self.getContactNameAndImage(chatDto.id)
(chatName, chatImage) = self.controller.getOneToOneChatNameAndImage(chatDto.id)
let item = result_item.initItem(m.id, m.text, $m.timestamp, m.`from`, senderName,
SEARCH_RESULT_MESSAGES_SECTION_NAME, senderImage, "", chatName, "", chatImage, chatDto.color, chatImage.len == 0)

View File

@ -76,5 +76,5 @@ method setActiveItemSubItem*(self: Controller, itemId: string, subItemId: string
self.delegate.activeItemSubItemSet(self.activeItemId, self.activeSubItemId)
method getPrettyChatName*(self: Controller, chatId: string): string =
return self.chatService.prettyChatName(chatId)
method getOneToOneChatNameAndImage*(self: Controller, chatId: string): tuple[name: string, image: string] =
return self.chatService.getOneToOneChatNameAndImage(chatId)

View File

@ -38,5 +38,6 @@ method getChatDetailsForChatTypes*(self: AccessInterface, types: seq[ChatType]):
method setActiveItemSubItem*(self: AccessInterface, itemId: string, subItemId: string) {.base.} =
raise newException(ValueError, "No implementation available")
method getPrettyChatName*(self: AccessInterface, chatId: string): string {.base.} =
method getOneToOneChatNameAndImage*(self: AccessInterface, chatId: string): tuple[name: string, image: string]
{.base.} =
raise newException(ValueError, "No implementation available")

View File

@ -72,9 +72,11 @@ proc buildChatUI(self: Module, events: EventEmitter, chatService: chat_service.S
let hasNotification = c.unviewedMessagesCount > 0 or c.unviewedMentionsCount > 0
let notificationsCount = c.unviewedMentionsCount
var chatName = c.name
var chatImage = c.identicon
if(c.chatType == ChatType.OneToOne):
chatName = self.controller.getPrettyChatName(c.id)
let item = initItem(c.id, chatName, c.identicon, c.color, c.description, c.chatType.int, hasNotification,
(chatName, chatImage) = self.controller.getOneToOneChatNameAndImage(c.id)
let item = initItem(c.id, chatName, chatImage, c.color, c.description, c.chatType.int, hasNotification,
notificationsCount, c.muted, false, 0)
self.view.appendItem(item)
self.addSubmodule(c.id, false, events, chatService, communityService, messageService)

View File

@ -53,9 +53,8 @@ method getChatById*(self: Service, chatId: string): ChatDto =
return self.chats[chatId]
method prettyChatName*(self: Service, chatId: string): string =
let contact = self.contactService.getContactById(chatId)
return contact.userNameOrAlias()
method getOneToOneChatNameAndImage*(self: Service, chatId: string): tuple[name: string, image: string] =
return self.contactService.getContactNameAndImage(chatId)
# TODO refactor this to new object types
proc parseChatResponse*(self: Service, response: string): (seq[Chat], seq[Message]) =

View File

@ -23,7 +23,8 @@ method getChatsOfChatTypes*(self: ServiceInterface, types: seq[chat_dto.ChatType
method getChatById*(self: ServiceInterface, chatId: string): ChatDto {.base.} =
raise newException(ValueError, "No implementation available")
method prettyChatName*(self: ServiceInterface, chatId: string): string {.base.} =
method getOneToOneChatNameAndImage*(self: ServiceInterface, chatId: string): tuple[name: string, image: string]
{.base.} =
raise newException(ValueError, "No implementation available")
method parseChatResponse*(self: ServiceInterface, response: string): (seq[Chat], seq[Message]) {.base.} =

View File

@ -110,7 +110,16 @@ QtObject:
hasAddedUs: false
)
proc saveContact(self: Service, contact: ContactsDto) =
proc getContactNameAndImage*(self: Service, publicKey: string): tuple[name: string, image: string] =
## This proc should be used accross the app in order to have for the same contact
## same image and name displayed everywhere in the app.
let contactDto = self.getContactById(publicKey)
result.name = contactDto.userNameOrAlias()
result.image = contactDto.identicon
if(contactDto.image.thumbnail.len > 0):
result.image = contactDto.image.thumbnail
proc saveContact(self: Service, contact: ContactsDto) =
# we must keep local contacts updated
self.contacts[contact.id] = contact