status-go/services/gif/gif.go

163 lines
3.6 KiB
Go
Raw Permalink Normal View History

package gif
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/ethereum/go-ethereum/log"
"github.com/status-im/status-go/multiaccounts/accounts"
Sync Settings (#2478) * Sync Settings * Added valueHandlers and Database singleton Some issues remain, need a way to comparing incoming sql.DB to check if the connection is to a different file or not. Maybe make singleton instance per filename * Added functionality to check the sqlite filename * Refactor of Database.SaveSyncSettings to be used as a handler * Implemented inteface for setting sync protobuf factories * Refactored and completed adhoc send setting sync * Tidying up * Immutability refactor * Refactor settings into dedicated package * Breakout structs * Tidy up * Refactor of bulk settings sync * Bug fixes * Addressing feedback * Fix code dropped during rebase * Fix for db closed * Fix for node config related crashes * Provisional fix for type assertion - issue 2 * Adding robust type assertion checks * Partial fix for null literal db storage and json encoding * Fix for passively handling nil sql.DB, and checking if elem has len and if len is 0 * Added test for preferred name behaviour * Adding saved sync settings to MessengerResponse * Completed granular initial sync and clock from network on save * add Settings to isEmpty * Refactor of protobufs, partially done * Added syncSetting receiver handling, some bug fixes * Fix for sticker packs * Implement inactive flag on sync protobuf factory * Refactor of types and structs * Added SettingField.CanSync functionality * Addressing rebase artifact * Refactor of Setting SELECT queries * Refactor of string return queries * VERSION bump and migration index bump * Deactiveate Sync Settings * Deactiveated preferred_name and send_status_updates Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2022-03-23 18:47:00 +00:00
"github.com/status-im/status-go/multiaccounts/settings"
)
type Gif struct {
ID string `json:"id"`
Title string `json:"title"`
URL string `json:"url"`
TinyURL string `json:"tinyUrl"`
Height int `json:"height"`
IsFavorite bool `json:"isFavorite"`
}
type Container struct {
Items []Gif `json:"items"`
}
var tenorAPIKey = ""
var defaultParams = "&media_filter=minimal&limit=50&key="
const maxRetry = 3
const baseURL = "https://g.tenor.com/v1/"
func NewGifAPI(db *accounts.Database) *API {
return &API{db}
}
// API is class with methods available over RPC.
type API struct {
db *accounts.Database
}
func (api *API) SetTenorAPIKey(key string) (err error) {
log.Info("[GifAPI::SetTenorAPIKey]")
Sync Settings (#2478) * Sync Settings * Added valueHandlers and Database singleton Some issues remain, need a way to comparing incoming sql.DB to check if the connection is to a different file or not. Maybe make singleton instance per filename * Added functionality to check the sqlite filename * Refactor of Database.SaveSyncSettings to be used as a handler * Implemented inteface for setting sync protobuf factories * Refactored and completed adhoc send setting sync * Tidying up * Immutability refactor * Refactor settings into dedicated package * Breakout structs * Tidy up * Refactor of bulk settings sync * Bug fixes * Addressing feedback * Fix code dropped during rebase * Fix for db closed * Fix for node config related crashes * Provisional fix for type assertion - issue 2 * Adding robust type assertion checks * Partial fix for null literal db storage and json encoding * Fix for passively handling nil sql.DB, and checking if elem has len and if len is 0 * Added test for preferred name behaviour * Adding saved sync settings to MessengerResponse * Completed granular initial sync and clock from network on save * add Settings to isEmpty * Refactor of protobufs, partially done * Added syncSetting receiver handling, some bug fixes * Fix for sticker packs * Implement inactive flag on sync protobuf factory * Refactor of types and structs * Added SettingField.CanSync functionality * Addressing rebase artifact * Refactor of Setting SELECT queries * Refactor of string return queries * VERSION bump and migration index bump * Deactiveate Sync Settings * Deactiveated preferred_name and send_status_updates Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2022-03-23 18:47:00 +00:00
err = api.db.SaveSettingField(settings.GifAPIKey, key)
if err != nil {
return err
}
tenorAPIKey = key
return nil
}
func (api *API) GetContentWithRetry(path string) (value string, err error) {
var currentRetry = 0
var response *http.Response
for currentRetry < maxRetry {
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
ResponseHeaderTimeout: time.Second * 1,
}
client := http.Client{
Timeout: 1 * time.Second,
Transport: transport,
}
response, err = client.Get(baseURL + path + defaultParams + tenorAPIKey)
if err != nil {
log.Error("can't get content from path %s with %s", path, err.Error())
currentRetry++
time.Sleep(100 * time.Millisecond)
} else {
break
}
}
if response == nil {
return "", fmt.Errorf("Could not reach Tenor API")
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return "", fmt.Errorf("Status error: %v", response.StatusCode)
}
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", fmt.Errorf("Read body: %v", err)
}
return string(data), nil
}
func (api *API) FetchGifs(path string) (value string, err error) {
log.Info("[GifAPI::fetchGifs]")
return api.GetContentWithRetry(path)
}
func (api *API) UpdateRecentGifs(updatedGifs json.RawMessage) (err error) {
log.Info("[GifAPI::updateRecentGifs]")
recentGifsContainer := Container{}
err = json.Unmarshal(updatedGifs, &recentGifsContainer)
if err != nil {
return err
}
Sync Settings (#2478) * Sync Settings * Added valueHandlers and Database singleton Some issues remain, need a way to comparing incoming sql.DB to check if the connection is to a different file or not. Maybe make singleton instance per filename * Added functionality to check the sqlite filename * Refactor of Database.SaveSyncSettings to be used as a handler * Implemented inteface for setting sync protobuf factories * Refactored and completed adhoc send setting sync * Tidying up * Immutability refactor * Refactor settings into dedicated package * Breakout structs * Tidy up * Refactor of bulk settings sync * Bug fixes * Addressing feedback * Fix code dropped during rebase * Fix for db closed * Fix for node config related crashes * Provisional fix for type assertion - issue 2 * Adding robust type assertion checks * Partial fix for null literal db storage and json encoding * Fix for passively handling nil sql.DB, and checking if elem has len and if len is 0 * Added test for preferred name behaviour * Adding saved sync settings to MessengerResponse * Completed granular initial sync and clock from network on save * add Settings to isEmpty * Refactor of protobufs, partially done * Added syncSetting receiver handling, some bug fixes * Fix for sticker packs * Implement inactive flag on sync protobuf factory * Refactor of types and structs * Added SettingField.CanSync functionality * Addressing rebase artifact * Refactor of Setting SELECT queries * Refactor of string return queries * VERSION bump and migration index bump * Deactiveate Sync Settings * Deactiveated preferred_name and send_status_updates Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2022-03-23 18:47:00 +00:00
err = api.db.SaveSettingField(settings.GifRecents, recentGifsContainer.Items)
if err != nil {
return err
}
return nil
}
func (api *API) UpdateFavoriteGifs(updatedGifs json.RawMessage) (err error) {
log.Info("[GifAPI::updateFavoriteGifs]", updatedGifs)
favsGifsContainer := Container{}
err = json.Unmarshal(updatedGifs, &favsGifsContainer)
if err != nil {
return err
}
Sync Settings (#2478) * Sync Settings * Added valueHandlers and Database singleton Some issues remain, need a way to comparing incoming sql.DB to check if the connection is to a different file or not. Maybe make singleton instance per filename * Added functionality to check the sqlite filename * Refactor of Database.SaveSyncSettings to be used as a handler * Implemented inteface for setting sync protobuf factories * Refactored and completed adhoc send setting sync * Tidying up * Immutability refactor * Refactor settings into dedicated package * Breakout structs * Tidy up * Refactor of bulk settings sync * Bug fixes * Addressing feedback * Fix code dropped during rebase * Fix for db closed * Fix for node config related crashes * Provisional fix for type assertion - issue 2 * Adding robust type assertion checks * Partial fix for null literal db storage and json encoding * Fix for passively handling nil sql.DB, and checking if elem has len and if len is 0 * Added test for preferred name behaviour * Adding saved sync settings to MessengerResponse * Completed granular initial sync and clock from network on save * add Settings to isEmpty * Refactor of protobufs, partially done * Added syncSetting receiver handling, some bug fixes * Fix for sticker packs * Implement inactive flag on sync protobuf factory * Refactor of types and structs * Added SettingField.CanSync functionality * Addressing rebase artifact * Refactor of Setting SELECT queries * Refactor of string return queries * VERSION bump and migration index bump * Deactiveate Sync Settings * Deactiveated preferred_name and send_status_updates Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2022-03-23 18:47:00 +00:00
err = api.db.SaveSettingField(settings.GifFavourites, favsGifsContainer.Items)
if err != nil {
return err
}
return nil
}
func (api *API) GetRecentGifs() (recentGifs []Gif, err error) {
log.Info("[GifAPI::getRecentGifs]")
gifs, err := api.db.GifRecents()
if err != nil {
return nil, err
}
recentGifs = make([]Gif, 0)
savedRecentGifs := []Gif{}
if len(gifs) > 0 {
err = json.Unmarshal(gifs, &savedRecentGifs)
if err != nil {
return nil, err
}
recentGifs = savedRecentGifs
}
return recentGifs, nil
}
func (api *API) GetFavoriteGifs() (favoriteGifs []Gif, err error) {
log.Info("[GifAPI::getFavoriteGifs]")
gifs, err := api.db.GifFavorites()
if err != nil {
return nil, err
}
favoriteGifs = make([]Gif, 0)
savedFavGifs := []Gif{}
if len(gifs) > 0 {
err = json.Unmarshal(gifs, &savedFavGifs)
if err != nil {
return nil, err
}
favoriteGifs = savedFavGifs
}
return favoriteGifs, nil
}