feat: implement browser extension for media publishing and add backend support for media management
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
import type {
|
||||
PublisherBindRequest,
|
||||
PublisherBindResponse,
|
||||
PublisherLocalPlatformState,
|
||||
PublisherPluginPingResponse,
|
||||
PublisherRegisterInstallationPayload,
|
||||
PublisherRegisterInstallationResult,
|
||||
PublisherPublishArticleRequest,
|
||||
PublisherPublishResponse,
|
||||
} from "@geo/shared-types";
|
||||
|
||||
type PluginAction = "ping" | "detectPlatforms" | "registerInstallation" | "bindAccount" | "publishArticle";
|
||||
|
||||
type PluginSuccessPayload<T> = {
|
||||
source: "geo-extension";
|
||||
type: "geo.publisher.response";
|
||||
requestId: string;
|
||||
success: true;
|
||||
data: T;
|
||||
};
|
||||
|
||||
type PluginErrorPayload = {
|
||||
source: "geo-extension";
|
||||
type: "geo.publisher.response";
|
||||
requestId: string;
|
||||
success: false;
|
||||
error: string;
|
||||
};
|
||||
|
||||
type PluginPayloadMap = {
|
||||
ping: PublisherPluginPingResponse;
|
||||
detectPlatforms: PublisherLocalPlatformState[];
|
||||
registerInstallation: PublisherRegisterInstallationResult;
|
||||
bindAccount: PublisherBindResponse;
|
||||
publishArticle: PublisherPublishResponse;
|
||||
};
|
||||
|
||||
function nextRequestId(): string {
|
||||
if (typeof crypto !== "undefined" && "randomUUID" in crypto) {
|
||||
return crypto.randomUUID();
|
||||
}
|
||||
return `geo-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
||||
}
|
||||
|
||||
async function requestPlugin<T extends PluginAction>(
|
||||
action: T,
|
||||
payload?: T extends "bindAccount"
|
||||
? PublisherBindRequest
|
||||
: T extends "registerInstallation"
|
||||
? PublisherRegisterInstallationPayload
|
||||
: T extends "publishArticle"
|
||||
? PublisherPublishArticleRequest
|
||||
: undefined,
|
||||
timeoutMs = 4000,
|
||||
): Promise<PluginPayloadMap[T]> {
|
||||
if (typeof window === "undefined") {
|
||||
throw new Error("plugin bridge is only available in browser contexts");
|
||||
}
|
||||
|
||||
const requestId = nextRequestId();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const timer = window.setTimeout(() => {
|
||||
cleanup();
|
||||
reject(new Error("publisher_plugin_timeout"));
|
||||
}, timeoutMs);
|
||||
|
||||
const handleMessage = (event: MessageEvent<PluginSuccessPayload<PluginPayloadMap[T]> | PluginErrorPayload>) => {
|
||||
if (event.source !== window || !event.data || event.data.type !== "geo.publisher.response") {
|
||||
return;
|
||||
}
|
||||
if (event.data.requestId !== requestId || event.data.source !== "geo-extension") {
|
||||
return;
|
||||
}
|
||||
|
||||
cleanup();
|
||||
if (event.data.success) {
|
||||
resolve(event.data.data);
|
||||
return;
|
||||
}
|
||||
reject(new Error(event.data.error || "publisher_plugin_unknown_error"));
|
||||
};
|
||||
|
||||
const cleanup = () => {
|
||||
window.clearTimeout(timer);
|
||||
window.removeEventListener("message", handleMessage);
|
||||
};
|
||||
|
||||
window.addEventListener("message", handleMessage);
|
||||
window.postMessage(
|
||||
{
|
||||
source: "geo-admin",
|
||||
type: "geo.publisher.request",
|
||||
requestId,
|
||||
action,
|
||||
payload,
|
||||
},
|
||||
window.location.origin,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
export async function pingPublisherPlugin(): Promise<PublisherPluginPingResponse> {
|
||||
try {
|
||||
return await requestPlugin("ping", undefined, 1500);
|
||||
} catch {
|
||||
return {
|
||||
installed: false,
|
||||
capabilities: [],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export async function detectPublisherPlatforms(): Promise<PublisherLocalPlatformState[]> {
|
||||
return requestPlugin("detectPlatforms", undefined, 2500);
|
||||
}
|
||||
|
||||
export async function registerPublisherInstallation(
|
||||
payload: PublisherRegisterInstallationPayload,
|
||||
): Promise<PublisherRegisterInstallationResult> {
|
||||
return requestPlugin("registerInstallation", payload, 5000);
|
||||
}
|
||||
|
||||
export async function bindPublisherPlatform(payload: PublisherBindRequest): Promise<PublisherBindResponse> {
|
||||
return requestPlugin("bindAccount", payload, 10000);
|
||||
}
|
||||
|
||||
export async function publishWithPublisherPlugin(
|
||||
payload: PublisherPublishArticleRequest,
|
||||
): Promise<PublisherPublishResponse> {
|
||||
return requestPlugin("publishArticle", payload, 25000);
|
||||
}
|
||||
Reference in New Issue
Block a user