Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change request native permission as sub action #1002

Merged
merged 3 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 0 additions & 41 deletions lib/agent/actions/nativepermission/index.js

This file was deleted.

68 changes: 68 additions & 0 deletions lib/agent/actions/request_permission/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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 = () => {
};
4 changes: 4 additions & 0 deletions lib/agent/providers/geo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ exports.get_location = (cb) => {
hooks.trigger('get_location', cb);
};

exports.get_location_native = (cb) => {
strategies.native((err) => { cb(err); });
};

const strategyCallback = (err, res, cb) => {
if (err) {
logError(err, current);
Expand Down