Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Ref

Manav Rathi56f584c086b7
diff --git a/web/apps/photos/src/components/Collections/AlbumCastDialog.tsx b/web/apps/photos/src/components/Collections/AlbumCastDialog.tsx
index e503e956ea..9516743db8 100644
--- a/web/apps/photos/src/components/Collections/AlbumCastDialog.tsx
+++ b/web/apps/photos/src/components/Collections/AlbumCastDialog.tsx
@@ -1,13 +1,12 @@
import { TitledMiniDialog } from "@/base/components/MiniDialog";
import { ActivityIndicator } from "@/base/components/mui/ActivityIndicator";
-import { boxSeal } from "@/base/crypto";
-import { newID } from "@/base/id";
import log from "@/base/log";
import type { Collection } from "@/media/collection";
import { photosDialogZIndex } from "@/new/photos/components/utils/z-index";
import {
- publicKeyForPairingCode,
+ publishCastPayload,
revokeAllCastTokens,
+ unknownDeviceCodeErrorMessage,
} from "@/new/photos/services/cast";
import { loadCast } from "@/new/photos/utils/chromecast-sender";
import SingleInputForm, {
@@ -55,42 +54,21 @@ export const AlbumCastDialog: React.FC<AlbumCastDialogProps> = ({
setFieldError,
) => {
try {
- if (await doCast(value.trim())) {
- onClose();
- } else {
- setFieldError(t("tv_not_found"));
- }
+ await publishCastPayload(value.trim(), collection);
+ onClose();
} catch (e) {
log.error("Failed to cast", e);
- setFieldError(t("generic_error_retry"));
+ if (
+ e instanceof Error &&
+ e.message == unknownDeviceCodeErrorMessage
+ ) {
+ setFieldError(t("tv_not_found"));
+ } else {
+ setFieldError(t("generic_error_retry"));
+ }
}
};
- const doCast = async (code: string) => {
- // Find out the public key associated with the given pairing code (if
- // indeed a device has published one).
- const publicKey = await publicKeyForPairingCode(code);
- if (!publicKey) return false;
-
- // Generate random id.
- const castToken = newID("cast_");
-
- // Publish the payload so that the other end can use it.
- const payload = JSON.stringify({
- castToken: castToken,
- collectionID: collection.id,
- collectionKey: collection.key,
- });
- const encryptedPayload = await boxSeal(btoa(payload), publicKey);
-
- await castGateway.publishCastPayload(
- code,
- encryptedPayload,
- collection.id,
- castToken,
- );
- };
-
useEffect(() => {
if (view === "auto") {
loadCast().then(async (cast) => {
@@ -111,7 +89,7 @@ export const AlbumCastDialog: React.FC<AlbumCastDialogProps> = ({
const code = obj.code;
if (code) {
- doCast(code)
+ publishCastPayload(`${code}`, collection)
.then(() => {
setView("choose");
onClose();
diff --git a/web/packages/new/photos/services/cast.ts b/web/packages/new/photos/services/cast.ts
index 1c4cb74287..1e0029d6ea 100644
--- a/web/packages/new/photos/services/cast.ts
+++ b/web/packages/new/photos/services/cast.ts
@@ -1,15 +1,20 @@
+import { boxSeal } from "@/base/crypto";
import { authenticatedRequestHeaders, ensureOk } from "@/base/http";
+import { newID } from "@/base/id";
import { apiURL } from "@/base/origins";
+import type { Collection } from "@/media/collection";
import { z } from "zod";
/**
* Revoke all existing outstanding cast tokens for the current user on remote.
*/
export const revokeAllCastTokens = async () =>
- fetch(await apiURL("/cast/revoke-all-tokens"), {
- method: "DELETE",
- headers: await authenticatedRequestHeaders(),
- });
+ ensureOk(
+ await fetch(await apiURL("/cast/revoke-all-tokens"), {
+ method: "DELETE",
+ headers: await authenticatedRequestHeaders(),
+ }),
+ );
/**
* Fetch the public key (represented as a base64 string) associated with the
@@ -26,23 +31,47 @@ export const publicKeyForPairingCode = async (code: string) => {
.publicKey;
};
+export const unknownDeviceCodeErrorMessage = "Unknown device code";
+
/**
* Publish encrypted payload for a cast session so that the paired device can
* obtain the information it needs to cast a collection.
+ *
+ * If no device was found for the given {@link deviceCode}, then this function
+ * will throw an error with the message
+ * @param deviceCode The PIN / device code that the user entered to pair with
+ * the casting end.
+ *
+ * @param collection The collection that the user wants to cast.
*/
export const publishCastPayload = async (
- code: string,
- castPayload: string,
- collectionID: number,
- castToken: string,
-) =>
- fetch(await apiURL("/cast/cast-data"), {
+ deviceCode: string,
+ collection: Collection,
+) => {
+ // Find out the public key associated with the given pairing code (if
+ // indeed a device has published one).
+ const publicKey = await publicKeyForPairingCode(deviceCode);
+ if (!publicKey) throw new Error(unknownDeviceCodeErrorMessage);
+
+ // Generate random id.
+ const castToken = newID("cast_");
+
+ // Publish the payload so that the other end can use it.
+ const payload = JSON.stringify({
+ castToken,
+ collectionID: collection.id,
+ collectionKey: collection.key,
+ });
+ const encryptedPayload = await boxSeal(btoa(payload), publicKey);
+ const res = await fetch(await apiURL("/cast/cast-data"), {
method: "POST",
headers: await authenticatedRequestHeaders(),
body: JSON.stringify({
- deviceCode: code,
- encPayload: castPayload,
- collectionID: collectionID,
- castToken: castToken,
+ castToken,
+ deviceCode,
+ encPayload: encryptedPayload,
+ collectionID: collection.id,
}),
});
+ ensureOk(res);
+};