appdata-patch
Some checks failed
deploy / deploy (push) Failing after 5s

This commit is contained in:
Untone 2024-02-22 10:40:39 +03:00
parent 94917e7735
commit 8fca4cf4c0
4 changed files with 47 additions and 95 deletions

View File

@ -1,27 +0,0 @@
package redis
import (
"fmt"
)
// GetUserProfile возвращает профиль пользователя в виде сырого JSON
func (c *provider) GetUserProfile(userId string) (string, error) {
key := fmt.Sprintf("user:%s:profile", userId)
profileJSON, err := c.store.Get(c.ctx, key).Result()
if err != nil {
return "", err
}
return profileJSON, nil
}
// GetUserFollows возвращает список подписок пользователя в виде сырого JSON
func (c *provider) GetUserFollows(userId string) (string, error) {
key := fmt.Sprintf("user:%s:follows", userId)
followsJSON, err := c.store.Get(c.ctx, key).Result()
if err != nil {
return "", err
}
return followsJSON, nil
}

View File

@ -218,3 +218,26 @@ func (c *provider) GetBoolStoreEnvVariable(key string) (bool, error) {
return data == "1", nil
}
// GetUserAppDataFromRedis retrieves user profile and follows from Redis, combines them into a JSON format,
// and assigns the JSON string to the provided user's ID.
func (c *provider) GetUserAppDataFromRedis(userId string) (string, error) {
// Retrieve user data from Redis
key := fmt.Sprintf("user:%s:author", userId)
authorJSON, err := c.store.Get(c.ctx, key).Result()
if err != nil {
return "", err
}
key = fmt.Sprintf("user:%s:follows", userId)
followsJSON, err := c.store.Get(c.ctx, key).Result()
if err != nil {
return "", err
}
// Combine user data into a JSON string
combinedData := fmt.Sprintf(`{"profile": %s, "follows": %s}`, authorJSON, followsJSON)
return combinedData, nil
}

View File

@ -318,39 +318,18 @@ func LoginResolver(ctx context.Context, params model.LoginInput) (*model.AuthRes
expiresIn = 1
}
redisURL := os.Getenv(constants.EnvKeyRedisURL)
redisURL := os.Getenv("REDIS_URL")
if redisURL != "" {
log.Info("Initializing Redis provider")
Provider, err := redis.NewRedisProvider(redisURL)
if err != nil {
log.Debug("Failed to init Redis: ", err)
}
if user.AppData == nil {
user.AppData = make(map[string]interface{})
}
follows, err := Provider.GetUserFollows(user.ID)
if err != nil {
log.Debug("Failed to get follows from Redis: ", err)
} else {
if follows != "" {
user.AppData["follows"] = follows
} else {
log.Debug("Follows data from Redis is empty")
log.Info("Initializing Redis memory store")
RedisProvider, err := redis.NewRedisProvider(redisURL)
if err == nil {
appData, err := RedisProvider.GetUserAppDataFromRedis(user.ID)
if err == nil {
// Assign the combined data to the provided pointer
user.AppData = &appData
}
}
author, err := Provider.GetUserProfile(user.ID)
if err != nil {
log.Debug("Failed to get author from Redis: ", err)
} else {
if author != "" {
user.AppData["author"] = author
} else {
log.Debug("Author data from Redis is empty")
}
}
}
res = &model.AuthResponse{

View File

@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"os"
"os/user"
"time"
"github.com/google/uuid"
@ -93,37 +92,15 @@ func SessionResolver(ctx context.Context, params *model.SessionQueryInput) (*mod
expiresIn = 1
}
redisURL := os.Getenv(constants.EnvKeyRedisURL)
redisURL := os.Getenv("REDIS_URL")
if redisURL != "" {
log.Info("Initializing Redis provider")
Provider, err := redis.NewRedisProvider(redisURL)
log.Info("Initializing Redis memory store")
RedisProvider, err := redis.NewRedisProvider(redisURL)
if err != nil {
log.Debug("Failed to init Redis: ", err)
}
if user.AppData == nil {
user.AppData = make(map[string]interface{})
}
follows, err := Provider.GetUserFollows(user.ID)
if err != nil {
log.Debug("Failed to get follows from Redis: ", err)
} else {
if follows != "" {
user.AppData["follows"] = follows
} else {
log.Debug("Follows data from Redis is empty")
}
}
author, err := Provider.GetUserProfile(user.ID)
if err != nil {
log.Debug("Failed to get author from Redis: ", err)
} else {
if author != "" {
user.AppData["author"] = author
} else {
log.Debug("Author data from Redis is empty")
appData, err := RedisProvider.GetUserAppDataFromRedis(user.ID)
if err == nil {
// Assign the combined data to the provided pointer
user.AppData = &appData
}
}
}