fix: Move gif to status-go

fixes #4355
This commit is contained in:
Khushboo Mehta 2022-01-26 20:31:45 +01:00 committed by Sale Djenic
parent 50fb0dc45f
commit 358e3c4a16
5 changed files with 73 additions and 93 deletions

View File

@ -259,6 +259,7 @@ proc delete*(self: AppController) =
self.profileService.delete self.profileService.delete
self.generalService.delete self.generalService.delete
self.ensService.delete self.ensService.delete
self.gifService.delete
proc startupDidLoad*(self: AppController) = proc startupDidLoad*(self: AppController) =
singletonInstance.engine.setRootContextProperty("localAppSettings", self.localAppSettingsVariant) singletonInstance.engine.setRootContextProperty("localAppSettings", self.localAppSettingsVariant)
@ -302,6 +303,7 @@ proc load(self: AppController) =
self.devicesService.init() self.devicesService.init()
self.mailserversService.init() self.mailserversService.init()
self.ensService.init() self.ensService.init()
self.gifService.init()
let pubKey = self.settingsService.getPublicKey() let pubKey = self.settingsService.getPublicKey()
singletonInstance.localAccountSensitiveSettings.setFileName(pubKey) singletonInstance.localAccountSensitiveSettings.setFileName(pubKey)
@ -366,4 +368,4 @@ proc buildAndRegisterUserProfile(self: AppController) =
singletonInstance.userProfile.setLargeImage(large) singletonInstance.userProfile.setLargeImage(large)
singletonInstance.userProfile.setUserStatus(sendUserStatus) singletonInstance.userProfile.setUserStatus(sendUserStatus)
singletonInstance.engine.setRootContextProperty("userProfile", self.userProfileVariant) singletonInstance.engine.setRootContextProperty("userProfile", self.userProfileVariant)

View File

@ -7,6 +7,7 @@ import chronicles
import sequtils import sequtils
import ../settings/service_interface as settings_service import ../settings/service_interface as settings_service
import status/statusgo_backend_new/gif as status_gif
import ./dto import ./dto
import ./service_interface import ./service_interface
@ -24,17 +25,12 @@ let TENOR_API_KEY_RESOLVED =
else: else:
TENOR_API_KEY TENOR_API_KEY
const baseUrl = "https://g.tenor.com/v1/"
let defaultParams = fmt("&media_filter=minimal&limit=50&key={TENOR_API_KEY_RESOLVED}")
type type
Service* = ref object of service_interface.ServiceInterface Service* = ref object of service_interface.ServiceInterface
settingsService: settings_service.ServiceInterface settingsService: settings_service.ServiceInterface
client: HttpClient client: HttpClient
favorites: seq[GifDto] favorites: seq[GifDto]
recents: seq[GifDto] recents: seq[GifDto]
favoritesLoaded: bool
recentsLoaded: bool
method delete*(self: Service) = method delete*(self: Service) =
discard discard
@ -46,33 +42,51 @@ proc newService*(settingsService: settings_service.ServiceInterface): Service =
result.favorites = @[] result.favorites = @[]
result.recents = @[] result.recents = @[]
proc setFavoriteGifs(self: Service, gifDtos: seq[GifDto]) = proc setTenorAPIKey(self: Service) =
let node = %*{"items": map(gifDtos, toJsonNode)} try:
discard self.settingsService.saveGifFavorites(node) 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]) = except Exception as e:
let node = %*{"items": map(gifDtos, toJsonNode)} error "error: ", methodName="setTenorAPIKey", errName = e.name, errDesription = e.msg
discard self.settingsService.saveGifRecents(node)
proc getContentWithRetry(self: Service, path: string, maxRetry: int = 3): string = proc getRecentGifs(self: Service) =
var currentRetry = 0 try:
while true: let response = status_gif.getRecentGifs()
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
if currentRetry >= maxRetry: if(not response.error.isNil):
raise 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] = proc tenorQuery(self: Service, path: string): seq[GifDto] =
try: try:
let content = self.getContentWithRetry(path) let response = status_gif.fetchGifs(path)
let doc = content.parseJson() let doc = response.result.str.parseJson()
var items: seq[GifDto] = @[] var items: seq[GifDto] = @[]
for json in doc["results"]: for json in doc["results"]:
@ -88,46 +102,9 @@ proc search*(self: Service, query: string): seq[GifDto] =
proc getTrendings*(self: Service): seq[GifDto] = proc getTrendings*(self: Service): seq[GifDto] =
return self.tenorQuery("trending?") 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] = 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 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) = proc addToRecents*(self: Service, gifDto: GifDto) =
let recents = self.getRecents() let recents = self.getRecents()
var newRecents: seq[GifDto] = @[gifDto] var newRecents: seq[GifDto] = @[gifDto]
@ -145,4 +122,32 @@ proc addToRecents*(self: Service, gifDto: GifDto) =
idx += 1 idx += 1
self.recents = newRecents self.recents = newRecents
self.setRecentGifs(newRecents) 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)

View File

@ -43,6 +43,7 @@ const KEY_WAKU_BLOOM_FILTER_MODE* = "waku-bloom-filter-mode"
const KEY_AUTO_MESSAGE_ENABLED* = "auto-message-enabled?" const KEY_AUTO_MESSAGE_ENABLED* = "auto-message-enabled?"
const KEY_GIF_FAVORITES* = "gifs/favorite-gifs" const KEY_GIF_FAVORITES* = "gifs/favorite-gifs"
const KEY_GIF_RECENTS* = "gifs/recent-gifs" const KEY_GIF_RECENTS* = "gifs/recent-gifs"
const KEY_GIF_API_KEY* = "gifs/api-key"
type UpstreamConfig* = object type UpstreamConfig* = object
Enabled*: bool Enabled*: bool
@ -264,4 +265,4 @@ proc pinnedMailserverToJsonNode*(mailserver: PinnedMailserver): JsonNode =
"wakuv2.prod": mailserver.wakuv2Prod, "wakuv2.prod": mailserver.wakuv2Prod,
"wakuv2.test": mailserver.wakuv2Test, "wakuv2.test": mailserver.wakuv2Test,
"go-waku.test": mailserver.goWakuTest "go-waku.test": mailserver.goWakuTest
} }

View File

@ -450,20 +450,4 @@ method autoMessageEnabled*(self: Service): bool =
method getWakuBloomFilterMode*(self: Service): bool = method getWakuBloomFilterMode*(self: Service): bool =
return self.settings.wakuBloomFilterMode 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

View File

@ -259,15 +259,3 @@ method autoMessageEnabled*(self: ServiceInterface): bool {.base.} =
method getWakuBloomFilterMode*(self: ServiceInterface): bool {.base.} = method getWakuBloomFilterMode*(self: ServiceInterface): bool {.base.} =
raise newException(ValueError, "No implementation available") 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")