Skip to content

Commit

Permalink
Chore: Restrict ChatMessages API - Phase 2 (#27457)
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan authored Dec 5, 2022
1 parent af3ec69 commit 6163275
Show file tree
Hide file tree
Showing 67 changed files with 2,039 additions and 1,560 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import type { IRoom } from '@rocket.chat/core-typings';
import moment from 'moment';
import _ from 'underscore';

import { Users } from '../../../../../models/client';
import { roomCoordinator } from '../../../../../../client/lib/rooms/roomCoordinator';
import { settings } from '../../../../../settings/client';
import { RoomManager } from '../../../../../ui-utils/client';
import { ChatMessages } from '../../../lib/ChatMessages';
import { Users } from '../../../models/client';
import { roomCoordinator } from '../../../../client/lib/rooms/roomCoordinator';
import { settings } from '../../../settings/client';
import type { ThreadTemplateInstance } from './thread';

const userCanDrop = (rid: IRoom['_id']) =>
!roomCoordinator.readOnly(rid, Users.findOne({ _id: Meteor.userId() }, { fields: { username: 1 } }));
Expand All @@ -23,7 +22,7 @@ async function createFileFromUrl(url: string): Promise<File> {
const metadata = {
type: data.type,
};
const { mime } = await import('../../../../../utils/lib/mimeTypes');
const { mime } = await import('../../../utils/lib/mimeTypes');
const file = new File(
[data],
`File - ${moment().format(settings.get('Message_TimeAndDateFormat'))}.${mime.extension(data.type)}`,
Expand All @@ -32,19 +31,6 @@ async function createFileFromUrl(url: string): Promise<File> {
return file;
}

function addToInput(text: string): void {
const input = RoomManager.openedRoom ? ChatMessages.get({ rid: RoomManager.openedRoom })?.input : undefined;
if (!input) {
return;
}

const initText = input.value.slice(0, input.selectionStart ?? undefined);
const finalText = input.value.slice(input.selectionEnd ?? undefined, input.value.length);

input.value = initText + text + finalText;
$(input).change().trigger('input');
}

export const dropzoneHelpers = {
dragAndDrop(): string | undefined {
return settings.get('FileUpload_Enabled') ? 'dropzone--disabled' : undefined;
Expand All @@ -54,8 +40,8 @@ export const dropzoneHelpers = {
return settings.get('FileUpload_Enabled') ? 'dropzone-overlay--enabled' : 'dropzone-overlay--disabled';
},

dragAndDropLabel(this: { _id: IRoom['_id']; rid: IRoom['_id'] }): string {
if (!userCanDrop(this._id)) {
dragAndDropLabel(this: ThreadTemplateInstance['data']): string {
if (!userCanDrop(this.rid)) {
return 'error-not-allowed';
}

Expand All @@ -68,14 +54,14 @@ export const dropzoneHelpers = {
};

export const dropzoneEvents = {
'dragenter .dropzone'(this: { _id: IRoom['_id'] }, e: JQuery.DragEnterEvent) {
'dragenter .dropzone'(this: ThreadTemplateInstance['data'], e: JQuery.DragEnterEvent) {
const types = e.originalEvent?.dataTransfer?.types;

if (
types &&
types.length > 0 &&
_.some(types, (type) => type.indexOf('text/') === -1 || type.indexOf('text/uri-list') !== -1 || type.indexOf('text/plain') !== -1) &&
userCanDrop(this._id)
userCanDrop(this.rid)
) {
e.currentTarget.classList.add('over');
}
Expand Down Expand Up @@ -105,24 +91,13 @@ export const dropzoneEvents = {
event.stopPropagation();
},

async 'dropped .dropzone-overlay'(
this: { _id: IRoom['_id']; rid: IRoom['_id'] },
event: JQuery.DropEvent,
instance: Blaze.TemplateInstance & {
onFile?: (
filesToUpload: {
file: File;
name: string;
}[],
) => void;
},
) {
async 'dropped .dropzone-overlay'(this: ThreadTemplateInstance['data'], event: JQuery.DropEvent, instance: ThreadTemplateInstance) {
event.currentTarget.parentNode.classList.remove('over');

event.stopPropagation();
event.preventDefault();

if (!userCanDrop(this._id) || !settings.get('FileUpload_Enabled')) {
if (!userCanDrop(this.rid) || !settings.get('FileUpload_Enabled')) {
return false;
}

Expand All @@ -147,23 +122,23 @@ export const dropzoneEvents = {

const file = await createFileFromUrl(imgURL);
if (typeof file === 'string') {
return addToInput(file);
instance.onTextDrop?.(file);
return;
}
files = [file];
}
if (dataTransfer.types.includes('text/plain') && !dataTransfer.types.includes('text/x-moz-url')) {
return addToInput(transferData?.trim());
instance.onTextDrop?.(transferData.trim());
return;
}
}
const { mime } = await import('../../../../../utils/lib/mimeTypes');
const filesToUpload = Array.from(files).map((file) => {
Object.defineProperty(file, 'type', { value: mime.lookup(file.name) });
return {
file,
name: file.name,
};
});

return instance.onFile?.(filesToUpload);
const { mime } = await import('../../../utils/lib/mimeTypes');

instance.onFileDrop?.(
Array.from(files).map((file) => {
Object.defineProperty(file, 'type', { value: mime.lookup(file.name) });
return file;
}),
);
},
};
6 changes: 3 additions & 3 deletions apps/meteor/app/threads/client/flextab/thread.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
<div class="dropzone-overlay {{isDropzoneDisabled}} background-transparent-darkest color-content-background-color">{{_ dragAndDropLabel}}</div>
<div class="thread-list js-scroll-thread">
<ul class="thread">
{{#with messageContext}}
{{#with _messageContext}}
{{#if isLoading}}
<li class="load-more">
{{> loading}}
</li>
{{else}}
{{#if mainMessage }}
{{> message groupable=false hideRoles=true msg=mainMessage room=room subscription=subscription settings=settings customClass="thread-message" templatePrefix='thread-' customClass="thread-main" u=u ignored=false shouldCollapseReplies=true}}
{{> message groupable=false hideRoles=true msg=mainMessage room=room subscription=subscription settings=settings templatePrefix='thread-' customClass=customClassMain u=u ignored=false shouldCollapseReplies=true chatContext=chatContext messageContext=messageContext}}
{{/if}}
{{#each msg in messages}}
{{> message hideRoles=true msg=msg room=room shouldCollapseReplies=true subscription=subscription settings=settings templatePrefix='thread-' u=u context="threads"}}
{{> message hideRoles=true msg=msg room=room shouldCollapseReplies=true subscription=subscription settings=settings templatePrefix='thread-' customClass=(customClass msg) u=u context="threads" chatContext=chatContext messageContext=messageContext}}
{{/each}}
{{/if}}
{{/with}}
Expand Down
Loading

0 comments on commit 6163275

Please sign in to comment.