feat: implement browser extension for media publishing and add backend support for media management
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
import type { PublisherLocalPlatformState, PublisherPluginPingResponse } from "@geo/shared-types";
|
||||
|
||||
import { getApiBaseURL, mediaApi } from "./api";
|
||||
import {
|
||||
detectPublisherPlatforms,
|
||||
pingPublisherPlugin,
|
||||
registerPublisherInstallation,
|
||||
} from "./publisher-plugin";
|
||||
|
||||
export interface PublisherRuntimeState {
|
||||
ping: PublisherPluginPingResponse;
|
||||
localPlatforms: PublisherLocalPlatformState[];
|
||||
pluginInstallationId: number | null;
|
||||
}
|
||||
|
||||
function detectBrowserName(): string {
|
||||
const ua = navigator.userAgent;
|
||||
if (/Edg\//.test(ua)) {
|
||||
return "Edge";
|
||||
}
|
||||
if (/Chrome\//.test(ua) && !/Edg\//.test(ua)) {
|
||||
return "Chrome";
|
||||
}
|
||||
if (/Firefox\//.test(ua)) {
|
||||
return "Firefox";
|
||||
}
|
||||
if (/Safari\//.test(ua) && !/Chrome\//.test(ua)) {
|
||||
return "Safari";
|
||||
}
|
||||
return navigator.platform || "Unknown";
|
||||
}
|
||||
|
||||
export async function loadPublisherRuntimeState(): Promise<PublisherRuntimeState> {
|
||||
const ping = await pingPublisherPlugin();
|
||||
if (!ping.installed || !ping.installation_key) {
|
||||
return {
|
||||
ping,
|
||||
localPlatforms: [],
|
||||
pluginInstallationId: null,
|
||||
};
|
||||
}
|
||||
|
||||
const localPlatforms = await detectPublisherPlatforms();
|
||||
let pluginInstallationId = ping.plugin_installation_id ?? null;
|
||||
|
||||
try {
|
||||
const registration = await mediaApi.registerPluginInstallation({
|
||||
installation_key: ping.installation_key,
|
||||
installation_name: "GEO Publisher",
|
||||
browser_name: detectBrowserName(),
|
||||
client_version: ping.version,
|
||||
});
|
||||
|
||||
await registerPublisherInstallation({
|
||||
plugin_installation_id: registration.plugin_installation_id,
|
||||
installation_token: registration.installation_token,
|
||||
api_base_url: getApiBaseURL(),
|
||||
});
|
||||
|
||||
pluginInstallationId = registration.plugin_installation_id;
|
||||
} catch (error) {
|
||||
console.error("publisher installation registration failed", error);
|
||||
}
|
||||
|
||||
return {
|
||||
ping: {
|
||||
...ping,
|
||||
plugin_installation_id: pluginInstallationId,
|
||||
},
|
||||
localPlatforms,
|
||||
pluginInstallationId,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user