Skip to content

Commit

Permalink
Ask for permission only when button is clicked
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriellsh committed Jun 23, 2020
1 parent 65384bb commit d810aa5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
55 changes: 35 additions & 20 deletions app/ui-message/client/messageBox/messageBoxActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,43 @@ messageBox.actions.add('Add_files_from', 'Computer', {
},
});

const geolocation = new ReactiveVar(false);
const canGetGeolocation = new ReactiveVar(false);

const getGeolocationPermission = () => new Promise((resolve) => {
if (!navigator.permissions) { resolve(true); }
navigator.permissions.query({ name: 'geolocation' }).then((result) => {
if (result.state === 'denied') {
resolve(false);
} else {
resolve(true);
}
});
});

const getGeoPosition = () => new Promise((resolve) => navigator.geolocation.getCurrentPosition(resolve, () => { resolve(false); }, {
enableHighAccuracy: true,
maximumAge: 0,
timeout: 10000,
}));

messageBox.actions.add('Share', 'My_location', {
id: 'share-location',
icon: 'map-pin',
condition: () => geolocation.get() !== false,
action({ rid, tmid }) {
const position = geolocation.get();
condition: () => canGetGeolocation.get(),
async action({ rid, tmid }) {
const hasPermission = await getGeolocationPermission();
if (!hasPermission) {
modal.open({
title: t('Share_Location_Error_Title'),
text: t('Share_Location_No_Permission'),
confirmButtonText: t('Ok'),
closeOnConfirm: true,
closeOnCancel: true,
});
return;
}
const position = await getGeoPosition();

const { latitude, longitude } = position.coords;
const text = `<div class="upload-preview"><div class="upload-preview-file" style="background-size: cover; box-shadow: 0 0 0px 1px #dfdfdf; border-radius: 2px; height: 250px; width:100%; max-width: 500px; background-image:url(https://maps.googleapis.com/maps/api/staticmap?zoom=14&size=500x250&markers=color:gray%7Clabel:%7C${ latitude },${ longitude }&key=${ settings.get('MapView_GMapsAPIKey') })" ></div></div>`;

Expand Down Expand Up @@ -102,24 +131,10 @@ messageBox.actions.add('Share', 'My_location', {
});

Meteor.startup(() => {
const handleGeolocation = (position) => geolocation.set(position);
const handleGeolocationError = () => geolocation.set(false);

Tracker.autorun(() => {
const isMapViewEnabled = settings.get('MapView_Enabled') === true;
const isGeolocationWatchSupported = navigator.geolocation && navigator.geolocation.watchPosition;
const isGeolocationCurrentPositionSupported = navigator.geolocation && navigator.geolocation.getCurrentPosition;
const googleMapsApiKey = settings.get('MapView_GMapsAPIKey');
const canGetGeolocation = isMapViewEnabled && isGeolocationWatchSupported && (googleMapsApiKey && googleMapsApiKey.length);

if (!canGetGeolocation) {
geolocation.set(false);
return;
}

navigator.geolocation.watchPosition(handleGeolocation, handleGeolocationError, {
enableHighAccuracy: true,
maximumAge: 0,
timeout: 10000,
});
canGetGeolocation.set(isMapViewEnabled && isGeolocationCurrentPositionSupported && googleMapsApiKey && googleMapsApiKey.length);
});
});
2 changes: 2 additions & 0 deletions packages/rocketchat-i18n/i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -3218,6 +3218,8 @@
"Setup_Wizard": "Setup Wizard",
"Setup_Wizard_Info": "We'll guide you through setting up your first admin user, configuring your organisation and registering your server to receive free push notifications and more.",
"Share_Location_Title": "Share Location?",
"Share_Location_Error_Title": "Cannot share your location...",
"Share_Location_No_Permission": "The necessary browser permissions for location sharing are not granted",
"Shared_Location": "Shared Location",
"Shared_Secret": "Shared Secret",
"Should_be_a_URL_of_an_image": "Should be a URL of an image.",
Expand Down

0 comments on commit d810aa5

Please sign in to comment.