Beispiel #1
0
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")
		}
	}
}