This commit is contained in:
parent
94917e7735
commit
8fca4cf4c0
|
@ -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
|
|
||||||
}
|
|
|
@ -218,3 +218,26 @@ func (c *provider) GetBoolStoreEnvVariable(key string) (bool, error) {
|
||||||
|
|
||||||
return data == "1", nil
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -318,39 +318,18 @@ func LoginResolver(ctx context.Context, params model.LoginInput) (*model.AuthRes
|
||||||
expiresIn = 1
|
expiresIn = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
redisURL := os.Getenv(constants.EnvKeyRedisURL)
|
redisURL := os.Getenv("REDIS_URL")
|
||||||
if redisURL != "" {
|
if redisURL != "" {
|
||||||
log.Info("Initializing Redis provider")
|
log.Info("Initializing Redis memory store")
|
||||||
Provider, err := redis.NewRedisProvider(redisURL)
|
RedisProvider, err := redis.NewRedisProvider(redisURL)
|
||||||
if err != nil {
|
if err == nil {
|
||||||
log.Debug("Failed to init Redis: ", err)
|
appData, err := RedisProvider.GetUserAppDataFromRedis(user.ID)
|
||||||
}
|
if err == nil {
|
||||||
|
// Assign the combined data to the provided pointer
|
||||||
if user.AppData == nil {
|
user.AppData = &appData
|
||||||
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")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res = &model.AuthResponse{
|
res = &model.AuthResponse{
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
@ -93,37 +92,15 @@ func SessionResolver(ctx context.Context, params *model.SessionQueryInput) (*mod
|
||||||
expiresIn = 1
|
expiresIn = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
redisURL := os.Getenv(constants.EnvKeyRedisURL)
|
redisURL := os.Getenv("REDIS_URL")
|
||||||
if redisURL != "" {
|
if redisURL != "" {
|
||||||
log.Info("Initializing Redis provider")
|
log.Info("Initializing Redis memory store")
|
||||||
Provider, err := redis.NewRedisProvider(redisURL)
|
RedisProvider, err := redis.NewRedisProvider(redisURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debug("Failed to init Redis: ", err)
|
appData, err := RedisProvider.GetUserAppDataFromRedis(user.ID)
|
||||||
}
|
if err == nil {
|
||||||
|
// Assign the combined data to the provided pointer
|
||||||
if user.AppData == nil {
|
user.AppData = &appData
|
||||||
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")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user