Skip to content

Commit 581ca9e

Browse files
committed
fix: empty data compatibility
1 parent 0391607 commit 581ca9e

File tree

3 files changed

+94
-84
lines changed

3 files changed

+94
-84
lines changed

src/modules/aggregate/aggregate.controller.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,21 @@ export class AggregateController {
2323
@CacheKey(CacheKeys.AggregateCatch)
2424
@CacheTTL(300)
2525
async aggregate(@IsMaster() isMaster: boolean) {
26-
const [user, categories, pageMeta, lastestNoteNid] = await Promise.all([
26+
const tasks = await Promise.allSettled([
2727
this.configsService.getMaster(),
2828
this.aggregateService.getAllCategory(),
2929
this.aggregateService.getAllPages(),
3030
this.aggregateService
3131
.getLatestNote(addConditionToSeeHideContent(isMaster))
3232
.then((r) => r.nid),
3333
])
34+
const [user, categories, pageMeta, lastestNoteNid] = tasks.map((t) => {
35+
if (t.status === 'fulfilled') {
36+
return t.value
37+
} else {
38+
return null
39+
}
40+
})
3441
return {
3542
user,
3643
seo: this.configsService.get('seo'),

src/modules/markdown/markdown.controller.ts

Lines changed: 85 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -173,90 +173,94 @@ export class MarkdownController {
173173
@HTTPDecorators.Bypass
174174
@CacheTTL(60 * 60)
175175
async renderArticle(@Param() params: MongoIdDto) {
176-
const { id } = params
177-
const now = performance.now()
178-
const {
179-
html: markdown,
180-
document,
181-
type,
182-
} = await this.service.renderArticle(id)
176+
try {
177+
const { id } = params
178+
const now = performance.now()
179+
const {
180+
html: markdown,
181+
document,
182+
type,
183+
} = await this.service.renderArticle(id)
183184

184-
const style = await this.assetService.getAsset('markdown.css', {
185-
encoding: 'utf8',
186-
})
185+
const style = await this.assetService.getAsset('markdown.css', {
186+
encoding: 'utf8',
187+
})
187188

188-
const relativePath = (() => {
189-
switch (type.toLowerCase()) {
190-
case 'post':
191-
return `/posts/${((document as PostModel).category as any).slug}/${
192-
(document as PostModel).slug
193-
}`
194-
case 'note':
195-
return `/notes/${(document as NoteModel).nid}`
196-
case 'page':
197-
return `/${(document as PageModel).slug}`
198-
}
199-
})()
200-
const {
201-
url: { webUrl },
202-
} = await this.configs.waitForConfigReady()
203-
const url = new URL(relativePath, webUrl)
204-
205-
const html = minify(
206-
`
207-
<!DOCTYPE html>
208-
<html lang="en">
209-
<head>
210-
<meta charset="UTF-8">
211-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
212-
<meta http-equiv="X-UA-Compatible" content="ie=edge">
213-
<meta name="referrer" content="no-referrer">
214-
<style>
215-
${style}
216-
</style>
217-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/mx-space/assets@master/newsprint.css">
218-
<title>${document.title}</title>
219-
</head>
220-
<body>
221-
<article>
222-
<h1>${document.title}</h1>
223-
${markdown}
224-
</article>
225-
</body>
226-
<footer style="text-align: right; padding: 2em 0;">
227-
<p>本文渲染于 ${dayjs().format('llll')},用时 ${
228-
performance.now() - now
229-
}ms</p>
230-
<p>原文地址:<a href="${url}">${decodeURIComponent(
231-
url.toString(),
232-
)}</a></p>
233-
</footer>
234-
<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
235-
<link rel="stylesheet"
236-
href="//cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.2.0/build/styles/default.min.css">
237-
<script src="//cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.2.0/build/highlight.min.js"></script>
238-
<script>
239-
window.mermaid.initialize({
240-
theme: 'default',
241-
startOnLoad: false,
242-
})
243-
window.mermaid.init(undefined, '.mermaid')
189+
const relativePath = (() => {
190+
switch (type.toLowerCase()) {
191+
case 'post':
192+
return `/posts/${((document as PostModel).category as any).slug}/${
193+
(document as PostModel).slug
194+
}`
195+
case 'note':
196+
return `/notes/${(document as NoteModel).nid}`
197+
case 'page':
198+
return `/${(document as PageModel).slug}`
199+
}
200+
})()
201+
const {
202+
url: { webUrl },
203+
} = await this.configs.waitForConfigReady()
204+
const url = new URL(relativePath, webUrl)
244205

245-
document.addEventListener('DOMContentLoaded', (event) => {
246-
document.querySelectorAll('pre code').forEach((el) => {
247-
hljs.highlightElement(el);
206+
const html = minify(
207+
`
208+
<!DOCTYPE html>
209+
<html lang="en">
210+
<head>
211+
<meta charset="UTF-8">
212+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
213+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
214+
<meta name="referrer" content="no-referrer">
215+
<style>
216+
${style}
217+
</style>
218+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/mx-space/assets@master/newsprint.css">
219+
<title>${document.title}</title>
220+
</head>
221+
<body>
222+
<article>
223+
<h1>${document.title}</h1>
224+
${markdown}
225+
</article>
226+
</body>
227+
<footer style="text-align: right; padding: 2em 0;">
228+
<p>本文渲染于 ${dayjs().format('llll')},用时 ${
229+
performance.now() - now
230+
}ms</p>
231+
<p>原文地址:<a href="${url}">${decodeURIComponent(
232+
url.toString(),
233+
)}</a></p>
234+
</footer>
235+
<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
236+
<link rel="stylesheet"
237+
href="//cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.2.0/build/styles/default.min.css">
238+
<script src="//cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.2.0/build/highlight.min.js"></script>
239+
<script>
240+
window.mermaid.initialize({
241+
theme: 'default',
242+
startOnLoad: false,
243+
})
244+
window.mermaid.init(undefined, '.mermaid')
245+
246+
document.addEventListener('DOMContentLoaded', (event) => {
247+
document.querySelectorAll('pre code').forEach((el) => {
248+
hljs.highlightElement(el);
249+
});
248250
});
249-
});
250-
</script>
251-
</html>
252-
`,
253-
{
254-
removeAttributeQuotes: true,
255-
removeComments: true,
256-
minifyCSS: true,
257-
collapseWhitespace: true,
258-
},
259-
)
260-
return html
251+
</script>
252+
</html>
253+
`,
254+
{
255+
removeAttributeQuotes: true,
256+
removeComments: true,
257+
minifyCSS: true,
258+
collapseWhitespace: true,
259+
},
260+
)
261+
return html
262+
} catch {
263+
return `404`
264+
}
261265
}
262266
}

src/modules/say/say.controller.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Body, Delete, Get, Param, Post } from '@nestjs/common'
22
import { sample } from 'lodash'
33
import { Auth } from '~/common/decorator/auth.decorator'
4-
import { CannotFindException } from '~/common/exceptions/cant-find.exception'
54
import { EventTypes } from '~/processors/gateway/events.types'
65
import { MongoIdDto } from '~/shared/dto/id.dto'
76
import { BaseCrudFactory } from '~/utils/crud.util'
@@ -12,7 +11,7 @@ export class SayController extends BaseCrudFactory({ model: SayModel }) {
1211
async getRandomOne() {
1312
const res = await this.model.find({}).lean()
1413
if (!res.length) {
15-
throw new CannotFindException()
14+
return { data: null }
1615
}
1716
return { data: sample(res) }
1817
}

0 commit comments

Comments
 (0)