Skip to content

Commit

Permalink
[NEW] Add an option to delete file in files list (#13815)
Browse files Browse the repository at this point in the history
  • Loading branch information
marceloschmidt authored and sampaiodiego committed Apr 7, 2019
1 parent 14b79fe commit 2143056
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 70 deletions.
2 changes: 1 addition & 1 deletion app/authorization/client/route.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FlowRouter } from 'meteor/kadira:flow-router';
import { BlazeLayout } from 'meteor/kadira:blaze-layout';
import { t } from '../../utils';
import { t } from '../../utils/client';

FlowRouter.route('/admin/permissions', {
name: 'admin-permissions',
Expand Down
54 changes: 49 additions & 5 deletions app/ui-flextab/client/tabs/uploadedFilesList.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import _ from 'underscore';

import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import { Mongo } from 'meteor/mongo';
import { ReactiveVar } from 'meteor/reactive-var';
import { DateFormat } from '../../../lib';
import { t, getURL } from '../../../utils';
import { popover } from '../../../ui-utils';
import { Template } from 'meteor/templating';
import _ from 'underscore';

import { DateFormat } from '../../../lib/client';
import { canDeleteMessage, getURL, handleError, t } from '../../../utils/client';
import { popover, modal } from '../../../ui-utils/client';

const roomFiles = new Mongo.Collection('room_files');

Expand Down Expand Up @@ -122,6 +125,12 @@ Template.uploadedFilesList.events({
'click .js-action'(e) {
e.currentTarget.parentElement.classList.add('active');

const canDelete = canDeleteMessage({
rid: this.rid,
ts: this.file.uploadedAt,
uid: this.file.userId,
});

const config = {
columns: [
{
Expand All @@ -143,6 +152,41 @@ Template.uploadedFilesList.events({
},
],
},
...(canDelete ? [{
items: [
{
icon: 'trash',
name: t('Delete'),
modifier: 'alert',
action: () => {
modal.open({
title: t('Are_you_sure'),
text: t('You_will_not_be_able_to_recover_file'),
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: t('Yes_delete_it'),
cancelButtonText: t('Cancel'),
html: false,
}, () => {
Meteor.call('deleteFileMessage', this.file._id, (error) => {
if (error) {
handleError(error);
} else {
modal.open({
title: t('Deleted'),
text: t('Your_entry_has_been_deleted'),
type: 'success',
timer: 1000,
showConfirmButton: false,
});
}
});
});
},
},
],
}] : []),
],
},
],
Expand Down
48 changes: 16 additions & 32 deletions app/ui-utils/client/lib/MessageAction.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import _ from 'underscore';
import moment from 'moment';
import toastr from 'toastr';
import mem from 'mem';

import { Meteor } from 'meteor/meteor';
import { TAPi18n } from 'meteor/tap:i18n';
import { ReactiveVar } from 'meteor/reactive-var';
import { Tracker } from 'meteor/tracker';
import { Session } from 'meteor/session';
import { t, handleError, roomTypes } from '../../../utils';

import { t, handleError, roomTypes, canDeleteMessage } from '../../../utils/client';
import { messageArgs } from '../../../ui-utils/client/lib/messageArgs';
import { Messages, Rooms, Subscriptions } from '../../../models';
import { hasAtLeastOnePermission } from '../../../authorization';
import { settings } from '../../../settings';
import _ from 'underscore';
import moment from 'moment';
import toastr from 'toastr';
import mem from 'mem';
import { Messages, Rooms, Subscriptions } from '../../../models/client';
import { hasAtLeastOnePermission } from '../../../authorization/client';
import { settings } from '../../../settings/client';

const call = (method, ...args) => new Promise((resolve, reject) => {
Meteor.call(method, ...args, function(err, data) {
Expand Down Expand Up @@ -232,30 +234,12 @@ Meteor.startup(async function() {
if (Subscriptions.findOne({ rid: message.rid }) == null) {
return false;
}
const forceDelete = hasAtLeastOnePermission('force-delete-message', message.rid);
const hasPermission = hasAtLeastOnePermission('delete-message', message.rid);
const isDeleteAllowed = settings.get('Message_AllowDeleting');
const deleteOwn = message.u && message.u._id === Meteor.userId();
if (!(hasPermission || (isDeleteAllowed && deleteOwn) || forceDelete)) {
return;
}
const blockDeleteInMinutes = settings.get('Message_AllowDeleting_BlockDeleteInMinutes');
if (forceDelete) {
return true;
}
if (blockDeleteInMinutes != null && blockDeleteInMinutes !== 0) {
let msgTs;
if (message.ts != null) {
msgTs = moment(message.ts);
}
let currentTsDiff;
if (msgTs != null) {
currentTsDiff = moment().diff(msgTs, 'minutes');
}
return currentTsDiff < blockDeleteInMinutes;
} else {
return true;
}

return canDeleteMessage({
rid: message.rid,
ts: message.ts,
uid: message.u._id,
});
},
order: 3,
group: 'menu',
Expand Down
1 change: 1 addition & 0 deletions app/utils/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ export { getValidRoomName } from '../lib/getValidRoomName';
export { placeholders } from '../lib/placeholders';
export { templateVarHandler } from '../lib/templateVarHandler';
export { APIClient } from './lib/RestApiClient';
export { canDeleteMessage } from './lib/canDeleteMessage';
42 changes: 42 additions & 0 deletions app/utils/client/lib/canDeleteMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Meteor } from 'meteor/meteor';
import moment from 'moment';

import { hasAtLeastOnePermission } from '../../../authorization/client';
import { settings } from '../../../settings/client';

export const canDeleteMessage = ({ rid, ts, uid }) => {
const userId = Meteor.userId();

const forceDelete = hasAtLeastOnePermission('force-delete-message', rid);
if (forceDelete) {
return true;
}

const isDeleteAllowed = settings.get('Message_AllowDeleting');
if (!isDeleteAllowed) {
return false;
}

const hasPermission = hasAtLeastOnePermission('delete-message', rid);
const deleteOwn = uid === userId;
if (!hasPermission && !deleteOwn) {
return false;
}

const blockDeleteInMinutes = settings.get('Message_AllowDeleting_BlockDeleteInMinutes');
if (blockDeleteInMinutes != null && blockDeleteInMinutes !== 0) {
let msgTs;
if (ts != null) {
msgTs = moment(ts);
}
let currentTsDiff;
if (msgTs != null) {
currentTsDiff = moment().diff(msgTs, 'minutes');
}
return currentTsDiff < blockDeleteInMinutes;
}

return true;
};


44 changes: 12 additions & 32 deletions client/methods/deleteMessage.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,27 @@
import { Meteor } from 'meteor/meteor';
import { Tracker } from 'meteor/tracker';
import { ChatMessage } from '../../app/models';
import { hasAtLeastOnePermission } from '../../app/authorization';
import { settings } from '../../app/settings';
import _ from 'underscore';
import moment from 'moment';
import { ChatMessage } from '../../app/models/client';
import { canDeleteMessage } from '../../app/utils/client';

Meteor.methods({
deleteMessage(message) {
deleteMessage(msg) {
if (!Meteor.userId()) {
return false;
}

// We're now only passed in the `_id` property to lower the amount of data sent to the server
message = ChatMessage.findOne({ _id: message._id });
const message = ChatMessage.findOne({ _id: msg._id });

const hasPermission = hasAtLeastOnePermission('delete-message', message.rid);
const forceDelete = hasAtLeastOnePermission('force-delete-message', message.rid);
const deleteAllowed = settings.get('Message_AllowDeleting');
let deleteOwn = false;

if (message && message.u && message.u._id) {
deleteOwn = message.u._id === Meteor.userId();
}
if (!(forceDelete || hasPermission || (deleteAllowed && deleteOwn))) {
if (!canDeleteMessage({
rid: message.rid,
ts: message.ts,
uid: message.u._id,
})) {
return false;
}
const blockDeleteInMinutes = settings.get('Message_AllowDeleting_BlockDeleteInMinutes');
if (!forceDelete && _.isNumber(blockDeleteInMinutes) && blockDeleteInMinutes !== 0) {
const msgTs = moment(message.ts);
const currentTsDiff = moment().diff(msgTs, 'minutes');
if (currentTsDiff > blockDeleteInMinutes) {
return false;
}


}

Tracker.nonreactive(function() {
ChatMessage.remove({
_id: message._id,
'u._id': Meteor.userId(),
});
ChatMessage.remove({
_id: message._id,
'u._id': Meteor.userId(),
});
},
});

0 comments on commit 2143056

Please sign in to comment.