From 88bbfef179dd207880e57001b74a585f59fa7e01 Mon Sep 17 00:00:00 2001 From: Sale Djenic Date: Thu, 19 May 2022 12:24:53 +0200 Subject: [PATCH] fix(@desktop/profile): changing username is not immediately updating Fixes #5765 --- src/app/boot/app_controller.nim | 17 +++++-- src/app/global/user_profile.nim | 2 + .../profile_section/profile/controller.nim | 6 +-- .../main/profile_section/profile/module.nim | 18 +------ src/app_service/service/contacts/service.nim | 3 +- src/app_service/service/profile/service.nim | 47 +++++++++++++++---- .../service/settings/dto/settings.nim | 2 +- src/app_service/service/settings/service.nim | 6 +++ 8 files changed, 67 insertions(+), 34 deletions(-) diff --git a/src/app/boot/app_controller.nim b/src/app/boot/app_controller.nim index 77d53f54d0..289bcf929f 100644 --- a/src/app/boot/app_controller.nim +++ b/src/app/boot/app_controller.nim @@ -1,4 +1,4 @@ -import NimQml +import NimQml, chronicles import ../../app_service/service/general/service as general_service import ../../app_service/service/keychain/service as keychain_service @@ -38,6 +38,9 @@ import ../global/global_singleton import ../core/[main] +logScope: + topics = "app-controller" + type AppController* = ref object of RootObj statusFoundation: StatusFoundation @@ -147,7 +150,7 @@ proc newAppController*(statusFoundation: StatusFoundation): AppController = result.transactionService = transaction_service.newService(statusFoundation.events, statusFoundation.threadpool, result.walletAccountService, result.networkService, result.settingsService, result.tokenService) result.bookmarkService = bookmark_service.newService() - result.profileService = profile_service.newService() + result.profileService = profile_service.newService(result.contactsService, result.settingsService) result.stickersService = stickers_service.newService( statusFoundation.events, statusFoundation.threadpool, @@ -353,8 +356,9 @@ proc buildAndRegisterLocalAccountSensitiveSettings(self: AppController) = proc buildAndRegisterUserProfile(self: AppController) = let pubKey = self.settingsService.getPublicKey() + let alias = self.settingsService.getName() let preferredName = self.settingsService.getPreferredName() - let displayName = self.settingsService.getDisplayName() + var displayName = self.settingsService.getDisplayName() let ensUsernames = self.settingsService.getEnsUsernames() let firstEnsName = if (ensUsernames.len > 0): ensUsernames[0] else: "" let sendUserStatus = self.settingsService.getSendStatusUpdates() @@ -369,7 +373,12 @@ proc buildAndRegisterUserProfile(self: AppController) = elif(img.imgType == "thumbnail"): thumbnail = img.uri - singletonInstance.userProfile.setFixedData(loggedInAccount.name, loggedInAccount.keyUid, pubKey) + # loggedInAccount.name and displayName must be the same, if not, we are using loggedInAccount.name + if (loggedInAccount.name != displayName): + info "login account name and display name stored in settings differ" + displayName = loggedInAccount.name + + singletonInstance.userProfile.setFixedData(alias, loggedInAccount.keyUid, pubKey) singletonInstance.userProfile.setDisplayName(displayName) singletonInstance.userProfile.setPreferredName(preferredName) singletonInstance.userProfile.setEnsName(firstEnsName) diff --git a/src/app/global/user_profile.nim b/src/app/global/user_profile.nim index c536e9258d..29804d92d3 100644 --- a/src/app/global/user_profile.nim +++ b/src/app/global/user_profile.nim @@ -115,6 +115,8 @@ QtObject: notify = nameChanged proc setDisplayName*(self: UserProfile, displayName: string) = # Not a slot + if(self.displayName == displayName): + return self.displayName = displayName self.nameChanged() diff --git a/src/app/modules/main/profile_section/profile/controller.nim b/src/app/modules/main/profile_section/profile/controller.nim index 23f46288ab..d07f86c453 100644 --- a/src/app/modules/main/profile_section/profile/controller.nim +++ b/src/app/modules/main/profile_section/profile/controller.nim @@ -19,11 +19,11 @@ proc delete*(self: Controller) = proc init*(self: Controller) = discard -proc storeIdentityImage*(self: Controller, address: string, image: string, aX: int, aY: int, bX: int, bY: int): seq[Image] = - return self.profileService.storeIdentityImage(address, image, aX, aY, bX, bY) +proc storeIdentityImage*(self: Controller, address: string, image: string, aX: int, aY: int, bX: int, bY: int) = + self.profileService.storeIdentityImage(address, image, aX, aY, bX, bY) proc deleteIdentityImage*(self: Controller, address: string) = self.profileService.deleteIdentityImage(address) -proc setDisplayName*(self: Controller, displayName: string): bool = +proc setDisplayName*(self: Controller, displayName: string) = self.profileService.setDisplayName(displayName) \ No newline at end of file diff --git a/src/app/modules/main/profile_section/profile/module.nim b/src/app/modules/main/profile_section/profile/module.nim index 39d531414c..1fb2618af6 100644 --- a/src/app/modules/main/profile_section/profile/module.nim +++ b/src/app/modules/main/profile_section/profile/module.nim @@ -50,25 +50,11 @@ method viewDidLoad*(self: Module) = method storeIdentityImage*(self: Module, imageUrl: string, aX: int, aY: int, bX: int, bY: int) = let address = singletonInstance.userProfile.getAddress() let image = singletonInstance.utils.formatImagePath(imageUrl) - let storedImages = self.controller.storeIdentityImage(address, image, aX, aY, bX, bY) - if(storedImages.len == 0): - error "error: array of stored images is empty" - return - - for img in storedImages: - if(img.imgType == "large"): - singletonInstance.userProfile.setLargeImage(img.uri) - elif(img.imgType == "thumbnail"): - singletonInstance.userProfile.setThumbnailImage(img.uri) + self.controller.storeIdentityImage(address, image, aX, aY, bX, bY) method deleteIdentityImage*(self: Module) = let address = singletonInstance.userProfile.getAddress() self.controller.deleteIdentityImage(address) - singletonInstance.userProfile.setLargeImage("") - singletonInstance.userProfile.setThumbnailImage("") method setDisplayName*(self: Module, displayName: string) = - if self.controller.setDisplayName(displayName): - singletonInstance.userProfile.setDisplayName(displayName) - else: - error "could not set display name" + self.controller.setDisplayName(displayName) diff --git a/src/app_service/service/contacts/service.nim b/src/app_service/service/contacts/service.nim index 94a282b644..3f00f3060b 100644 --- a/src/app_service/service/contacts/service.nim +++ b/src/app_service/service/contacts/service.nim @@ -228,7 +228,8 @@ QtObject: # If we try to get the contact details of ourselves, just return our own info return ContactsDto( id: singletonInstance.userProfile.getPubKey(), - name: singletonInstance.userProfile.getName(), + displayName: singletonInstance.userProfile.getDisplayName(), + name: singletonInstance.userProfile.getEnsName(), alias: singletonInstance.userProfile.getUsername(), ensVerified: singletonInstance.userProfile.getEnsName().len > 0, added: true, diff --git a/src/app_service/service/profile/service.nim b/src/app_service/service/profile/service.nim index e17e660ef3..afcc26c61f 100644 --- a/src/app_service/service/profile/service.nim +++ b/src/app_service/service/profile/service.nim @@ -1,5 +1,9 @@ import json, chronicles +import ../contacts/service as contact_service +import ../settings/service as settings_service +import ../../../app/global/global_singleton + import ../../../backend/accounts as status_accounts import ./dto/profile as profile_dto @@ -11,26 +15,39 @@ logScope: type Service* = ref object of RootObj + contactService: contact_service.Service + settingsService: settings_service.Service proc delete*(self: Service) = discard -proc newService*(): Service = +proc newService*(contactService: contact_service.Service, settingsService: settings_service.Service): Service = result = Service() + result.contactService = contactService + result.settingsService = settingsService proc init*(self: Service) = discard -proc storeIdentityImage*(self: Service, address: string, image: string, aX: int, aY: int, bX: int, bY: int): seq[Image] = +proc storeIdentityImage*(self: Service, address: string, image: string, aX: int, aY: int, bX: int, bY: int) = try: let response = status_accounts.storeIdentityImage(address, image, aX, aY, bX, bY) - + if(not response.error.isNil): + error "could not store identity images" + return if(response.result.kind != JArray): error "error: ", procName="storeIdentityImage", errDesription = "response is not an array" return + if(response.result.len == 0): + error "error: array of stored images is empty" + return for img in response.result: - result.add(toImage(img)) + let imageDto = toImage(img) + if(imageDto.imgType == "large"): + singletonInstance.userProfile.setLargeImage(imageDto.uri) + elif(imageDto.imgType == "thumbnail"): + singletonInstance.userProfile.setThumbnailImage(imageDto.uri) except Exception as e: error "error: ", procName="storeIdentityImage", errName = e.name, errDesription = e.msg @@ -38,14 +55,26 @@ proc storeIdentityImage*(self: Service, address: string, image: string, aX: int, proc deleteIdentityImage*(self: Service, address: string) = try: let response = status_accounts.deleteIdentityImage(address) + if(not response.error.isNil): + error "could not delete identity images" + return + singletonInstance.userProfile.setLargeImage("") + singletonInstance.userProfile.setThumbnailImage("") except Exception as e: error "error: ", procName="deleteIdentityImage", errName = e.name, errDesription = e.msg -proc setDisplayName*(self: Service, displayName: string): bool = +proc setDisplayName*(self: Service, displayName: string) = try: - discard status_accounts.setDisplayName(displayName) - return true + let response = status_accounts.setDisplayName(displayName) + if(not response.error.isNil): + error "could not set display name" + return + if(not self.settingsService.saveDisplayName(displayName)): + error "could save display name to the settings" + return + + singletonInstance.userProfile.setDisplayName(displayName) + except Exception as e: - error "error: ", procName="setDisplayName", errName = e.name, errDesription = e.msg - return false \ No newline at end of file + error "error: ", procName="setDisplayName", errName = e.name, errDesription = e.msg \ No newline at end of file diff --git a/src/app_service/service/settings/dto/settings.nim b/src/app_service/service/settings/dto/settings.nim index 63fc3c3676..425ec417c4 100644 --- a/src/app_service/service/settings/dto/settings.nim +++ b/src/app_service/service/settings/dto/settings.nim @@ -44,7 +44,7 @@ const KEY_AUTO_MESSAGE_ENABLED* = "auto-message-enabled?" const KEY_GIF_FAVORITES* = "gifs/favorite-gifs" const KEY_GIF_RECENTS* = "gifs/recent-gifs" const KEY_GIF_API_KEY* = "gifs/api-key" -const KEY_DISPLAY_NAME = "display-name" +const KEY_DISPLAY_NAME* = "display-name" const KEY_TEST_NETWORKS_ENABLED* = "test-networks-enabled?" const PROFILE_PICTURES_VISIBILITY_CONTACTS_ONLY* = 1 diff --git a/src/app_service/service/settings/service.nim b/src/app_service/service/settings/service.nim index 077b95b35b..39a158ac2c 100644 --- a/src/app_service/service/settings/service.nim +++ b/src/app_service/service/settings/service.nim @@ -121,6 +121,12 @@ proc getPreferredName*(self: Service): string = proc getDisplayName*(self: Service): string = return self.settings.displayName +proc saveDisplayName*(self: Service, value: string): bool = + if(self.saveSetting(KEY_DISPLAY_NAME, value)): + self.settings.displayName = value + return true + return false + proc saveNewEnsUsername*(self: Service, username: string): bool = var newEnsUsernames = self.settings.ensUsernames newEnsUsernames.add(username)