init
This commit is contained in:
app
controllers
AuthController.goBaseController.goBlogController.goFileController.goIndexController.goMobileController.goNoteContentHistoryController.goNoteController.goNotebookController.goOauthController.goShareController.goUserController.goinit.go
db
i18n
info
BlogInfo.goNoteInfo.goNotebookInfo.goREADME.mdRe.goShareNotebookNoteInfo.goSuggestionInfo.goTagInfo.goTokenInfo.goUserInfo.gocommon.go
init.golea
release
routes
service
AuthService.goBlogService.goNoteContentHistoryService.goNoteService.goNotebookService.goPwdService.goREADME.mdShareService.goSuggestionService.goTagService.goTokenService.goTrashService.goUserService.gocommon.goinit.go
test
tmp
views
App
Blog
Errors
File
Home
find_password.htmlfind_password2.htmlfind_password2_timeout.htmlfooter.htmlheader.htmlheader_box.htmlindex.htmllogin.htmlmodal.htmlregister.html
Mobile
Note
Notebook
Oauth
Share
User
conf
messages
public
css
blog
blog.cssblog_daqi.cssblog_daqi.lessblog_default.cssblog_default.lessblog_default2.cssblog_left_fixed.cssblog_left_fixed.less
bootstrap-theme.cssbootstrap-theme.min.cssbootstrap.cssbootstrap.min.cssbootstrap修改editor.csseditor.lessfont-awesome-4.0.3
css
fonts
FontAwesome.otffontawesome-webfont.eotfontawesome-webfont.svgfontawesome-webfont.ttffontawesome-webfont.woff
less
bordered-pulled.lesscore.lessfixed-width.lessfont-awesome.lessicons.lesslarger.lesslist.lessmixins.lesspath.lessrotated-flipped.lessspinning.lessstacked.lessvariables.less
scss
theme
fonts
open-sans2
DXI1ORHCpsQm3Vp6mXoaTXhCUOGz7vYGh680lGh-uXM.woffcJZKeOuBrn4kERxqtaUH3T8E0i7KZn-EPnyo3HZu7kw.woffk3k702ZOKiLJc3WVjuplzHhCUOGz7vYGh680lGh-uXM.woffxjAJXh38I15wypJXxuGMBobN6UDyHWBl620a-IRfuBk.woff
weibo
images
favicon.icofavicon_ever.ico
home
leanote
logo-100.giflogo-120.giflogo-20-a-6.pnglogo-32-a-5.pnglogo-32-a-7.pnglogo-32.pnglogo-60-a-6.pnglogo-60.gifold-preview.png
loading-24.gifloading-30.gifloading-32.gifloading-a-20-2.gifloading-a-20.gifloading-a-24.giflogo.pngnoise.pngoutofcode.pngslider
js
ZeroClipboard
all.jsapp
blog
note-min.jsnote.jsnotebook-min.jsnotebook.jspage-min.jspage.jsshare-min.jsshare.jstag-min.jstag.jscontextmenu
ever
i18n
jQuery-slimScroll-1.3.0
jquery-1.9.0.min.jsjquery-cookie-min.jsjquery-cookie.jsobject_id-min.jsobject_id.jsmdeditor
css
bootstrap-responsive.cssbootstrap.cssindex2.html
smoothness
images
animated-overlay.gifui-bg_flat_0_aaaaaa_40x100.pngui-bg_flat_75_ffffff_40x100.pngui-bg_glass_55_fbf9ee_1x400.pngui-bg_glass_65_ffffff_1x400.pngui-bg_glass_75_dadada_1x400.pngui-bg_glass_75_e6e6e6_1x400.pngui-bg_glass_95_fef1ec_1x400.pngui-bg_highlight-soft_75_cccccc_1x100.pngui-icons_222222_256x240.pngui-icons_2e83ff_256x240.pngui-icons_454545_256x240.pngui-icons_888888_256x240.pngui-icons_cd0a0a_256x240.png
jquery-ui-1.10.2.custom.cssjquery-ui-1.10.2.custom.min.csseditor
Markdown.Extra-min.jsMarkdown.Extra.jscss_browser_selector.jseditor-min.jseditor.csseditor.jseditor2.csseditor2.js
google-code-prettify
lang-apollo.jslang-basic.jslang-clj.jslang-css.jslang-dart.jslang-erlang.jslang-go.jslang-hs.jslang-lisp.jslang-llvm.jslang-lua.jslang-matlab.jslang-ml.jslang-mumps.jslang-n.jslang-pascal.jslang-proto.jslang-r.jslang-rd.jslang-scala.jslang-sql.jslang-tcl.jslang-tex.jslang-vb.jslang-vhdl.jslang-wiki.jslang-xq.jslang-yaml.jsmine.cssprettify.cssprettify.jsrun_prettify.js
jquery.autosize-min.jsjquery.waitforimages-min.jsjquery.waitforimages.jsmathJax-min.jsmathJax.jsmd-helppagedown
LICENSE.txtMarkdown.Converter-min.jsMarkdown.Converter.jsMarkdown.Editor-min.jsMarkdown.Editor.jsMarkdown.Sanitizer-min.jsMarkdown.Sanitizer.jsREADME.txt
scrollLink-min.jsscrollLink.jsunderscore-min.jsunderscore.jsdemo
local
node-pagedown.jspackage.jsonresources
wmd-buttons.pngfont-awesome
css
font
FontAwesome.otffontawesome-webfont.eotfontawesome-webfont.svgfontawesome-webfont.ttffontawesome-webfont.woff
scss
img
mobile
tinymce
classes
AddOnManager.jsCompat.jsEditor.jsEditorCommands.jsEditorManager.jsEnterKey.jsEnv.jsFocusManager.jsForceBlocks.jsFormatter.jsLegacyInput.jsShortcuts.jsUndoManager.jsWindowManager.js
index.htmljquery.tinymce.min.jsdom
ControlSelection.jsDOMUtils.jsDomQuery.jsEventUtils.jsRange.jsRangeUtils.jsScriptLoader.jsSelection.jsSerializer.jsSizzle.jQuery.jsSizzle.jsTreeWalker.jsTridentSelection.js
html
jquery.tinymce.jsui
AbsoluteLayout.jsButton.jsButtonGroup.jsCheckbox.jsCollection.jsColorButton.jsComboBox.jsContainer.jsControl.jsDomUtils.jsDragHelper.jsElementPath.jsFactory.jsFieldSet.jsFilePicker.jsFitLayout.jsFlexLayout.jsFloatPanel.jsFlowLayout.jsForm.jsFormItem.jsFormatControls.jsGridLayout.jsIframe.jsKeyboardNavigation.jsLabel.jsLayout.jsListBox.jsMenu.jsMenuBar.jsMenuButton.jsMenuItem.jsMessageBox.jsMovable.jsPanel.jsPanelButton.jsPath.jsRadio.jsResizable.jsResizeHandle.jsScrollable.jsSelector.jsSpacer.jsSplitButton.jsStackLayout.jsTabPanel.jsTextBox.jsThrobber.jsToolbar.jsTooltip.jsWidget.jsWindow.js
util
langs
plugins
advlist
anchor
autolink
autoresize
autosave
bbcode
charmap
code
codemirror
CodeMirror
.gitattributes.gitignore.travis.ymlAUTHORSCONTRIBUTING.mdLICENSEREADME.mdpackage.json
LICENSE.txtREADME.txtaddon
comment
dialog
display
edit
fold
hint
anyword-hint.jscss-hint.jshtml-hint.jsjavascript-hint.jspig-hint.jspython-hint.jsshow-hint.cssshow-hint.jssql-hint.jsxml-hint.js
lint
merge
mode
runmode
scroll
search
selection
tern
wrap
bin
bower.jsondemo
activeline.htmlanywordhint.htmlbidi.htmlbtree.htmlbuffers.htmlchangemode.htmlclosebrackets.htmlclosetag.htmlcomplete.htmlemacs.htmlfolding.htmlfullscreen.htmlhardwrap.htmlhtml5complete.htmlindentwrap.htmllint.htmlloadmode.htmlmarker.htmlmarkselection.htmlmatchhighlighter.htmlmatchtags.htmlmerge.htmlmultiplex.htmlmustache.htmlplaceholder.htmlpreview.htmlresize.htmlrunmode.htmlsearch.htmlspanaffectswrapping_shim.htmltern.htmltheme.htmltrailingspace.htmlvariableheight.htmlvim.htmlvisibletabs.htmlwidget.htmlxmlcomplete.html
doc
activebookmark.jscompress.htmldocs.cssinternals.htmllogo.pnglogo.svgmanual.htmlrealworld.htmlreleases.htmlreporting.htmlupgrade_v2.2.htmlupgrade_v3.html
index.htmlkeymap
lib
mode
apl
asterisk
clike
clojure
cobol
coffeescript
commonlisp
css
d
diff
dtd
ecl
eiffel
erlang
fortran
gas
gfm
gherkin
go
groovy
haml
haskell
haxe
htmlembedded
htmlmixed
http
index.htmljade
javascript
jinja2
julia
less
livescript
lua
markdown
meta.jsmirc
mllike
nginx
ntriples
octave
pascal
pegjs
perl
php
pig
properties
python
q
r
rpm
rst
ruby
rust
sass
scheme
shell
sieve
smalltalk
smarty
smartymixed
sparql
sql
stex
tcl
tiddlywiki
tiki
toml
turtle
vb
vbscript
velocity
verilog
xml
xquery
yaml
z80
test
comment_test.jsdoc_test.jsdriver.jsemacs_test.jsindex.html
lint
mode_test.cssmode_test.jsphantom_driver.jsrun.jssearch_test.jstest.jsvim_test.jstheme
3024-day.css3024-night.cssambiance-mobile.cssambiance.cssbase16-dark.cssbase16-light.cssblackboard.csscobalt.csseclipse.csselegant.csserlang-dark.csslesser-dark.cssmbo.cssmidnight.cssmonokai.cssneat.cssnight.cssparaiso-dark.cssparaiso-light.csspastel-on-dark.cssrubyblue.csssolarized.cssthe-matrix.csstomorrow-night-eighties.csstwilight.cssvibrant-ink.cssxq-dark.cssxq-light.css
img
langs
plugin.jsplugin.min.jssource.htmlcodesyntax
compat3x
contextmenu
directionality
emoticons
img
smiley-cool.gifsmiley-cry.gifsmiley-embarassed.gifsmiley-foot-in-mouth.gifsmiley-frown.gifsmiley-innocent.gifsmiley-kiss.gifsmiley-laughing.gifsmiley-money-mouth.gifsmiley-sealed.gifsmiley-smile.gifsmiley-surprised.gifsmiley-tongue-out.gifsmiley-undecided.gifsmiley-wink.gifsmiley-yell.gif
plugin.jsplugin.min.jsexample
example_dependency
fullpage
fullscreen
hr
image
importcss
insertdatetime
layer
leanote_code
leanote_image
leanote_nav
legacyoutput
link
lists
media
nonbreaking
noneditable
pagebreak
paste
preview
print
save
searchreplace
spellchecker
tabfocus
table
template
textcolor
upload_image
visualblocks
css
img
address.gifarticle.gifaside.gifblockquote.gifdiv.giffigure.gifh1.gifh2.gifh3.gifh4.gifh5.gifh6.gifhgroup.gifol.gifp.gifpre.gifsection.giful.gif
plugin.jsplugin.min.jsvisualchars
wordcount
skins
custom
content.inline.min.csscontent.min.css
fonts
icomoon-small.eoticomoon-small.svgicomoon-small.ttficomoon-small.wofficomoon.eoticomoon.svgicomoon.ttficomoon.woffreadme.md
img
skin.ie7.min.cssskin.jsonskin.min.csslightgray
AbsoluteLayout.lessAnimations.lessButton.lessButtonGroup.lessCheckbox.lessColorButton.lessComboBox.lessContainer.lessContent.Inline.lessContent.Objects.lessContent.lessFieldSet.lessFitLayout.lessFloatPanel.lessFlowLayout.lessIcons.Ie7.lessIcons.lessIframe.lessLabel.lessListBox.lessMenu.lessMenuBar.lessMenuButton.lessMenuItem.lessMixins.lessPanel.lessPath.lessRadio.lessReset.lessResizeHandle.lessScrollable.lessSpacer.lessSplitButton.lessStackLayout.lessTabPanel.lessTextBox.lessThrobber.lessTinyMCE.lessToolTip.lessVariables.lessWindow.lesscontent.inline.min.csscontent.min.css
fonts
readme.mdtinymce-small.dev.svgtinymce-small.eottinymce-small.svgtinymce-small.ttftinymce-small.wofftinymce.dev.svgtinymce.eottinymce.svgtinymce.ttftinymce.woff
img
skin.dev.lessskin.ie7.lessskin.ie7.min.cssskin.lessskin.min.cssthemes
modern
upload
52d3e8ac99c37b7f0d000001
5368c9fc99c37b095a000006
images
449
app/service/NoteService.go
Normal file
449
app/service/NoteService.go
Normal file
@ -0,0 +1,449 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"github.com/leanote/leanote/app/info"
|
||||
"github.com/leanote/leanote/app/db"
|
||||
. "github.com/leanote/leanote/app/lea"
|
||||
"labix.org/v2/mgo/bson"
|
||||
"time"
|
||||
)
|
||||
|
||||
type NoteService struct {
|
||||
}
|
||||
|
||||
// 通过id, userId得到note
|
||||
func (this *NoteService) GetNote(noteId, userId string) (note info.Note) {
|
||||
note = info.Note{}
|
||||
db.GetByIdAndUserId(db.Notes, noteId, userId, ¬e)
|
||||
return
|
||||
}
|
||||
// 得到blog, blogService用
|
||||
// 不要传userId, 因为是公开的
|
||||
func (this *NoteService) GetBlogNote(noteId string) (note info.Note) {
|
||||
note = info.Note{}
|
||||
db.GetByQ(db.Notes, bson.M{"_id": bson.ObjectIdHex(noteId), "IsBlog": true, "IsTrash": false}, ¬e)
|
||||
return
|
||||
}
|
||||
// 通过id, userId得到noteContent
|
||||
func (this *NoteService) GetNoteContent(noteContentId, userId string) (noteContent info.NoteContent) {
|
||||
noteContent = info.NoteContent{}
|
||||
db.GetByIdAndUserId(db.NoteContents, noteContentId, userId, ¬eContent)
|
||||
return
|
||||
}
|
||||
|
||||
// 列出note, 排序规则, 还有分页
|
||||
// CreatedTime, UpdatedTime, title 来排序
|
||||
func (this *NoteService) ListNotes(userId, notebookId string,
|
||||
isTrash bool, pageNumber, pageSize int, sortField string, isAsc bool, isBlog bool) (count int, notes []info.Note) {
|
||||
notes = []info.Note{}
|
||||
skipNum, sortFieldR := parsePageAndSort(pageNumber, pageSize, sortField, isAsc)
|
||||
// 不是trash的
|
||||
query := bson.M{"UserId": bson.ObjectIdHex(userId), "IsTrash": isTrash}
|
||||
if isBlog {
|
||||
query["IsBlog"] = true
|
||||
}
|
||||
if notebookId != "" {
|
||||
query["NotebookId"] = bson.ObjectIdHex(notebookId)
|
||||
}
|
||||
|
||||
q := db.Notes.Find(query);
|
||||
|
||||
// 总记录数
|
||||
count, _ = q.Count()
|
||||
|
||||
q.Sort(sortFieldR).
|
||||
Skip(skipNum).
|
||||
Limit(pageSize).
|
||||
All(¬es)
|
||||
return
|
||||
}
|
||||
|
||||
// 通过noteIds来查询
|
||||
// ShareService调用
|
||||
func (this *NoteService) ListNotesByNoteIdsWithPageSort(noteIds []bson.ObjectId, userId string,
|
||||
pageNumber, pageSize int, sortField string, isAsc bool, isBlog bool) (notes []info.Note) {
|
||||
skipNum, sortFieldR := parsePageAndSort(pageNumber, pageSize, sortField, isAsc)
|
||||
notes = []info.Note{}
|
||||
|
||||
// 不是trash
|
||||
db.Notes.
|
||||
Find(bson.M{"_id": bson.M{"$in": noteIds}, "IsTrash": false}).
|
||||
Sort(sortFieldR).
|
||||
Skip(skipNum).
|
||||
Limit(pageSize).
|
||||
All(¬es)
|
||||
return
|
||||
}
|
||||
// shareService调用
|
||||
func (this *NoteService) ListNotesByNoteIds(noteIds []bson.ObjectId) (notes []info.Note) {
|
||||
notes = []info.Note{}
|
||||
|
||||
db.Notes.
|
||||
Find(bson.M{"_id": bson.M{"$in": noteIds}}).
|
||||
All(¬es)
|
||||
return
|
||||
}
|
||||
// blog需要
|
||||
func (this *NoteService) ListNoteContentsByNoteIds(noteIds []bson.ObjectId) (notes []info.NoteContent) {
|
||||
notes = []info.NoteContent{}
|
||||
|
||||
db.NoteContents.
|
||||
Find(bson.M{"_id": bson.M{"$in": noteIds}}).
|
||||
All(¬es)
|
||||
return
|
||||
}
|
||||
// 只得到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"}, ¬es)
|
||||
return
|
||||
}
|
||||
|
||||
// 添加笔记
|
||||
// 首先要判断Notebook是否是Blog, 是的话设为blog
|
||||
// [ok]
|
||||
func (this *NoteService) AddNote(note info.Note) info.Note {
|
||||
if(note.NoteId.Hex() == "") {
|
||||
noteId := bson.NewObjectId();
|
||||
note.NoteId = noteId;
|
||||
}
|
||||
note.CreatedTime = time.Now()
|
||||
note.UpdatedTime = note.CreatedTime
|
||||
note.IsTrash = false
|
||||
note.UpdatedUserId = note.UserId
|
||||
|
||||
// 设为blog
|
||||
note.IsBlog = notebookService.IsBlog(note.NotebookId.Hex())
|
||||
|
||||
db.Insert(db.Notes, note)
|
||||
|
||||
// tag1
|
||||
tagService.AddTags(note.UserId.Hex(), note.Tags)
|
||||
|
||||
return note
|
||||
}
|
||||
|
||||
// 添加共享d笔记
|
||||
func (this *NoteService) AddSharedNote(note info.Note, myUserId bson.ObjectId) info.Note {
|
||||
// 判断我是否有权限添加
|
||||
if shareService.HasUpdateNotebookPerm(note.UserId.Hex(), myUserId.Hex(), note.NotebookId.Hex()) {
|
||||
note.CreatedUserId = myUserId // 是我给共享我的人创建的
|
||||
return this.AddNote(note)
|
||||
}
|
||||
return info.Note{}
|
||||
}
|
||||
|
||||
|
||||
// 添加笔记本内容
|
||||
// [ok]
|
||||
func (this *NoteService) AddNoteContent(noteContent info.NoteContent) info.NoteContent {
|
||||
noteContent.CreatedTime = time.Now()
|
||||
noteContent.UpdatedTime = noteContent.CreatedTime
|
||||
noteContent.UpdatedUserId = noteContent.UserId
|
||||
db.Insert(db.NoteContents, noteContent)
|
||||
return noteContent;
|
||||
}
|
||||
|
||||
// 添加笔记和内容
|
||||
// 这里使用 info.NoteAndContent 接收?
|
||||
func (this *NoteService) AddNoteAndContent(note info.Note, noteContent info.NoteContent, myUserId bson.ObjectId) info.Note {
|
||||
if(note.NoteId.Hex() == "") {
|
||||
noteId := bson.NewObjectId()
|
||||
note.NoteId = noteId
|
||||
}
|
||||
noteContent.NoteId = note.NoteId
|
||||
if note.UserId != myUserId {
|
||||
note = this.AddSharedNote(note, myUserId)
|
||||
} else {
|
||||
note = this.AddNote(note)
|
||||
}
|
||||
if note.NoteId != "" {
|
||||
this.AddNoteContent(noteContent)
|
||||
}
|
||||
return note
|
||||
}
|
||||
|
||||
// 修改笔记
|
||||
// [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)
|
||||
}
|
||||
|
||||
// 这里要判断权限, 如果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()})
|
||||
}
|
||||
|
||||
// 修改笔记本内容
|
||||
// [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
|
||||
}
|
||||
|
||||
// 更新tags
|
||||
// [ok] [del]
|
||||
func (this *NoteService) UpdateTags(noteId string, userId string, tags []string) bool {
|
||||
return db.UpdateByIdAndUserIdField(db.Notes, noteId, userId, "Tags", tags)
|
||||
}
|
||||
|
||||
// 移动note
|
||||
// trash, 正常的都可以用
|
||||
// 1. 要检查下notebookId是否是自己的
|
||||
// 2. 要判断之前是否是blog, 如果不是, 那么notebook是否是blog?
|
||||
func (this *NoteService) MoveNote(noteId, notebookId, userId string) info.Note {
|
||||
if notebookService.IsMyNotebook(notebookId, userId) {
|
||||
re := db.UpdateByIdAndUserId(db.Notes, noteId, userId,
|
||||
bson.M{"$set": bson.M{"IsTrash": false,
|
||||
"NotebookId": bson.ObjectIdHex(notebookId)}})
|
||||
|
||||
if re {
|
||||
// 更新blog状态
|
||||
this.updateToNotebookBlog(noteId, notebookId, userId)
|
||||
}
|
||||
|
||||
return this.GetNote(noteId, userId);
|
||||
}
|
||||
return info.Note{}
|
||||
}
|
||||
|
||||
// 如果自己的blog状态是true, 不用改变,
|
||||
// 否则, 如果notebookId的blog是true, 则改为true之
|
||||
// 返回blog状态
|
||||
func (this *NoteService) updateToNotebookBlog(noteId, notebookId, userId string) bool {
|
||||
if this.IsBlog(noteId) {
|
||||
return true
|
||||
}
|
||||
if notebookService.IsBlog(notebookId) {
|
||||
db.UpdateByIdAndUserId(db.Notes, noteId, userId,
|
||||
bson.M{"$set": bson.M{"IsBlog": true}})
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
// 判断是否是blog
|
||||
func (this *NoteService) IsBlog(noteId string) bool {
|
||||
note := info.Note{}
|
||||
db.GetByQWithFields(db.Notes, bson.M{"_id": bson.ObjectIdHex(noteId)}, []string{"IsBlog"}, ¬e);
|
||||
return note.IsBlog
|
||||
}
|
||||
|
||||
// 复制note
|
||||
// 正常的可以用
|
||||
// 先查, 再新建
|
||||
// 要检查下notebookId是否是自己的
|
||||
func (this *NoteService) CopyNote(noteId, notebookId, userId string) info.Note {
|
||||
if notebookService.IsMyNotebook(notebookId, userId) {
|
||||
note := this.GetNote(noteId, userId)
|
||||
noteContent := this.GetNoteContent(noteId, userId)
|
||||
|
||||
// 重新生成noteId
|
||||
note.NoteId = bson.NewObjectId();
|
||||
note.NotebookId = bson.ObjectIdHex(notebookId)
|
||||
|
||||
noteContent.NoteId = note.NoteId
|
||||
this.AddNoteAndContent(note, noteContent, note.UserId);
|
||||
|
||||
// 更新blog状态
|
||||
isBlog := this.updateToNotebookBlog(note.NoteId.Hex(), notebookId, userId)
|
||||
|
||||
note.IsBlog = isBlog
|
||||
return note
|
||||
}
|
||||
|
||||
return info.Note{}
|
||||
}
|
||||
|
||||
// 复制别人的共享笔记给我
|
||||
// TODO 判断是否共享了给我
|
||||
func (this *NoteService) CopySharedNote(noteId, notebookId, fromUserId, myUserId string) info.Note {
|
||||
if notebookService.IsMyNotebook(notebookId, myUserId) {
|
||||
note := this.GetNote(noteId, fromUserId)
|
||||
if note.NoteId == "" {
|
||||
return info.Note{}
|
||||
}
|
||||
noteContent := this.GetNoteContent(noteId, fromUserId)
|
||||
|
||||
// 重新生成noteId
|
||||
note.NoteId = bson.NewObjectId();
|
||||
note.NotebookId = bson.ObjectIdHex(notebookId)
|
||||
note.UserId = bson.ObjectIdHex(myUserId)
|
||||
note.IsTop = false
|
||||
note.IsBlog = false // 别人的可能是blog
|
||||
|
||||
// content
|
||||
noteContent.NoteId = note.NoteId
|
||||
noteContent.UserId = note.UserId
|
||||
|
||||
// 添加之
|
||||
note = this.AddNoteAndContent(note, noteContent, note.UserId);
|
||||
|
||||
// 更新blog状态
|
||||
isBlog := this.updateToNotebookBlog(note.NoteId.Hex(), notebookId, myUserId)
|
||||
|
||||
note.IsBlog = isBlog
|
||||
return note
|
||||
}
|
||||
|
||||
return info.Note{}
|
||||
}
|
||||
|
||||
// 通过noteId得到notebookId
|
||||
// shareService call
|
||||
// [ok]
|
||||
func (this *NoteService) GetNotebookId(noteId string) bson.ObjectId {
|
||||
note := &info.Note{}
|
||||
db.Get(db.Notes, noteId, note)
|
||||
return note.NotebookId
|
||||
}
|
||||
|
||||
//------------------
|
||||
// 搜索Note
|
||||
func (this *NoteService) SearchNote(key, userId string, pageNumber, pageSize int, sortField string, isAsc, isBlog bool) (count int, notes []info.Note) {
|
||||
notes = []info.Note{}
|
||||
skipNum, sortFieldR := parsePageAndSort(pageNumber, pageSize, sortField, isAsc)
|
||||
|
||||
// 不是trash的
|
||||
query := bson.M{"UserId": bson.ObjectIdHex(userId),
|
||||
"IsTrash": false,
|
||||
"Title": bson.M{"$regex": bson.RegEx{".*?" + key + ".*", "i"}},
|
||||
}
|
||||
if isBlog {
|
||||
query["IsBlog"] = true
|
||||
}
|
||||
q := db.Notes.Find(query);
|
||||
|
||||
// 总记录数
|
||||
count, _ = q.Count()
|
||||
|
||||
q.Sort(sortFieldR).
|
||||
Skip(skipNum).
|
||||
Limit(pageSize).
|
||||
All(¬es)
|
||||
|
||||
// 如果 < pageSize 那么搜索content, 且id不在这些id之间的
|
||||
if len(notes) < pageSize {
|
||||
notes = this.searchNoteFromContent(notes, userId, key, pageSize, sortFieldR, isBlog)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 搜索noteContents, 补集pageSize个
|
||||
func (this *NoteService) searchNoteFromContent(notes []info.Note, userId, key string, pageSize int, sortField string, isBlog bool) []info.Note {
|
||||
var remain = pageSize - len(notes)
|
||||
noteIds := make([]bson.ObjectId, len(notes))
|
||||
for i, note := range notes {
|
||||
noteIds[i] = note.NoteId
|
||||
}
|
||||
LogJ(noteIds)
|
||||
noteContents := []info.NoteContent{}
|
||||
query := bson.M{"_id": bson.M{"$nin": noteIds}, "UserId": bson.ObjectIdHex(userId), "Content": bson.M{"$regex": bson.RegEx{".*?" + key + ".*", "i"}}}
|
||||
if isBlog {
|
||||
query["IsBlog"] = true
|
||||
}
|
||||
db.NoteContents.
|
||||
Find(query).
|
||||
Sort(sortField).
|
||||
Limit(remain).
|
||||
Select(bson.M{"_id": true}).
|
||||
All(¬eContents)
|
||||
var lenContent = len(noteContents)
|
||||
if(lenContent == 0) {
|
||||
return notes
|
||||
}
|
||||
|
||||
// 收集ids
|
||||
noteIds2 := make([]bson.ObjectId, lenContent)
|
||||
for i, content := range noteContents {
|
||||
noteIds2[i] = content.NoteId
|
||||
}
|
||||
|
||||
// Log(" content search ")
|
||||
// Log(lenContent)
|
||||
|
||||
// 得到notes
|
||||
notes2 := this.ListNotesByNoteIds(noteIds2)
|
||||
|
||||
// 合并之
|
||||
notes = append(notes, notes2...)
|
||||
return notes
|
||||
}
|
||||
|
||||
//----------------
|
||||
// tag搜索
|
||||
func (this *NoteService) SearchNoteByTags(tags []string, userId string, pageNumber, pageSize int, sortField string, isAsc bool) (count int, notes []info.Note) {
|
||||
notes = []info.Note{}
|
||||
skipNum, sortFieldR := parsePageAndSort(pageNumber, pageSize, sortField, isAsc)
|
||||
|
||||
// 不是trash的
|
||||
query := bson.M{"UserId": bson.ObjectIdHex(userId),
|
||||
"IsTrash": false,
|
||||
"Tags": bson.M{"$all": tags}}
|
||||
|
||||
q := db.Notes.Find(query);
|
||||
|
||||
// 总记录数
|
||||
count, _ = q.Count()
|
||||
|
||||
Log(count)
|
||||
|
||||
q.Sort(sortFieldR).
|
||||
Skip(skipNum).
|
||||
Limit(pageSize).
|
||||
All(¬es)
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user