Exemple #1
0
// 修改笔记
// [ok] TODO perm还没测
func (this *NoteService) UpdateNote(userId, updatedUserId, noteId string, needUpdate bson.M) bool {
	// updatedUserId 要修改userId的note, 此时需要判断是否有修改权限
	if userId != updatedUserId {
		if !shareService.HasUpdatePerm(userId, updatedUserId, noteId) {
			Log("NO AUTH")
			return false
		} else {
			Log("HAS AUTH -----------")
		}
	}

	needUpdate["UpdatedUserId"] = bson.ObjectIdHex(updatedUserId)
	needUpdate["UpdatedTime"] = time.Now()

	// 添加tag2
	if tags, ok := needUpdate["Tags"]; ok {
		tagService.AddTagsI(userId, tags)
	}

	// 是否修改了isBlog
	// 也要修改noteContents的IsBlog
	if isBlog, ok := needUpdate["IsBlog"]; ok {
		db.UpdateByIdAndUserIdMap(db.NoteContents, noteId, userId, bson.M{"IsBlog": isBlog})
	}

	return db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId, needUpdate)
}
Exemple #2
0
// 修改博客的图片, 描述, 摘要
func (this *BlogService) UpateBlogAbstract(userId string, noteId, imgSrc, desc, abstract string) (ok bool) {
	ok = db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId, bson.M{
		"ImgSrc":         imgSrc,
		"Desc":           desc,
		"HasSelfDefined": true,
	})
	ok = db.UpdateByIdAndUserIdMap(db.NoteContents, noteId, userId, bson.M{
		"Abstract": abstract,
	})
	return ok
}
Exemple #3
0
// 修改笔记urlTitle
func (this *BlogService) UpateBlogUrlTitle(userId string, noteId, urlTitle string) (ok bool, url string) {
	url = urlTitle
	// 先清空
	ok = db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId, bson.M{
		"UrlTitle": "",
	})
	url = GetUrTitle(userId, urlTitle, "note", noteId)
	ok = db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId, bson.M{
		"UrlTitle": url,
	})
	// 返回给前端的是decode
	url = decodeValue(url)
	return
}
Exemple #4
0
func (this *TrashService) DeleteTrashApi(noteId, userId string, usn int) (bool, string, int) {
	note := noteService.GetNote(noteId, userId)

	if note.NoteId == "" || note.IsDeleted {
		return false, "notExists", 0
	}

	if note.Usn != usn {
		return false, "conflict", 0
	}

	// delete note's attachs
	ok := attachService.DeleteAllAttachs(noteId, userId)

	// 设置删除位
	afterUsn := userService.IncrUsn(userId)
	ok = db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId,
		bson.M{"IsDeleted": true,
			"Usn": afterUsn})

	// delete content
	ok = db.DeleteByIdAndUserId(db.NoteContents, noteId, userId)

	// 删除content history
	ok = db.DeleteByIdAndUserId(db.NoteContentHistories, noteId, userId)

	// 一个BUG, iOS删除直接调用这个API, 结果没有重新recount
	// recount notebooks' notes number
	notebookService.ReCountNotebookNumberNotes(note.NotebookId.Hex())

	return ok, "", afterUsn
}
// 修改笔记本内容
// [ok] TODO perm未测
func (this *NoteService) UpdateNoteContent(userId, updatedUserId, noteId, content, abstract string) bool {
	// updatedUserId 要修改userId的note, 此时需要判断是否有修改权限
	if userId != updatedUserId {
		if !shareService.HasUpdatePerm(userId, updatedUserId, noteId) {
			Log("NO AUTH")
			return false
		}
	}

	data := bson.M{"UpdatedUserId": bson.ObjectIdHex(updatedUserId),
		"Content":     content,
		"Abstract":    abstract,
		"UpdatedTime": time.Now()}

	// 是否已自定义
	note := this.GetNoteById(noteId)
	if note.IsBlog && note.HasSelfDefined {
		delete(data, "Abstract")
	}

	if db.UpdateByIdAndUserIdMap(db.NoteContents, noteId, userId, data) {
		// 这里, 添加历史记录
		noteContentHistoryService.AddHistory(noteId, userId, info.EachHistory{UpdatedUserId: bson.ObjectIdHex(updatedUserId),
			Content:     content,
			UpdatedTime: time.Now(),
		})

		// 更新笔记图片
		noteImageService.UpdateNoteImages(userId, noteId, note.ImgSrc, content)

		return true
	}
	return false
}
func (this *TrashService) DeleteTrashApi(noteId, userId string, usn int) (bool, string, int) {
	note := noteService.GetNote(noteId, userId)

	if note.NoteId == "" || note.IsDeleted {
		return false, "notExists", 0
	}

	if note.Usn != usn {
		return false, "conflict", 0
	}

	// delete note's attachs
	ok := attachService.DeleteAllAttachs(noteId, userId)

	// 设置删除位
	afterUsn := userService.IncrUsn(userId)
	ok = db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId,
		bson.M{"IsDeleted": true,
			"Usn": afterUsn})

	// delete content
	ok = db.DeleteByIdAndUserId(db.NoteContents, noteId, userId)

	return ok, "", afterUsn
}
Exemple #7
0
// 更新或添加
func (this *BlogService) AddOrUpdateSingle(userId, singleId, title, content string) (ok bool) {
	ok = false
	if singleId != "" {
		ok = db.UpdateByIdAndUserIdMap(db.BlogSingles, singleId, userId, bson.M{
			"Title":       title,
			"Content":     content,
			"UpdatedTime": time.Now(),
		})
		if ok {
			// 还要修改UserBlog中的Singles
			this.updateBlogSingles(userId, false, false, singleId, title, "")
		}
		return
	}
	// 添加
	page := info.BlogSingle{
		SingleId:    bson.NewObjectId(),
		UserId:      bson.ObjectIdHex(userId),
		Title:       title,
		Content:     content,
		UrlTitle:    GetUrTitle(userId, title, "single", singleId),
		CreatedTime: time.Now(),
	}
	page.UpdatedTime = page.CreatedTime
	ok = db.Insert(db.BlogSingles, page)

	// 还要修改UserBlog中的Singles
	this.updateBlogSingles(userId, false, true, page.SingleId.Hex(), title, page.UrlTitle)

	return
}
Exemple #8
0
// 删除trash
func (this *TrashService) DeleteTrash(noteId, userId string) bool {
	note := noteService.GetNote(noteId, userId)
	if note.NoteId == "" {
		return false
	}
	// delete note's attachs
	ok := attachService.DeleteAllAttachs(noteId, userId)

	// 设置删除位
	ok = db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId,
		bson.M{"IsDeleted": true,
			"Usn": userService.IncrUsn(userId)})
	// delete note
	//	ok = db.DeleteByIdAndUserId(db.Notes, noteId, userId)

	// delete content
	ok = db.DeleteByIdAndUserId(db.NoteContents, noteId, userId)

	// 删除content history
	ok = db.DeleteByIdAndUserId(db.NoteContentHistories, noteId, userId)

	// 重新统计tag's count
	// TODO 这里会改变tag's Usn
	tagService.reCountTagCount(userId, note.Tags)

	return ok
}
// 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
}
// 更新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)
}
Exemple #11
0
func (this *NoteService) ToBlog(userId, noteId string, isBlog, isTop bool) bool {
	noteUpdate := bson.M{}
	if isTop {
		isBlog = true
	}
	if !isBlog {
		isTop = false
	}
	noteUpdate["IsBlog"] = isBlog
	noteUpdate["IsTop"] = isTop
	if isBlog {
		noteUpdate["PublicTime"] = time.Now()
	} else {
		noteUpdate["HasSelfDefined"] = false
	}
	noteUpdate["Usn"] = userService.IncrUsn(userId)

	ok := db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId, noteUpdate)
	// 重新计算tags
	go (func() {
		this.UpdateNoteContentIsBlog(noteId, userId, isBlog)

		blogService.ReCountBlogTags(userId)
	})()
	return ok
}
Exemple #12
0
// 更新笔记, api
func (this *NotebookService) UpdateNotebookApi(userId, notebookId, title, parentNotebookId string, seq, usn int) (bool, string, info.Notebook) {
	if notebookId == "" {
		return false, "notebookIdNotExists", info.Notebook{}
	}

	// 先判断usn是否和数据库的一样, 如果不一样, 则冲突, 不保存
	notebook := this.GetNotebookById(notebookId)
	// 不存在
	if notebook.NotebookId == "" {
		return false, "notExists", notebook
	} else if notebook.Usn != usn {
		return false, "conflict", notebook
	}
	notebook.Usn = userService.IncrUsn(userId)
	notebook.Title = title

	updates := bson.M{"Title": title, "Usn": notebook.Usn, "Seq": seq, "UpdatedTime": time.Now()}
	if parentNotebookId != "" && bson.IsObjectIdHex(parentNotebookId) {
		updates["ParentNotebookId"] = bson.ObjectIdHex(parentNotebookId)
	} else {
		updates["ParentNotebookId"] = ""
	}
	ok := db.UpdateByIdAndUserIdMap(db.Notebooks, notebookId, userId, updates)
	if ok {
		return ok, "", this.GetNotebookById(notebookId)
	}
	return false, "", notebook
}
Exemple #13
0
// 修改笔记本内容
// [ok] TODO perm未测
// hasBeforeUpdateNote 之前是否更新过note其它信息, 如果有更新, usn不用更新
// TODO abstract这里生成
func (this *NoteService) UpdateNoteContent(updatedUserId, noteId, content, abstract string,
	hasBeforeUpdateNote bool,
	usn int, updatedTime time.Time) (bool, string, int) {
	// 是否已自定义
	note := this.GetNoteById(noteId)
	if note.NoteId == "" {
		return false, "notExists", 0
	}
	userId := note.UserId.Hex()
	// updatedUserId 要修改userId的note, 此时需要判断是否有修改权限
	if userId != updatedUserId {
		if !shareService.HasUpdatePerm(userId, updatedUserId, noteId) {
			Log("NO AUTH")
			return false, "noAuth", 0
		}
	}

	updatedTime = FixUrlTime(updatedTime)

	// abstract重置
	data := bson.M{"UpdatedUserId": bson.ObjectIdHex(updatedUserId),
		"Content":     content,
		"Abstract":    abstract,
		"UpdatedTime": updatedTime}

	if note.IsBlog && note.HasSelfDefined {
		delete(data, "Abstract")
	}

	// usn, 修改笔记不可能单独修改内容
	afterUsn := 0
	// 如果之前没有修改note其它信息, 那么usn++
	if !hasBeforeUpdateNote {
		// 需要验证
		if usn >= 0 && note.Usn != usn {
			return false, "conflict", 0
		}
		afterUsn = userService.IncrUsn(userId)
		db.UpdateByIdAndUserIdField(db.Notes, noteId, userId, "Usn", usn)
	}

	if db.UpdateByIdAndUserIdMap(db.NoteContents, noteId, userId, data) {
		// 这里, 添加历史记录
		noteContentHistoryService.AddHistory(noteId, userId, info.EachHistory{UpdatedUserId: bson.ObjectIdHex(updatedUserId),
			Content:     content,
			UpdatedTime: time.Now(),
		})

		// 更新笔记图片
		noteImageService.UpdateNoteImages(userId, noteId, note.ImgSrc, content)

		return true, "", afterUsn
	}
	return false, "", 0
}
Exemple #14
0
// 这里要判断权限, 如果userId != updatedUserId, 那么需要判断权限
// [ok] TODO perm还没测 [del]
func (this *NoteService) UpdateNoteTitle(userId, updatedUserId, noteId, title string) bool {
	// updatedUserId 要修改userId的note, 此时需要判断是否有修改权限
	if userId != updatedUserId {
		if !shareService.HasUpdatePerm(userId, updatedUserId, noteId) {
			println("NO AUTH")
			return false
		}
	}

	return db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId,
		bson.M{"UpdatedUserId": bson.ObjectIdHex(updatedUserId), "Title": title, "UpdatedTime": time.Now()})
}
Exemple #15
0
// 排序
// 传入 notebookId => Seq
// 为什么要传入userId, 防止修改其它用户的信息 (恶意)
// [ok]
func (this *NotebookService) SortNotebooks(userId string, notebookId2Seqs map[string]int) bool {
	if len(notebookId2Seqs) == 0 {
		return false
	}

	for notebookId, seq := range notebookId2Seqs {
		if !db.UpdateByIdAndUserIdMap(db.Notebooks, notebookId, userId, bson.M{"Seq": seq, "Usn": userService.IncrUsn(userId)}) {
			return false
		}
	}

	return true
}
Exemple #16
0
// 排序和设置父
func (this *NotebookService) DragNotebooks(userId string, curNotebookId string, parentNotebookId string, siblings []string) bool {
	ok := false
	// 如果没parentNotebookId, 则parentNotebookId设空
	if parentNotebookId == "" {
		ok = db.UpdateByIdAndUserIdMap(db.Notebooks, curNotebookId, userId, bson.M{"ParentNotebookId": "", "Usn": userService.IncrUsn(userId)})
	} else {
		ok = db.UpdateByIdAndUserIdMap(db.Notebooks, curNotebookId, userId, bson.M{"ParentNotebookId": bson.ObjectIdHex(parentNotebookId), "Usn": userService.IncrUsn(userId)})
	}

	if !ok {
		return false
	}

	// 排序
	for seq, notebookId := range siblings {
		if !db.UpdateByIdAndUserIdMap(db.Notebooks, notebookId, userId, bson.M{"Seq": seq, "Usn": userService.IncrUsn(userId)}) {
			return false
		}
	}

	return true
}
Exemple #17
0
// 修改urlTitle
func (this *BlogService) UpdateSingleUrlTitle(userId, singleId, urlTitle string) (ok bool, url string) {
	url = urlTitle
	/*
		// 先清空
		ok = db.UpdateByIdAndUserIdMap(db.BlogSingles, singleId, userId, bson.M{
			"UrlTitle": "",
		})
	*/
	url = GetUrTitle(userId, urlTitle, "single", singleId)
	ok = db.UpdateByIdAndUserIdMap(db.BlogSingles, singleId, userId, bson.M{
		"UrlTitle": url,
	})
	if ok {
		// 还要修改UserBlog中的Singles
		this.updateBlogSingles(userId, false, false, singleId, "", url)
	}
	// 返回给前端的是decode
	url = decodeValue(url)
	return
}
Exemple #18
0
// 修改笔记本内容
// [ok] TODO perm未测
func (this *NoteService) UpdateNoteContent(userId, updatedUserId, noteId, content, abstract string) bool {
	// updatedUserId 要修改userId的note, 此时需要判断是否有修改权限
	if userId != updatedUserId {
		if !shareService.HasUpdatePerm(userId, updatedUserId, noteId) {
			Log("NO AUTH")
			return false
		}
	}

	if db.UpdateByIdAndUserIdMap(db.NoteContents, noteId, userId,
		bson.M{"UpdatedUserId": bson.ObjectIdHex(updatedUserId),
			"Content":     content,
			"Abstract":    abstract,
			"UpdatedTime": time.Now()}) {

		// 这里, 添加历史记录
		noteContentHistoryService.AddHistory(noteId, userId, info.EachHistory{UpdatedUserId: bson.ObjectIdHex(updatedUserId),
			Content:     content,
			UpdatedTime: time.Now(),
		})
		return true
	}
	return false
}
Exemple #19
0
// 当设置/取消了笔记为博客
func (this *NoteService) UpdateNoteContentIsBlog(noteId, userId string, isBlog bool) {
	db.UpdateByIdAndUserIdMap(db.NoteContents, noteId, userId, bson.M{"IsBlog": isBlog})
}
Exemple #20
0
// 附件修改, 增加noteIncr
func (this *NoteService) IncrNoteUsn(noteId, userId string) int {
	afterUsn := userService.IncrUsn(userId)
	db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId,
		bson.M{"UpdatedTime": time.Now(), "Usn": afterUsn})
	return afterUsn
}
// 更新notebook
func (this *NotebookService) UpdateNotebook(userId, notebookId string, needUpdate bson.M) bool {
	needUpdate["UpdatedTime"] = time.Now()
	return db.UpdateByIdAndUserIdMap(db.Notebooks, notebookId, userId, needUpdate)
}
Exemple #22
0
// 更新tags
// [ok] [del]
func (this *NoteService) UpdateTags(noteId string, userId string, tags []string) bool {
	return db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId, bson.M{"Tags": tags, "Usn": userService.IncrUsn(userId)})
}
Exemple #23
0
// 修改笔记
// 这里没有判断usn
func (this *NoteService) UpdateNote(updatedUserId, noteId string, needUpdate bson.M, usn int) (bool, string, int) {
	// 是否存在
	note := this.GetNoteById(noteId)
	if note.NoteId == "" {
		return false, "notExists", 0
	}

	userId := note.UserId.Hex()
	// updatedUserId 要修改userId的note, 此时需要判断是否有修改权限
	if userId != updatedUserId {
		if !shareService.HasUpdatePerm(userId, updatedUserId, noteId) {
			Log("NO AUTH2")
			return false, "noAuth", 0
		} else {
			Log("HAS AUTH -----------")
		}
	}

	if usn > 0 && note.Usn != usn {
		return false, "conflict", 0
	}

	// 是否已自定义
	if note.IsBlog && note.HasSelfDefined {
		delete(needUpdate, "ImgSrc")
		delete(needUpdate, "Desc")
	}

	needUpdate["UpdatedUserId"] = bson.ObjectIdHex(updatedUserId)

	// 可以将时间传过来
	updatedTime, ok := needUpdate["UpdatedTime"].(time.Time)
	if ok {
		needUpdate["UpdatedTime"] = FixUrlTime(updatedTime)
	} else {
		needUpdate["UpdatedTime"] = time.Now()
	}

	afterUsn := userService.IncrUsn(userId)
	needUpdate["Usn"] = afterUsn

	needRecountTags := false

	// 是否修改了isBlog
	// 也要修改noteContents的IsBlog
	if isBlog, ok := needUpdate["IsBlog"]; ok {
		isBlog2 := isBlog.(bool)
		if note.IsBlog != isBlog2 {
			this.UpdateNoteContentIsBlog(noteId, userId, isBlog2)

			// 重新发布成博客
			if !note.IsBlog {
				needUpdate["PublicTime"] = needUpdate["UpdatedTime"]
			}

			needRecountTags = true
		}
	}

	// 添加tag2
	// TODO 这个tag去掉, 添加tag另外添加, 不要这个
	if tags, ok := needUpdate["Tags"]; ok {
		tagService.AddTagsI(userId, tags)

		// 如果是博客, 标签改了, 那么重新计算
		if note.IsBlog {
			needRecountTags = true
		}
	}

	ok = db.UpdateByIdAndUserIdMap(db.Notes, noteId, userId, needUpdate)
	if !ok {
		return ok, "", 0
	}

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

	// 重新获取之
	note = this.GetNoteById(noteId)

	hasRecount := false

	// 如果修改了notebookId, 则更新notebookId'count
	// 两方的notebook也要修改
	notebookIdI := needUpdate["NotebookId"]
	if notebookIdI != nil {
		notebookId := notebookIdI.(bson.ObjectId)
		if notebookId != "" {
			notebookService.ReCountNotebookNumberNotes(note.NotebookId.Hex())
			notebookService.ReCountNotebookNumberNotes(notebookId.Hex())
			hasRecount = true
		}
	}

	// 不要多次更新, isTrash = false, = true都要重新统计
	if isTrashI, ok := needUpdate["IsTrash"]; ok {
		// 如果是垃圾, 则删除之共享
		isTrash := isTrashI.(bool)
		if isTrash {
			shareService.DeleteShareNoteAll(noteId, userId)
		}
		if !hasRecount {
			notebookService.ReCountNotebookNumberNotes(note.NotebookId.Hex())
		}
	}

	return true, "", afterUsn
}
Exemple #24
0
// 更新笔记本标题
// [ok]
func (this *NotebookService) UpdateNotebookTitle(notebookId, userId, title string) bool {
	usn := userService.IncrUsn(userId)
	return db.UpdateByIdAndUserIdMap(db.Notebooks, notebookId, userId, bson.M{"Title": title, "Usn": usn})
}