2026-04-03 00:39:15 +08:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-03 17:48:30 +08:00
|
|
|
function normalizePingResponse(ping: PublisherPluginPingResponse | null | undefined): PublisherPluginPingResponse {
|
|
|
|
|
if (!ping || typeof ping.installed !== "boolean") {
|
|
|
|
|
return {
|
|
|
|
|
installed: false,
|
|
|
|
|
capabilities: [],
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
installed: ping.installed,
|
|
|
|
|
version: ping.version,
|
|
|
|
|
capabilities: Array.isArray(ping.capabilities) ? ping.capabilities : [],
|
|
|
|
|
installation_key: ping.installation_key,
|
|
|
|
|
plugin_installation_id: ping.plugin_installation_id ?? null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-03 00:39:15 +08:00
|
|
|
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> {
|
2026-04-03 17:48:30 +08:00
|
|
|
const ping = normalizePingResponse(await pingPublisherPlugin());
|
2026-04-03 00:39:15 +08:00
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
}
|