Skip to content

Commit

Permalink
feat: #1053 新增发布平台 telegra.ph
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Feb 28, 2024
1 parent 9e7fb80 commit 1f53556
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 14 deletions.
63 changes: 62 additions & 1 deletion src/adaptors/api/telegraph/telegraphApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,73 @@
*/

import { BaseBlogApi } from "~/src/adaptors/api/base/baseBlogApi.ts"
import { UserBlog } from "zhi-blog-api"
import { TelegraphConfig } from "~/src/adaptors/api/telegraph/telegraphConfig.ts"

/**
* Telegraph API 适配器
*
* @see https://telegra.ph/ telegra.ph
*/
class TelegraphApiAdaptor extends BaseBlogApi {}
class TelegraphApiAdaptor extends BaseBlogApi {
public async getUsersBlogs(): Promise<UserBlog[]> {
const result: UserBlog[] = []

const checkJson = await this.telegraphRequest("/check", "page_id=0", "POST")
if (checkJson.error) {
throw new Error("telegraph request error =>" + checkJson.error)
}
this.logger.debug("checkJson =>", checkJson)

// 数据适配
const userblog: UserBlog = new UserBlog()
const cfg = this.cfg as TelegraphConfig
userblog.blogid = checkJson.save_hash
userblog.blogName = "telegra.ph"
userblog.url = cfg.apiUrl
// 元数据映射
// @since 1.20.0
userblog.metadataMap = {
password: checkJson.save_hash,
}
result.push(userblog)
this.logger.debug("get telegraph cfg =>", result)

return result
}

// ================
// private methods
// ================
/**
* 向 Telegraph 请求数据
*/
private async telegraphRequest(
url: string,
params?: any,
method: "GET" | "POST" | "PUT" | "DELETE" = "POST",
header: Record<any, any> = {}
) {
const contentType = "text/plain"
const headers = {
"Content-Type": contentType,
origin: "https://telegra.ph",
referer: "https://telegra.ph/",
...header,
}

// 输出日志
const apiUrl = `${this.cfg.apiUrl}${url}`
this.logger.debug("向 Telegraph 请求数据,apiUrl =>", apiUrl)
this.logger.debug("向 Telegraph 请求数据,content =>", params)

// 使用兼容的fetch调用并返回统一的JSON数据
const body = params
const resJson = await this.proxyFetch(apiUrl, [headers], body, method, contentType)
this.logger.debug("向 Telegraph 请求数据,resJson =>", resJson)

return resJson ?? null
}
}

export { TelegraphApiAdaptor }
9 changes: 5 additions & 4 deletions src/adaptors/api/telegraph/telegraphConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,19 @@
* questions.
*/

import {CommonBlogConfig} from "~/src/adaptors/api/base/commonBlogConfig.ts"
import {PasswordType} from "zhi-blog-api";
import { CommonBlogConfig } from "~/src/adaptors/api/base/commonBlogConfig.ts"
import { PasswordType } from "zhi-blog-api"

/**
* Telegraph 配置
*/
class TelegraphConfig extends CommonBlogConfig {
constructor(telegraphUrl: string, middlewareUrl?: string) {
super(telegraphUrl, telegraphUrl, "", "", middlewareUrl)
constructor(telegraphUrl: string, telegraphToken: string, middlewareUrl?: string) {
super(telegraphUrl, "https://edit.telegra.ph", "", telegraphToken, middlewareUrl)

this.passwordType = PasswordType.PasswordType_Token
this.previewUrl = "/[postid]"
this.allowPreviewUrlChange = false
}
}

Expand Down
23 changes: 14 additions & 9 deletions src/adaptors/api/telegraph/useTelegraphApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
* questions.
*/

import {TelegraphConfig} from "~/src/adaptors/api/telegraph/telegraphConfig.ts"
import {createAppLogger} from "~/src/utils/appLogger.ts"
import {PublisherAppInstance} from "~/src/publisherAppInstance.ts"
import {usePublishSettingStore} from "~/src/stores/usePublishSettingStore.ts";
import {JsonUtil, ObjectUtil, StrUtil} from "zhi-common";
import {Utils} from "~/src/utils/utils.ts";
import {getDynPostidKey} from "~/src/platforms/dynamicConfig.ts";
import {TelegraphApiAdaptor} from "~/src/adaptors/api/telegraph/telegraphApiAdaptor.ts";
import { TelegraphConfig } from "~/src/adaptors/api/telegraph/telegraphConfig.ts"
import { createAppLogger } from "~/src/utils/appLogger.ts"
import { PublisherAppInstance } from "~/src/publisherAppInstance.ts"
import { usePublishSettingStore } from "~/src/stores/usePublishSettingStore.ts"
import { JsonUtil, ObjectUtil, StrUtil } from "zhi-common"
import { Utils } from "~/src/utils/utils.ts"
import { getDynPostidKey } from "~/src/platforms/dynamicConfig.ts"
import { TelegraphApiAdaptor } from "~/src/adaptors/api/telegraph/telegraphApiAdaptor.ts"

const useTelegraphApi = async (key: string, newCfg?: TelegraphConfig) => {
const logger = createAppLogger("use-telegraph-api")
Expand All @@ -49,7 +49,12 @@ const useTelegraphApi = async (key: string, newCfg?: TelegraphConfig) => {

if (ObjectUtil.isEmptyObject(cfg)) {
const telegraphUrl = Utils.emptyOrDefault(process.env.VITE_TELEGRAPH_URL, "https://telegra.ph")
cfg = new TelegraphConfig(telegraphUrl)
const middlewareUrl = Utils.emptyOrDefault(
process.env.VITE_MIDDLEWARE_URL,
"https://api.terwer.space/api/middleware"
)
const telegraphToken = Utils.emptyOrDefault(process.env.VITE_TELEGRAPH_TOKEN, "")
cfg = new TelegraphConfig(telegraphUrl, telegraphToken, middlewareUrl)
logger.info("Configuration is empty, using default environment variables.")
} else {
logger.info("Using configuration from settings...")
Expand Down
1 change: 1 addition & 0 deletions testdata/telegra-ph/telegra.ph.http
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
POST https://edit.telegra.ph/check
origin: https://telegra.ph
referer: https://telegra.ph/
Content-Type: text/plain

page_id=0

Expand Down

0 comments on commit 1f53556

Please sign in to comment.