func UserFollow(user *models.User, theUser *models.User) { if err := models.GetById(theUser.Id, theUser); err != nil { var mutual bool tFollow := models.Follow{UserId: theUser.Id, FollowUserId: user.Id} if err := models.GetByExample(&tFollow); err == nil { mutual = true } follow := models.Follow{UserId: user.Id, FollowUserId: theUser.Id, Mutual: mutual} if err := models.Insert(&follow); err == nil && mutual { tFollow.Mutual = mutual models.UpdateById(tFollow.Id, &tFollow, "mutual") } if nums, err := models.Count(&models.Follow{UserId: user.Id}); err == nil { user.Following = int(nums) models.UpdateById(user.Id, user, "following") } if nums, err := models.Count(&models.Follow{FollowUserId: theUser.Id}); err == nil { theUser.Followers = int(nums) models.UpdateById(theUser.Id, theUser, "followers") } } }
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 }