From 12d5ed43a7376e0ca361160698f07066218d6ed2 Mon Sep 17 00:00:00 2001 From: Wu Cheng-Han Date: Mon, 10 Oct 2016 21:04:24 +0800 Subject: Update to support delete note --- .../20161009040430-support-delete-note.js | 11 ++++++++ lib/models/note.js | 1 + lib/realtime.js | 29 ++++++++++++++++++++++ public/js/index.js | 15 ++++++++++- public/views/body.ejs | 22 ++++++++++++++++ 5 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 lib/migrations/20161009040430-support-delete-note.js diff --git a/lib/migrations/20161009040430-support-delete-note.js b/lib/migrations/20161009040430-support-delete-note.js new file mode 100644 index 00000000..f478b6fe --- /dev/null +++ b/lib/migrations/20161009040430-support-delete-note.js @@ -0,0 +1,11 @@ +'use strict'; + +module.exports = { + up: function (queryInterface, Sequelize) { + queryInterface.addColumn('Notes', 'deletedAt', Sequelize.DATE); + }, + + down: function (queryInterface, Sequelize) { + queryInterface.removeColumn('Notes', 'deletedAt', Sequelize.DATE); + } +}; diff --git a/lib/models/note.js b/lib/models/note.js index 08ef083d..6efa5d4f 100644 --- a/lib/models/note.js +++ b/lib/models/note.js @@ -67,6 +67,7 @@ module.exports = function (sequelize, DataTypes) { type: DataTypes.DATE } }, { + paranoid: true, classMethods: { associate: function (models) { Note.belongsTo(models.User, { diff --git a/lib/realtime.js b/lib/realtime.js index c6c9ffeb..0fcd2eaa 100644 --- a/lib/realtime.js +++ b/lib/realtime.js @@ -796,6 +796,35 @@ function connection(socket) { } }); + // delete a note + socket.on('delete', function () { + //need login to do more actions + if (socket.request.user && socket.request.user.logged_in) { + var noteId = socket.noteId; + if (!noteId || !notes[noteId]) return; + var note = notes[noteId]; + //Only owner can delete note + if (note.owner && note.owner == socket.request.user.id) { + models.Note.destroy({ + where: { + id: noteId + } + }).then(function (count) { + if (!count) return; + for (var i = 0, l = note.socks.length; i < l; i++) { + var sock = note.socks[i]; + if (typeof sock !== 'undefined' && sock) { + sock.emit('delete'); + return sock.disconnect(true); + } + } + }).catch(function (err) { + return logger.error('delete note failed: ' + err); + }); + } + } + }); + //reveiced when user logout or changed socket.on('user changed', function () { logger.info('user changed'); diff --git a/public/js/index.js b/public/js/index.js index b139ad2a..63d7f3db 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -740,7 +740,8 @@ var ui = { editable: $(".ui-permission-editable"), locked: $(".ui-permission-locked"), private: $(".ui-permission-private") - } + }, + delete: $(".ui-delete-note") }, toc: { toc: $('.ui-toc'), @@ -2115,6 +2116,13 @@ ui.infobar.permission.locked.click(function () { ui.infobar.permission.private.click(function () { emitPermission("private"); }); +// delete note +ui.infobar.delete.click(function () { + $('.delete-modal').modal('show'); +}); +$('.ui-delete-modal-confirm').click(function () { + socket.emit('delete'); +}); function emitPermission(_permission) { if (_permission != permission) { @@ -2216,6 +2224,11 @@ socket.on('error', function (data) { if (data.message && data.message.indexOf('AUTH failed') === 0) location.href = serverurl + "/403"; }); +socket.on('delete', function () { + deleteServerHistory(noteid, function (err, data) { + if (!err) location.href = serverurl; + }); +}); var retryOnDisconnect = false; var retryTimer = null; socket.on('maintenance', function () { diff --git a/public/views/body.ejs b/public/views/body.ejs index 0b56edbb..79f2544e 100644 --- a/public/views/body.ejs +++ b/public/views/body.ejs @@ -19,6 +19,8 @@
  • Editable - Signed people can edit
  • Locked - Only owner can edit
  • Private - Only owner can view & edit
  • +
  • +
  • Delete this note

  • @@ -220,6 +222,26 @@ + + <%- include refresh-modal %> <%- include signin-modal %> <%- include help-modal %> -- cgit v1.2.3