deploy
All checks were successful
deploy / deploy (push) Successful in 1m44s

This commit is contained in:
2024-06-06 09:46:38 +03:00
3 changed files with 86 additions and 24 deletions

View File

@@ -8,6 +8,8 @@ import (
"github.com/authorizerdev/authorizer/server/constants"
log "github.com/sirupsen/logrus"
"github.com/redis/go-redis/v9"
)
var (
@@ -228,41 +230,98 @@ type AuthorProfile struct {
// 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) {
authorProfileString, err := c.store.Get(c.ctx, fmt.Sprintf(`author:user:%s`, userId)).Result()
// Получаем ID автора из Redis
rkey := fmt.Sprintf("author:user:%s", userId)
fmt.Println("get redis cached by key:", rkey)
authorIdString, err := c.store.Get(c.ctx, rkey).Result()
fmt.Println("redis found string value:", authorIdString)
if err != nil {
return "", err
}
// Parse authorProfileString into a map
// Преобразуем ID автора из строки в int
var authorIdFloat float64
err = json.Unmarshal([]byte(authorIdString), &authorIdFloat)
if err != nil {
return "", err
}
authorId := int(authorIdFloat)
fmt.Println("recognized author id: ", authorId)
// Получаем профиль автора из Redis
authorProfileString, err := c.store.Get(c.ctx, fmt.Sprintf("author:id:%d", authorId)).Result()
if err != nil {
return "", err
}
// Парсим профиль пользователя в map
var authorProfileMap map[string]interface{}
err = json.Unmarshal([]byte(authorProfileString), &authorProfileMap)
if err != nil {
return "", err
}
// Combine user data into a JSON string
combinedData := "{"
// Use authorProfileMap["id"] here if necessary
authorId := int(authorProfileMap["id"].(float64)) // convert float64 to int
combinedData += fmt.Sprintf(`"profile": %s`, authorProfileString)
authorFollowsAuthorsString := c.store.Get(c.ctx, fmt.Sprintf(`author:%d`, authorId)).Val()
if authorFollowsAuthorsString != "" {
combinedData += fmt.Sprintf(`,"authors": %s`, authorFollowsAuthorsString)
// Начинаем сбор данных в общий JSON
combinedData := map[string]interface{}{
"profile": authorProfileMap,
}
authorFollowsTopicsString := c.store.Get(c.ctx, fmt.Sprintf(`author:follows-authors:%d:follows-topics`, authorId)).Val()
if authorFollowsTopicsString != "" {
combinedData += fmt.Sprintf(`,"topics": %s`, authorFollowsTopicsString)
// Получаем подписки автора на других авторов
if authorsObjectsString, err := c.getFollowedObjectsString(fmt.Sprintf("author:follows-authors:%d", authorId), "author:id:%d"); err == nil {
combinedData["authors"] = authorsObjectsString
}
authorFollowers := c.store.Get(c.ctx, fmt.Sprintf(`author:followers:%d`, authorId)).Val()
if authorFollowers != "" {
combinedData += fmt.Sprintf(`,"followers": %s`, authorFollowers)
// Получаем подписки автора на темы
if topicsObjectString, err := c.getFollowedObjectsString(fmt.Sprintf("author:follows-topics:%d", authorId), "topic:id:%d"); err == nil {
combinedData["topics"] = topicsObjectString
}
combinedData += "}"
// log.Printf("%v", combinedData)
// Получаем подписчиков автора
if authorFollowersObjectsString, err := c.getFollowedObjectsString(fmt.Sprintf("author:followers:%d", authorId), "author:id:%d"); err == nil {
combinedData["followers"] = authorFollowersObjectsString
}
return combinedData, nil
// Преобразуем собранные данные в JSON строку
combinedDataString, err := json.Marshal(combinedData)
if err != nil {
return "", err
}
return string(combinedDataString), nil
}
// Универсальная функция для получения объектов по списку ID из Redis
func (c *provider) getFollowedObjectsString(followKey string, objectKeyPattern string) ([]map[string]interface{}, error) {
followsString, err := c.store.Get(c.ctx, followKey).Result()
if err != nil {
if err == redis.Nil {
return []map[string]interface{}{}, nil
}
return nil, err
}
var ids []int
err = json.Unmarshal([]byte(followsString), &ids)
if err != nil {
return nil, err
}
objects := make([]map[string]interface{}, 0, len(ids))
for _, id := range ids {
objectString, err := c.store.Get(c.ctx, fmt.Sprintf(objectKeyPattern, id)).Result()
if err == redis.Nil {
continue
} else if err != nil {
return nil, err
}
var object map[string]interface{}
err = json.Unmarshal([]byte(objectString), &object)
if err != nil {
return nil, err
}
objects = append(objects, object)
}
return objects, nil
}