// 是否是我的文件
func (this *FileService) IsMyFile(userId, fileId string) bool {
	// 如果有问题会panic
	if !bson.IsObjectIdHex(fileId) || !bson.IsObjectIdHex(userId) {
		return false
	}
	return db.Has(db.Files, bson.M{"UserId": bson.ObjectIdHex(userId), "_id": bson.ObjectIdHex(fileId)})
}
// 判断组中是否包含指定用户
func (this *GroupService) IsExistsGroupUser(userId, groupId string) (ok bool) {
	// 如果我拥有这个组, 那也行
	if this.isMyGroup(userId, groupId) {
		return true
	}
	return db.Has(db.GroupUsers, bson.M{"UserId": bson.ObjectIdHex(userId), "GroupId": bson.ObjectIdHex(groupId)})
}
Beispiel #3
0
func getUniqueUrlTitle(userId string, urlTitle string, types string, padding int) string {
	urlTitle2 := urlTitle

	// 判断urlTitle是不是过长, 过长则截断, 300
	// 不然生成index有问题
	// it will not index a single field with more than 1024 bytes.
	// If you're indexing a field that is 2.5MB, it's not really indexing it, it's being skipped.
	if len(urlTitle2) > 320 {
		urlTitle2 = urlTitle2[:300] // 为什么要少些, 因为怕无限循环, 因为把padding截了
	}

	if padding > 1 {
		urlTitle2 = urlTitle + "-" + strconv.Itoa(padding)
	}
	userIdO := bson.ObjectIdHex(userId)

	var collection *mgo.Collection
	if types == "note" {
		collection = db.Notes
	} else if types == "notebook" {
		collection = db.Notebooks
	} else if types == "single" {
		collection = db.BlogSingles
	}
	for db.Has(collection, bson.M{"UserId": userIdO, "UrlTitle": urlTitle2}) { // 用户下唯一
		padding++
		urlTitle2 = urlTitle + "-" + strconv.Itoa(padding)
	}

	return urlTitle2
}
// 点赞
// retun ok , isLike
func (this *BlogService) LikeBlog(noteId, userId string) (ok bool, isLike bool) {
	ok = false
	isLike = false
	if noteId == "" || userId == "" {
		return
	}
	// 判断是否点过赞, 如果点过那么取消点赞
	note := noteService.GetNoteById(noteId)
	if !note.IsBlog /*|| note.UserId.Hex() == userId */ {
		return
	}

	noteIdO := bson.ObjectIdHex(noteId)
	userIdO := bson.ObjectIdHex(userId)
	var n int
	if !db.Has(db.BlogLikes, bson.M{"NoteId": noteIdO, "UserId": userIdO}) {
		n = 1
		// 添加之
		db.Insert(db.BlogLikes, info.BlogLike{LikeId: bson.NewObjectId(), NoteId: noteIdO, UserId: userIdO, CreatedTime: time.Now()})
		isLike = true
	} else {
		// 已点过, 那么删除之
		n = -1
		db.Delete(db.BlogLikes, bson.M{"NoteId": noteIdO, "UserId": userIdO})
		isLike = false
	}
	ok = db.Update(db.Notes, bson.M{"_id": noteIdO}, bson.M{"$inc": bson.M{"LikeNum": n}})

	return
}
func (this *BlogService) IsILikeIt(noteId, userId string) bool {
	if userId == "" {
		return false
	}
	if db.Has(db.BlogLikes, bson.M{"NoteId": bson.ObjectIdHex(noteId), "UserId": bson.ObjectIdHex(userId)}) {
		return true
	}
	return false
}
// noteId的notebook是否共享了给我
func (this *ShareService) HasSharedNotebook(noteId, myUserId, sharedUserId string) bool {
	notebookId := noteService.GetNotebookId(noteId)
	if notebookId != "" {
		return db.Has(db.ShareNotebooks, bson.M{"NotebookId": notebookId,
			"UserId":   bson.ObjectIdHex(sharedUserId),
			"ToUserId": bson.ObjectIdHex(myUserId),
		})
	}
	return false
}
// 使用主题
func (this *ThemeService) ActiveTheme(userId, themeId string) (ok bool) {
	if db.Has(db.Themes, bson.M{"_id": bson.ObjectIdHex(themeId), "UserId": bson.ObjectIdHex(userId)}) {
		// 之前的设为false
		db.UpdateByQField(db.Themes, bson.M{"UserId": bson.ObjectIdHex(userId), "IsActive": true}, "IsActive", false)
		// 现在的设为true
		db.UpdateByQField(db.Themes, bson.M{"_id": bson.ObjectIdHex(themeId)}, "IsActive", true)

		// UserBlog ThemeId
		db.UpdateByQField(db.UserBlogs, bson.M{"_id": bson.ObjectIdHex(userId)}, "ThemeId", bson.ObjectIdHex(themeId))
		return true
	}
	return false
}
// updatedUserId是否有查看userId noteId的权限?
// userId是所有者
func (this *ShareService) HasReadPerm(userId, updatedUserId, noteId string) bool {
	q := this.getOrQ(updatedUserId) // (toUserId == "xxx" || ToGroupId in (1, 2,3))
	q["UserId"] = bson.ObjectIdHex(userId)
	q["NoteId"] = bson.ObjectIdHex(noteId)
	if !db.Has(db.ShareNotes, q) {
		// noteId的notebookId是否被共享了?
		notebookId := noteService.GetNotebookId(noteId)
		if notebookId.Hex() == "" {
			return false
		}

		delete(q, "NoteId")
		q["NotebookId"] = notebookId

		// 判断notebook是否被共享
		if !db.Has(db.ShareNotebooks, q) {
			return false
		} else {
			return true
		}
	} else {
		return true
	}
}
// 更新username
func (this *UserService) UpdateUsername(userId, username string) (bool, string) {
	//modified by JacobXie
	if userId == configService.adminUserId { //admin 不能修改用户名
		return false, "adminUser is not permitted for modifying name"
	}
	usernameRaw := username // 原先的, 可能是同一个, 但有大小写
	username = strings.ToLower(username)
	if userId == "" || username == "" || username == "admin" { // admin用户是内置的, 不能设置
		return false, "usernameIsExisted"
	}

	// 先判断是否存在
	userIdO := bson.ObjectIdHex(userId)
	if db.Has(db.Users, bson.M{"Username": username, "_id": bson.M{"$ne": userIdO}}) {
		return false, "usernameIsExisted"
	}

	ok := db.UpdateByQMap(db.Users, bson.M{"_id": userIdO}, bson.M{"Username": username, "UsernameRaw": usernameRaw})
	return ok, ""
}
// 为group添加用户
// 用户是否已存在?
func (this *GroupService) AddUser(ownUserId, groupId, userId string) (ok bool, msg string) {
	// groupId是否是ownUserId的?
	/*
		if !this.IsExistsGroupUser(ownUserId, groupId) {
			return false, "forbidden"
		}
	*/
	if !this.isMyGroup(ownUserId, groupId) {
		return false, "forbidden"
	}

	// 是否已存在
	if db.Has(db.GroupUsers, bson.M{"GroupId": bson.ObjectIdHex(groupId), "UserId": bson.ObjectIdHex(userId)}) {
		return false, "hasUsers"
	}

	return db.Insert(db.GroupUsers, info.GroupUser{
		GroupUserId: bson.NewObjectId(),
		GroupId:     bson.ObjectIdHex(groupId),
		UserId:      bson.ObjectIdHex(userId),
		CreatedTime: time.Now(),
	}), ""
}
func (this *GroupService) isMyGroup(ownUserId, groupId string) (ok bool) {
	return db.Has(db.Groups, bson.M{"_id": bson.ObjectIdHex(groupId), "UserId": bson.ObjectIdHex(ownUserId)})
}
// 判断是否是我的notebook
func (this *NotebookService) IsMyNotebook(notebookId, userId string) bool {
	return db.Has(db.Notebooks, bson.M{"_id": bson.ObjectIdHex(notebookId), "UserId": bson.ObjectIdHex(userId)})
}
// 获取文件路径
// 要判断是否具有权限
// userId是否具有fileId的访问权限
func (this *FileService) GetFile(userId, fileId string) string {
	if fileId == "" {
		return ""
	}

	file := info.File{}
	db.Get(db.Files, fileId, &file)
	path := file.Path
	if path == "" {
		return ""
	}

	// 1. 判断权限

	// 是否是我的文件
	if userId != "" && file.UserId.Hex() == userId {
		return path
	}

	// 得到使用过该fileId的所有笔记NoteId
	// 这些笔记是否有public的, 若有则ok
	// 这些笔记(笔记本)是否有共享给我的, 若有则ok

	noteIds := noteImageService.GetNoteIds(fileId)
	if noteIds != nil && len(noteIds) > 0 {
		// 这些笔记是否有public的
		if db.Has(db.Notes, bson.M{"_id": bson.M{"$in": noteIds}, "IsBlog": true}) {
			return path
		}

		// 2014/12/28 修复, 如果是分享给用户组, 那就不行, 这里可以实现
		for _, noteId := range noteIds {
			note := noteService.GetNoteById(noteId.Hex())
			if shareService.HasReadPerm(note.UserId.Hex(), userId, noteId.Hex()) {
				return path
			}
		}
		/*
			// 若有共享给我的笔记?
			// 对该笔记可读?
			if db.Has(db.ShareNotes, bson.M{"ToUserId": bson.ObjectIdHex(userId), "NoteId": bson.M{"$in": noteIds}}) {
				return path
			}

			// 笔记本是否共享给我?
			// 通过笔记得到笔记本
			notes := []info.Note{}
			db.ListByQWithFields(db.Notes, bson.M{"_id": bson.M{"$in": noteIds}}, []string{"NotebookId"}, &notes)
			if notes != nil && len(notes) > 0 {
				notebookIds := make([]bson.ObjectId, len(notes))
				for i := 0; i < len(notes); i++ {
					notebookIds[i] = notes[i].NotebookId
				}

				if db.Has(db.ShareNotebooks, bson.M{"ToUserId": bson.ObjectIdHex(userId), "NotebookId": bson.M{"$in": notebookIds}}) {
					return path
				}
			}
		*/
	}

	// 可能是刚复制到owner上, 但内容又没有保存, 所以没有note->imageId的映射, 此时看是否有fromFileId
	if file.FromFileId != "" {
		fromFile := info.File{}
		db.Get2(db.Files, file.FromFileId, &fromFile)
		if fromFile.UserId.Hex() == userId {
			return fromFile.Path
		}
	}

	return ""
}
// userId是否被共享了noteId
func (this *ShareService) HasSharedNote(noteId, myUserId string) bool {
	return db.Has(db.ShareNotes, bson.M{"ToUserId": bson.ObjectIdHex(myUserId), "NoteId": bson.ObjectIdHex(noteId)})
}