Beispiel #1
0
func (this *UserRouter) getFollows(user *models.User, following bool) []map[string]interface{} {
	var follow models.Follow
	if following {
		follow.UserId = user.Id
	} else {
		follow.FollowUserId = user.Id
	}

	nums, _ := models.Count(&follow)

	limit := 20
	pager := this.SetPaginator(limit, nums)

	var follows []*models.Follow
	models.ORM().Limit(limit, pager.Offset()).Find(&follows, &follow)

	if len(follows) == 0 {
		return nil
	}

	ids := make([]int, 0, len(follows))
	for _, follow := range follows {
		if following {
			ids = append(ids, int(follow.FollowUserId))
		} else {
			ids = append(ids, int(follow.UserId))
		}
	}

	var fids = make(map[int]bool)
	models.ORM().In("follow_user_id", ids).Iterate(&models.Follow{UserId: this.User.Id},
		func(idx int, bean interface{}) error {
			tid, _ := utils.StrTo(utils.ToStr(bean.(*models.Follow).Id)).Int()
			if tid > 0 {
				fids[tid] = true
			}
			return nil
		})

	users := make([]map[string]interface{}, 0, len(follows))
	for _, follow := range follows {
		IsFollowed := false
		var u *models.User
		if following {
			u = follow.FollowUser()
		} else {
			u = follow.User()
		}
		if fids != nil {
			IsFollowed = fids[int(u.Id)]
		}
		users = append(users, map[string]interface{}{
			"User":       u,
			"IsFollowed": IsFollowed,
		})
	}

	return users
}