From 358e3c4a1633dfb02bc4f4d2dd7dd8acb61af00a Mon Sep 17 00:00:00 2001 From: Khushboo Mehta Date: Wed, 26 Jan 2022 20:31:45 +0100 Subject: [PATCH] fix: Move gif to status-go fixes #4355 --- src/app/boot/app_controller.nim | 4 +- src/app_service/service/gif/service.nim | 131 +++++++++--------- .../service/settings/dto/settings.nim | 3 +- src/app_service/service/settings/service.nim | 16 --- .../service/settings/service_interface.nim | 12 -- 5 files changed, 73 insertions(+), 93 deletions(-) diff --git a/src/app/boot/app_controller.nim b/src/app/boot/app_controller.nim index 446dcb2c1e..227c495fbf 100644 --- a/src/app/boot/app_controller.nim +++ b/src/app/boot/app_controller.nim @@ -259,6 +259,7 @@ proc delete*(self: AppController) = self.profileService.delete self.generalService.delete self.ensService.delete + self.gifService.delete proc startupDidLoad*(self: AppController) = singletonInstance.engine.setRootContextProperty("localAppSettings", self.localAppSettingsVariant) @@ -302,6 +303,7 @@ proc load(self: AppController) = self.devicesService.init() self.mailserversService.init() self.ensService.init() + self.gifService.init() let pubKey = self.settingsService.getPublicKey() singletonInstance.localAccountSensitiveSettings.setFileName(pubKey) @@ -366,4 +368,4 @@ proc buildAndRegisterUserProfile(self: AppController) = singletonInstance.userProfile.setLargeImage(large) singletonInstance.userProfile.setUserStatus(sendUserStatus) - singletonInstance.engine.setRootContextProperty("userProfile", self.userProfileVariant) \ No newline at end of file + singletonInstance.engine.setRootContextProperty("userProfile", self.userProfileVariant) diff --git a/src/app_service/service/gif/service.nim b/src/app_service/service/gif/service.nim index 4156a1b8c6..e46d1af5a1 100644 --- a/src/app_service/service/gif/service.nim +++ b/src/app_service/service/gif/service.nim @@ -7,6 +7,7 @@ import chronicles import sequtils import ../settings/service_interface as settings_service +import status/statusgo_backend_new/gif as status_gif import ./dto import ./service_interface @@ -24,17 +25,12 @@ let TENOR_API_KEY_RESOLVED = else: TENOR_API_KEY -const baseUrl = "https://g.tenor.com/v1/" -let defaultParams = fmt("&media_filter=minimal&limit=50&key={TENOR_API_KEY_RESOLVED}") - type Service* = ref object of service_interface.ServiceInterface settingsService: settings_service.ServiceInterface client: HttpClient favorites: seq[GifDto] recents: seq[GifDto] - favoritesLoaded: bool - recentsLoaded: bool method delete*(self: Service) = discard @@ -46,33 +42,51 @@ proc newService*(settingsService: settings_service.ServiceInterface): Service = result.favorites = @[] result.recents = @[] -proc setFavoriteGifs(self: Service, gifDtos: seq[GifDto]) = - let node = %*{"items": map(gifDtos, toJsonNode)} - discard self.settingsService.saveGifFavorites(node) +proc setTenorAPIKey(self: Service) = + try: + let response = status_gif.setTenorAPIKey(TENOR_API_KEY_RESOLVED) + if(not response.error.isNil): + error "error setTenorAPIKey: ", errDescription = response.error.message -proc setRecentGifs(self: Service, gifDtos: seq[GifDto]) = - let node = %*{"items": map(gifDtos, toJsonNode)} - discard self.settingsService.saveGifRecents(node) + except Exception as e: + error "error: ", methodName="setTenorAPIKey", errName = e.name, errDesription = e.msg -proc getContentWithRetry(self: Service, path: string, maxRetry: int = 3): string = - var currentRetry = 0 - while true: - try: - let content = self.client.getContent(fmt("{baseUrl}{path}{defaultParams}")) - return content - except Exception as e: - currentRetry += 1 - error "could not query tenor API", msg=e.msg +proc getRecentGifs(self: Service) = + try: + let response = status_gif.getRecentGifs() - if currentRetry >= maxRetry: - raise + if(not response.error.isNil): + error "error getRecentGifs: ", errDescription = response.error.message - sleep(100 * currentRetry) + self.recents = map(response.result.getElems(), settingToGifDto) + + except Exception as e: + error "error: ", methodName="getRecentGifs", errName = e.name, errDesription = e.msg + +proc getFavoriteGifs(self: Service) = + try: + let response = status_gif.getFavoriteGifs() + + if(not response.error.isNil): + error "error getFavoriteGifs: ", errDescription = response.error.message + + self.favorites = map(response.result.getElems(), settingToGifDto) + + except Exception as e: + error "error: ", methodName="getFavoriteGifs", errName = e.name, errDesription = e.msg + +method init*(self: Service) = + # set Tenor API Key + self.setTenorAPIKey() + + # get recent and favorite gifs on the database + self.getRecentGifs() + self.getFavoriteGifs() proc tenorQuery(self: Service, path: string): seq[GifDto] = try: - let content = self.getContentWithRetry(path) - let doc = content.parseJson() + let response = status_gif.fetchGifs(path) + let doc = response.result.str.parseJson() var items: seq[GifDto] = @[] for json in doc["results"]: @@ -88,46 +102,9 @@ proc search*(self: Service, query: string): seq[GifDto] = proc getTrendings*(self: Service): seq[GifDto] = return self.tenorQuery("trending?") -proc getFavorites*(self: Service): seq[GifDto] = - if not self.favoritesLoaded: - self.favoritesLoaded = true - let node = self.settingsService.getGifFavorites() - self.favorites = map(node{"items"}.getElems(), settingToGifDto) - - return self.favorites - proc getRecents*(self: Service): seq[GifDto] = - if not self.recentsLoaded: - self.recentsLoaded = true - let node = self.settingsService.getGifRecents() - self.recents = map(node{"items"}.getElems(), settingToGifDto) - return self.recents -proc isFavorite*(self: Service, gifDto: GifDto): bool = - for favorite in self.getFavorites(): - if favorite.id == gifDto.id: - return true - - return false - -proc toggleFavorite*(self: Service, gifDto: GifDto) = - var newFavorites: seq[GifDto] = @[] - var found = false - - for favoriteGif in self.getFavorites(): - if favoriteGif.id == gifDto.id: - found = true - continue - - newFavorites.add(favoriteGif) - - if not found: - newFavorites.add(gifDto) - - self.favorites = newFavorites - self.setFavoriteGifs(newFavorites) - proc addToRecents*(self: Service, gifDto: GifDto) = let recents = self.getRecents() var newRecents: seq[GifDto] = @[gifDto] @@ -145,4 +122,32 @@ proc addToRecents*(self: Service, gifDto: GifDto) = idx += 1 self.recents = newRecents - self.setRecentGifs(newRecents) \ No newline at end of file + let recent = %*{"items": map(newRecents, toJsonNode)} + discard status_gif.updateRecentGifs(recent) + +proc getFavorites*(self: Service): seq[GifDto] = + return self.favorites + +proc isFavorite*(self: Service, gifDto: GifDto): bool = + for favorite in self.favorites: + if favorite.id == gifDto.id: + return true + return false + +proc toggleFavorite*(self: Service, gifDto: GifDto) = + var newFavorites: seq[GifDto] = @[] + var found = false + + for favoriteGif in self.getFavorites(): + if favoriteGif.id == gifDto.id: + found = true + continue + + newFavorites.add(favoriteGif) + + if not found: + newFavorites.add(gifDto) + + self.favorites = newFavorites + let favorites = %*{"items": map(newFavorites, toJsonNode)} + discard status_gif.updateFavoriteGifs(favorites) diff --git a/src/app_service/service/settings/dto/settings.nim b/src/app_service/service/settings/dto/settings.nim index e08379b4af..433bb19a86 100644 --- a/src/app_service/service/settings/dto/settings.nim +++ b/src/app_service/service/settings/dto/settings.nim @@ -43,6 +43,7 @@ const KEY_WAKU_BLOOM_FILTER_MODE* = "waku-bloom-filter-mode" 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" type UpstreamConfig* = object Enabled*: bool @@ -264,4 +265,4 @@ proc pinnedMailserverToJsonNode*(mailserver: PinnedMailserver): JsonNode = "wakuv2.prod": mailserver.wakuv2Prod, "wakuv2.test": mailserver.wakuv2Test, "go-waku.test": mailserver.goWakuTest - } \ No newline at end of file + } diff --git a/src/app_service/service/settings/service.nim b/src/app_service/service/settings/service.nim index 13521bfc27..abf2419838 100644 --- a/src/app_service/service/settings/service.nim +++ b/src/app_service/service/settings/service.nim @@ -450,20 +450,4 @@ method autoMessageEnabled*(self: Service): bool = method getWakuBloomFilterMode*(self: Service): bool = return self.settings.wakuBloomFilterMode -method getGifRecents*(self: Service): JsonNode = - return self.settings.gifRecents -method getGifFavorites*(self: Service): JsonNode = - return self.settings.gifFavorites - -method saveGifRecents*(self: Service, value: JsonNode): bool = - if(self.saveSetting(KEY_GIF_RECENTS, value)): - self.settings.gifRecents = value - return true - return false - -method saveGifFavorites*(self: Service, value: JsonNode): bool = - if(self.saveSetting(KEY_GIF_FAVORITES, value)): - self.settings.gifFavorites = value - return true - return false \ No newline at end of file diff --git a/src/app_service/service/settings/service_interface.nim b/src/app_service/service/settings/service_interface.nim index fb468da8d4..b5ea48286a 100644 --- a/src/app_service/service/settings/service_interface.nim +++ b/src/app_service/service/settings/service_interface.nim @@ -259,15 +259,3 @@ method autoMessageEnabled*(self: ServiceInterface): bool {.base.} = method getWakuBloomFilterMode*(self: ServiceInterface): bool {.base.} = raise newException(ValueError, "No implementation available") - -method getGifRecents*(self: ServiceInterface): JsonNode {.base.} = - raise newException(ValueError, "No implementation available") - -method getGifFavorites*(self: ServiceInterface): JsonNode {.base.} = - raise newException(ValueError, "No implementation available") - -method saveGifRecents*(self: ServiceInterface, value: JsonNode): bool {.base.} = - raise newException(ValueError, "No implementation available") - -method saveGifFavorites*(self: ServiceInterface, value: JsonNode): bool {.base.} = - raise newException(ValueError, "No implementation available") \ No newline at end of file