-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.js
206 lines (160 loc) · 4.42 KB
/
notes.js
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
var Util = require('./util');
var Db = require('mongodb').Db,
MongoClient = require('mongodb').MongoClient,
ObjectID = require('mongodb').ObjectID,
Server = require('mongodb').Server,
crypto = require('crypto'),
Zip = require('node-zip'),
log4js = require('log4js'),
logger = log4js.getLogger();
// See docs @ http://mongodb.github.io/node-mongodb-native/
var Notes = Util.Class({
init: function(db) {
this._db = db;
},
getTree: function(req, resp) {
var folders = this._db.collection('folders');
var notes = this._db.collection('notes');
var tree = [];
var c = 0, ready = false;
folders.find({user: req.session.username}).toArray(function(err, docs) {
if (err) {
logger.error(err);
resp.end(500, "Error retrieving tree");
return;
}
docs.forEach(function(folderItem) {
var o = {
_id: folderItem._id,
name: folderItem.name,
colour: folderItem.colour,
folder: true
};
c++;
notes.find({parent: folderItem._id, user: req.session.username}).toArray(function(err, noteDocs) {
c--;
if (!o.items) o.items = [];
noteDocs.forEach(function(noteItem) {
o.items.push({
_id: noteItem._id,
name: noteItem.name,
content: noteItem.content
});
}.bind(this));
if (ready && !c) resp.end(JSON.stringify(tree));
});
tree.push(o);
}.bind(this));
c++;
notes.find({user: req.session.username}).toArray(function(err, noteDocs) {
c--;
noteDocs.forEach(function(noteItem) {
if (noteItem.parent) return;
tree.push({
_id: noteItem._id,
name: noteItem.name,
content: noteItem.content
});
}.bind(this))
if (ready && !c) resp.end(JSON.stringify(tree));
});
ready = true;
}.bind(this));
},
dump: function(req, resp) {
var notes = this._db.collection('notes');
notes.find({user: req.session.username}).toArray(function(err, noteDocs) {
var zip = new Zip();
noteDocs.forEach(function(noteItem) {
zip.file(noteItem.name + '.md', noteItem.content);
}.bind(this))
resp.end(zip.generate({base64: false, compression: 'DEFLATE'}), 'binary');
});
},
addNote: function(req, resp) {
// name, parent
var notes = this._db.collection('notes');
var o = {
name: req.body.name,
parent: req.body.parent,
content: '',
user: req.session.username
};
// _id generated automatically
notes.insert(o, {w: 1}, function(err, result) {
resp.end(JSON.stringify(o));
});
},
deleteNote: function(req, resp) {
var notes = this._db.collection('notes');
notes.remove({_id: new ObjectID(req.body.note)}, true);
resp.end(JSON.stringify({
success: true
}));
},
getContent: function(req, resp) {
var notes = this._db.collection('notes');
notes.findOne({_id: new ObjectID(req.body.note), user: req.session.username}, function(err, item) {
if (err || !item) {
logger.error(err);
resp.end(500, "Could not retrieve content");
return;
}
resp.end(JSON.stringify(item));
}.bind(this));
},
saveContent: function(req, resp) {
if (!req || !req.body || !req.body.note) {
resp.end(JSON.stringify({
success: true
}));
return;
}
var notes = this._db.collection('notes');
notes.findOne({_id: new ObjectID(req.body.note), user: req.session.username}, function(err, item) {
if (err || !item) {
logger.error(err);
resp.end(JSON.stringify({
error: err || 'Object is null'
}));
return;
}
item.content = req.body.content;
notes.save(item);
resp.end(JSON.stringify({
success: true
}));
}.bind(this));
},
saveName: function(req, resp) {
var notes = this._db.collection('notes');
notes.findOne({_id: new ObjectID(req.body.note), user: req.session.username}, function(err, item) {
if (err || !item) {
logger.error(err);
resp.end(500, "Could not save note name");
return;
}
item.name = req.body.name;
notes.save(item);
resp.end(JSON.stringify({
success: true
}));
}.bind(this));
},
move: function(req, resp) {
var notes = this._db.collection('notes');
notes.findOne({_id: new ObjectID(req.body.note), user: req.session.username}, function(err, item) {
if (err || !item) {
logger.error(err);
resp.end(500, "Could not move note");
return;
}
item.parent = req.body.parent ? new ObjectID(req.body.parent) : null;
notes.save(item);
resp.end(JSON.stringify({
success: true
}));
}.bind(this));
}
});
module.exports = Notes;