check if extension has permissions

This commit is contained in:
Jorrin 2024-01-14 18:19:38 +01:00
parent a4b925dc1b
commit 9488437698
3 changed files with 55 additions and 6 deletions

View File

@ -34,17 +34,23 @@ function sendMessage<MessageKey extends keyof MessagesMetadata>(
} }
export async function sendExtensionRequest<T>( export async function sendExtensionRequest<T>(
ops: Omit<MessagesMetadata["makeRequest"]["req"], "requestDomain">, ops: MessagesMetadata["makeRequest"]["req"],
): Promise<ExtensionMakeRequestResponse<T> | null> { ): Promise<ExtensionMakeRequestResponse<T> | null> {
return sendMessage("makeRequest", ops); return sendMessage("makeRequest", ops);
} }
export async function setDomainRule( export async function setDomainRule(
ops: Omit<MessagesMetadata["prepareStream"]["req"], "requestDomain">, ops: MessagesMetadata["prepareStream"]["req"],
): Promise<MessagesMetadata["prepareStream"]["res"] | null> { ): Promise<MessagesMetadata["prepareStream"]["res"] | null> {
return sendMessage("prepareStream", ops); return sendMessage("prepareStream", ops);
} }
export async function sendPage(
ops: MessagesMetadata["openPage"]["req"],
): Promise<MessagesMetadata["openPage"]["res"] | null> {
return sendMessage("openPage", ops);
}
export async function extensionInfo(): Promise< export async function extensionInfo(): Promise<
MessagesMetadata["hello"]["res"] | null MessagesMetadata["hello"]["res"] | null
> { > {

View File

@ -12,6 +12,7 @@ export type ExtensionBaseResponse<T = object> =
export type ExtensionHelloResponse = ExtensionBaseResponse<{ export type ExtensionHelloResponse = ExtensionBaseResponse<{
version: string; version: string;
allowed: boolean; allowed: boolean;
hasPermission: boolean;
}>; }>;
export interface ExtensionMakeRequest extends ExtensionBaseRequest { export interface ExtensionMakeRequest extends ExtensionBaseRequest {
@ -50,6 +51,13 @@ export interface MmMetadata {
req: ExtensionPrepareStreamRequest; req: ExtensionPrepareStreamRequest;
res: ExtensionBaseResponse; res: ExtensionBaseResponse;
}; };
openPage: {
req: ExtensionBaseRequest & {
page: string;
redirectUrl: string;
};
res: ExtensionBaseResponse;
};
} }
interface MpMetadata {} interface MpMetadata {}

View File

@ -3,7 +3,8 @@ import { useNavigate, useParams } from "react-router-dom";
import { useAsync } from "react-use"; import { useAsync } from "react-use";
import type { AsyncReturnType } from "type-fest"; import type { AsyncReturnType } from "type-fest";
import { isExtensionActive } from "@/backend/extension/messaging"; import { isAllowedExtensionVersion } from "@/backend/extension/compatibility";
import { extensionInfo, sendPage } from "@/backend/extension/messaging";
import { import {
fetchMetadata, fetchMetadata,
setCachedMetadata, setCachedMetadata,
@ -43,12 +44,16 @@ export function MetaPart(props: MetaPartProps) {
const navigate = useNavigate(); const navigate = useNavigate();
const { error, value, loading } = useAsync(async () => { const { error, value, loading } = useAsync(async () => {
// check extension const info = await extensionInfo();
const isActive = await isExtensionActive(); const isAllowed = info?.success && isAllowedExtensionVersion(info.version);
if (isAllowed) {
if (!info.hasPermission) throw new Error("extension-no-permission");
}
// use api metadata or providers metadata // use api metadata or providers metadata
const providerApiUrl = getLoadbalancedProviderApiUrl(); const providerApiUrl = getLoadbalancedProviderApiUrl();
if (providerApiUrl && !isActive) { if (providerApiUrl && !isAllowed) {
try { try {
await fetchMetadata(providerApiUrl); await fetchMetadata(providerApiUrl);
} catch (err) { } catch (err) {
@ -105,6 +110,36 @@ export function MetaPart(props: MetaPartProps) {
props.onGetMeta?.(meta, epId); props.onGetMeta?.(meta, epId);
}, []); }, []);
if (error && error.message === "extension-no-permission") {
return (
<ErrorLayout>
<ErrorContainer>
<IconPill icon={Icons.WAND}>
{t("player.metadata.failed.badge")}
</IconPill>
<Title>Configure the extension</Title>
<Paragraph>
You have the browser extension, but we need your permission to get
started using the extension.
</Paragraph>
<Button
onClick={() => {
sendPage({
page: "PermissionGrant",
redirectUrl: window.location.href,
});
}}
theme="purple"
padding="md:px-12 p-2.5"
className="mt-6"
>
Use extension
</Button>
</ErrorContainer>
</ErrorLayout>
);
}
if (error && error.message === "dmca") { if (error && error.message === "dmca") {
return ( return (
<ErrorLayout> <ErrorLayout>