2019-07-19 13:51:57 -07:00
|
|
|
var express = require('express')
|
|
|
|
var router = express.Router()
|
|
|
|
|
2020-05-18 00:45:35 -07:00
|
|
|
let Note = require('@models/Note')
|
2020-05-10 14:15:59 -07:00
|
|
|
let User = require('@models/User')
|
|
|
|
let ShareNote = require('@models/ShareNote')
|
2020-02-01 14:21:22 -08:00
|
|
|
|
2019-07-19 13:51:57 -07:00
|
|
|
let userId = null
|
2020-05-06 00:10:27 -07:00
|
|
|
let masterKey = null
|
2019-07-19 13:51:57 -07:00
|
|
|
|
|
|
|
// middleware that is specific to this router
|
|
|
|
router.use(function setUserId (req, res, next) {
|
2020-02-01 14:21:22 -08:00
|
|
|
if(req.headers.userId){
|
2019-07-19 13:51:57 -07:00
|
|
|
userId = req.headers.userId
|
2020-05-06 00:10:27 -07:00
|
|
|
masterKey = req.headers.masterKey
|
2020-02-01 14:21:22 -08:00
|
|
|
}
|
2019-07-19 13:51:57 -07:00
|
|
|
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
|
2020-02-10 09:44:43 -08:00
|
|
|
//
|
|
|
|
// Note actions
|
|
|
|
//
|
2019-07-19 13:51:57 -07:00
|
|
|
router.post('/get', function (req, res) {
|
2020-05-18 00:45:35 -07:00
|
|
|
Note.get(userId, req.body.noteId, masterKey)
|
2020-02-01 14:21:22 -08:00
|
|
|
.then( data => {
|
|
|
|
res.send(data)
|
|
|
|
})
|
2019-07-19 13:51:57 -07:00
|
|
|
})
|
|
|
|
|
2019-07-29 00:22:47 -07:00
|
|
|
router.post('/delete', function (req, res) {
|
2020-05-18 20:38:43 -07:00
|
|
|
Note.delete(userId, req.body.noteId, masterKey)
|
2019-07-29 00:22:47 -07:00
|
|
|
.then( data => res.send(data) )
|
|
|
|
})
|
|
|
|
|
2019-07-19 13:51:57 -07:00
|
|
|
router.post('/create', function (req, res) {
|
2020-05-18 00:45:35 -07:00
|
|
|
Note.create(userId, req.body.title, req.body.text, masterKey)
|
2019-07-19 13:51:57 -07:00
|
|
|
.then( id => res.send({id}) )
|
|
|
|
})
|
|
|
|
|
|
|
|
router.post('/update', function (req, res) {
|
2020-05-18 00:45:35 -07:00
|
|
|
Note.update(userId, req.body.noteId, req.body.text, req.body.title, req.body.color, req.body.pinned, req.body.archived, req.body.hash, masterKey)
|
2019-07-19 13:51:57 -07:00
|
|
|
.then( id => res.send({id}) )
|
|
|
|
})
|
|
|
|
|
|
|
|
router.post('/search', function (req, res) {
|
2020-05-18 00:45:35 -07:00
|
|
|
Note.search(userId, req.body.searchQuery, req.body.searchTags, req.body.fastFilters, masterKey)
|
|
|
|
.then( NoteAndTags => {
|
|
|
|
res.send(NoteAndTags)
|
2020-02-01 14:21:22 -08:00
|
|
|
})
|
2019-07-19 13:51:57 -07:00
|
|
|
})
|
|
|
|
|
2020-01-02 17:26:55 -08:00
|
|
|
router.post('/difftext', function (req, res) {
|
2020-05-18 00:45:35 -07:00
|
|
|
Note.getDiffText(userId, req.body.noteId, req.body.text, req.body.updated)
|
2020-01-02 17:26:55 -08:00
|
|
|
.then( fullDiffText => {
|
|
|
|
//Response should be full diff text
|
|
|
|
res.send(fullDiffText)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-06 00:10:27 -07:00
|
|
|
router.post('/reindex', function (req, res) {
|
2020-05-18 00:45:35 -07:00
|
|
|
Note.reindex(userId, masterKey)
|
2020-05-06 00:10:27 -07:00
|
|
|
.then( data => {
|
|
|
|
res.send(data)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2020-03-01 21:33:49 -08:00
|
|
|
//
|
|
|
|
// Update single note attributes
|
|
|
|
//
|
|
|
|
router.post('/setpinned', function (req, res) {
|
2020-05-18 00:45:35 -07:00
|
|
|
Note.setPinned(userId, req.body.noteId, req.body.pinned)
|
2020-03-01 21:33:49 -08:00
|
|
|
.then( results => {
|
|
|
|
res.send(results)
|
|
|
|
})
|
|
|
|
})
|
2020-03-10 20:47:07 -07:00
|
|
|
router.post('/setarchived', function (req, res) {
|
2020-05-18 00:45:35 -07:00
|
|
|
Note.setArchived(userId, req.body.noteId, req.body.archived)
|
|
|
|
.then( results => {
|
|
|
|
res.send(results)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
router.post('/settrashed', function (req, res) {
|
2020-05-18 20:38:43 -07:00
|
|
|
Note.setTrashed(userId, req.body.noteId, req.body.trashed, masterKey)
|
2020-03-10 20:47:07 -07:00
|
|
|
.then( results => {
|
|
|
|
res.send(results)
|
|
|
|
})
|
|
|
|
})
|
2020-03-01 21:33:49 -08:00
|
|
|
|
2020-02-10 09:44:43 -08:00
|
|
|
//
|
|
|
|
// Share Note Actions
|
|
|
|
//
|
|
|
|
router.post('/getshareusers', function (req, res) {
|
|
|
|
ShareNote.getUsers(userId, req.body.rawTextId)
|
|
|
|
.then(results => res.send(results))
|
|
|
|
})
|
|
|
|
|
|
|
|
router.post('/shareadduser', function (req, res) {
|
2020-05-10 14:15:59 -07:00
|
|
|
// ShareNote.addUser(userId, req.body.noteId, req.body.rawTextId, req.body.username, masterKey)
|
|
|
|
User.getByUserName(req.body.username)
|
|
|
|
.then( user => {
|
|
|
|
return ShareNote.migrateNoteToShared(userId, req.body.noteId, user.id, masterKey)
|
|
|
|
})
|
2020-02-13 17:08:46 -08:00
|
|
|
.then( ({success, shareUserId}) => {
|
|
|
|
|
|
|
|
res.send(success)
|
|
|
|
})
|
2020-02-10 09:44:43 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
router.post('/shareremoveuser', function (req, res) {
|
2020-05-18 00:45:35 -07:00
|
|
|
// (userId, noteId, shareNoteUserId, shareUserId, masterKey)
|
|
|
|
ShareNote.removeUserFromShared(userId, req.body.noteId, req.body.shareUserNoteId, masterKey)
|
2020-02-10 09:44:43 -08:00
|
|
|
.then(results => res.send(results))
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Testing Action
|
|
|
|
//
|
2020-05-18 00:45:35 -07:00
|
|
|
//Reindex all Note. Not a very good function, not public
|
2019-12-19 21:50:50 -08:00
|
|
|
router.get('/reindex5yu43prchuj903mrc', function (req, res) {
|
|
|
|
|
2020-05-18 00:45:35 -07:00
|
|
|
Note.migrateNoteTextToNewTable().then(status => {
|
2020-02-10 09:44:43 -08:00
|
|
|
return res.send(status)
|
|
|
|
})
|
2020-02-01 14:21:22 -08:00
|
|
|
|
2019-12-19 21:50:50 -08:00
|
|
|
})
|
2019-07-19 13:51:57 -07:00
|
|
|
|
|
|
|
|
|
|
|
module.exports = router
|