|
2 | 2 |
|
3 | 3 | const config = require('../config')
|
4 | 4 | const logger = require('../logger')
|
5 |
| - |
6 | 5 | const { Note, User } = require('../models')
|
7 | 6 |
|
8 |
| -const { newCheckViewPermission, errorForbidden, responseCodiMD, errorNotFound } = require('../response') |
| 7 | +const { newCheckViewPermission, errorForbidden, responseCodiMD, errorNotFound, errorInternalError } = require('../response') |
9 | 8 | const { updateHistory } = require('../history')
|
10 | 9 | const { actionPublish, actionSlide, actionInfo, actionDownload, actionPDF, actionGist, actionRevision, actionPandoc } = require('./noteActions')
|
11 | 10 |
|
@@ -190,6 +189,55 @@ async function noteActions (req, res) {
|
190 | 189 | }
|
191 | 190 | }
|
192 | 191 |
|
| 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 | + |
193 | 238 | exports.showNote = showNote
|
194 | 239 | exports.showPublishNote = showPublishNote
|
195 | 240 | exports.noteActions = noteActions
|
| 241 | +exports.listMyNotes = listMyNotes |
| 242 | + |
| 243 | + |
0 commit comments