This commit is contained in:
parent
c18209418f
commit
42017035fc
|
@ -227,42 +227,95 @@ type AuthorProfile struct {
|
||||||
|
|
||||||
// GetUserAppDataFromRedis retrieves user profile and follows from Redis, combines them into a JSON format,
|
// 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.
|
// and assigns the JSON string to the provided user's ID.
|
||||||
|
f
|
||||||
func (c *provider) GetUserAppDataFromRedis(userId string) (string, error) {
|
func (c *provider) GetUserAppDataFromRedis(userId string) (string, error) {
|
||||||
authorProfileString, err := c.store.Get(c.ctx, fmt.Sprintf(`user:%s`, userId)).Result()
|
// Получаем ID автора из Redis
|
||||||
|
authorIdString, err := c.store.Get(c.ctx, fmt.Sprintf("user:id:%s", userId)).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse authorProfileString into a map
|
// Преобразуем ID автора из строки в int
|
||||||
|
var authorId int
|
||||||
|
err = json.Unmarshal([]byte(authorIdString), &authorId)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Получаем профиль автора из 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{}
|
var authorProfileMap map[string]interface{}
|
||||||
err = json.Unmarshal([]byte(authorProfileString), &authorProfileMap)
|
err = json.Unmarshal([]byte(authorProfileString), &authorProfileMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
// Combine user data into a JSON string
|
|
||||||
combinedData := "{"
|
|
||||||
|
|
||||||
// Use authorProfileMap["id"] here if necessary
|
// Начинаем сбор данных в общий JSON
|
||||||
authorId := int(authorProfileMap["id"].(float64)) // convert float64 to int
|
combinedData := map[string]interface{}{
|
||||||
combinedData += fmt.Sprintf(`"profile": %s`, authorProfileString)
|
"profile": authorProfileMap,
|
||||||
|
|
||||||
authorFollowsAuthorsString := c.store.Get(c.ctx, fmt.Sprintf(`author:%d:follows-authors`, authorId)).Val()
|
|
||||||
if authorFollowsAuthorsString != "" {
|
|
||||||
combinedData += fmt.Sprintf(`,"authors": %s`, authorFollowsAuthorsString)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
authorFollowsTopicsString := c.store.Get(c.ctx, fmt.Sprintf(`author:%d:follows-topics`, authorId)).Val()
|
// Получаем подписки автора на других авторов
|
||||||
if authorFollowsTopicsString != "" {
|
if authorsObjectsString, err := c.getFollowedObjectsString(fmt.Sprintf("author:follows-authors:%d", authorId), "author:id:%d"); err == nil {
|
||||||
combinedData += fmt.Sprintf(`,"topics": %s`, authorFollowsTopicsString)
|
combinedData["authors"] = authorsObjectsString
|
||||||
}
|
}
|
||||||
|
|
||||||
authorFollowers := c.store.Get(c.ctx, fmt.Sprintf(`author:%d:followers`, authorId)).Val()
|
// Получаем подписки автора на темы
|
||||||
if authorFollowers != "" {
|
if topicsObjectString, err := c.getFollowedObjectsString(fmt.Sprintf("author:follows-topics:%d", authorId), "topic:id:%d"); err == nil {
|
||||||
combinedData += fmt.Sprintf(`,"followers": %s`, authorFollowers)
|
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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user