Added undo delete and recycle bin to library

This commit is contained in:
Stefan Zermatten
2021-08-27 13:00:01 +02:00
parent 518880fa5c
commit 608ab4e588
5 changed files with 107 additions and 4 deletions

View File

@@ -14,6 +14,7 @@ import { storedIconsSchema } from '/imports/api/icons/Icons.js';
import '/imports/api/library/methods/index.js';
import { updateReferenceNodeWork } from '/imports/api/library/methods/updateReferenceNode.js';
import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js';
import { restore } from '/imports/api/parenting/softRemove.js';
let LibraryNodes = new Mongo.Collection('libraryNodes');
@@ -186,6 +187,25 @@ const softRemoveLibraryNode = new ValidatedMethod({
}
});
const restoreLibraryNode = new ValidatedMethod({
name: 'libraryNodes.restore',
validate: new SimpleSchema({
_id: SimpleSchema.RegEx.Id
}).validator(),
mixins: [RateLimiterMixin],
rateLimit: {
numRequests: 5,
timeInterval: 5000,
},
run({_id}){
// Permissions
let node = LibraryNodes.findOne(_id);
assertNodeEditPermission(node, this.userId);
// Do work
restore({_id, collection: LibraryNodes});
}
});
export default LibraryNodes;
export {
LibraryNodeSchema,
@@ -194,4 +214,5 @@ export {
pullFromLibraryNode,
pushToLibraryNode,
softRemoveLibraryNode,
restoreLibraryNode,
};

View File

@@ -10,17 +10,17 @@ Meteor.startup(() => {
];
/**
* Deletes all soft removed documents that were removed more than 30 minutes ago
* Deletes all soft removed documents that were removed more than 1 day ago
* and were not restored
* @return {Number} Number of documents removed
*/
const deleteOldSoftRemovedDocs = function(){
const now = new Date();
const thirtyMinutesAgo = new Date(now.getTime() - 30*60000);
const yesterday = new Date(now.getTime() - (24 * 60 * 60 * 1000));
collections.forEach(collection => {
collection.remove({
removed: true,
removedAt: {$lt: thirtyMinutesAgo} // dates *before* 30 minutes ago
removedAt: {$lt: yesterday} // dates *before* yesterday
}, function(error){
if (error){
console.error(JSON.stringify(error, null, 2));

View File

@@ -69,6 +69,28 @@ Meteor.publish('libraryNodes', function(libraryId){
});
});
Meteor.publish('softRemovedLibraryNodes', function(libraryId){
if (!libraryId) return [];
libraryIdSchema.validate({libraryId});
this.autorun(function (){
let userId = this.userId;
let library = Libraries.findOne(libraryId);
try { assertViewPermission(library, userId) }
catch(e){
return this.error(e);
}
return [
LibraryNodes.find({
'ancestors.0.id': libraryId,
removed: true,
removedWith: {$exists: false},
}, {
sort: {order: 1},
}),
];
});
});
Meteor.publish('descendantLibraryNodes', function(nodeId){
let node = LibraryNodes.findOne(nodeId);
let libraryId = node?.ancestors[0]?.id;

View File

@@ -27,6 +27,35 @@
@change="updateName"
/>
</template>
<template v-if="removedDocs.length">
<h3>Recently Deleted Properties</h3>
<v-list>
<v-list-item
v-for="model in removedDocs"
:key="model._id"
>
<v-list-item-content>
<v-list-item-title>
<tree-node-view :model="model" />
</v-list-item-title>
</v-list-item-content>
<v-list-item-action>
<v-btn
color="accent"
text
@click="restore(model._id)"
>
Restore
</v-btn>
</v-list-item-action>
</v-list-item>
</v-list>
</template>
<v-progress-circular
v-if="!$subReady.softRemovedLibraryNodes"
indeterminate
color="primary"
/>
<template slot="actions">
<v-spacer />
<v-btn
@@ -43,10 +72,13 @@
<script lang="js">
import DialogBase from '/imports/ui/dialogStack/DialogBase.vue';
import Libraries, { updateLibraryName, removeLibrary } from '/imports/api/library/Libraries.js';
import LibraryNodes, { restoreLibraryNode } from '/imports/api/library/LibraryNodes.js';
import TreeNodeView from '/imports/ui/properties/treeNodeViews/TreeNodeView.vue';
export default {
components: {
DialogBase,
TreeNodeView,
},
props: {
_id: String,
@@ -90,11 +122,28 @@ export default {
},
});
},
restore(_id){
restoreLibraryNode.call({_id});
},
},
meteor: {
'$subscribe':{
softRemovedLibraryNodes(){
return [this._id];
},
},
model(){
return Libraries.findOne(this._id);
},
removedDocs(){
return LibraryNodes.find({
'ancestors.0.id': this._id,
removed: true,
removedWith: {$exists: false},
}, {
sort: {order: 1},
});
}
}
}
</script>

View File

@@ -75,6 +75,7 @@
pushToLibraryNode,
pullFromLibraryNode,
softRemoveLibraryNode,
restoreLibraryNode,
} from '/imports/api/library/LibraryNodes.js';
import duplicateLibraryNode from '/imports/api/library/methods/duplicateLibraryNode.js';
import DialogBase from '/imports/ui/dialogStack/DialogBase.vue';
@@ -86,6 +87,8 @@
import { get } from 'lodash';
import { assertDocEditPermission } from '/imports/api/sharing/sharingPermissions.js';
import { organizeDoc } from '/imports/api/parenting/organizeMethods.js';
import { snackbar } from '/imports/ui/components/snackbars/SnackbarQueue.js';
import getPropertyTitle from '/imports/ui/properties/shared/getPropertyTitle.js';
let formIndex = {};
for (let key in propertyFormIndex){
@@ -212,12 +215,20 @@
});
},
remove(){
softRemoveLibraryNode.call({_id: this.currentId});
let _id = this.currentId;
softRemoveLibraryNode.call({_id});
if (this.embedded){
this.$emit('removed');
} else {
this.$store.dispatch('popDialogStack');
}
snackbar({
text: `Deleted ${getPropertyTitle(this.model)}`,
callbackName: 'undo',
callback(){
restoreLibraryNode.call({_id});
},
});
},
}
};