// 发表评论。入topics_reply库,更新topics和topics_ex库 // objname 注册的评论对象名 func PostComment(objid, objtype, uid int, content string, objname string) error { comment := model.NewComment() comment.Objid = objid comment.Objtype = objtype comment.Uid = uid comment.Content = content // TODO:评论楼层怎么处理,避免冲突?最后的楼层信息保存在内存中? // 暂时只是从数据库中取出最后的评论楼层 stringBuilder := util.NewBuffer() stringBuilder.Append("objid=").AppendInt(objid).Append(" AND objtype=").AppendInt(objtype) tmpCmt, err := model.NewComment().Where(stringBuilder.String()).Order("ctime DESC").Find() if err != nil { logger.Errorln("post comment service error:", err) return err } else { comment.Floor = tmpCmt.Floor + 1 } // 入评论库 cid, err := comment.Insert() if err != nil { logger.Errorln("post comment service error:", err) return err } // 回调,不关心处理结果(有些对象可能不需要回调) if commenter, ok := commenters[objname]; ok { logger.Debugf("评论[objid:%d] [objtype:%d] [uid:%d] 成功,通知被评论者更新", objid, objtype, uid) go commenter.UpdateComment(cid, objid, uid, time.Now().Format("2006-01-02 15:04:05")) } return nil }
// 发表评论(或回复)。 // objid 注册的评论对象 // uid 评论人 func PostComment(uid, objid int, form url.Values) (*model.Comment, error) { comment := model.NewComment() comment.Objid = objid objtype := util.MustInt(form.Get("objtype")) comment.Objtype = objtype comment.Uid = uid comment.Content = form.Get("content") // TODO:评论楼层怎么处理,避免冲突?最后的楼层信息保存在内存中? // 暂时只是从数据库中取出最后的评论楼层 stringBuilder := util.NewBuffer() stringBuilder.Append("objid=").AppendInt(objid).Append(" AND objtype=").AppendInt(objtype) tmpCmt, err := model.NewComment().Where(stringBuilder.String()).Order("ctime DESC").Find() if err != nil { logger.Errorln("post comment service error:", err) return nil, err } else { comment.Floor = tmpCmt.Floor + 1 } // 入评论库 cid, err := comment.Insert() if err != nil { logger.Errorln("post comment service error:", err) return nil, err } comment.Cid = cid comment.Ctime = util.TimeNow() decodeCmtContent(comment) // 回调,不关心处理结果(有些对象可能不需要回调) if commenter, ok := commenters[objtype]; ok { logger.Debugf("评论[objid:%d] [objtype:%d] [uid:%d] 成功,通知被评论者更新", objid, objtype, uid) go commenter.UpdateComment(cid, objid, uid, time.Now().Format("2006-01-02 15:04:05")) } // 发评论,活跃度+5 go IncUserWeight("uid="+strconv.Itoa(uid), 5) // 给被评论对象所有者发系统消息 ext := map[string]interface{}{ "objid": objid, "objtype": objtype, "cid": cid, "uid": uid, } go SendSystemMsgTo(0, objtype, ext) // @某人 发系统消息 go SendSysMsgAtUids(form.Get("uid"), ext) go SendSysMsgAtUsernames(form.Get("usernames"), ext) return comment, nil }
// 获得帖子详细信息(包括详细回复) // 为了避免转换,tid传string类型 func FindTopicByTid(tid string) (topicMap map[string]interface{}, replies []map[string]interface{}, err error) { condition := "tid=" + tid // 帖子信息 topic := model.NewTopic() err = topic.Where(condition).Find() if err != nil { logger.Errorln("topic service FindTopicByTid Error:", err) return } // 帖子不存在 if topic.Tid == 0 { return } topicMap = make(map[string]interface{}) util.Struct2Map(topicMap, topic) topicEx := model.NewTopicEx() err = topicEx.Where(condition).Find() if err != nil { logger.Errorln("topic service FindTopicByTid Error:", err) return } if topicEx.Tid == 0 { return } util.Struct2Map(topicMap, topicEx) // 节点名字 topicMap["node"] = model.GetNodeName(topic.Nid) // 回复信息(评论) replyList, err := model.NewComment().Where("objid=" + tid + " and objtype=" + strconv.Itoa(model.TYPE_TOPIC)).FindAll() if err != nil { logger.Errorln("topic service FindTopicByTid Error:", err) return } replyNum := len(replyList) uids := make(map[int]int, replyNum+1) uids[topic.Uid] = topic.Uid for _, reply := range replyList { uids[reply.Uid] = reply.Uid } // 获得用户信息 userMap := getUserInfos(uids) topicMap["user"] = userMap[topic.Uid] // 有人回复 if topic.Lastreplyuid != 0 { topicMap["lastreplyusername"] = userMap[topicMap["lastreplyuid"].(int)].Username } replies = make([]map[string]interface{}, 0, replyNum) for _, reply := range replyList { tmpMap := make(map[string]interface{}) util.Struct2Map(tmpMap, reply) tmpMap["user"] = userMap[reply.Uid] replies = append(replies, tmpMap) } return }
// 获得某个对象的所有评论 // owner: 被评论对象属主 // TODO:分页暂不做 func FindObjComments(objid, objtype string, owner, lastCommentUid int /*, page, pageNum int*/) (comments []map[string]interface{}, ownerUser, lastReplyUser *model.User) { commentList, err := model.NewComment().Where("objid=" + objid + " and objtype=" + objtype).FindAll() if err != nil { logger.Errorln("comment service FindObjComments Error:", err) return } commentNum := len(commentList) uids := make(map[int]int, commentNum+1) uids[owner] = owner // 避免某些情况下最后回复人没在回复列表中 uids[lastCommentUid] = lastCommentUid for _, comment := range commentList { uids[comment.Uid] = comment.Uid } // 获得用户信息 userMap := getUserInfos(uids) ownerUser = userMap[owner] if lastCommentUid != 0 { lastReplyUser = userMap[lastCommentUid] } comments = make([]map[string]interface{}, 0, commentNum) for _, comment := range commentList { tmpMap := make(map[string]interface{}) util.Struct2Map(tmpMap, comment) tmpMap["content"] = decodeCmtContent(comment) tmpMap["user"] = userMap[comment.Uid] comments = append(comments, tmpMap) } return }
// 获得某个对象的所有评论 // owner: 被评论对象属主 // TODO:分页暂不做 func FindObjComments(objid, objtype string, owner, lastCommentUid int /*, page, pageNum int*/) (comments []map[string]interface{}, ownerUser, lastReplyUser *model.User) { commentList, err := model.NewComment().Where("objid=" + objid + " and objtype=" + objtype).FindAll() if err != nil { logger.Errorln("comment service FindObjComments Error:", err) return } uids := util.Models2Intslice(commentList, "Uid") // 避免某些情况下最后回复人没在回复列表中 uids = append(uids, owner, lastCommentUid) // 获得用户信息 userMap := GetUserInfos(uids) ownerUser = userMap[owner] if lastCommentUid != 0 { lastReplyUser = userMap[lastCommentUid] } comments = make([]map[string]interface{}, 0, len(commentList)) for _, comment := range commentList { tmpMap := make(map[string]interface{}) util.Struct2Map(tmpMap, comment) tmpMap["content"] = template.HTML(decodeCmtContent(comment)) tmpMap["user"] = userMap[comment.Uid] comments = append(comments, tmpMap) } return }
// 某类型的评论总数 func CommentsTotal(objtype int) (total int) { total, err := model.NewComment().Where("objtype=" + strconv.Itoa(objtype)).Count() if err != nil { logger.Errorln("comment service CommentsTotal error:", err) return } return }
// 获得某人在某种类型最近的评论 func FindRecentComments(uid, objtype int) []*model.Comment { comments, err := model.NewComment().Where("uid=" + strconv.Itoa(uid) + " AND objtype=" + strconv.Itoa(objtype)).Order("ctime DESC").Limit("0, 5").FindAll() if err != nil { logger.Errorln("comment service FindRecentComments error:", err) return nil } return comments }
func ModifyComment(cid, content string) (errMsg string, err error) { err = model.NewComment().Set("content=?", content).Where("cid=" + cid).Update() if err != nil { logger.Errorf("更新评论内容 【%s】 失败:%s\n", cid, err) errMsg = "对不起,服务器内部错误,请稍后再试!" return } return }
// 获取多个评论信息 func FindCommentsByIds(cids []int) []*model.Comment { if len(cids) == 0 { return nil } inCids := util.Join(cids, ",") comments, err := model.NewComment().Where("cid in(" + inCids + ")").FindAll() if err != nil { logger.Errorln("comment service FindCommentsByIds error:", err) return nil } return comments }
// 获得某个对象的所有评论(新版) // TODO:分页暂不做 func FindObjectComments(objid, objtype string) (commentList []*model.Comment, err error) { commentList, err = model.NewComment().Where("objid=" + objid + " and objtype=" + objtype).FindAll() if err != nil { logger.Errorln("comment service FindObjectComments Error:", err) } for _, comment := range commentList { decodeCmtContent(comment) } return }
// 评论总数(objtype != -1 时,取某一类型的评论总数) func CommentsTotal(objtype int) (total int) { var cond string if objtype != -1 { cond = "objtype=" + strconv.Itoa(objtype) } total, err := model.NewComment().Where(cond).Count() if err != nil { logger.Errorln("comment service CommentsTotal error:", err) return } return }
// 获得最近的评论 // 如果 uid!=0,表示获取某人的评论; // 如果 objtype!=-1,表示获取某类型的评论; func FindRecentComments(uid, objtype int, limit string) []*model.Comment { cond := "" if uid != 0 { cond = "uid=" + strconv.Itoa(uid) } if objtype != -1 { if cond != "" { cond += " AND " } cond += "objtype=" + strconv.Itoa(objtype) } comments, err := model.NewComment().Where(cond).Order("cid DESC").Limit(limit).FindAll() if err != nil { logger.Errorln("comment service FindRecentComments error:", err) return nil } cmtMap := make(map[int][]*model.Comment, len(model.PathUrlMap)) for _, comment := range comments { decodeCmtContent(comment) if _, ok := cmtMap[comment.Objtype]; !ok { cmtMap[comment.Objtype] = make([]*model.Comment, 0, 10) } cmtMap[comment.Objtype] = append(cmtMap[comment.Objtype], comment) } cmtObjs := []CommentObjecter{ model.TYPE_TOPIC: TopicComment{}, model.TYPE_ARTICLE: ArticleComment{}, model.TYPE_RESOURCE: ResourceComment{}, model.TYPE_WIKI: nil, model.TYPE_PROJECT: ProjectComment{}, } for cmtType, cmts := range cmtMap { FillCommentObjs(cmts, cmtObjs[cmtType]) } return comments }