Esempio n. 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")
		}
	}
}
Esempio n. 2
0
func UserUnFollow(user *models.User, theUser *models.User) {
	follow := &models.Follow{UserId: user.Id, FollowUserId: theUser.Id}
	num, _ := models.ORM().Delete(follow)
	if num > 0 {
		models.ORM().UseBool().Update(&models.Follow{}, follow)

		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")
		}
	}
}