github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/routers/NewReply.go (about) 1 package routers 2 3 import ( 4 "fmt" 5 6 "github.com/insionng/makross" 7 "github.com/insionng/makross/cache" 8 9 "strconv" 10 11 "github.com/insionng/yougam/helper" 12 "github.com/insionng/yougam/models" 13 ) 14 15 func PostNewReplyHandler(self *makross.Context) error { 16 17 //TODO:暂时登录才能发评论,以后在后台增加允许设置是否对游客开放评论 18 19 _usr_, okay := self.Session.Get("SignedUser").(*models.User) 20 if !okay { 21 return self.NoContent(401) 22 } 23 cc := cache.Store(self) 24 25 tid := self.Param("tid").MustInt64() 26 pid := self.Param("pid").MustInt64() 27 28 author := self.FormValue("author") 29 nickname := self.FormValue("nickname") 30 email := self.FormValue("email") 31 website := self.FormValue("website") 32 33 rc := self.FormValue("comment") 34 if len(rc) > 0 { 35 policy := helper.ObjPolicy() 36 rc = policy.Sanitize(rc) 37 } 38 39 images := self.FormValue("images") 40 41 if len(_usr_.Avatar) > 0 { 42 self.Set("hasavatar", true) 43 } else { 44 self.Set("hasavatar", false) 45 self.Flash.Warning("请设置你的头像,有头像能更好的引起其他成员的注意~") 46 47 } 48 49 if _usr_.Id > 0 { 50 if (tid > 0) && (len(rc) > 0) { 51 52 allow := bool(false) 53 if _usr_.Balance >= 1 { 54 allow = true 55 } else { 56 self.Flash.Error(fmt.Sprintf("你的余额为[%v],不足以评论!", _usr_.Balance)) 57 } 58 59 if tp, err := models.GetTopic(tid); err == nil && allow { 60 61 if tp.Pid != 0 { 62 self.Flash.Error("子话题不允许评论!") 63 return self.Redirect("/topic/" + self.Param("tid").String() + "/") 64 } 65 66 //为安全计,先行保存回应,顺手获得rid 67 //ctype不等于0,即是注册用户或管理层的回复 此时把ctype设置为1 主要是为了区分游客 68 if rid, err := models.AddReply(tid, pid, _usr_.Id, 1, rc, images, _usr_.Username, _usr_.Avatar, _usr_.AvatarLarge, _usr_.AvatarMedium, _usr_.AvatarSmall, _usr_.Content, _usr_.Nickname, _usr_.Email, _usr_.Website); err != nil { 69 self.Flash.Error(fmt.Sprint("#", rid, ":", err)) 70 } else { 71 //更新统计缓存 这里不需要全局计算 意思下就行 在PrepareHandler中间件会定期统计全局数据 72 if cc.IsExist("ReplysCount") { 73 var ReplysCount int 74 cc.Get("ReplysCount", &ReplysCount) 75 ReplysCount = ReplysCount + 1 76 cc.Set("ReplysCount", ReplysCount, 60*60*2) 77 self.Set("ReplysCount", ReplysCount) 78 } else { 79 ReplysCount := 1 80 cc.Set("ReplysCount", ReplysCount, 60*60*2) 81 self.Set("ReplysCount", ReplysCount) 82 } 83 84 //是否允许写入标记 85 var allow = false 86 switch kind := tp.Ctype; { 87 /* 88 tp.Ctype == 0 [普通话题] 89 tp.Ctype == -1 [回复可见] 90 tp.Ctype == -2 [付费可见] 91 tp.Ctype == -3 [会员可见] 92 */ 93 case kind == 0: 94 allow = false //如果是普通类型则无须写入标记 95 case kind == -1: 96 allow = true //须要回复 并写入标记 97 if _usr_ != nil { //若果是登录状态 98 if _usr_.Id == tp.Uid { //如果当前用户即是作者则无须写入标记 99 allow = false 100 } 101 102 if _usr_.Role == -1000 { //如果是管理层则无须写入标记 103 allow = false 104 } 105 } 106 case kind == -2: 107 allow = false //须要另外付费 并写入标记 108 case kind == -3: 109 allow = false //另外判断即可 无须写入标记 110 } 111 112 if allow { 113 //添加UID标记 114 models.PutTailinfo2Topic(tid, fmt.Sprint(_usr_.Id)) 115 } 116 117 hasSend := false //判断作者是否已经通知 118 if users := helper.AtUsers(rc); len(users) > 0 { 119 for _, v := range users { 120 //不是评论者本人自己@自己则继续执行 121 if _usr_.Username != v { 122 //判断被通知之用户名是否真实存在 123 if u, e := models.GetUserByUsername(v); e == nil && u != nil { 124 125 n_, e_ := models.AddNotification(tp.Id, rid, u.Id, 0, helper.Substr(helper.HTML2str(tp.Title), 0, 100, "..."), helper.Substr(helper.HTML2str(rc), 0, 200, "..."), _usr_.Username, _usr_.Avatar, _usr_.AvatarLarge, _usr_.AvatarMedium, _usr_.AvatarSmall) 126 if (n_ != -1) && (e_ == nil) { 127 _u, _e := models.GetUser(u.Id) 128 if (_u != nil) && (_e == nil) { 129 cc.Set(fmt.Sprintf("SignedUser:%v", u.Id), _u, 60*60*24) 130 } 131 } 132 } 133 if v == tp.Author { 134 hasSend = true 135 } 136 } 137 } 138 } 139 140 if e := models.SetAmountByUid(_usr_.Id, -1, -1, "创建回复,付出1金币"); e == nil { 141 if usr, e := models.GetUser(_usr_.Id); e == nil && usr != nil { 142 cc.Set(fmt.Sprintf("SignedUser:%v", usr.Id), usr, 60*60*24) 143 } 144 } 145 146 if tp.Uid != _usr_.Id { //若果当前评论用户不是主题作者则处理 147 models.SetAmountByUid(tp.Uid, 1, +1, "主题被评论,收益1金币") 148 } else { //若果当前评论用户是主题作者则处理 149 hasSend = true //不对自己发通知 150 } 151 152 if !hasSend { 153 //通知话题作者 154 n_, e_ := models.AddNotification(tp.Id, rid, tp.Uid, 0, helper.Substr(helper.HTML2str(tp.Title), 0, 100, "..."), helper.Substr(helper.HTML2str(rc), 0, 200, "..."), _usr_.Username, _usr_.Avatar, _usr_.AvatarLarge, _usr_.AvatarMedium, _usr_.AvatarSmall) 155 if (n_ != -1) && (e_ == nil) { 156 _u, _e := models.GetUser(tp.Uid) 157 if (_u != nil) && (_e == nil) { 158 cc.Set(fmt.Sprintf("SignedUser:%v", tp.Uid), _u, 60*60*24) 159 } 160 } 161 162 } 163 164 return self.Redirect("/topic/" + self.Param("tid").String() + "/#reply" + strconv.FormatInt(rid, 10)) 165 } 166 } 167 168 return self.Redirect("/topic/" + self.Param("tid").String() + "/") 169 170 } else if tid > 0 { 171 return self.Redirect("/topic/" + self.Param("tid").String() + "/") 172 } else { 173 return self.Redirect("/") 174 } 175 } else { //游客回应 此时把ctype设置为-1 游客不开放@通知功能 176 if len(author) > 0 && len(email) > 0 && tid > 0 && len(rc) > 0 { 177 if rid, err := models.AddReply(tid, pid, _usr_.Id, -1, rc, images, author, "", "", "", "", "", nickname, email, website); err != nil { 178 self.Flash.Error(fmt.Sprint("#", rid, ":", err)) 179 return self.Redirect("/topic/" + self.Param("tid").String() + "/") 180 181 } else { 182 return self.Redirect("/topic/" + self.Param("tid").String() + "/#reply" + strconv.Itoa(int(rid))) 183 184 } 185 } else if tid > 0 { 186 return self.Redirect("/topic/" + self.Param("tid").String() + "/") 187 188 } else { 189 return self.Redirect("/") 190 191 } 192 193 } 194 }