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 }
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") } } }
func (this *Posts) Get() error { var user models.User if this.getUser(&user) { return nil } limit := 20 nums, _ := models.Count(&models.Post{UserId: user.Id}) pager := this.SetPaginator(limit, nums) var posts = make([]*models.Post, 0) models.Find(limit, pager.Offset(), &posts) this.Data["TheUserPosts"] = posts return this.Render("user/posts.html", this.Data) }
func (this *BulletinAdminDelete) Post() { if this.FormOnceNotMatch() { return } cnt, _ := models.Count(&models.Bulletin{Id: this.object.Id}) if cnt > 0 { // delete object if err := models.DeleteById(this.object.Id, new(models.Bulletin)); err == nil { this.FlashRedirect("/admin/bulletin", 302, "DeleteSuccess") return } else { log.Error(err) this.Data["Error"] = err } } }
// view for delete object func (this *TopicAdminDelete) Post() { if this.FormOnceNotMatch() { return } //check whether there are posts under this topic cnt, _ := models.Count(&models.Post{TopicId: this.object.Id}) if cnt > 0 { this.FlashRedirect("/admin/topic", 302, "DeleteNotAllowed") return } else { // delete object if err := models.DeleteById(this.object.Id, this.object); err == nil { this.FlashRedirect("/admin/topic", 302, "DeleteSuccess") return } else { log.Error(err) this.Data["Error"] = err } } }