// 最新评论 // uri: /comments/recent.json func RecentCommentHandler(rw http.ResponseWriter, req *http.Request) { limit := req.FormValue("limit") if limit == "" { limit = "10" } recentComments := service.FindRecentComments(0, -1, limit) uids := util.Models2Intslice(recentComments, "Uid") users := service.GetUserInfos(uids) result := map[string]interface{}{ "comments": recentComments, } // json encode 不支持 map[int]... for uid, user := range users { result[strconv.Itoa(uid)] = user } buf, err := json.Marshal(result) if err != nil { logger.Errorln("[RecentCommentHandler] json.marshal error:", err) fmt.Fprint(rw, `{"ok": 0, "error":"解析json出错"}`) return } fmt.Fprint(rw, `{"ok": 1, "data":`+string(buf)+`}`) }
// 获取某对象的评论信息 // uri: /object/comments.json func ObjectCommentsHandler(rw http.ResponseWriter, req *http.Request) { objid := req.FormValue("objid") objtype := req.FormValue("objtype") commentList, err := service.FindObjectComments(objid, objtype) uids := util.Models2Intslice(commentList, "Uid") users := service.GetUserInfos(uids) result := map[string]interface{}{ "comments": commentList, } // json encode 不支持 map[int]... for uid, user := range users { result[strconv.Itoa(uid)] = user } buf, err := json.Marshal(result) if err != nil { logger.Errorln("[RecentCommentHandler] json.marshal error:", err) fmt.Fprint(rw, `{"ok": 0, "error":"解析json出错"}`) return } fmt.Fprint(rw, `{"ok": 1, "data":`+string(buf)+`}`) }
// 获得回复最多的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 } uids := util.Models2Intslice(topics, "Uid") userMap := GetUserInfos(uids) 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 }
// 获得某个对象的所有评论 // 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 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() } } }
// 索引资源 func IndexingResource(isAll bool) { solrClient := NewSolrClient() resourceObj := model.NewResource() resourceExObj := model.NewResourceEx() limit := strconv.Itoa(MaxRows) if isAll { id := 0 for { resourceList, err := resourceObj.Where("id>?", id).Limit(limit).FindAll() if err != nil { logger.Errorln("IndexingResource error:", err) break } if len(resourceList) == 0 { break } ids := util.Models2Intslice(resourceList, "Id") tmpStr := strings.Repeat("?,", len(ids)) query := "id in(" + tmpStr[:len(tmpStr)-1] + ")" args := make([]interface{}, len(ids)) for i, rid := range ids { args[i] = rid } resourceExList, err := resourceExObj.Where(query, args...).FindAll() if err != nil { logger.Errorln("IndexingResource error:", err) break } resourceExMap := make(map[int]*model.ResourceEx, len(resourceExList)) for _, resourceEx := range resourceExList { resourceExMap[resourceEx.Id] = resourceEx } for _, resource := range resourceList { if id < resource.Id { id = resource.Id } resourceEx, _ := resourceExMap[resource.Id] document := model.NewDocument(resource, resourceEx) addCommand := model.NewDefaultArgsAddCommand(document) solrClient.Push(addCommand) } solrClient.Post() } } }
// 索引主题 func (self SearcherLogic) IndexingTopic(isAll bool) { solrClient := NewSolrClient() var ( topicList []*model.Topic topicExList map[int]*model.TopicEx err error ) if isAll { id := 0 for { topicList = make([]*model.Topic, 0) topicExList = make(map[int]*model.TopicEx) err = MasterDB.Where("tid>?", id).OrderBy("tid ASC").Limit(self.maxRows).Find(&topicList) if err != nil { logger.Errorln("IndexingTopic error:", err) break } if len(topicList) == 0 { break } tids := util.Models2Intslice(topicList, "Tid") err = MasterDB.In("tid", tids).Find(&topicExList) if err != nil { logger.Errorln("IndexingTopic error:", err) break } for _, topic := range topicList { if id < topic.Tid { id = topic.Tid } topicEx := topicExList[topic.Tid] document := model.NewDocument(topic, topicEx) addCommand := model.NewDefaultArgsAddCommand(document) solrClient.PushAdd(addCommand) } solrClient.Post() } } }
// 索引资源 func (self SearcherLogic) IndexingResource(isAll bool) { solrClient := NewSolrClient() var ( resourceList []*model.Resource resourceExList map[int]*model.ResourceEx err error ) if isAll { id := 0 for { resourceList = make([]*model.Resource, 0) resourceExList = make(map[int]*model.ResourceEx) err = MasterDB.Where("id>?", id).OrderBy("id ASC").Limit(self.maxRows).Find(&resourceList) if err != nil { logger.Errorln("IndexingResource error:", err) break } if len(resourceList) == 0 { break } ids := util.Models2Intslice(resourceList, "Id") err = MasterDB.In("id", ids).Find(&resourceExList) if err != nil { logger.Errorln("IndexingResource error:", err) break } for _, resource := range resourceList { if id < resource.Id { id = resource.Id } resourceEx := resourceExList[resource.Id] document := model.NewDocument(resource, resourceEx) addCommand := model.NewDefaultArgsAddCommand(document) solrClient.PushAdd(addCommand) } solrClient.Post() } } }
// 我的(某人的)收藏 // uri: /favorites/{username} func SomeoneFavoritesHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) username := vars["username"] user := service.FindUserByUsername(username) if user == nil { util.Redirect(rw, req, "/") return } objtype, err := strconv.Atoi(req.FormValue("objtype")) if err != nil { objtype = model.TYPE_ARTICLE } p, err := strconv.Atoi(req.FormValue("p")) if err != nil { p = 1 } data := map[string]interface{}{"objtype": objtype, "user": user} rows := 20 favorites, total := service.FindUserFavorites(user.Uid, objtype, (p-1)*rows, rows) if total > 0 { objids := util.Models2Intslice(favorites, "Objid") switch objtype { case model.TYPE_TOPIC: data["topics"] = service.FindTopicsByIds(objids) case model.TYPE_ARTICLE: data["articles"] = service.FindArticlesByIds(objids) case model.TYPE_RESOURCE: data["resources"] = service.FindResourcesByIds(objids) case model.TYPE_WIKI: // data["wikis"] = service.FindArticlesByIds(objids) case model.TYPE_PROJECT: data["projects"] = service.FindProjectsByIds(objids) } } uri := fmt.Sprintf("/favorites/%s?objtype=%d&p=%d", user.Username, objtype, p) data["pageHtml"] = service.GenPageHtml(p, rows, total, uri) // 设置模板数据 filter.SetData(req, data) req.Form.Set(filter.CONTENT_TPL_KEY, "/template/favorite.html") }
// 获得最新资源 func FindRecentResources() []map[string]interface{} { resourceList, err := model.NewResource().Limit("0,10").Order("mtime DESC").FindAll() if err != nil { logger.Errorln("resource service FindRecentResources error:", err) return nil } uids := util.Models2Intslice(resourceList, "Uid") userMap := GetUserInfos(uids) count := len(resourceList) resources := make([]map[string]interface{}, count) for i, resource := range resourceList { tmpMap := make(map[string]interface{}) util.Struct2Map(tmpMap, resource) tmpMap["user"] = userMap[resource.Uid] resources[i] = tmpMap } return resources }