Skip to content
Navigation Menu
{{ message }}
forked from leanote/desktop-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnote.js
More file actions
1896 lines (1712 loc) · 50.8 KB
/
Copy pathnote.js
File metadata and controls
1896 lines (1712 loc) · 50.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var async = require('async');
var db = require('db');
var fs = require('fs');
var File = require('file');
var Evt = require('evt');
var User = require('user');
var Notebook = require('notebook');
// var Tag = require('tag');
// var Api = require('api');
var Common = require('common');
var Web = require('web');
// var Notes = db.notes;
var Api = null; // require('api')
var Tag = null;
function log(o) {
console.trace(o);
}
// Web.alertWeb('alert(process.type);');
/*
type NoteOrContent struct {
NotebookId string
NoteId string
UserId string
Title string
Desc string
ImgSrc string
Tags []string
Content string
Abstract string
IsNew bool
IsMarkdown bool
FromUserId string // 为共享而新建
IsBlog bool // 是否是blog, 更新note不需要修改, 添加note时才有可能用到, 此时需要判断notebook是否设为Blog
}
*/
// 笔记服务
var Note = {
// 更新笔记
updateNoteOrContent: function(noteOrContent, callback, needAddToHistory) {
var me = this;
// Web.alertWeb(process.type); // render
var userId = User.getCurActiveUserId();
noteOrContent['UserId'] = userId;
var date = new Date();
if (!Common.isValidDate(noteOrContent.UpdatedTime)) {
noteOrContent.UpdatedTime = date;
}
noteOrContent['IsDirty'] = true; // 已修改
noteOrContent['LocalIsDelete'] = false;
// 为什么不以noteOrContent.IsNew来判断是否是新建还是更新?
// 怕前端重复发请求, 保险起见
me.getNote(noteOrContent.NoteId, function(dbNote) {
// 新建的
if (!dbNote) {
// 新建笔记, IsNew还是保存着
if(noteOrContent.IsNew) {
if (!Common.isValidDate(noteOrContent.CreatedTime)) {
noteOrContent.CreatedTime = date;
}
noteOrContent['IsTrash'] = false;
delete noteOrContent['IsNew'];
noteOrContent['LocalIsNew'] = true;
db.notes.insert(noteOrContent, function (err, newDoc) { // Callback is optional
if(err) {
console.log(err);
callback && callback(false);
} else {
// 为什么又设置成true, 因为js的对象是共享的, callback用到了noteOrContent.IsNew来做判断
noteOrContent['IsNew'] = true;
callback && callback(newDoc);
// 重新统计笔记本的笔记数量
Notebook.reCountNotebookNumberNotes(noteOrContent.NotebookId);
me.addNoteHistory(noteOrContent.NoteId, noteOrContent.Content);
}
});
}
else {
callback && callback(false);
}
}
// 更新
else {
var updateFields = ['Desc', 'ImgSrc', 'Title', 'Tags', 'Content'];
var updates = {};
var needUpdate = false;
for(var i in updateFields) {
var field = updateFields[i];
if(field in noteOrContent) {
updates[field] = noteOrContent[field];
needUpdate = true;
}
}
if(needUpdate) {
var isDirty = false;
// 只有title, Content, Tags修改了才算是IsDirty
if('Content' in updates && dbNote['Content'] != updates['Content']) {
isDirty = true;
// console.error(' content not same');
// ContentIsDirty 才会发Content
updates['ContentIsDirty'] = true;
} else if('Title' in updates && dbNote['Title'] != updates['Title']) {
isDirty = true;
// console.error(' title not same');
} else if('Tags' in updates) {
var dbTags = dbNote['Tags'] || [];
var nowTags = updates['Tags'] || [];
if(dbTags.join(',') != nowTags.join(',')) {
isDirty = true;
// console.error(' tag not same');
}
}
// 没有任何修改
if(!isDirty) {
console.log('没有任何修改, 不保存');
return callback && callback(dbNote);
}
updates['IsDirty'] = isDirty;
updates['LocalIsDelete'] = false;
if(isDirty) {
updates.UpdatedTime = date;
}
// console.log('finally update:');
// console.log(updates);
// Set an existing field's value
db.notes.update({NoteId: noteOrContent.NoteId}, { $set: updates }, {}, function (err, numReplaced) {
if(err) {
callback && callback(false);
} else {
callback && callback(noteOrContent);
if('Content' in updates) {
// 需要添加到历史记录中才加
if (needAddToHistory) {
me.addNoteHistory(noteOrContent.NoteId, noteOrContent.Content);
}
}
}
});
}
else {
callback && callback(false);
}
}
});
},
// 公开/取消为博客
setNote2Blog: function(noteIds, isBlog, callback) {
var me = this;
var ok = false;
async.eachSeries(noteIds, function(noteId, cb) {
me._setNote2Blog(noteId, isBlog, function (ret) {
if (ret) {
ok = true;
}
cb();
});
}, function () {
callback(ok);
});
},
_setNote2Blog: function(noteId, isBlog, callback) {
var me = this;
me.getNote(noteId, function(note) {
if(note) {
if(note.IsBlog == isBlog) {
return callback && callback(true);
}
// 更新, 设置isDirty
db.notes.update({_id: note._id}, { $set: {IsBlog: isBlog, IsDirty: true} }, {}, function (err, numReplaced) {
return callback && callback(true);
});
} else {
return callback && callback(false);
}
});
},
// 添加笔记历史
/*
type NoteContentHistory struct {
NoteId bson.ObjectId `bson:"_id,omitempty"`
UserId bson.ObjectId `bson:"UserId"` // 所属者
Histories []EachHistory `Histories`
}
*/
addNoteHistory: function(noteId, content, callback) {
var me = this;
db.noteHistories.loadDB(function (ok) {
if (!ok) {
callback && callback(false);
return;
}
// 先判断是否存在, 不存在则新建之
db.noteHistories.findOne({_id: noteId}, function(err, history) {
var now = new Date();
// 新建之
if(!history) {
db.noteHistories.insert({_id: noteId, Histories: [{Content: content, "UpdatedTime": now}], "UpdatedTime": now}, function () {
callback && callback(true);
});
}
// 更新之
else {
var histories = history.Histories;
// 如果与前一个内容一样, 则不加入历史中
if (histories
&& histories[histories.length - 1].Content
&& histories[histories.length - 1].Content == content) {
return (callback && callback(true));
}
histories.push({Content: content, UpdatedTime: now});
// 不能多了, 多了有麻烦, 20个最多
var max = 20;
var len = histories.length;
if (len > max) {
for (var i = 0; i < len - max; ++i) {
histories.shift();
}
}
db.noteHistories.update({_id: noteId}, {$set: {Histories: histories}}, function () {
callback && callback(true);
});
}
});
});
},
// 删除笔记时, 删除历史记录
deleteNoteHistory: function (noteId) {
db.noteHistories.remove({_id: noteId});
},
// 获取笔记历史记录
getNoteHistories: function(noteId, callback) {
var me = this;
db.noteHistories.loadDB(function (ok) {
if (!ok) {
callback(false);
return;
}
db.noteHistories.findOne({_id: noteId}, function(err, doc) {
if(err || !doc) {
callback([]);
}
else {
var histories = [];
for(var i = doc.Histories.length - 1; i >= 0; --i) {
var history = doc.Histories[i];
var content, updatedTime;
if (typeof history == 'object') {
content = history.Content;
updatedTime = history.UpdatedTime;
}
else {
content = history;
updatedTime = doc.UpdatedTime || new Date();
}
histories.push({Content: content, UpdatedTime: updatedTime});
}
callback(histories);
}
});
});
},
// 获取笔记列表
getNotes: function(notebookId, callback) {
var me = this;
me._getNotes(notebookId, false, false, callback);
},
// 获取trash笔记
getTrashNotes: function(callback) {
var me = this;
me._getNotes('', true, false, callback);
},
getStarNotes: function(callback) {
var me = this;
me._getNotes('', false, true, callback);
},
_getNotes: function(notebookId, isTrash, isStar, callback) {
var userId = User.getCurActiveUserId();
var query = {
UserId: userId,
// 现在还不明确为什么会有IsDeleted的笔记
$or:[
{IsDeleted: {$exists: false}},
{IsDeleted: false}
],
IsTrash: false,
LocalIsDelete: false, // 未删除的
};
if(isStar) {
query['Star'] = true;
}
if(notebookId) {
query['NotebookId'] = notebookId;
}
if(isTrash) {
query['IsTrash'] = true;
}
db.notes.find(query).sort({'UpdatedTime': -1}).exec(function(err, notes) {
if(err) {
log(err);
return callback && callback(false);
}
return callback && callback(notes);
});
},
searchNote: function(key, callback) {
var reg = new RegExp(key, 'i');
var userId = User.getCurActiveUserId();
db.notes.find({UserId: userId, IsTrash: false, LocalIsDelete: false, $or: [{Title: reg}, {Content: reg}]}).sort({'UpdatedTime': -1}).exec(function(err, notes) {
if(!err && notes) {
console.log('search ' + key + ' result: ' + notes.length);
callback(notes);
} else {
callback([]);
}
});
},
searchNoteByTag: function(tag, callback) {
var userId = User.getCurActiveUserId();
db.notes.find({UserId: userId, IsTrash: false, LocalIsDelete: false, Tags: {$in: [tag]}}).sort({'UpdatedTime': -1}).exec(function(err, notes) {
if(!err && notes) {
console.log('search by tag: ' + tag + ' result: ' + notes.length);
callback(notes);
} else {
callback([]);
}
});
},
clearTrash: function(callback) {
var me = this;
var userId = User.getCurActiveUserId();
db.notes.update(
{UserId: userId, IsTrash: true},
{$set: {LocalIsDelete: true, IsDirty: true}},
{multi: true},
function(err, n) {
// Web.alertWeb(n);
callback && callback();
});
},
deleteNote: function(noteIds, callback) {
var me = this;
async.eachSeries(noteIds, function (noteId, cb) {
me._deleteNote(noteId, function () {
cb();
});
}, function () {
callback(true);
});
},
_deleteNote: function(noteId, callback) {
var me = this;
me.getNote(noteId, function(note) {
if(!note) {
callback(false);
}
// 如果已经是trash, 再删除则彻底删除
if (note.IsTrash) {
me.deleteTrash(note, callback);
return;
}
// {multi: true},
db.notes.update({NoteId: noteId}, {$set: {IsTrash: true, IsDirty: true}}, {multi: true}, function(err, n) {
if(err || !n) {
callback(false);
} else {
callback(true);
// 重新统计
Notebook.reCountNotebookNumberNotes(note.NotebookId);
}
});
});
},
// sync时调用
// 是新的, 又是deleted的, 则删除之
deleteLocalNote: function(noteId, callback) {
var me = this;
this.getNote(noteId, function(note) {
if (!note) {
callback && callback();
return;
}
db.notes.remove({NoteId: noteId}, function() {
me.deleteNoteAllOthers(note);
callback && callback();
});
});
},
// 删除笔记的所有
// 1) notes
// 2) noteHistory
// 3) imgages, attachs
deleteNoteAllOthers: function (note) {
if (!note) {
return;
}
var me = this;
// 删除图片
me.deleteImages(note);
// 删除附件
me.deleteAttachs(note.Attachs);
// 删除历史记录
me.deleteNoteHistory(note.NoteId);
},
// 彻底删除笔记, 如果有tags, 则需要更新tags's count
deleteTrash: function(note, callback) {
var me = this;
if(note) {
// 如果是本地用户, 则直接删除
if(User.isLocal()) {
db.notes.remove({_id: note._id}, {multi: true}, function(err, n) {
if(n) {
me.deleteNoteAllOthers(note);
// 如果有tags, 则重新更新tags' count
me.updateTagCount(note.Tags);
callback(true);
return;
}
callback(false);
});
}
else {
note.LocalIsDelete = true;
note.IsDirty = true;
db.notes.update({_id: note._id}, {$set: {IsDirty: true, LocalIsDelete: true}}, function(err, n) {
if(n) {
me.deleteNoteAllOthers(note);
me.updateTagCount(note.Tags);
callback(true);
return;
}
callback(false);
});
}
} else {
callback(false);
}
},
// 移动note
// 重新统计另一个notebookId的笔记数
moveNote: function(noteIds, notebookId, callback) {
var me = this;
if (Common.isEmpty(noteIds)) {
callback(false);
return;
}
var preNotebookIds = {};
async.eachSeries(noteIds, function(noteId, cb) {
me.getNote(noteId, function(note) {
if(note) {
// var to = !note.Star;
// 是原笔记本
var preNotebookId = note.NotebookId;
if (preNotebookId == notebookId && !note.IsTrash) {
cb();
return;
}
preNotebookIds[preNotebookId] = true;
note.NotebookId = notebookId;
db.notes.update({_id: note._id}, {$set: {IsDirty: true, NotebookId: notebookId, IsTrash: false, LocalIsDelete: false, UpdatedTime: new Date()}}, function(err, n) {
cb();
});
} else {
cb();
}
});
}, function () {
// 重新统计
for (var i in preNotebookIds) {
Notebook.reCountNotebookNumberNotes(i);
}
if (!preNotebookIds[notebookId]) {
Notebook.reCountNotebookNumberNotes(notebookId);
}
callback(true);
});
},
// 加星或取消
star: function(noteId, callback) {
var me = this;
me.getNote(noteId, function(note) {
if(note) {
var to = !note.Star;
db.notes.update({_id: note._id}, {$set: {Star: to, UpdatedTime: new Date()}});
callback(true, to);
}
});
},
conflictIsFixed: function(noteId) {
var me = this;
db.notes.update({NoteId: noteId}, {$set: {ConflictNoteId: ""}});
},
// 笔记本下是否有笔记
hasNotes: function(notebookId, callback) {
db.notes.count({NotebookId: notebookId, IsTrash: false, LocalIsDelete: false}, function(err, n) {
console.log(n);
if(err || n > 0) {
return callback(true);
}
callback(false);
});
},
// 得到笔记
getNote: function(noteId, callback) {
var me = this;
db.notes.findOne({NoteId: noteId}, function(err, doc) {
if(err || !doc) {
log('不存在');
callback && callback(false);
} else {
callback && callback(doc);
}
});
},
// 服务器上的数据到本地
fixNoteContent: function(content) {
if(!content) {
return content;
}
// https, http都行
var url = Evt.leanoteUrl.replace('https', 'https*');
// http://leanote.com/file/outputImage?fileId=54f9079f38f4115c0200001b
var reg0 = new RegExp(url + '/file/outputImage', 'g');
content = content.replace(reg0, Evt.getImageLocalUrlPrefix());
var reg = new RegExp(url + '/api/file/getImage', 'g');
content = content.replace(reg, Evt.getImageLocalUrlPrefix());
var reg2 = new RegExp(url + '/api/file/getAttach', 'g');
content = content.replace(reg2, Evt.getAttachLocalUrlPrefix());
var reg3 = new RegExp(url + '/attach/download?attachId', 'g');
content = content.replace(reg3, Evt.getAttachLocalUrlPrefix() + '?fileId');
// 无用
// api/file/getAllAttachs?noteId=xxxxxxxxx, 这里的noteId是服务器上的noteId啊
var reg4 = new RegExp(url + '/api/file/getAllAttachs', 'g');
content = content.replace(reg4, Evt.getAllAttachsLocalUrlPrefix());
return content;
},
// 将本地的url改下, 发送数据到服务器上
fixNoteContentForSend: function(content) {
if(!content) {
return content;
}
// console.log(Evt.localUrl + '/api/file/getImage');
// console.log(content);
var reg = new RegExp(Evt.getImageLocalUrlPrefix(), 'g');
content = content.replace(reg, Evt.leanoteUrl + '/api/file/getImage');
var reg2 = new RegExp(Evt.getAttachLocalUrlPrefix(), 'g');
content = content.replace(reg2, Evt.leanoteUrl + '/api/file/getAttach');
var reg3 = new RegExp(Evt.getAllAttachsLocalUrlPrefix(), 'g');
content = content.replace(reg3, Evt.leanoteUrl + '/api/file/getAllAttachs');
return content;
},
// 远程修改本地内容
updateNoteContentForce: function(noteId, content, callback) {
var me = this;
// 修复内容, 修改图片, 附件链接为本地链接
content = me.fixNoteContent(content);
db.notes.update({NoteId: noteId}, { $set: {Err: '', Content: content, InitSync: false, IsContentDirty: false} }, {}, function (err, numReplaced) {
if(err) {
log(err);
callback && callback(false);
} else {
callback && callback(content);
}
});
// 添加到历史中
me.addNoteHistory(noteId, content);
},
// sync调用, 用于判断是否真的有冲突
// 从服务器上获取内容
getNoteContentFromServer: function (serverNoteId, callback) {
var me = this;
if (!serverNoteId) {
callback(false);
return;
}
if (!Api) {
Api = require('api');
}
Api.getNoteContent(serverNoteId, function(noteContent) {
// 同步到本地
if(Common.isOk(noteContent)) {
var content = me.fixNoteContent(noteContent.Content);
callback(content);
} else {
callback(false);
}
});
},
// 得到笔记内容
// noteId是本地Id
inSyncContent: {}, // 正在同步中的
inSyncTimes: {}, // 10次就要再尝试了
getNoteContent: function(noteId, callback) {
var me = this;
// console.trace('getNoteContent------' + noteId);
// 如果是正在sync的话, 返回
/*
if(me.inSyncContent[noteId]) {
console.log('in sync now' + noteId); // 下周分享 node-webkit
return;
}
*/
me.inSyncContent[noteId] = true;
me.inSyncTimes[noteId]++;
if(me.inSyncTimes[noteId] > 10) {
callback && callback(false);
}
me.getNote(noteId, function(note) {
if(!Common.isOk(note)) {
me.inSyncContent[noteId] = false;
console.log('not ok');
console.log(note);
callback && callback(false);
} else {
// 如果笔记是刚同步过来的, 那么内容要重新获取
if(note.InitSync) {
console.log('need load from server');
if(!Api) {
Api = require('api')
}
var serverNoteId = note.ServerNoteId;
// 远程获取
// me.getServerNoteIdByNoteId(noteId, function(serverNoteId) {
if(!serverNoteId) {
console.error(noteId + ' getServerNoteIdByNoteId error');
me.inSyncContent[noteId] = false;
return callback && callback(false);
}
Api.getNoteContent(serverNoteId, function(noteContent) {
me.inSyncContent[noteId] = false;
// 同步到本地
if(Common.isOk(noteContent)) {
me.updateNoteContentForce(noteId, noteContent.Content, function(content) {
noteContent.Content = content;
noteContent.NoteId = noteId;
callback && callback(noteContent);
});
} else {
console.error(noteId + ' api.getNoteContent error');
// 这里, 可能太多的要同步了
setTimeout(function() {
me.getNoteContent(noteId, callback);
}, 500);
// callback && callback(false);
}
});
// });
} else {
me.inSyncContent[noteId] = false;
// console.log('not need');
callback && callback(note);
// Web.alertWeb("NONO");
}
}
});
},
//----------------
// 同步
//----------------
getNoteByServerNoteId: function(noteId, callback) {
var me = this;
db.notes.find({ServerNoteId: noteId}, function(err, doc) {
// console.log(doc.length + '...');
if(doc.length > 1) {
console.error(doc.length + '. ..');
}
// console.log('note length: ' + doc.length + '. ..');
if(err || !doc || !doc.length) {
// log('getNoteByServerNoteId 不存在' + noteId);
callback && callback(false);
} else {
doc = doc[0];
callback && callback(doc);
}
});
},
getNoteIdByServerNoteId: function(noteId, callback) {
var me = this;
db.notes.findOne({ServerNoteId: noteId}, function(err, doc) {
if(err || !doc) {
// log('getNoteIdByServerNoteId 不存在' + noteId);
callback && callback(false);
} else {
callback && callback(doc.NoteId);
}
});
},
getServerNoteIdByNoteId: function(noteId, callback) {
var me = this;
db.notes.findOne({NoteId: noteId}, function(err, doc) {
if(err || !doc) {
log('getServerNoteIdByNoteId 不存在');
callback && callback(false);
} else {
callback && callback(doc.ServerNoteId);
}
});
},
// 强制删除
// TODO 是否真的删除 ?
// 有可能服务器上删除了是误删 ?
deleteNoteForce: function(noteId, callback) {
var me = this;
me.getNoteByServerNoteId(noteId, function(note) {
if(!note) {
callback && callback(false);
return;
}
db.notes.remove({_id: note._id}, function(err, n) {
if(err) {
callback && callback(false);
} else {
me.deleteNoteAllOthers(note);
Notebook.reCountNotebookNumberNotes(note.NotebookId);
callback && callback(true);
}
});
});
},
// 添加笔记本, note object
// note是服务器传过来的, 需要处理下fix
// NoteId, ServerNoteId, NotebookId(本地的)
addNoteForce: function(note, callback) {
var me = this;
note.InitSync = true; // 刚同步完, 表示content, images, attach没有同步
note.IsDirty = false;
note.LocalIsDelete = false;
// 这里, 悲剧, 一个大BUG, 应该和server端IsTrash一致,
// 不然同步的时候将IsTrash的笔记同步到非IsTrash, 2015/10/31 fixed 谢谢 3601提供的信息
// note.IsTrash = false;
if (typeof note.IsTrash == 'boolean') {
note.IsTrash = note.IsTrash;
}
else {
note.IsTrash = false;
}
note.ServerNoteId = note.NoteId;
note.NoteId = Common.objectId();
note.CreatedTime = Common.goNowToDate(note.CreatedTime);
note.UpdatedTime = Common.goNowToDate(note.UpdatedTime);
// 附件操作
var files = note.Files || [];
var attachs = [];
for(var i in files) {
var file = files[i];
if(file.IsAttach) { // LocalFileId, FileId
file.ServerFileId = file.FileId;
file.FileId = file.ServerFileId; // 弄成一样的, 只是没有Path
attachs.push(file);
}
}
note.Attachs = attachs;
delete note['Files'];
Notebook.getNotebookIdByServerNotebookId(note.NotebookId, function(localNotebookId) {
note.NotebookId = localNotebookId;
db.notes.insert(note, function (err, newDoc) { // Callback is optional
if(err) {
console.log(err);
callback && callback(false);
} else {
// console.log("?????????")
// console.log(note);
// console.log(note.CreatedTime);
callback && callback(newDoc);
// 添加到历史中
// 因为没有内容, 所以不用添加
// me.addNoteHistory(note.NoteId, note.Content);
// 重新统计
Notebook.reCountNotebookNumberNotes(note.NotebookId);
// 下载内容, 图片, 附件
// 添加时不要, 这个请求由前端发出
// me.syncContentAndImagesAndAttachs(newDoc, 2000);
}
});
});
},
// send change后, 发现内容是一样的, 此时修改本地Usn和server的一样, 下次再push
updateNoteUsn: function (noteId, usn) {
db.notes.update({NoteId: note.NoteId}, {$set: {Usn: usn, IsDirty: true}});
},
// sync <- 时
// 更新笔记, 合并之, 内容要重新获取
// note是服务器传过来的, 需要处理下fix
// note.NoteId是服务器的
// needReloadContent 内容是否需要重新加载, 如果处理冲突没有冲突, 已有内容, 不用更新, 只是把其它的覆盖
updateNoteForce: function(note, callback, needReloadContent) {
var me = this;
if(needReloadContent === undefined) {
needReloadContent = true;
}
// console.log('...', JSON.stringify(note))
note.IsDirty = false;
note.InitSync = needReloadContent;
note.LocalIsNew = false;
note.LocalIsDelete = false;
note.ContentIsDirty = false;
// 附件处理
var files = note.Files || [];
var attachsMap = [];
for(var i in files) {
var file = files[i];
if(file.IsAttach) { // LocalFileId, FileId
// 对于服务器上的, 只有FileId会传过来, 此时要与之前的做对比
file.ServerFileId = file.FileId;
delete file['FileId'];
attachsMap[file.ServerFileId] = file;
}
}
// 之前也是有attachs的, 得到之前的attachs, 进行个merge
// TODO, 这里, 如果serverNoteId有两个一样的, 就有问题了, 待重现
me.getNoteByServerNoteId(note.NoteId, function(everNote) {
if(!everNote) {
return;
}
var everAttachs = everNote.Attachs;
var everAttachsMap = {};
// var needAdds = [];
// 得到要删除的
var needDeletes = [];
for(var i in everAttachs) {
var everAttach = everAttachs[i];
everAttachsMap[everAttach.ServerFileId] = everAttach;
if(!attachsMap[everAttach.ServerFileId]) {
needDeletes.push(everAttach);
}
}
// console.log('everAttachs');
// console.log(everAttachs);
// console.log('attachsMap')
// console.log(attachsMap);
// 通过FileId删除文件
me.deleteAttachs(needDeletes);
// 得到要添加的,所有的
// 新添加的没有Path
var allAttachs = [];
for(var serverFileId in attachsMap) {
if(!everAttachsMap[serverFileId]) {
// needAdds.push(attachMap[serverFileId]);
attachsMap[serverFileId].FileId = serverFileId; // 生成一个Id(一样的), 但是没有Path
allAttachs.push(attachsMap[serverFileId]);
} else {
allAttachs.push(everAttachsMap[serverFileId]);
}
}
// console.log('allAttachs', allAttachs);
note.Attachs = allAttachs;
note.ServerNoteId = note.NoteId;
note.NoteId = everNote.NoteId;
delete note['Files'];
// console.log('evernote');
// console.log(everNote);
// 得到本地笔记本Id
Notebook.getNotebookIdByServerNotebookId(note.NotebookId, function(localNotebookId) {
note['NotebookId'] = localNotebookId;
// console.log("updateNoteForce 后的")
// console.log(note);
// console.log(note.ServerNoteId + " " + note.IsDirty);
// console.log('ever note');
// console.log(everNote.NoteId);
// console.log(everNote);
// 不要服务器上的
delete note['UpdatedTime'];
delete note['CreatedTime'];
note.Err = '';
db.notes.update({NoteId: note.NoteId}, {$set: note}, {}, function (err, cnt) { // Callback is optional
// console.log('re:');
// console.log(err);
// console.log(cnt);
if(err) {
console.error(err);
callback && callback(false);
} else {
console.log(' 强制更新本地笔记...', note.Title);
callback && callback(note);
/*
me.getNoteByServerNoteId(note.ServerNoteId, function(t) {
console.log('强制更新后的...');
console.log(t);
});
*/
// 重新统计之
Notebook.reCountNotebookNumberNotes(note.NotebookId);
// 下载内容, 图片, 附件
// console.log('..', note)
// console.log(JSON.stringify(note), note.Attachs)
me.syncContentAndImagesAndAttachs(note);
}
});
});
});
},
You can’t perform that action at this time.
