Update to support delete note

This commit is contained in:
Wu Cheng-Han 2016-10-10 21:04:24 +08:00
parent 68457ed3a2
commit 12d5ed43a7
5 changed files with 77 additions and 1 deletions

View File

@ -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);
}
};

View File

@ -67,6 +67,7 @@ module.exports = function (sequelize, DataTypes) {
type: DataTypes.DATE type: DataTypes.DATE
} }
}, { }, {
paranoid: true,
classMethods: { classMethods: {
associate: function (models) { associate: function (models) {
Note.belongsTo(models.User, { Note.belongsTo(models.User, {

View File

@ -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 //reveiced when user logout or changed
socket.on('user changed', function () { socket.on('user changed', function () {
logger.info('user changed'); logger.info('user changed');

View File

@ -740,7 +740,8 @@ var ui = {
editable: $(".ui-permission-editable"), editable: $(".ui-permission-editable"),
locked: $(".ui-permission-locked"), locked: $(".ui-permission-locked"),
private: $(".ui-permission-private") private: $(".ui-permission-private")
} },
delete: $(".ui-delete-note")
}, },
toc: { toc: {
toc: $('.ui-toc'), toc: $('.ui-toc'),
@ -2115,6 +2116,13 @@ ui.infobar.permission.locked.click(function () {
ui.infobar.permission.private.click(function () { ui.infobar.permission.private.click(function () {
emitPermission("private"); 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) { function emitPermission(_permission) {
if (_permission != permission) { if (_permission != permission) {
@ -2216,6 +2224,11 @@ socket.on('error', function (data) {
if (data.message && data.message.indexOf('AUTH failed') === 0) if (data.message && data.message.indexOf('AUTH failed') === 0)
location.href = serverurl + "/403"; location.href = serverurl + "/403";
}); });
socket.on('delete', function () {
deleteServerHistory(noteid, function (err, data) {
if (!err) location.href = serverurl;
});
});
var retryOnDisconnect = false; var retryOnDisconnect = false;
var retryTimer = null; var retryTimer = null;
socket.on('maintenance', function () { socket.on('maintenance', function () {

View File

@ -19,6 +19,8 @@
<li class="ui-permission-editable"><a><i class="fa fa-shield fa-fw"></i> Editable - Signed people can edit</a></li> <li class="ui-permission-editable"><a><i class="fa fa-shield fa-fw"></i> Editable - Signed people can edit</a></li>
<li class="ui-permission-locked"><a><i class="fa fa-lock fa-fw"></i> Locked - Only owner can edit</a></li> <li class="ui-permission-locked"><a><i class="fa fa-lock fa-fw"></i> Locked - Only owner can edit</a></li>
<li class="ui-permission-private"><a><i class="fa fa-hand-stop-o fa-fw"></i> Private - Only owner can view &amp; edit</a></li> <li class="ui-permission-private"><a><i class="fa fa-hand-stop-o fa-fw"></i> Private - Only owner can view &amp; edit</a></li>
<li class="divider"></li>
<li class="ui-delete-note"><a><i class="fa fa-trash-o fa-fw"></i> Delete this note</a></li>
</ul> </ul>
</span> </span>
<br> <br>
@ -220,6 +222,26 @@
</div> </div>
</div> </div>
</div> </div>
<!-- delete modal -->
<div class="modal fade delete-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
</button>
<h4 class="modal-title" id="myModalLabel"><%= __('Are you sure?') %></h4>
</div>
<div class="modal-body" style="color:black;">
<h5 class="ui-delete-modal-msg">Do you really want to delete this note?</h5>
<strong class="ui-delete-modal-item">All users will lost their connection.</strong>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal"><%= __('Cancel') %></button>
<button type="button" class="btn btn-danger ui-delete-modal-confirm"><%= __('Yes, do it!') %></button>
</div>
</div>
</div>
</div>
<%- include refresh-modal %> <%- include refresh-modal %>
<%- include signin-modal %> <%- include signin-modal %>
<%- include help-modal %> <%- include help-modal %>