[NEW] Add an option to delete file in files list (#13815)

pull/14018/head^2
Marcelo Schmidt 7 years ago committed by Diego Sampaio
parent 14b79fe1ac
commit 2143056ff7
  1. 2
      app/authorization/client/route.js
  2. 54
      app/ui-flextab/client/tabs/uploadedFilesList.js
  3. 48
      app/ui-utils/client/lib/MessageAction.js
  4. 1
      app/utils/client/index.js
  5. 42
      app/utils/client/lib/canDeleteMessage.js
  6. 44
      client/methods/deleteMessage.js

@ -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',

@ -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');
@ -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: [
{
@ -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,
});
}
});
});
},
},
],
}] : []),
],
},
],

@ -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) {
@ -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',

@ -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';

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

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

Loading…
Cancel
Save