Ejemplo n.º 1
0
// 根据tag搜索博客
func (this *BlogService) SearchBlogByTags(tags []string, userId string, pageNumber, pageSize int, sortField string, isAsc bool) (pageInfo info.Page, blogs []info.BlogItem) {
	notes := []info.Note{}
	skipNum, sortFieldR := parsePageAndSort(pageNumber, pageSize, sortField, isAsc)

	// 不是trash的
	query := bson.M{"UserId": bson.ObjectIdHex(userId),
		"IsTrash":   false,
		"IsDeleted": false,
		"IsBlog":    true,
		"Tags":      bson.M{"$all": tags}}

	q := db.Notes.Find(query)

	// 总记录数
	count, _ := q.Count()
	if count == 0 {
		return
	}

	q.Sort(sortFieldR).
		Skip(skipNum).
		Limit(pageSize).
		All(&notes)

	blogs = this.notes2BlogItems(notes)
	pageInfo = info.NewPage(pageNumber, pageSize, count, nil)

	return
}
Ejemplo n.º 2
0
func (this *BlogService) SearchBlog(key, userId string, page, pageSize int, sortField string, isAsc bool) (info.Page, []info.BlogItem) {
	count, notes := noteService.SearchNote(key, userId, page, pageSize, sortField, isAsc, true)

	if notes == nil || len(notes) == 0 {
		return info.Page{}, nil
	}

	blogs := this.notes2BlogItems(notes)
	pageInfo := info.NewPage(page, pageSize, count, nil)
	return pageInfo, blogs
}
Ejemplo n.º 3
0
// 评论列表
// userId主要是显示userId是否点过某评论的赞
// 还要获取用户信息
func (this *BlogService) ListComments(userId, noteId string, page, pageSize int) (info.Page, []info.BlogCommentPublic, map[string]info.UserAndBlog) {
	pageInfo := info.Page{CurPage: page}

	comments2 := []info.BlogComment{}

	skipNum, sortFieldR := parsePageAndSort(page, pageSize, "CreatedTime", false)

	query := bson.M{"NoteId": bson.ObjectIdHex(noteId)}
	q := db.BlogComments.Find(query)

	// 总记录数
	count, _ := q.Count()
	q.Sort(sortFieldR).Skip(skipNum).Limit(pageSize).All(&comments2)

	if len(comments2) == 0 {
		return pageInfo, nil, nil
	}

	comments := make([]info.BlogCommentPublic, len(comments2))
	// 我是否点过赞呢?
	for i, comment := range comments2 {
		comments[i].BlogComment = comment
		if comment.LikeNum > 0 && comment.LikeUserIds != nil && len(comment.LikeUserIds) > 0 && InArray(comment.LikeUserIds, userId) {
			comments[i].IsILikeIt = true
		}
	}

	note := noteService.GetNoteById(noteId)

	// 得到用户信息
	userIdsMap := map[bson.ObjectId]bool{note.UserId: true}
	for _, comment := range comments {
		userIdsMap[comment.UserId] = true
		if comment.ToUserId != "" { // 可能为空
			userIdsMap[comment.ToUserId] = true
		}
	}
	userIds := make([]bson.ObjectId, len(userIdsMap))
	i := 0
	for userId, _ := range userIdsMap {
		userIds[i] = userId
		i++
	}

	// 得到用户信息
	userMap := userService.MapUserAndBlogByUserIds(userIds)
	pageInfo = info.NewPage(page, pageSize, count, nil)

	return pageInfo, comments, userMap
}
Ejemplo n.º 4
0
func (this *EmailService) ListEmailLogs(pageNumber, pageSize int, sortField string, isAsc bool, email string) (page info.Page, emailLogs []info.EmailLog) {
	emailLogs = []info.EmailLog{}
	skipNum, sortFieldR := parsePageAndSort(pageNumber, pageSize, sortField, isAsc)
	query := bson.M{}
	if email != "" {
		query["Email"] = bson.M{"$regex": bson.RegEx{".*?" + email + ".*", "i"}}
	}
	q := db.EmailLogs.Find(query)
	// 总记录数
	count, _ := q.Count()
	// 列表
	q.Sort(sortFieldR).
		Skip(skipNum).
		Limit(pageSize).
		All(&emailLogs)
	page = info.NewPage(pageNumber, pageSize, count, nil)
	return
}
Ejemplo n.º 5
0
// 博客列表
// userId 表示谁的blog
func (this *BlogService) ListBlogs(userId, notebookId string, page, pageSize int, sortField string, isAsc bool) (info.Page, []info.BlogItem) {
	count, notes := noteService.ListNotes(userId, notebookId, false, page, pageSize, sortField, isAsc, true)

	if notes == nil || len(notes) == 0 {
		return info.Page{}, nil
	}

	// 得到content, 并且每个都要substring
	noteIds := make([]bson.ObjectId, len(notes))
	for i, note := range notes {
		noteIds[i] = note.NoteId
	}

	// 直接得到noteContents表的abstract
	// 这里可能是乱序的
	noteContents := noteService.ListNoteAbstractsByNoteIds(noteIds) // 返回[info.NoteContent]
	noteContentsMap := make(map[bson.ObjectId]info.NoteContent, len(noteContents))
	for _, noteContent := range noteContents {
		noteContentsMap[noteContent.NoteId] = noteContent
	}

	// 组装成blogItem
	// 按照notes的顺序
	blogs := make([]info.BlogItem, len(noteIds))
	for i, note := range notes {
		hasMore := true
		var content string
		var abstract string
		if noteContent, ok := noteContentsMap[note.NoteId]; ok {
			abstract = noteContent.Abstract
			content = noteContent.Content
		}
		blogs[i] = info.BlogItem{note, abstract, content, hasMore, info.User{}}
	}

	pageInfo := info.NewPage(page, pageSize, count, nil)

	return pageInfo, blogs
}
Ejemplo n.º 6
0
//-------------
// user admin
func (this *UserService) ListUsers(pageNumber, pageSize int, sortField string, isAsc bool, email string) (page info.Page, users []info.User) {
	users = []info.User{}
	skipNum, sortFieldR := parsePageAndSort(pageNumber, pageSize, sortField, isAsc)
	query := bson.M{}
	if email != "" {
		orQ := []bson.M{
			bson.M{"Email": bson.M{"$regex": bson.RegEx{".*?" + email + ".*", "i"}}},
			bson.M{"Username": bson.M{"$regex": bson.RegEx{".*?" + email + ".*", "i"}}},
		}
		query["$or"] = orQ
	}
	q := db.Users.Find(query)
	// 总记录数
	count, _ := q.Count()
	// 列表
	q.Sort(sortFieldR).
		Skip(skipNum).
		Limit(pageSize).
		All(&users)
	page = info.NewPage(pageNumber, pageSize, count, nil)
	return
}
Ejemplo n.º 7
0
//-------
// p
// 平台 lea+
// 博客列表
func (this *BlogService) ListAllBlogs(userId, tag string, keywords string, isRecommend bool, page, pageSize int, sorterField string, isAsc bool) (info.Page, []info.BlogItem) {
	pageInfo := info.Page{CurPage: page}
	notes := []info.Note{}

	skipNum, sortFieldR := parsePageAndSort(page, pageSize, sorterField, isAsc)

	// 不是trash的
	query := bson.M{"IsTrash": false, "IsDeleted": false, "IsBlog": true, "Title": bson.M{"$ne": "欢迎来到leanote!"}}
	if tag != "" {
		query["Tags"] = bson.M{"$in": []string{tag}}
	}
	if userId != "" {
		query["UserId"] = bson.ObjectIdHex(userId)
	}
	// 不是demo的博客
	demoUserId := configService.GetGlobalStringConfig("demoUserId")
	if userId == "" && demoUserId != "" {
		query["UserId"] = bson.M{"$ne": bson.ObjectIdHex(demoUserId)}
	}

	if isRecommend {
		query["IsRecommend"] = isRecommend
	}
	if keywords != "" {
		query["Title"] = bson.M{"$regex": bson.RegEx{".*?" + keywords + ".*", "i"}}
	}
	q := db.Notes.Find(query)

	// 总记录数
	count, _ := q.Count()

	q.Sort(sortFieldR).
		Skip(skipNum).
		Limit(pageSize).
		All(&notes)

	if notes == nil || len(notes) == 0 {
		return pageInfo, nil
	}

	// 得到content, 并且每个都要substring
	noteIds := make([]bson.ObjectId, len(notes))
	userIds := make([]bson.ObjectId, len(notes))
	for i, note := range notes {
		noteIds[i] = note.NoteId
		userIds[i] = note.UserId
	}

	// 可以不要的
	// 直接得到noteContents表的abstract
	// 这里可能是乱序的
	/*
		noteContents := noteService.ListNoteAbstractsByNoteIds(noteIds) // 返回[info.NoteContent]
		noteContentsMap := make(map[bson.ObjectId]info.NoteContent, len(noteContents))
		for _, noteContent := range noteContents {
			noteContentsMap[noteContent.NoteId] = noteContent
		}
	*/

	// 得到用户信息
	userMap := userService.MapUserInfoAndBlogInfosByUserIds(userIds)

	// 组装成blogItem
	// 按照notes的顺序
	blogs := make([]info.BlogItem, len(noteIds))
	for i, note := range notes {
		hasMore := true
		var content string
		/*
			if noteContent, ok := noteContentsMap[note.NoteId]; ok {
				content = noteContent.Abstract
			}
		*/
		if len(note.Tags) == 1 && note.Tags[0] == "" {
			note.Tags = nil
		}
		blogs[i] = info.BlogItem{note, "", content, hasMore, userMap[note.UserId]}
	}
	pageInfo = info.NewPage(page, pageSize, count, nil)

	return pageInfo, blogs
}