Exemple #1
0
// FindObjComments 获得某个对象的所有评论
// owner: 被评论对象属主
// TODO:分页暂不做
func (self CommentLogic) FindObjComments(ctx context.Context, objid, objtype int, owner, lastCommentUid int /*, page, pageNum int*/) (comments []map[string]interface{}, ownerUser, lastReplyUser *model.User) {
	objLog := GetLogger(ctx)

	commentList := make([]*model.Comment, 0)
	err := MasterDB.Where("objid=? AND objtype=?", objid, objtype).Find(&commentList)
	if err != nil {
		objLog.Errorln("CommentLogic FindObjComments Error:", err)
		return
	}

	uids := slices.StructsIntSlice(commentList, "Uid")

	// 避免某些情况下最后回复人没在回复列表中
	uids = append(uids, owner, lastCommentUid)

	// 获得用户信息
	userMap := DefaultUser.FindUserInfos(ctx, uids)
	ownerUser = userMap[owner]
	if lastCommentUid != 0 {
		lastReplyUser = userMap[lastCommentUid]
	}
	comments = make([]map[string]interface{}, 0, len(commentList))
	for _, comment := range commentList {
		tmpMap := structs.Map(comment)
		tmpMap["content"] = template.HTML(self.decodeCmtContent(ctx, comment))
		tmpMap["user"] = userMap[comment.Uid]
		comments = append(comments, tmpMap)
	}
	return
}
Exemple #2
0
// RecentComment 最新评论
func (SidebarController) RecentComment(ctx echo.Context) error {
	limit := goutils.MustInt(ctx.QueryParam("limit"), 10)
	recentComments := logic.DefaultComment.FindRecent(ctx, 0, -1, limit)

	uids := slices.StructsIntSlice(recentComments, "Uid")
	users := logic.DefaultUser.FindUserInfos(ctx, uids)

	result := map[string]interface{}{
		"comments": recentComments,
	}

	// json encode 不支持 map[int]...
	for uid, user := range users {
		result[strconv.Itoa(uid)] = user
	}

	return success(ctx, result)
}
Exemple #3
0
// ReadList 我的(某人的)收藏
func (FavoriteController) ReadList(ctx echo.Context) error {
	username := ctx.Param("username")
	user := logic.DefaultUser.FindOne(ctx, "username", username)
	if user == nil || user.Uid == 0 {
		return ctx.Redirect(http.StatusSeeOther, "/")
	}

	objtype := goutils.MustInt(ctx.QueryParam("objtype"), model.TypeArticle)
	p := goutils.MustInt(ctx.QueryParam("p"), 1)

	data := map[string]interface{}{"objtype": objtype, "user": user}

	rows := goutils.MustInt(ctx.QueryParam("rows"), 20)
	if rows > 20 {
		rows = 20
	}
	favorites, total := logic.DefaultFavorite.FindUserFavorites(ctx, user.Uid, objtype, (p-1)*rows, rows)
	if total > 0 {
		objids := slices.StructsIntSlice(favorites, "Objid")

		switch objtype {
		case model.TypeTopic:
			data["topics"] = logic.DefaultTopic.FindByTids(objids)
		case model.TypeArticle:
			data["articles"] = logic.DefaultArticle.FindByIds(objids)
		case model.TypeResource:
			data["resources"] = logic.DefaultResource.FindByIds(objids)
		case model.TypeWiki:
			// data["wikis"] = logic.DefaultWiki.FindWikisByIds(objids)
		case model.TypeProject:
			data["projects"] = logic.DefaultProject.FindByIds(objids)
		}
	}

	uri := fmt.Sprintf("/favorites/%s?objtype=%d&rows=%d&", user.Username, objtype, rows)
	paginator := logic.NewPaginatorWithPerPage(p, rows)
	data["pageHtml"] = paginator.SetTotal(total).GetPageHtml(uri)

	return render(ctx, "favorite.html", data)
}
Exemple #4
0
// CommentList 获取某对象的评论信息
func (CommentController) CommentList(ctx echo.Context) error {
	objid := goutils.MustInt(ctx.QueryParam("objid"))
	objtype := goutils.MustInt(ctx.QueryParam("objtype"))

	commentList, err := logic.DefaultComment.FindObjectComments(ctx, objid, objtype)
	if err != nil {
		return fail(ctx, 1, "服务器内部错误")
	}

	uids := slices.StructsIntSlice(commentList, "Uid")
	users := logic.DefaultUser.FindUserInfos(ctx, uids)

	result := map[string]interface{}{
		"comments": commentList,
	}

	// json encode 不支持 map[int]...
	for uid, user := range users {
		result[strconv.Itoa(uid)] = user
	}

	return success(ctx, result)
}