2022-01-31 12:58:03 +00:00
|
|
|
package gif
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2024-10-28 20:54:17 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/logutils"
|
2022-01-31 12:58:03 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
2022-03-23 18:47:00 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/settings"
|
2022-01-31 12:58:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2022-02-07 12:55:57 +00:00
|
|
|
return &API{db}
|
2022-01-31 12:58:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// API is class with methods available over RPC.
|
|
|
|
type API struct {
|
2022-02-07 12:55:57 +00:00
|
|
|
db *accounts.Database
|
2022-01-31 12:58:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) SetTenorAPIKey(key string) (err error) {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Info("[GifAPI::SetTenorAPIKey]")
|
2022-03-23 18:47:00 +00:00
|
|
|
err = api.db.SaveSettingField(settings.GifAPIKey, key)
|
2022-01-31 12:58:03 +00:00
|
|
|
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 {
|
2022-02-07 12:55:57 +00:00
|
|
|
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)
|
|
|
|
|
2022-01-31 12:58:03 +00:00
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("can't get content", zap.String("path", path), zap.Error(err))
|
2022-01-31 12:58:03 +00:00
|
|
|
currentRetry++
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 12:55:57 +00:00
|
|
|
if response == nil {
|
|
|
|
return "", fmt.Errorf("Could not reach Tenor API")
|
|
|
|
}
|
2022-01-31 12:58:03 +00:00
|
|
|
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) {
|
|
|
|
return api.GetContentWithRetry(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) UpdateRecentGifs(updatedGifs json.RawMessage) (err error) {
|
|
|
|
recentGifsContainer := Container{}
|
|
|
|
err = json.Unmarshal(updatedGifs, &recentGifsContainer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-23 18:47:00 +00:00
|
|
|
err = api.db.SaveSettingField(settings.GifRecents, recentGifsContainer.Items)
|
2022-01-31 12:58:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) UpdateFavoriteGifs(updatedGifs json.RawMessage) (err error) {
|
|
|
|
favsGifsContainer := Container{}
|
|
|
|
err = json.Unmarshal(updatedGifs, &favsGifsContainer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-23 18:47:00 +00:00
|
|
|
err = api.db.SaveSettingField(settings.GifFavourites, favsGifsContainer.Items)
|
2022-01-31 12:58:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) GetRecentGifs() (recentGifs []Gif, err error) {
|
|
|
|
gifs, err := api.db.GifRecents()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-07 11:29:22 +00:00
|
|
|
recentGifs = make([]Gif, 0)
|
2022-01-31 12:58:03 +00:00
|
|
|
savedRecentGifs := []Gif{}
|
2023-02-07 11:29:22 +00:00
|
|
|
if len(gifs) > 0 {
|
|
|
|
err = json.Unmarshal(gifs, &savedRecentGifs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
recentGifs = savedRecentGifs
|
2022-01-31 12:58:03 +00:00
|
|
|
}
|
|
|
|
return recentGifs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) GetFavoriteGifs() (favoriteGifs []Gif, err error) {
|
|
|
|
gifs, err := api.db.GifFavorites()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-08 10:11:36 +00:00
|
|
|
favoriteGifs = make([]Gif, 0)
|
2022-01-31 12:58:03 +00:00
|
|
|
savedFavGifs := []Gif{}
|
2023-02-08 10:11:36 +00:00
|
|
|
if len(gifs) > 0 {
|
|
|
|
err = json.Unmarshal(gifs, &savedFavGifs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
favoriteGifs = savedFavGifs
|
2022-01-31 12:58:03 +00:00
|
|
|
}
|
|
|
|
return favoriteGifs, nil
|
|
|
|
}
|