Skip to content

Commit

Permalink
feat: #83 多平台分发需要考虑同步更新思源笔记
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Aug 1, 2023
1 parent 44261c9 commit 103203d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
6 changes: 6 additions & 0 deletions src/adaptors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { useCsdnWeb } from "~/src/adaptors/web/csdn/useCsdnWeb.ts"
import { useJianshuWeb } from "~/src/adaptors/web/jianshu/useJianshuWeb.ts"
import { useJuejinWeb } from "~/src/adaptors/web/juejin/useJuejinWeb.ts"
import { useWechatWeb } from "~/src/adaptors/web/wechat/useWechatWeb.ts"
import { useSiyuanApi } from "~/src/composables/useSiyuanApi.ts"

/**
* 适配器统一入口
Expand Down Expand Up @@ -101,6 +102,11 @@ class Adaptors {
blogAdaptor = webApi
break
}
case SubPlatformType.System_Siyuan: {
const { blogApi } = useSiyuanApi()
blogAdaptor = blogApi
break
}
default: {
break
}
Expand Down
51 changes: 29 additions & 22 deletions src/composables/usePublish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,21 @@ const usePublish = () => {
const api = Utils.blogApi(appInstance, apiAdaptor)
logger.info("api=>", api)

// 检测是否发布
const posidKey = singleFormData.cfg.posidKey
if (StrUtil.isEmptyString(posidKey)) {
throw new Error("配置错误,posidKey不能为空,请检查配置")
if (isSys) {
// 内置平台直接用思源的ID
singleFormData.postid = id
} else {
// 检测是否发布
const posidKey = singleFormData.cfg.posidKey
if (StrUtil.isEmptyString(posidKey)) {
throw new Error("配置错误,posidKey不能为空,请检查配置")
}
const postMeta = singleFormData.setting[id] ?? {}
singleFormData.postid = postMeta[posidKey] ?? ""
}
const postMeta = singleFormData.setting[id] ?? {}
singleFormData.postid = postMeta[posidKey] ?? ""
singleFormData.isAdd = StrUtil.isEmptyString(singleFormData.postid)

if (!singleFormData.isAdd || isSys) {
logger.info("文章已发布,准备更新")
const post = new Post()
post.title = doc.title
post.description = doc.description
// result 正常情况下就是 postid
const result = await api.editPost(singleFormData.postid, post)
logger.info("edit post=>", result)
} else {
singleFormData.isAdd = StrUtil.isEmptyString(singleFormData.postid)
if (singleFormData.isAdd) {
logger.info("文章未发布,准备发布")
const post = new Post()
post.title = doc.title
Expand All @@ -103,14 +100,24 @@ const usePublish = () => {

// 写入postid到配置
singleFormData.postid = result
const posidKey = singleFormData.cfg.posidKey
const postMeta = singleFormData.setting[id] ?? {}
postMeta[posidKey] = singleFormData.postid
singleFormData.setting[id] = postMeta
await updateSetting(singleFormData.setting)
logger.info("new post=>", result)
} else {
logger.info("文章已发布,准备更新")
const post = new Post()
post.title = doc.title
post.description = doc.description
// result 正常情况下就是 postid
const result = await api.editPost(singleFormData.postid, post)
logger.info("edit post=>", result)
}
const previewUrl = await api.getPreviewUrl(singleFormData.postid)
const isAbsoluteUrl = /^http/.test(previewUrl);
singleFormData.previewUrl = isAbsoluteUrl ? previewUrl : `${singleFormData.cfg.home}${previewUrl}`;
const isAbsoluteUrl = /^http/.test(previewUrl)
singleFormData.previewUrl = isAbsoluteUrl ? previewUrl : `${singleFormData.cfg.home}${previewUrl}`

singleFormData.publishProcessStatus = true
} catch (e) {
Expand Down Expand Up @@ -171,13 +178,13 @@ const usePublish = () => {
// 移除文章发布信息
const posidKey = singleFormData.cfg.posidKey
if (!StrUtil.isEmptyString(posidKey)) {
const postMeta = singleFormData.setting[id] ?? {};
const updatedPostMeta = { ...postMeta };
const postMeta = singleFormData.setting[id] ?? {}
const updatedPostMeta = { ...postMeta }
if (updatedPostMeta.hasOwnProperty(posidKey)) {
delete updatedPostMeta[posidKey];
delete updatedPostMeta[posidKey]
}

singleFormData.setting[id] = updatedPostMeta;
singleFormData.setting[id] = updatedPostMeta
await updateSetting(singleFormData.setting)
logger.info(`[${key}] [${id}] 文章发布信息已移除`)
}
Expand Down

0 comments on commit 103203d

Please sign in to comment.