lib/agent/actions/request_permission/index.js
const { EventEmitter } = require('events');
const socket = require('../../socket');
const geo = require('../../providers/geo');
const location = require('../../triggers/location');
const permissionFile = require('../../../utils/permissionfile');
const osName = process.platform.replace('win32', 'windows').replace('darwin', 'mac');
const permissionFunction = 'check-location-perms';
let emitter;
const done = (id, err) => {
if (!emitter) emitter = new EventEmitter();
emitter.emit('end', id, err);
};
/**
* Requests native permission on MacOS.
*
* @param {function} cb - The callback function to be executed after the permission is requested.
* @return {void}
*/
// eslint-disable-next-line consistent-return
const requestNativePermission = (cb) => {
if (osName.localeCompare('mac') !== 0) return cb(new Error('Action only allowed on MacOS'));
// eslint-disable-next-line consistent-return
socket.writeMessage(permissionFunction, () => {
const permissionNative = permissionFile.getData('nativeLocation');
if (permissionNative.localeCompare('false') !== 0 && permissionNative.localeCompare('true') !== 0) {
try {
geo.get_location_native((err) => {
cb(err);
});
} catch (ex) {
cb(new Error(ex.message));
}
} else {
cb(null);
}
});
};
/**
* Starts the process based on the provided ID and options.
*
* @param {type} id - The ID for the process
* @param {type} opts - The options for the process
* @param {type} cb - Callback function
* @return {type} description of return value
*/
// eslint-disable-next-line consistent-return
exports.start = (id, opts, cb) => {
// eslint-disable-next-line consistent-return
cb();
if (!opts.name) return done(id, new Error('Invalid permission name'));
switch (opts.name) {
case 'native_location':
requestNativePermission((err) => {
setTimeout(() => {
location.start();
}, 1000 * 60 * 3);
done(id, err);
});
break;
default:
done(id, new Error('Invalid permission name'));
}
};
exports.stop = () => {
};