import { Meteor } from 'meteor/meteor'; import { Random } from 'meteor/random'; import { ReactiveVar } from 'meteor/reactive-var'; import { Tracker } from 'meteor/tracker'; import { VRecDialog } from '../../../ui-vrecord/client'; import { messageBox, modal } from '../../../ui-utils'; import { fileUpload } from '../../../ui'; import { settings } from '../../../settings'; import { t } from '../../../utils'; import { mime } from '../../../utils/lib/mimeTypes'; messageBox.actions.add('Create_new', 'Video_message', { id: 'video-message', icon: 'video', condition: () => (navigator.mediaDevices || navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia) && window.MediaRecorder && settings.get('FileUpload_Enabled') && settings.get('Message_VideoRecorderEnabled') && (!settings.get('FileUpload_MediaTypeBlackList') || !settings.get('FileUpload_MediaTypeBlackList').match(/video\/webm|video\/\*/i)) && (!settings.get('FileUpload_MediaTypeWhiteList') || settings.get('FileUpload_MediaTypeWhiteList').match(/video\/webm|video\/\*/i)), action: ({ rid, tmid, messageBox }) => (VRecDialog.opened ? VRecDialog.close() : VRecDialog.open(messageBox, { rid, tmid })), }); messageBox.actions.add('Add_files_from', 'Computer', { id: 'file-upload', icon: 'computer', condition: () => settings.get('FileUpload_Enabled'), action({ rid, tmid, event, messageBox }) { event.preventDefault(); const $input = $(document.createElement('input')); $input.css('display', 'none'); $input.attr({ id: 'fileupload-input', type: 'file', multiple: 'multiple', }); $(document.body).append($input); $input.one('change', function(e) { const filesToUpload = [...e.target.files].map((file) => { Object.defineProperty(file, 'type', { value: mime.lookup(file.name), }); return { file, name: file.name, }; }); fileUpload(filesToUpload, $('.js-input-message', messageBox).get(0), { rid, tmid }); $input.remove(); }); $input.click(); // Simple hack for iOS aka codegueira if (navigator.userAgent.match(/(iPad|iPhone|iPod)/g)) { $input.click(); } }, }); const canGetGeolocation = new ReactiveVar(false); const getGeolocationPermission = () => new Promise((resolve) => { if (!navigator.permissions) { resolve(true); } navigator.permissions.query({ name: 'geolocation' }).then(({ state }) => { resolve(state); }); }); const getGeolocationPosition = () => new Promise((resolvePos) => { navigator.geolocation.getCurrentPosition(resolvePos, () => { resolvePos(false); }, { enableHighAccuracy: true, maximumAge: 0, timeout: 10000, }); }); const getCoordinates = async () => { const status = await getGeolocationPermission(); if (status === 'prompt') { let resolveModal; const modalAnswer = new Promise((resolve) => { resolveModal = resolve; }); modal.open({ title: t('You_will_be_asked_for_permissions'), confirmButtonText: t('Continue'), showCancelButton: true, closeOnConfirm: true, closeOnCancel: true, }, async (isConfirm) => { if (!isConfirm) { resolveModal(false); } const position = await getGeolocationPosition(); if (!position) { const newStatus = getGeolocationPermission(); resolveModal(newStatus); } resolveModal(position); }); const position = await modalAnswer; return position; } if (status === 'denied') { return status; } const position = await getGeolocationPosition(); return position; }; messageBox.actions.add('Share', 'My_location', { id: 'share-location', icon: 'map-pin', condition: () => canGetGeolocation.get(), async action({ rid, tmid }) { const position = await getCoordinates(); if (!position) { return; } if (position === 'denied') { modal.open({ title: t('Cannot_share_your_location'), text: t('The_necessary_browser_permissions_for_location_sharing_are_not_granted'), confirmButtonText: t('Ok'), closeOnConfirm: true, }); return; } const { coords: { latitude, longitude } } = position; const text = `