UNPKG

@snap/camera-kit

Version:
93 lines 4.81 kB
import { __awaiter } from "tslib"; import { catchError, firstValueFrom, map, of } from "rxjs"; import { Writer } from "protobufjs"; import { Lens } from "../generated-proto/pb_schema/camera_kit/v3/lens"; import { lensesClientFactory } from "../clients/lensesClient"; import { Injectable } from "../dependency-injection/Injectable"; import { remoteConfigurationFactory } from "../remote-configuration/remoteConfiguration"; import { getLogger } from "../logger/logger"; import { lensSourcesFactory } from "./LensSource"; import { watermarksLensGroup } from "./fetchWatermarkLens"; const logger = getLogger("CameraKitLensSource"); const hasAnyValue = (c) => { var _a, _b; return ((_b = (_a = c.value) === null || _a === void 0 ? void 0 : _a.anyValue) === null || _b === void 0 ? void 0 : _b.value) instanceof Uint8Array; }; const defaultWatermarkLens = { id: "60515300902", name: "Watermark", content: { lnsSha256: "3EDEAEBCD51A547FF4D1F5708FBD6F4D628AD736BEE07AB3844B14E6C69EC510", lnsUrlBolt: "https://bolt-gcdn.sc-cdn.net/3/L6uAe5Fhyg0ZFf3RLsCVZ?bo=EhgaABoAMgF9OgEEQgYIkbHPpgZIAlASYAE%3D&uc=18", }, }; export const cameraKitLensSourceFactory = Injectable(lensSourcesFactory.token, [lensSourcesFactory.token, lensesClientFactory.token, remoteConfigurationFactory.token], (lensSources, lensesClient, remoteConfig) => [ { isGroupOwner(groupId) { return groupId === watermarksLensGroup; }, loadLens() { return __awaiter(this, void 0, void 0, function* () { const lensMessage = yield firstValueFrom(remoteConfig.get("CAMERA_KIT_WATERMARK_LENS").pipe(map((configResults) => { var _a; const lensMessage = (_a = configResults.find(hasAnyValue)) === null || _a === void 0 ? void 0 : _a.value.anyValue.value; if (!lensMessage) throw new Error("Failed to read watermark Lens from COF response."); return lensMessage; }), catchError((error) => { logger.error(error); return of(Lens.encode(Lens.fromPartial(defaultWatermarkLens)).finish()); }))); return Writer.create().uint32(10).bytes(lensMessage).finish(); }); }, loadLensGroup() { return __awaiter(this, void 0, void 0, function* () { throw new Error("Not implemented."); }); }, }, ...lensSources, { isGroupOwner() { return true; }, loadLens(lensId, groupId) { var _a; return __awaiter(this, void 0, void 0, function* () { const result = yield lensesClient.getGroupLens({ lensId, groupId }); if (!result.ok) { const error = result.unwrapErr(); throw new Error(`Cannot load lens lens ${lensId} from group ${groupId}. An error occured in the ` + `gRPC client:\n\t[${error.status}] ${error.statusMessage}`); } const response = result.unwrap(); if (!((_a = response.message) === null || _a === void 0 ? void 0 : _a.lens)) { throw new Error(`Cannot load lens ${lensId} from group ${groupId}. The response did not contain ` + `a lens.\n\t${JSON.stringify(result)} for requestId ${response.headers.get("x-request-id")}`); } return Lens.encode(response.message.lens, Writer.create().uint32(10).fork()).ldelim().finish(); }); }, loadLensGroup(groupId) { var _a; return __awaiter(this, void 0, void 0, function* () { const result = yield lensesClient.getGroup({ id: groupId }); if (!result.ok) { const error = result.unwrapErr(); throw new Error(`Cannot load lens group ${groupId}. An error occurred in the gRPC client:\n` + `\t[${error.status}] ${error.statusMessage}`); } const response = result.unwrap(); if (!((_a = response.message) === null || _a === void 0 ? void 0 : _a.lenses)) { throw new Error(`Cannot load lens group ${groupId}. The response contained no lenses ` + `\n\t${JSON.stringify(response)} for requestId ${response.headers.get("x-request-id")}`); } const writer = Writer.create(); response.message.lenses.forEach((lens) => Lens.encode(lens, writer.uint32(10).fork()).ldelim()); return writer.finish(); }); }, }, ]); //# sourceMappingURL=cameraKitLensSource.js.map