The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Rocket.Chat/app/ui-message/client/messageBox/messageBoxAudioMessage.js

160 lines
4.4 KiB

import { ReactiveVar } from 'meteor/reactive-var';
import { Template } from 'meteor/templating';
import { settings } from '../../../settings';
import { AudioRecorder, fileUpload, USER_ACTIVITIES, UserAction } from '../../../ui';
import { t } from '../../../utils';
import './messageBoxAudioMessage.html';
const startRecording = async (rid, tmid) => {
try {
await AudioRecorder.start();
UserAction.performContinuously(rid, USER_ACTIVITIES.USER_RECORDING, { tmid });
} catch (error) {
throw error;
}
};
const stopRecording = async (rid, tmid) => {
const result = await new Promise((resolve) => AudioRecorder.stop(resolve));
UserAction.stop(rid, USER_ACTIVITIES.USER_RECORDING, { tmid });
return result;
};
const recordingInterval = new ReactiveVar(null);
const recordingRoomId = new ReactiveVar(null);
const clearIntervalVariables = () => {
if (recordingInterval.get()) {
clearInterval(recordingInterval.get());
recordingInterval.set(null);
recordingRoomId.set(null);
}
};
const cancelRecording = async (instance, rid, tmid) => {
clearIntervalVariables();
instance.time.set('00:00');
const blob = await stopRecording(rid, tmid);
instance.state.set(null);
return blob;
};
Template.messageBoxAudioMessage.onCreated(async function () {
this.state = new ReactiveVar(null);
this.time = new ReactiveVar('00:00');
this.isMicrophoneDenied = new ReactiveVar(false);
if (navigator.permissions) {
try {
const permissionStatus = await navigator.permissions.query({ name: 'microphone' });
this.isMicrophoneDenied.set(permissionStatus.state === 'denied');
permissionStatus.onchange = () => {
this.isMicrophoneDenied.set(permissionStatus.state === 'denied');
};
return;
} catch (error) {
console.warn(error);
}
}
if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {
this.isMicrophoneDenied.set(true);
return;
}
try {
if (!(await navigator.mediaDevices.enumerateDevices()).some(({ kind }) => kind === 'audioinput')) {
this.isMicrophoneDenied.set(true);
return;
}
} catch (error) {
console.warn(error);
}
});
Template.messageBoxAudioMessage.onDestroyed(async function () {
if (this.state.get() === 'recording') {
const { rid, tmid } = this.data;
await cancelRecording(this, rid, tmid);
}
});
Template.messageBoxAudioMessage.helpers({
isAllowed() {
return (
AudioRecorder.isSupported() &&
!Template.instance().isMicrophoneDenied.get() &&
settings.get('FileUpload_Enabled') &&
settings.get('Message_AudioRecorderEnabled') &&
(!settings.get('FileUpload_MediaTypeBlackList') || !settings.get('FileUpload_MediaTypeBlackList').match(/audio\/mp3|audio\/\*/i)) &&
(!settings.get('FileUpload_MediaTypeWhiteList') || settings.get('FileUpload_MediaTypeWhiteList').match(/audio\/mp3|audio\/\*/i))
);
},
stateClass() {
if (recordingRoomId.get() && recordingRoomId.get() !== Template.currentData().rid) {
return 'rc-message-box__audio-message--busy';
}
const state = Template.instance().state.get();
return state && `rc-message-box__audio-message--${state}`;
},
time() {
return Template.instance().time.get();
},
});
Template.messageBoxAudioMessage.events({
async 'click .js-audio-message-record'(event, instance) {
event.preventDefault();
if (recordingRoomId.get() && recordingRoomId.get() !== this.rid) {
return;
}
instance.state.set('recording');
try {
await startRecording(this.rid, this.tmid);
const startTime = new Date();
recordingInterval.set(
setInterval(() => {
const now = new Date();
const distance = (now.getTime() - startTime.getTime()) / 1000;
const minutes = Math.floor(distance / 60);
const seconds = Math.floor(distance % 60);
instance.time.set(`${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`);
}, 1000),
);
recordingRoomId.set(this.rid);
} catch (error) {
console.log(error);
instance.isMicrophoneDenied.set(true);
instance.state.set(null);
}
},
async 'click .js-audio-message-cancel'(event, instance) {
event.preventDefault();
await cancelRecording(instance, this.rid, this.tmid);
},
async 'click .js-audio-message-done'(event, instance) {
event.preventDefault();
instance.state.set('loading');
[NEW] Threads V 1.0 (#13996) * first commit * empty reply method * permissions and settings * hooks * canSendMessage server function * follow unfollow methods * message tmid index * removed useless permissons * Notification and Hooks * remove edit-room-title * flextab threads and thread view * improved message render * open threads on click * group message * Save unread threads on subscription * group thread messages * useless css * follow unfollow methods * Fix unread threads * follow unfollow actions and badge on flextab * unread button * fix multiple getThreadMessages * Add notifications * Move thread queries to models * Move lib file to server folder * Fix notifications for users in thread * small changes * Remove stub thread reply * Normalize thread files * message template * Fix notification on first reply * Fix follow/unfollow * Fix removing a thread on last message delete * fix open flextab * getmessages instead getSinglemessage * Fix remove thread message * Fix delete thread * fix open multiple threads * Fix removing threads * Add more tests to todo * fix * fix * icons and i18n * Fix thread title on replies * Fix async * onViewRendered * fix reactions and removed css code * fix blaze variable * threads tab order * thread replies button * i18n * fix test * fix tests and css * removed limits to thread list * fix grouping time * fix load message * style changes * fix unread badge * fix role description * clear read thread * ajust badge * time ago threads * jump to messages * mention link * tick mention * fix reloading threadlist after reply * Pass rid and showFormattingTips as parameters to messageBox template * Remove references to RoomManager in messageBox template * Remove some invalid references * Remove some invalid references * Reduce messageBox coupling * Add small fixes * Extract more parameters from messageBox * Fix emoji picker button * Remove all references to chatMessages in messageBox * Change focus handling * Refactor autogrow plugin * Fix calling modal.open() on modal confirm callback * Disable message reply action for same user * Refactor ChatMessages * Pass rid to messagePopupConfig * Fix attachment description update * Move RTL change logic to messageBox * Pass rid to fileUpload helper * Don't use openedRoom session variable in room template * Add tmid support * Rename mountReply helper as prependReplies * scroll at bottom thread * Simplify messageBox events * Refactor ChatMessages.send * Fix messagePopupConfig for emojis * Split chatMessages initialization * Revert "Disable message reply action for same user" This reverts commit f9dc0b486e8fe8aa52012fa6e21bc4961ad5d674. * Set outline style for open thread buttons * Test atBottom condition on thread template before request scroll * Update join button * Protect messageBox from rid absence * Embed messageBox into thread template * Wait thread update before request scroll * Increase font-weight for rc-button
7 years ago
const { rid, tmid } = this;
const blob = await cancelRecording(instance, rid, tmid);
await fileUpload([{ file: blob, type: 'video', name: `${t('Audio record')}.mp3` }], { input: blob }, { rid, tmid });
},
});