feat: Add platform adapters for various content publishing platforms
- Implemented Dongchedi adapter for user detection and publishing. - Implemented Jianshu adapter for user detection and publishing. - Implemented Juejin adapter for user detection and publishing. - Implemented Qiehao adapter for user detection and publishing. - Implemented Smzdm adapter for user detection and publishing. - Implemented Sohuhao adapter for user detection and publishing. - Implemented Toutiaohao adapter for user detection and publishing. - Implemented Wangyihao adapter for user detection and publishing. - Implemented Weixin Gzh adapter for user detection and publishing. - Implemented Zol adapter for user detection and publishing. - Added documentation for manual testing of media publishing.
This commit is contained in:
@@ -35,6 +35,47 @@ type PluginPayloadMap = {
|
||||
publishArticle: PublisherPublishResponse;
|
||||
};
|
||||
|
||||
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null;
|
||||
}
|
||||
|
||||
function isPingPayload(value: unknown): value is PublisherPluginPingResponse {
|
||||
return isPlainObject(value) && typeof value.installed === "boolean";
|
||||
}
|
||||
|
||||
function isDetectPlatformsPayload(value: unknown): value is PublisherLocalPlatformState[] {
|
||||
return Array.isArray(value);
|
||||
}
|
||||
|
||||
function isRegisterInstallationPayload(value: unknown): value is PublisherRegisterInstallationResult {
|
||||
return isPlainObject(value) && typeof value.success === "boolean" && typeof value.plugin_installation_id === "number";
|
||||
}
|
||||
|
||||
function isBindPayload(value: unknown): value is PublisherBindResponse {
|
||||
return isPlainObject(value) && typeof value.success === "boolean" && typeof value.platform_id === "string";
|
||||
}
|
||||
|
||||
function isPublishPayload(value: unknown): value is PublisherPublishResponse {
|
||||
return isPlainObject(value) && Array.isArray(value.results);
|
||||
}
|
||||
|
||||
function isPayloadValid<T extends PluginAction>(action: T, value: unknown): value is PluginPayloadMap[T] {
|
||||
switch (action) {
|
||||
case "ping":
|
||||
return isPingPayload(value);
|
||||
case "detectPlatforms":
|
||||
return isDetectPlatformsPayload(value);
|
||||
case "registerInstallation":
|
||||
return isRegisterInstallationPayload(value);
|
||||
case "bindAccount":
|
||||
return isBindPayload(value);
|
||||
case "publishArticle":
|
||||
return isPublishPayload(value);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function nextRequestId(): string {
|
||||
if (typeof crypto !== "undefined" && "randomUUID" in crypto) {
|
||||
return crypto.randomUUID();
|
||||
@@ -42,6 +83,12 @@ function nextRequestId(): string {
|
||||
return `geo-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
||||
}
|
||||
|
||||
function sleep(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
window.setTimeout(resolve, ms);
|
||||
});
|
||||
}
|
||||
|
||||
async function requestPlugin<T extends PluginAction>(
|
||||
action: T,
|
||||
payload?: T extends "bindAccount"
|
||||
@@ -75,6 +122,10 @@ async function requestPlugin<T extends PluginAction>(
|
||||
|
||||
cleanup();
|
||||
if (event.data.success) {
|
||||
if (!isPayloadValid(action, event.data.data)) {
|
||||
reject(new Error("publisher_plugin_invalid_response"));
|
||||
return;
|
||||
}
|
||||
resolve(event.data.data);
|
||||
return;
|
||||
}
|
||||
@@ -104,10 +155,15 @@ export async function pingPublisherPlugin(): Promise<PublisherPluginPingResponse
|
||||
try {
|
||||
return await requestPlugin("ping", undefined, 1500);
|
||||
} catch {
|
||||
return {
|
||||
installed: false,
|
||||
capabilities: [],
|
||||
};
|
||||
try {
|
||||
await sleep(300);
|
||||
return await requestPlugin("ping", undefined, 2000);
|
||||
} catch {
|
||||
return {
|
||||
installed: false,
|
||||
capabilities: [],
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user