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
|
||||
}
|
||||
|
||||
// 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,41 +318,20 @@ func LoginResolver(ctx context.Context, params model.LoginInput) (*model.AuthRes
|
|||
expiresIn = 1
|
||||
}
|
||||
|
||||
redisURL := os.Getenv(constants.EnvKeyRedisURL)
|
||||
if redisURL != "" {
|
||||
log.Info("Initializing Redis provider")
|
||||
Provider, err := redis.NewRedisProvider(redisURL)
|
||||
if err != nil {
|
||||
log.Debug("Failed to init Redis: ", err)
|
||||
}
|
||||
redisURL := os.Getenv("REDIS_URL")
|
||||
if redisURL != "" {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res = &model.AuthResponse{
|
||||
Message: `Logged in successfully`,
|
||||
AccessToken: &authToken.AccessToken.Token,
|
||||
|
|
|
@ -5,7 +5,6 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/user"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
@ -93,40 +92,18 @@ func SessionResolver(ctx context.Context, params *model.SessionQueryInput) (*mod
|
|||
expiresIn = 1
|
||||
}
|
||||
|
||||
redisURL := os.Getenv(constants.EnvKeyRedisURL)
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
||||
}
|
||||
redisURL := os.Getenv("REDIS_URL")
|
||||
if redisURL != "" {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res = &model.AuthResponse{
|
||||
Message: `Session token refreshed`,
|
||||
|
|
Loading…
Reference in New Issue
Block a user