// ToBlog or Not
func (this *NotebookService) ToBlog(userId, notebookId string, isBlog bool) bool {
	// 笔记本
	db.UpdateByIdAndUserIdMap(db.Notebooks, notebookId, userId, bson.M{"IsBlog": isBlog})

	// 更新笔记
	q := bson.M{"UserId": bson.ObjectIdHex(userId),
		"NotebookId": bson.ObjectIdHex(notebookId)}
	data := bson.M{"IsBlog": isBlog}
	if isBlog {
		data["PublicTime"] = time.Now()
	} else {
		data["HasSelfDefined"] = false
	}
	db.UpdateByQMap(db.Notes, q, data)

	// noteContents也更新, 这个就麻烦了, noteContents表没有NotebookId
	// 先查该notebook下所有notes, 得到id
	notes := []info.Note{}
	db.ListByQWithFields(db.Notes, q, []string{"_id"}, &notes)
	if len(notes) > 0 {
		noteIds := make([]bson.ObjectId, len(notes))
		for i, each := range notes {
			noteIds[i] = each.NoteId
		}
		db.UpdateByQMap(db.NoteContents, bson.M{"_id": bson.M{"$in": noteIds}}, bson.M{"IsBlog": isBlog})
	}

	// 重新计算tags
	go (func() {
		blogService.ReCountBlogTags(userId)
	})()

	return true
}
Example #2
0
// 更新notebook
func (this *NotebookService) UpdateNotebook(userId, notebookId string, needUpdate bson.M) bool {
	needUpdate["UpdatedTime"] = time.Now()

	// 如果有IsBlog之类的, 需要特殊处理
	if isBlog, ok := needUpdate["IsBlog"]; ok {
		// 设为blog/取消
		if is, ok2 := isBlog.(bool); ok2 {
			q := bson.M{"UserId": bson.ObjectIdHex(userId),
				"NotebookId": bson.ObjectIdHex(notebookId)}
			db.UpdateByQMap(db.Notes, q, bson.M{"IsBlog": is})

			// noteContents也更新, 这个就麻烦了, noteContents表没有NotebookId
			// 先查该notebook下所有notes, 得到id
			notes := []info.Note{}
			db.ListByQWithFields(db.Notes, q, []string{"_id"}, &notes)
			if len(notes) > 0 {
				noteIds := make([]bson.ObjectId, len(notes))
				for i, each := range notes {
					noteIds[i] = each.NoteId
				}
				db.UpdateByQMap(db.NoteContents, bson.M{"_id": bson.M{"$in": noteIds}}, bson.M{"IsBlog": isBlog})
			}
		}
	}

	return db.UpdateByIdAndUserIdMap(db.Notebooks, notebookId, userId, needUpdate)
}
Example #3
0
// 重新计算博客的标签
// 在设置设置/取消为博客时调用
func (this *BlogService) ReCountBlogTags(userId string) bool {
	// 得到所有博客
	notes := []info.Note{}
	userIdO := bson.ObjectIdHex(userId)
	query := bson.M{"UserId": userIdO, "IsTrash": false, "IsDeleted": false, "IsBlog": true}
	db.ListByQWithFields(db.Notes, query, []string{"Tags"}, &notes)

	db.DeleteAll(db.TagCounts, bson.M{"UserId": userIdO, "IsBlog": true})
	if notes == nil || len(notes) == 0 {
		return true
	}
	// 统计所有的Tags和数目
	tagsCount := map[string]int{}
	for _, note := range notes {
		tags := note.Tags
		if tags != nil && len(tags) > 0 {
			for _, tag := range tags {
				count := tagsCount[tag]
				count++
				tagsCount[tag] = count
			}
		}
	}
	// 一个个插入
	for tag, count := range tagsCount {
		db.Insert(db.TagCounts,
			info.TagCount{UserId: userIdO, IsBlog: true, Tag: tag, Count: count})
	}
	return true
}
Example #4
0
// 复制图片, 把note的图片都copy给我, 且修改noteContent图片路径
func (this *NoteImageService) CopyNoteImages(fromNoteId, fromUserId, newNoteId, content, toUserId string) string {
	// 得到fromNoteId的noteImages, 如果为空, 则直接返回content
	noteImages := []info.NoteImage{}
	db.ListByQWithFields(db.NoteImages, bson.M{"NoteId": bson.ObjectIdHex(fromNoteId)}, []string{"ImageId"}, &noteImages)

	if len(noteImages) == 0 {
		return content
	}

	// <img src="/file/outputImage?fileId=12323232" />
	// 把fileId=1232替换成新的
	replaceMap := map[string]string{}
	for _, noteImage := range noteImages {
		imageId := noteImage.ImageId.Hex()
		ok, newImageId := fileService.CopyImage(fromUserId, imageId, toUserId)
		if ok {
			replaceMap[imageId] = newImageId
		}
	}

	if len(replaceMap) > 0 {
		// 替换之
		reg, _ := regexp.Compile("outputImage\\?fileId=([a-z0-9A-Z]{24})")
		content = reg.ReplaceAllStringFunc(content, func(each string) string {
			// each=outputImage?fileId=541bd2f599c37b4f3r000003
			fileId := each[len(each)-24:] // 得到后24位, 也即id
			if replaceFileId, ok := replaceMap[fileId]; ok {
				return "outputImage?fileId=" + replaceFileId
			}
			return each
		})
	}

	return content
}
Example #5
0
func (this *UpgradeService) setNoteUsn() {
	usnI := 1
	notes := []info.Note{}
	db.ListByQWithFields(db.Notes, bson.M{}, []string{"UserId"}, &notes)

	for _, note := range notes {
		db.UpdateByQField(db.Notes, bson.M{"_id": note.NoteId}, "Usn", usnI)
		usnI++
	}
}
Example #6
0
// 得到我所属的所有分组ids
func (this *GroupService) GetBelongToGroupIds(userId string) []bson.ObjectId {
	// 得到UserIds
	groupUsers := []info.GroupUser{}
	db.ListByQWithFields(db.GroupUsers, bson.M{"UserId": bson.ObjectIdHex(userId)}, []string{"GroupId"}, &groupUsers)
	if len(groupUsers) == 0 {
		return nil
	}
	groupIds := make([]bson.ObjectId, len(groupUsers))
	for i, each := range groupUsers {
		groupIds[i] = each.GroupId
	}
	return groupIds
}
Example #7
0
// 得到某分组下的用户
func (this *GroupService) GetUsers(groupId string) []info.User {
	// 得到UserIds
	groupUsers := []info.GroupUser{}
	db.ListByQWithFields(db.GroupUsers, bson.M{"GroupId": bson.ObjectIdHex(groupId)}, []string{"UserId"}, &groupUsers)
	if len(groupUsers) == 0 {
		return nil
	}
	userIds := make([]bson.ObjectId, len(groupUsers))
	for i, each := range groupUsers {
		userIds[i] = each.UserId
	}
	// 得到userInfos
	return userService.ListUserInfosByUserIds(userIds)
}
Example #8
0
// 通过id, userId得到noteIds
func (this *NoteImageService) GetNoteIds(imageId string) []bson.ObjectId {
	noteImages := []info.NoteImage{}
	db.ListByQWithFields(db.NoteImages, bson.M{"ImageId": bson.ObjectIdHex(imageId)}, []string{"NoteId"}, &noteImages)

	if noteImages != nil && len(noteImages) > 0 {
		noteIds := make([]bson.ObjectId, len(noteImages))
		cnt := len(noteImages)
		for i := 0; i < cnt; i++ {
			noteIds[i] = noteImages[i].NoteId
		}
		return noteIds
	}

	return nil
}
Example #9
0
// get all images names
// for upgrade
func (this *FileService) GetAllImageNamesMap(userId string) (m map[string]bool) {
	q := bson.M{"UserId": bson.ObjectIdHex(userId)}
	files := []info.File{}
	db.ListByQWithFields(db.Files, q, []string{"Name"}, &files)

	m = make(map[string]bool)
	if len(files) == 0 {
		return
	}

	for _, file := range files {
		m[file.Name] = true
	}
	return
}
Example #10
0
// 只得到abstract, 不需要content
func (this *NoteService) ListNoteAbstractsByNoteIds(noteIds []bson.ObjectId) (notes []info.NoteContent) {
	notes = []info.NoteContent{}
	db.ListByQWithFields(db.NoteContents, bson.M{"_id": bson.M{"$in": noteIds}}, []string{"_id", "Abstract"}, &notes)
	return
}