/* globals fileUpload AudioRecorder KonchatNotification chatMessages */ import toastr from 'toastr'; import mime from 'mime-type/with-db'; import moment from 'moment'; import {VRecDialog} from 'meteor/rocketchat:ui-vrecord'; function katexSyntax() { if (RocketChat.katex.katex_enabled()) { if (RocketChat.katex.dollar_syntax_enabled()) { return '$$KaTeX$$'; } if (RocketChat.katex.parenthesis_syntax_enabled()) { return '\\[KaTeX\\]'; } } return false; } Template.messageBox.helpers({ roomName() { const roomData = Session.get(`roomData${ this._id }`); if (!roomData) { return ''; } if (roomData.t === 'd') { const chat = ChatSubscription.findOne({ rid: this._id }, { fields: { name: 1 } }); return chat && chat.name; } else { return roomData.name; } }, showMarkdown() { return RocketChat.Markdown; }, showMarkdownCode() { return RocketChat.MarkdownCode; }, showKatex() { return RocketChat.katex; }, katexSyntax() { return katexSyntax(); }, showFormattingTips() { return RocketChat.settings.get('Message_ShowFormattingTips') && (RocketChat.Markdown || RocketChat.MarkdownCode || katexSyntax()); }, canJoin() { return RocketChat.roomTypes.verifyShowJoinLink(this._id); }, joinCodeRequired() { const code = Session.get(`roomData${ this._id }`); return code && code.joinCodeRequired; }, subscribed() { return RocketChat.roomTypes.verifyCanSendMessage(this._id); }, allowedToSend() { if (RocketChat.roomTypes.readOnly(this._id, Meteor.user())) { return false; } if (RocketChat.roomTypes.archived(this._id)) { return false; } const roomData = Session.get(`roomData${ this._id }`); if (roomData && roomData.t === 'd') { const subscription = ChatSubscription.findOne({ rid: this._id }, { fields: { archived: 1, blocked: 1, blocker: 1 } }); if (subscription && (subscription.archived || subscription.blocked || subscription.blocker)) { return false; } } return true; }, isBlockedOrBlocker() { const roomData = Session.get(`roomData${ this._id }`); if (roomData && roomData.t === 'd') { const subscription = ChatSubscription.findOne({ rid: this._id }, { fields: { blocked: 1, blocker: 1 } }); if (subscription && (subscription.blocked || subscription.blocker)) { return true; } } }, getPopupConfig() { const template = Template.instance(); return { getInput() { return template.find('.input-message'); } }; }, /* globals MsgTyping*/ usersTyping() { const users = MsgTyping.get(this._id); if (users.length === 0) { return; } if (users.length === 1) { return { multi: false, selfTyping: MsgTyping.selfTyping.get(), users: users[0] }; } let last = users.pop(); if (users.length > 4) { last = t('others'); } let usernames = users.join(', '); usernames = [usernames, last]; return { multi: true, selfTyping: MsgTyping.selfTyping.get(), users: usernames.join(` ${ t('and') } `) }; }, groupAttachHidden() { if (RocketChat.settings.get('Message_Attachments_GroupAttach')) { return 'hidden'; } }, fileUploadEnabled() { return RocketChat.settings.get('FileUpload_Enabled'); }, fileUploadAllowedMediaTypes() { return RocketChat.settings.get('FileUpload_MediaTypeWhiteList'); }, showFileUpload() { let roomData; if (RocketChat.settings.get('FileUpload_Enabled')) { roomData = Session.get(`roomData${ this._id }`); if (roomData && roomData.t === 'd') { return RocketChat.settings.get('FileUpload_Enabled_Direct'); } else { return true; } } else { return RocketChat.settings.get('FileUpload_Enabled'); } }, showMic() { return Template.instance().showMicButton.get(); }, showVRec() { return Template.instance().showVideoRec.get(); }, showSend() { if (!Template.instance().isMessageFieldEmpty.get()) { return 'show-send'; } }, showLocation() { return RocketChat.Geolocation.get() !== false; }, notSubscribedTpl() { return RocketChat.roomTypes.getNotSubscribedTpl(this._id); }, showSandstorm() { return Meteor.settings['public'].sandstorm && !Meteor.isCordova; } }); function firefoxPasteUpload(fn) { const user = navigator.userAgent.match(/Firefox\/(\d+)\.\d/); if (!user || user[1] > 49) { return fn; } return function(event, instance) { if ((event.originalEvent.ctrlKey || event.originalEvent.metaKey) && (event.keyCode === 86)) { const textarea = instance.find('textarea'); const {selectionStart, selectionEnd} = textarea; const contentEditableDiv = instance.find('#msg_contenteditable'); contentEditableDiv.focus(); Meteor.setTimeout(function() { const pastedImg = contentEditableDiv.querySelector('img'); const textareaContent = textarea.value; const startContent = textareaContent.substring(0, selectionStart); const endContent = textareaContent.substring(selectionEnd); const restoreSelection = function(pastedText) { textarea.value = startContent + pastedText + endContent; textarea.selectionStart = selectionStart + pastedText.length; return textarea.selectionEnd = textarea.selectionStart; }; if (pastedImg) { contentEditableDiv.innerHTML = ''; } textarea.focus; if (!pastedImg || contentEditableDiv.innerHTML.length > 0) { return [].slice.call(contentEditableDiv.querySelectorAll('br')).forEach(function(el) { contentEditableDiv.replaceChild(new Text('\n'), el); return restoreSelection(contentEditableDiv.innerText); }); } const imageSrc = pastedImg.getAttribute('src'); if (imageSrc.match(/^data:image/)) { return fetch(imageSrc).then(function(img) { return img.blob(); }).then(function(blob) { return fileUpload([ { file: blob, name: 'Clipboard' } ]); }); } }, 150); } return fn && fn.apply(this, arguments); }; } Template.messageBox.events({ 'click .join'(event) { event.stopPropagation(); event.preventDefault(); Meteor.call('joinRoom', this._id, Template.instance().$('[name=joinCode]').val(), (err) => { if (err != null) { toastr.error(t(err.reason)); } if (RocketChat.authz.hasAllPermission('preview-c-room') === false && RoomHistoryManager.getRoom(this._id).loaded === 0) { RoomManager.getOpenedRoomByRid(this._id).streamActive = false; RoomManager.getOpenedRoomByRid(this._id).ready = false; RoomHistoryManager.getRoom(this._id).loaded = null; RoomManager.computation.invalidate(); } }); }, 'focus .input-message'(event, instance) { KonchatNotification.removeRoomNotification(this._id); chatMessages[this._id].input = instance.find('.input-message'); }, 'click .send-button'(event, instance) { const input = instance.find('.input-message'); chatMessages[this._id].send(this._id, input, () => { // fixes https://github.com/RocketChat/Rocket.Chat/issues/3037 // at this point, the input is cleared and ready for autogrow input.updateAutogrow(); return instance.isMessageFieldEmpty.set(chatMessages[this._id].isEmpty()); }); return input.focus(); }, 'keyup .input-message'(event, instance) { chatMessages[this._id].keyup(this._id, event, instance); return instance.isMessageFieldEmpty.set(chatMessages[this._id].isEmpty()); }, 'paste .input-message'(e, instance) { Meteor.setTimeout(function() { const input = instance.find('.input-message'); return typeof input.updateAutogrow === 'function' && input.updateAutogrow(); }, 50); if (e.originalEvent.clipboardData == null) { return; } const items = e.originalEvent.clipboardData.items; const files = items.map(item => { if (item.kind === 'file' && item.type.indexOf('image/') !== -1) { e.preventDefault(); return { file: item.getAsFile(), name: `Clipboard - ${ moment().format(RocketChat.settings.get('Message_TimeAndDateFormat')) }` }; } }).filter(); if (files.length) { return fileUpload(files); } else { return instance.isMessageFieldEmpty.set(false); } }, 'keydown .input-message': firefoxPasteUpload(function(event) { return chatMessages[this._id].keydown(this._id, event, Template.instance()); }), 'input .input-message'(event) { return chatMessages[this._id].valueChanged(this._id, event, Template.instance()); }, 'propertychange .input-message'(event) { if (event.originalEvent.propertyName === 'value') { return chatMessages[this._id].valueChanged(this._id, event, Template.instance()); } }, 'click .editing-commands-cancel > button'() { return chatMessages[this._id].clearEditing(); }, 'click .editing-commands-save > button'() { return chatMessages[this._id].send(this._id, chatMessages[this._id].input); }, 'change .message-form input[type=file]'(event) { const e = event.originalEvent || event; let files = e.target.files; if (!files || files.length === 0) { files = (e.dataTransfer && e.dataTransfer.files) || []; } const filesToUpload = files.map(file => { // `file.type = mime.lookup(file.name)` does not work. Object.defineProperty(file, 'type', { value: mime.lookup(file.name) }); return { file, name: file.name }; }); return fileUpload(filesToUpload); }, 'click .message-buttons.share'(e, t) { t.$('.share-items').toggleClass('hidden'); return t.$('.message-buttons.share').toggleClass('active'); }, 'click .message-form .message-buttons.location'() { const roomId = this._id; const position = RocketChat.Geolocation.get(); const latitude = position.coords.latitude; const longitude = position.coords.longitude; const text = `