Skip to content

Commit 2973bfb

Browse files
committed
Add list-my-note API
Signed-off-by: James Tsai <[email protected]>
1 parent 7b14845 commit 2973bfb

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

lib/note/index.js

+50-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
const config = require('../config')
44
const logger = require('../logger')
5-
65
const { Note, User } = require('../models')
76

8-
const { newCheckViewPermission, errorForbidden, responseCodiMD, errorNotFound } = require('../response')
7+
const { newCheckViewPermission, errorForbidden, responseCodiMD, errorNotFound, errorInternalError } = require('../response')
98
const { updateHistory } = require('../history')
109
const { actionPublish, actionSlide, actionInfo, actionDownload, actionPDF, actionGist, actionRevision, actionPandoc } = require('./noteActions')
1110

@@ -190,6 +189,55 @@ async function noteActions (req, res) {
190189
}
191190
}
192191

192+
async function getMyNoteList(userid, callback) {
193+
const myNotes = await Note.findAll({
194+
where: {
195+
ownerId: userid
196+
}
197+
})
198+
if (!myNotes) {
199+
return callback(null, null)
200+
}
201+
try {
202+
let myNoteList = []
203+
for (let i = 0; i < myNotes.length; i++) {
204+
const note = myNotes[i]
205+
myNoteList[i] = {
206+
id: Note.encodeNoteId(note.id),
207+
text: note.title,
208+
tags: Note.parseNoteInfo(note.content).tags,
209+
createdAt: note.createdAt,
210+
lastchangeAt: note.lastchangeAt,
211+
shortId: note.shortid
212+
}
213+
}
214+
if (config.debug) {
215+
logger.info('Parse myNoteList success: ' + userid)
216+
}
217+
return callback(null, myNoteList)
218+
} catch (err){
219+
logger.error('Parse myNoteList failed')
220+
return callback(err, null)
221+
}
222+
}
223+
224+
function listMyNotes(req, res) {
225+
if (req.isAuthenticated()) {
226+
getMyNoteList(req.user.id, (err, myNoteList) => {
227+
if (err) return errorInternalError(req, res)
228+
if (!myNoteList) return errorNotFound(req, res)
229+
res.send({
230+
myNotes: myNoteList
231+
})
232+
})
233+
} else {
234+
return errorForbidden(req, res)
235+
}
236+
}
237+
193238
exports.showNote = showNote
194239
exports.showPublishNote = showPublishNote
195240
exports.noteActions = noteActions
241+
exports.listMyNotes = listMyNotes
242+
243+

lib/routes.js

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ appRouter.get('/s/:shortid/:action', response.publishNoteActions)
7070
appRouter.get('/p/:shortid', response.showPublishSlide)
7171
// publish slide actions
7272
appRouter.get('/p/:shortid/:action', response.publishSlideActions)
73+
// gey my note list
74+
appRouter.get('/myNotes', noteController.listMyNotes)
7375
// get note by id
7476
appRouter.get('/:noteId', wrap(noteController.showNote))
7577
// note actions

0 commit comments

Comments
 (0)