Skip to content

Commit

Permalink
feat: 授权码模式分享支持过期时间
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Jul 2, 2023
1 parent a3efbb9 commit ab622a0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 25 deletions.
17 changes: 12 additions & 5 deletions components/default/Detail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,16 @@ const { currentPost, setCurrentPost } = usePost()
await setCurrentPost(props.pageId)
// datas
const formData = reactive({
shareEnabled: true,
isExpires: false,
})
const attrs = JsonUtil.safeParse<any>(currentPost.post?.attrs ?? "{}", {})
const shareEnabled = attrs["custom-publish-status"] === "publish" || attrs["custom-publish-status"] === "preview"
const isExpires = checkExpires(attrs)
logger.info(`current document status,shareEnabled => ${shareEnabled}, isExpires => ${isExpires}`)
formData.shareEnabled = attrs["custom-publish-status"] === "publish" || attrs["custom-publish-status"] === "preview"
formData.isExpires = checkExpires(attrs)
logger.info(`current document status,shareEnabled => ${formData.shareEnabled}, isExpires => ${formData.isExpires}`)
if (!props.overrideSeo) {
const titleSign = " - " + t("blog.share")
const title = `${currentPost.post.title}${props.showTitleSign ? titleSign : ""}`
Expand Down Expand Up @@ -83,8 +89,9 @@ const VNode = () =>
</script>

<template>
<div v-if="!shareEnabled || isExpires">
<el-empty :description="isExpires ? t('blog.index.no.expires') : t('blog.index.no.permission')"> </el-empty>
<div v-if="!formData.shareEnabled || formData.isExpires">
<el-empty :description="formData.isExpires ? t('blog.index.no.expires') : t('blog.index.no.permission')">
</el-empty>
</div>
<div v-else class="fn__flex-1 protyle" data-loading="finished">
<div class="protyle-content protyle-content--transition" data-fullwidth="true">
Expand Down
25 changes: 15 additions & 10 deletions components/static/Detail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import { JsonUtil, ObjectUtil } from "zhi-common"
import { Post } from "zhi-blog-api"
import { createAppLogger } from "~/common/appLogger"
import { getSummery } from "~/utils/utils"
import { checkExpires, getSummery } from "~/utils/utils"
import { useServerAssets } from "~/plugins/renderer/useServerAssets"
import { useAuthModeFetch } from "~/composables/useAuthModeFetch"
Expand All @@ -52,20 +52,24 @@ const { getFirstImageSrc } = useServerAssets()
const { fetchPublicText } = useAuthModeFetch()
// datas
const formData = reactive({
post: {} as Post,
shareEnabled: true,
isExpires: false,
})
const getPostData = async () => {
const resText = await fetchPublicText(`${id}.json`)
formData.post = JsonUtil.safeParse<Post>(resText, {} as Post)
formData.shareEnabled = !ObjectUtil.isEmptyObject(formData.post)
// logger.info("post=>", formData.post)
// logger.info(`shareEnabled=>${formData.shareEnabled}`)
}
const formData = reactive({
post: {} as Post,
shareEnabled: true,
})
const attrs = JsonUtil.safeParse<any>(formData.post?.attrs ?? "{}", {})
formData.isExpires = checkExpires(attrs)
}
await getPostData()
if (!props.overrideSeo) {
const titleSign = " - " + t("blog.share")
const title = `${formData?.post?.title ?? "404 Not Found"}${props.showTitleSign ? titleSign : ""}`
Expand Down Expand Up @@ -94,8 +98,9 @@ const VNode = () =>
</script>

<template>
<div v-if="!formData.shareEnabled">
<el-empty :description="t('blog.index.no.permission')"> </el-empty>
<div v-if="!formData.shareEnabled || formData.isExpires">
<el-empty :description="formData.isExpires ? t('blog.index.no.expires') : t('blog.index.no.permission')">
</el-empty>
</div>
<div v-else class="fn__flex-1 protyle" data-loading="finished">
<div class="protyle-content protyle-content--transition" data-fullwidth="true">
Expand All @@ -122,4 +127,4 @@ const VNode = () =>
</div>
</template>

<style scoped></style>
<style scoped></style>
33 changes: 24 additions & 9 deletions composables/useStaticShare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,40 @@ import { Post } from "zhi-blog-api"
*/
export const useStaticShare = () => {
const logger = createAppLogger("use-static-share")
const { kernelApi } = useSiyuanApi()
const { blogApi, kernelApi } = useSiyuanApi()

/**
* 打开静态分享
*
* @param {string} pageId - 页面ID
* @param {Post} post - 文章对象
*/
const openStaticShare = async (pageId: string, post: Post) => {
const updateSharePage = async (pageId: string, post: Post) => {
const shareJsonFile = `/data/public/siyuan-blog/${pageId}.json`

// 只暴露有限的属性
const sPost = new Post()
sPost.attrs = post.attrs
sPost.title = post.title
sPost.editorDom = post.editorDom
const sJson = JSON.stringify(sPost) ?? "{}"
await kernelApi.saveTextData(shareJsonFile, sJson)
}

/**
* 打开静态分享
*
* @param {string} pageId - 页面ID
* @param {Post} post - 文章对象
*/
const openStaticShare = async (pageId: string, post: Post) => {
await updateSharePage(pageId, post)
}

/**
* 更新分享
*
* @param pageId - 文档ID
*/
const updateStaticShare = async (pageId: string) => {
const post = await blogApi.getPost(pageId, false, false)
await updateSharePage(pageId, post)
}

/**
* 关闭静态分享
*
Expand All @@ -61,5 +76,5 @@ export const useStaticShare = () => {
await kernelApi.removeFile(shareJsonFile)
}

return { openStaticShare, closeStaticShare }
return { openStaticShare, closeStaticShare, updateStaticShare }
}
7 changes: 6 additions & 1 deletion pages/share.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const { getSetting, updateSetting } = useSettingStore()
const { blogApi, kernelApi } = useSiyuanApi()
const { getShareType, isPrivateShare } = useShareType()
const { updateShareType } = useCommonShareType()
const { openStaticShare, closeStaticShare } = useStaticShare()
const { openStaticShare, closeStaticShare, updateStaticShare } = useStaticShare()
const id = useRouteQuery("id", "")
const origin = useRouteQuery("origin", "")
Expand Down Expand Up @@ -183,6 +183,11 @@ const handleExpiresTime = async () => {
await kernelApi.setBlockAttrs(id.value, {
"custom-expires": expiredTime.toString(),
})
if (formData.accessCodeEnabled) {
await updateStaticShare(id.value)
logger.info("updated static share in auth mode")
}
},
() => {
if (isNaN(expiredTime) || expiredTime < 0 || expiredTime > 7 * 24 * 60 * 60) {
Expand Down

0 comments on commit ab622a0

Please sign in to comment.