diff --git a/app/utils.js b/app/utils.js index 1bccfad..8ea256b 100644 --- a/app/utils.js +++ b/app/utils.js @@ -1,207 +1,207 @@ import uuidv4 from 'uuid/v4'; import SillyNames from './SillyNames'; import MaterialColors from './MaterialColors'; import { Clipboard, Dimensions } from 'react-native'; import Contacts from 'react-native-contacts'; const RNFS = require('react-native-fs'); const logfile = RNFS.DocumentDirectoryPath + '/logs.txt'; function log2file(text) { // append to logfile RNFS.appendFile(logfile, text + '\r\n', 'utf8') .then((success) => { console.log(text); }) .catch((err) => { console.log(err.message); }); } function isAnonymous(uri) { if (uri.indexOf('@guest.') > -1 || uri.indexOf('@anonymous.') > -1) { return true } if (uri.indexOf('@192.168.') > -1) { return true; } if (uri.indexOf('@10.') > -1) { return true; } return false; } function appendLeadingZeroes(n){ if (n <= 9) { return "0" + n; } return n; } function timestampedLog() { let current_datetime = new Date(); let formatted_date = current_datetime.getFullYear() + "-" + appendLeadingZeroes(current_datetime.getMonth() + 1) + "-" + appendLeadingZeroes(current_datetime.getDate()) + " " + appendLeadingZeroes(current_datetime.getHours()) + ":" + appendLeadingZeroes(current_datetime.getMinutes()) + ":" + appendLeadingZeroes(current_datetime.getSeconds()); let message = formatted_date; for (var i = 0; i < arguments.length; i++) { let txt = arguments[i] ? arguments[i].toString() : ''; message = message + ' ' + txt; } log2file(message); //console.log(message); } function generateUniqueId() { const uniqueId = uuidv4().replace(/-/g, '').slice(0, 16); return uniqueId; } function normalizeUri(uri, defaultDomain) { let targetUri = uri; let idx = targetUri.indexOf('@'); let username; let domain; if (idx !== -1) { username = targetUri.substring(0, idx); domain = targetUri.substring(idx + 1); } else { username = targetUri; domain = defaultDomain; } - username = username.replace(/[\s()-]/g, ''); + username = username.replace(/[<>\s()\[\]\'\"\~\!\%\&\*\{\}\|\\]/g, ''); return `${username}@${domain}`; } function copyToClipboard(text) { Clipboard.setString(text); return true; } function findContact(uri) { return new Promise((resolve, reject) => { //console.log('findContact') Contacts.checkPermission((err, permission) => { if (err) { //log the error console.log(err); return reject(err); } if (permission === 'authorized') { //console.log('HELLO', uri); Contacts.getContactsByEmailAddress(uri, (err, contacts) => { if (err) { console.log('error getting contacts by email') return reject(err); } if (contacts) { return resolve(contacts) } Contacts.getContactsMatchingString(uri, (err2, contacts2) => { if (err2) { console.log('error matching string') return reject(err2); } console.log(contacts); resolve(contacts2) }); }) } else { console.log('not authortised') reject(new Error('Not Authorised')) } }) }) } function generateSillyName() { const adjective = SillyNames.randomAdjective(); const number = Math.floor(Math.random() * 10); const noun1 = SillyNames.randomNoun(); const noun2 = SillyNames.randomNoun(); return adjective + noun1 + noun2 + number; } function generateMaterialColor(text) { return MaterialColors.generateColor(text); } function generateVideoTrack(stream, width = 640, height = 480) { // const audioCtx = new (window.AudioContext || window.webkitAudioContext)(); // const analyser = audioCtx.createAnalyser(); // const source = audioCtx.createMediaStreamSource(stream); // source.connect(analyser); // analyser.fftSize = 256; // const bufferLength = analyser.frequencyBinCount; // const dataArray = new Uint8Array(bufferLength); // const canvas = Object.assign(document.createElement('canvas'), {width, height}); // const ctx = canvas.getContext('2d'); // const img = new Image(); // const blinkLogo = new Image(); // img.addEventListener('load', () => { // draw(); // }); // const draw = () => { // if (stream.active) { // const drawVisual = requestAnimationFrame(draw); // } // analyser.getByteFrequencyData(dataArray); // ctx.fillStyle = 'rgb(35, 35, 35)'; // ctx.fillRect(0, 0, width, height); // ctx.filter = 'grayscale(100%) brightness(90%)'; // ctx.drawImage(blinkLogo, (width / 2) - 150, (height / 2) - 150, 300, 300); // ctx.filter = 'none'; // ctx.drawImage(img, (width / 2) - 45 , height / 3, 90, 90); // const barWidth = (width / bufferLength) * 2.5; // let barHeight; // let x = 0; // for(var i = 0; i < bufferLength; i++) { // barHeight = dataArray[i] / 2; // ctx.fillStyle = 'rgb(' + (barHeight + 100) + ', 50, 50)'; // ctx.fillRect(x, 2 * height / 3 - barHeight / 2, barWidth, barHeight); // x += barWidth + 1; // } // }; // img.src = 'assets/images/video-camera-slash.png'; // blinkLogo.src = 'assets/images/blink-white-big.png'; // const canvasStream = canvas.captureStream(); return Object.assign(stream.getVideoTracks()[0], {enabled: true}); } function getWindowHeight() { return Dimensions.get('window').height; } exports.copyToClipboard = copyToClipboard; exports.normalizeUri = normalizeUri; exports.generateSillyName = generateSillyName; exports.timestampedLog = timestampedLog; exports.appendLeadingZeroes = appendLeadingZeroes; exports.generateUniqueId = generateUniqueId; exports.generateMaterialColor = generateMaterialColor; exports.generateVideoTrack = generateVideoTrack; exports.getWindowHeight = getWindowHeight; exports.findContact = findContact; exports.findContact = findContact; exports.isAnonymous = isAnonymous;