// 获得回复最多的10条帖子(TODO:避免一直显示相同的) func FindHotTopics() []map[string]interface{} { topicExList, err := model.NewTopicEx().Order("reply DESC").Limit("0,10").FindAll() if err != nil { logger.Errorln("topic service FindHotReplies error:", err) return nil } tidMap := make(map[int]int, len(topicExList)) topicExMap := make(map[int]*model.TopicEx, len(topicExList)) for _, topicEx := range topicExList { tidMap[topicEx.Tid] = topicEx.Tid topicExMap[topicEx.Tid] = topicEx } tids := util.MapIntKeys(tidMap) topics := FindTopicsByTids(tids) if topics == nil { return nil } uidMap := make(map[int]int, len(topics)) for _, topic := range topics { uidMap[topic.Uid] = topic.Uid } userMap := getUserInfos(uidMap) result := make([]map[string]interface{}, len(topics)) for i, topic := range topics { oneTopic := make(map[string]interface{}) util.Struct2Map(oneTopic, topic) util.Struct2Map(oneTopic, topicExMap[topic.Tid]) oneTopic["user"] = userMap[topic.Uid] result[i] = oneTopic } return result }
// 更新该主题的喜欢数 // objid:被喜欢对象id;num: 喜欢数(负数表示取消喜欢) func (self TopicLike) UpdateLike(objid, num int) { // 更新喜欢数(TODO:暂时每次都更新表) err := model.NewTopicEx().Where("tid=?", objid).Increment("like", num) if err != nil { logger.Errorln("更新主题喜欢数失败:", err) } }
// 获得帖子详细信息(包括详细回复) // 为了避免转换,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 }
// 索引帖子 func IndexingTopic(isAll bool) { solrClient := NewSolrClient() topicObj := model.NewTopic() topicExObj := model.NewTopicEx() limit := strconv.Itoa(MaxRows) if isAll { id := 0 for { topicList, err := topicObj.Where("tid>?", id).Limit(limit).FindAll() if err != nil { logger.Errorln("IndexingTopic error:", err) break } if len(topicList) == 0 { break } tids := util.Models2Intslice(topicList, "Tid") tmpStr := strings.Repeat("?,", len(tids)) query := "tid in(" + tmpStr[:len(tmpStr)-1] + ")" args := make([]interface{}, len(tids)) for i, tid := range tids { args[i] = tid } topicExList, err := topicExObj.Where(query, args...).FindAll() if err != nil { logger.Errorln("IndexingTopic error:", err) break } topicExMap := make(map[int]*model.TopicEx, len(topicExList)) for _, topicEx := range topicExList { topicExMap[topicEx.Tid] = topicEx } for _, topic := range topicList { if id < topic.Tid { id = topic.Tid } topicEx, _ := topicExMap[topic.Tid] document := model.NewDocument(topic, topicEx) addCommand := model.NewDefaultArgsAddCommand(document) solrClient.Push(addCommand) } solrClient.Post() } } }
// 更新该帖子的回复信息 // cid:评论id;objid:被评论对象id;uid:评论者;cmttime:评论时间 func (self TopicComment) UpdateComment(cid, objid, uid int, cmttime string) { tid := strconv.Itoa(objid) // 更新最后回复信息 stringBuilder := util.NewBuffer().Append("lastreplyuid=").AppendInt(uid).Append(",lastreplytime=").Append(cmttime) err := model.NewTopic().Set(stringBuilder.String()).Where("tid=" + tid).Update() if err != nil { logger.Errorln("更新帖子最后回复人信息失败:", err) } // 更新回复数(TODO:暂时每次都更新表) err = model.NewTopicEx().Where("tid="+tid).Increment("reply", 1) if err != nil { logger.Errorln("更新帖子回复数失败:", err) } }
// 获得主题详细信息(包括详细回复) // 为了避免转换,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 { err = errors.New("The topic of tid is not exists") return } topicMap = make(map[string]interface{}) util.Struct2Map(topicMap, topic) // 解析内容中的 @ topicMap["content"] = decodeTopicContent(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"] = GetNodeName(topic.Nid) // 回复信息(评论) replies, owerUser, lastReplyUser := FindObjComments(tid, strconv.Itoa(model.TYPE_TOPIC), topic.Uid, topic.Lastreplyuid) topicMap["user"] = owerUser // 有人回复 if topic.Lastreplyuid != 0 { topicMap["lastreplyusername"] = lastReplyUser.Username } if topic.EditorUid != 0 { topicMap["editor_username"] = FindUsernameByUid(topic.EditorUid) } return }
// flush 将浏览数刷入数据库中 func (this *view) flush() { this.locker.Lock() defer this.locker.Unlock() objid := strconv.Itoa(this.objid) switch this.objtype { case model.TYPE_TOPIC: model.NewTopicEx().Where("tid="+objid).Increment("view", this.num) case model.TYPE_ARTICLE: model.NewArticle().Where("id="+objid).Increment("viewnum", this.num) case model.TYPE_RESOURCE: model.NewResourceEx().Where("id="+objid).Increment("viewnum", this.num) case model.TYPE_PROJECT: model.NewOpenProject().Where("id="+objid).Increment("viewnum", this.num) } this.num = 0 }
// 发布帖子。入topics和topics_ex库 func PublishTopic(topic *model.Topic) (errMsg string, err error) { tid, err := topic.Insert() if err != nil { errMsg = "内部服务器错误" logger.Errorln(errMsg, ":", err) return } // 存扩展信息 topicEx := model.NewTopicEx() topicEx.Tid = tid _, err = topicEx.Insert() if err != nil { errMsg = "内部服务器错误" logger.Errorln(errMsg, ":", err) return } return }
// 发布帖子。入topics和topics_ex库 func PublishTopic(topic *model.Topic) (errMsg string, err error) { tid, err := topic.Insert() if err != nil { errMsg = "内部服务器错误" logger.Errorln(errMsg, ":", err) return } // 存扩展信息 topicEx := model.NewTopicEx() topicEx.Tid = tid _, err = topicEx.Insert() if err != nil { errMsg = "内部服务器错误" logger.Errorln(errMsg, ":", err) return } // 发布帖子,活跃度+10 go IncUserWeight("uid="+strconv.Itoa(topic.Uid), 10) return }
// 获得帖子详细信息(包括详细回复) // 为了避免转换,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) // 回复信息(评论) replies, owerUser, lastReplyUser := FindObjComments(tid, strconv.Itoa(model.TYPE_TOPIC), topic.Uid, topic.Lastreplyuid) topicMap["user"] = owerUser // 有人回复 if topic.Lastreplyuid != 0 { topicMap["lastreplyusername"] = lastReplyUser.Username } return }
// 增加话题浏览数(TODO:刷屏暂时不处理) func IncrTopicView(tid string, uid int) { model.NewTopicEx().Where("tid="+tid).Increment("view", 1) }
func FindTopicsByWhere(where, order, limit string) (topics []map[string]interface{}, total int) { topicObj := model.NewTopic() if where != "" { topicObj.Where(where) } if order != "" { topicObj.Order(order) } if limit != "" { topicObj.Limit(limit) } topicList, err := topicObj.FindAll() if err != nil { logger.Errorln("topic service topicObj.FindAll Error:", err) return } // 获得总帖子数 total, err = topicObj.Count() if err != nil { logger.Errorln("topic service topicObj.Count Error:", err) return } count := len(topicList) tids := make([]int, count) uids := make(map[int]int) nids := make([]int, count) for i, topic := range topicList { tids[i] = topic.Tid uids[topic.Uid] = topic.Uid if topic.Lastreplyuid != 0 { uids[topic.Lastreplyuid] = topic.Lastreplyuid } nids[i] = topic.Nid } // 获取扩展信息(计数) topicExList, err := model.NewTopicEx().Where("tid in(" + util.Join(tids, ",") + ")").FindAll() if err != nil { logger.Errorln("topic service NewTopicEx FindAll Error:", err) return } topicExMap := make(map[int]*model.TopicEx, len(topicExList)) for _, topicEx := range topicExList { topicExMap[topicEx.Tid] = topicEx } userMap := getUserInfos(uids) // 获取节点信息 nodes := model.GetNodesName(nids) topics = make([]map[string]interface{}, count) for i, topic := range topicList { tmpMap := make(map[string]interface{}) util.Struct2Map(tmpMap, topic) util.Struct2Map(tmpMap, topicExMap[topic.Tid]) tmpMap["user"] = userMap[topic.Uid] // 有人回复 if tmpMap["lastreplyuid"].(int) != 0 { tmpMap["lastreplyusername"] = userMap[tmpMap["lastreplyuid"].(int)].Username } tmpMap["node"] = nodes[tmpMap["nid"].(int)] topics[i] = tmpMap } return }
// 发布主题。入topics和topics_ex库 func PublishTopic(user map[string]interface{}, form url.Values) (err error) { uid := user["uid"].(int) topic := model.NewTopic() if form.Get("tid") != "" { err = topic.Where("tid=?", form.Get("tid")).Find() if err != nil { logger.Errorln("Publish Topic find error:", err) return } isAdmin := false if _, ok := user["isadmin"]; ok { isAdmin = user["isadmin"].(bool) } if topic.Uid != uid && !isAdmin { err = NotModifyAuthorityErr return } _, err = ModifyTopic(user, form) if err != nil { logger.Errorln("Publish Topic error:", err) return } } else { util.ConvertAssign(topic, form) topic.Uid = uid topic.Ctime = util.TimeNow() var tid int tid, err = topic.Insert() if err != nil { logger.Errorln("Publish Topic error:", err) return } // 存扩展信息 topicEx := model.NewTopicEx() topicEx.Tid = tid _, err = topicEx.Insert() if err != nil { logger.Errorln("Insert TopicEx error:", err) return } // 给 被@用户 发系统消息 ext := map[string]interface{}{ "objid": tid, "objtype": model.TYPE_TOPIC, "uid": user["uid"], "msgtype": model.MsgtypePublishAtMe, } go SendSysMsgAtUsernames(form.Get("usernames"), ext) // 发布主题,活跃度+10 go IncUserWeight("uid="+strconv.Itoa(uid), 10) } return }