2022-03-01 18:00:07 +01:00
|
|
|
/*
|
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
2022-03-18 10:52:24 +01:00
|
|
|
import { makeLocationContent, makeBeaconInfoContent } from "matrix-js-sdk/src/content-helpers";
|
2022-03-01 18:00:07 +01:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2022-03-02 14:27:16 +00:00
|
|
|
import { IEventRelation } from "matrix-js-sdk/src/models/event";
|
2022-03-09 18:14:07 +01:00
|
|
|
import { LocationAssetType } from "matrix-js-sdk/src/@types/location";
|
2022-03-11 09:04:22 +00:00
|
|
|
import { THREAD_RELATION_TYPE } from "matrix-js-sdk/src/models/thread";
|
2022-03-01 18:00:07 +01:00
|
|
|
|
|
|
|
|
import { _t } from "../../../languageHandler";
|
|
|
|
|
import Modal from "../../../Modal";
|
|
|
|
|
import QuestionDialog from "../dialogs/QuestionDialog";
|
2022-03-02 14:27:16 +00:00
|
|
|
import SdkConfig from "../../../SdkConfig";
|
2022-03-01 18:00:07 +01:00
|
|
|
|
2022-03-09 18:14:07 +01:00
|
|
|
export enum LocationShareType {
|
|
|
|
|
Own = 'Own',
|
|
|
|
|
Pin = 'Pin',
|
|
|
|
|
Live = 'Live'
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 10:52:24 +01:00
|
|
|
export type LocationShareProps = {
|
|
|
|
|
timeout?: number;
|
|
|
|
|
uri?: string;
|
|
|
|
|
timestamp?: number;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// default duration to 5min for now
|
|
|
|
|
const DEFAULT_LIVE_DURATION = 300000;
|
|
|
|
|
|
|
|
|
|
export type ShareLocationFn = (props: LocationShareProps) => Promise<void>;
|
|
|
|
|
|
|
|
|
|
const handleShareError = (error: Error, openMenu: () => void, shareType: LocationShareType) => {
|
|
|
|
|
const errorMessage = shareType === LocationShareType.Live ?
|
|
|
|
|
"We couldn't start sharing your live location" :
|
|
|
|
|
"We couldn't send your location";
|
|
|
|
|
logger.error(errorMessage, error);
|
|
|
|
|
const analyticsAction = errorMessage;
|
|
|
|
|
const params = {
|
|
|
|
|
title: _t("We couldn't send your location"),
|
|
|
|
|
description: _t("%(brand)s could not send your location. Please try again later.", {
|
|
|
|
|
brand: SdkConfig.get().brand,
|
|
|
|
|
}),
|
|
|
|
|
button: _t('Try again'),
|
|
|
|
|
cancelButton: _t('Cancel'),
|
|
|
|
|
onFinished: (tryAgain: boolean) => {
|
|
|
|
|
if (tryAgain) {
|
|
|
|
|
openMenu();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
Modal.createTrackedDialog(analyticsAction, '', QuestionDialog, params);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const shareLiveLocation = (
|
|
|
|
|
client: MatrixClient, roomId: string, displayName: string, openMenu: () => void,
|
|
|
|
|
): ShareLocationFn => async ({ timeout }) => {
|
|
|
|
|
const description = _t(`%(displayName)s's live location`, { displayName });
|
|
|
|
|
try {
|
|
|
|
|
await client.unstable_createLiveBeacon(
|
|
|
|
|
roomId,
|
|
|
|
|
makeBeaconInfoContent(
|
|
|
|
|
timeout ?? DEFAULT_LIVE_DURATION,
|
|
|
|
|
true, /* isLive */
|
|
|
|
|
description,
|
|
|
|
|
LocationAssetType.Self,
|
|
|
|
|
),
|
|
|
|
|
// use timestamp as unique suffix in interim
|
|
|
|
|
`${Date.now()}`);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
handleShareError(error, openMenu, LocationShareType.Live);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-02 14:27:16 +00:00
|
|
|
export const shareLocation = (
|
|
|
|
|
client: MatrixClient,
|
|
|
|
|
roomId: string,
|
2022-03-09 18:14:07 +01:00
|
|
|
shareType: LocationShareType,
|
2022-03-02 14:27:16 +00:00
|
|
|
relation: IEventRelation | undefined,
|
|
|
|
|
openMenu: () => void,
|
2022-03-18 10:52:24 +01:00
|
|
|
): ShareLocationFn => async ({ uri, timestamp }) => {
|
|
|
|
|
if (!uri) return;
|
2022-03-02 14:27:16 +00:00
|
|
|
try {
|
2022-03-11 09:04:22 +00:00
|
|
|
const threadId = relation?.rel_type === THREAD_RELATION_TYPE.name ? relation.event_id : null;
|
2022-03-09 18:14:07 +01:00
|
|
|
const assetType = shareType === LocationShareType.Pin ? LocationAssetType.Pin : LocationAssetType.Self;
|
2022-03-18 10:52:24 +01:00
|
|
|
await client.sendMessage(
|
|
|
|
|
roomId,
|
|
|
|
|
threadId,
|
|
|
|
|
makeLocationContent(undefined, uri, timestamp, undefined, assetType),
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
handleShareError(error, openMenu, shareType);
|
2022-03-02 14:27:16 +00:00
|
|
|
}
|
|
|
|
|
};
|
2022-03-01 18:00:07 +01:00
|
|
|
|
|
|
|
|
export function textForLocation(
|
|
|
|
|
uri: string,
|
|
|
|
|
ts: number,
|
|
|
|
|
description: string | null,
|
|
|
|
|
): string {
|
|
|
|
|
const date = new Date(ts).toISOString();
|
|
|
|
|
if (description) {
|
|
|
|
|
return `Location "${description}" ${uri} at ${date}`;
|
|
|
|
|
} else {
|
|
|
|
|
return `Location ${uri} at ${date}`;
|
|
|
|
|
}
|
|
|
|
|
}
|