1
0
mirror of https://github.com/matrix-org/matrix-react-sdk.git synced 2025-07-30 02:21:17 +03:00

Live location sharing - handle geolocation errors (#8179)

* display live share warning only when geolocation is happening

Signed-off-by: Kerry Archibald <kerrya@element.io>

* kill beacons when geolocation is unavailable or permissions denied

Signed-off-by: Kerry Archibald <kerrya@element.io>

* polish and comments

Signed-off-by: Kerry Archibald <kerrya@element.io>
This commit is contained in:
Kerry
2022-03-28 18:46:39 +02:00
committed by GitHub
parent 2520d81784
commit d2b97e251e
12 changed files with 287 additions and 60 deletions

View File

@ -14,11 +14,13 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import { MockedObject } from "jest-mock";
import { makeBeaconInfoContent, makeBeaconContent } from "matrix-js-sdk/src/content-helpers";
import { MatrixEvent } from "matrix-js-sdk/src/matrix";
import { M_BEACON, M_BEACON_INFO } from "matrix-js-sdk/src/@types/beacon";
import { LocationAssetType } from "matrix-js-sdk/src/@types/location";
import { MockedObject } from "jest-mock";
import { getMockGeolocationPositionError } from "./location";
type InfoContentProps = {
timeout: number;
@ -150,16 +152,31 @@ export const mockGeolocation = (): MockedObject<Geolocation> => {
* ```
* will call the provided handler with a mock position at
* next tick, 1000ms, 6000ms, 6050ms
*
* to produce errors provide an array of error codes
* that will be applied to the delay with the same index
* eg:
* ```
* // return two good positions, then a permission denied error
* geolocation.watchPosition.mockImplementation(watchPositionMockImplementation(
* [0, 1000, 3000], [0, 0, 1]),
* );
* ```
* See for error codes: https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPositionError
*/
export const watchPositionMockImplementation = (delays: number[]) => {
return (callback: PositionCallback) => {
export const watchPositionMockImplementation = (delays: number[], errorCodes: number[] = []) => {
return (callback: PositionCallback, error: PositionErrorCallback) => {
const position = makeGeolocationPosition({});
let totalDelay = 0;
delays.map(delayMs => {
delays.map((delayMs, index) => {
totalDelay += delayMs;
const timeout = setTimeout(() => {
callback({ ...position, timestamp: position.timestamp + totalDelay });
if (errorCodes[index]) {
error(getMockGeolocationPositionError(errorCodes[index], 'error message'));
} else {
callback({ ...position, timestamp: position.timestamp + totalDelay });
}
}, totalDelay);
return timeout;
});