Skip to content

Commit 4cc26e6

Browse files
committed
* now allow returning query type postID when there's multiple param query by post id to match with 66e2ec1
* now will return query type `empty` only when all params is set to default value @ `currentQueryType().clearedParams()` * now allow querying with type `search` with no unique param `fid` or type `empty` @ `checkParams()` @ `getQueryFormDeps()` @ `utils/post/queryForm/index.ts` * replace `ApiPosts.response.forum` from object to array `forums` to match with 5bf8736 * remove `ApiPosts.response.type` to match with 83e9953 @ `api/types.ts` * now will only add forum name or thread title when they're not nil in `firstThread()` @ `utils/post/seo/index.ts` * replace prop `firstPostPageForum` with `firstPostPageForumName` @ `<OgImagePost>` & `utils/post/seo/index.ts` @ fe
1 parent ade8142 commit 4cc26e6

4 files changed

Lines changed: 31 additions & 48 deletions

File tree

fe/src/api/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ export type ApiPosts = Api<CursorPagination & {
3030
matchQueryPostCount: Record<PostType, UInt>,
3131
notMatchQueryParentPostCount: Record<Exclude<PostType, 'subRely'>, UInt>
3232
},
33-
type: 'index' | 'search',
34-
forum: Pick<ArrayElement<ApiForums['response']>, 'fid' | 'name'>,
33+
forums: Record<Fid, string>,
3534
threads: Array<Thread & {
3635
replies: Array<Reply & {
3736
subReplies: SubReply[]

fe/src/components/OgImage/Post.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="flex flex-row justify-between size-screen">
33
<div class="flex-1 flex-col basis-1/2 m-6">
44
<p>{{ useSiteConfig().name }} {{ routePath }}</p>
5-
<h2>{{ firstPostPageForum?.name }}吧</h2>
5+
<h2 v-if="firstPostPageForumName !== undefined">{{ firstPostPageForumName }}吧</h2>
66
<template v-if="currentQueryType !== 'postID'">
77
<p class="m-0">右侧为查询结果中第一张图片(不一定来自第一条帖子)</p>
88
<p class="m-0">下方为查询结果中第一条主题帖/回复帖/楼中楼</p>
@@ -30,7 +30,7 @@ import type { UnwrapRef } from 'vue';
3030
const { firstThread, firstPostPage } = defineProps<{
3131
routePath: string,
3232
firstPostPage?: ApiPosts['response'],
33-
firstPostPageForum?: ApiPosts['response']['forum'],
33+
firstPostPageForumName?: string,
3434
firstThread?: ArrayElement<ApiPosts['response']['threads']>,
3535
currentQueryType: UnwrapRef<QueryFormDeps['currentQueryType']>
3636
}>();

fe/src/utils/post/queryForm/index.ts

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,17 @@ export const getQueryFormDeps = () => {
3030

3131
const currentQueryType = computed(() => {
3232
const clearedParams = clearedParamsDefaultValue(); // not including unique params
33-
if (_.isEmpty(clearedParams)) { // is there no other params
34-
// ignore the post type param since index query (postID or fid) doesn't restrict them
35-
const clearedUniqueParams = _.omit(clearedUniqueParamsDefaultValue(), 'postTypes');
36-
if (_.isEmpty(clearedUniqueParams)) {
37-
return 'empty'; // only fill unique param postTypes and/or orderBy doesn't query anything
38-
} else if (clearedUniqueParams.fid !== undefined) {
39-
// note when query with postTypes and/or orderBy param, the route will go params instead of fid
33+
if (_.isEmpty(clearedParams)) {
34+
const clearedUniqueParams = clearedUniqueParamsDefaultValue();
35+
if (_.isEmpty(clearedUniqueParams))
36+
return 'empty';
37+
if (clearedUniqueParams.fid !== undefined)
4038
return 'fid';
41-
}
4239
}
4340

4441
// is there no other params except post id params
4542
if (_.isEmpty(_.reject(clearedParams, isPostIDParam))
4643

47-
// is there only one post id param
48-
&& _.filter(clearedParams, isPostIDParam).length === 1
49-
5044
// is all post ID params doesn't own any sub param
5145
&& _.chain(clearedParams).map('subParam').filter().isEmpty().value())
5246
return 'postID';
@@ -83,25 +77,10 @@ export const getQueryFormDeps = () => {
8377
// check query type
8478
isFidInvalid.value = false;
8579
const clearedUniqueParams = clearedUniqueParamsDefaultValue();
86-
switch (currentQueryType.value) {
87-
case 'empty':
88-
notyShow('warning', '请选择贴吧或/并输入查询参数<br>勿只选择帖子类型参数');
89-
90-
return false; // exit early
91-
case 'postID':
92-
if (clearedUniqueParams.fid !== undefined) {
93-
uniqueParams.value.fid.value = 0; // reset fid to default,
94-
notyShow('info', '已移除按帖索引查询所不需要的查询贴吧参数');
95-
await router.push(generateRoute()); // update route to match new params without fid
96-
}
97-
break;
98-
case 'search':
99-
if (clearedUniqueParams.fid === undefined) {
100-
isFidInvalid.value = true; // search query require fid param
101-
notyShow('warning', '搜索查询必须指定查询贴吧');
102-
}
103-
break;
104-
case 'fid':
80+
if (currentQueryType.value === 'postID' && clearedUniqueParams.fid !== undefined) {
81+
uniqueParams.value.fid.value = 0; // reset fid to default,
82+
notyShow('info', '已移除按帖索引查询所不需要的查询贴吧参数');
83+
await router.push(generateRoute()); // update route to match new params without fid
10584
}
10685

10786
const isRequiredPostTypes = (current: PostType[], required?: ObjValues<RequiredPostTypes>): required is undefined => {
@@ -111,10 +90,9 @@ export const getQueryFormDeps = () => {
11190
const requiredPostTypesToString = (required: NonNullable<ObjValues<RequiredPostTypes>>) => required.join(' | ');
11291
const postTypes = _.sortBy(uniqueParams.value.postTypes.value);
11392

114-
// check params required post types, index query doesn't restrict on post types
11593
invalidParamsIndex.value = []; // reset to prevent duplicate indexes
94+
// check params required post types, query by post id or fid doesn't restrict on post types
11695
if (currentQueryType.value !== 'postID' && currentQueryType.value !== 'fid') {
117-
/** we don't {@link Array.filter()} here for post types validate */
11896
params.value.map(clearParamDefaultValue).forEach((param, paramIndex) => {
11997
if (param?.name === undefined || param.value === undefined) {
12098
invalidParamsIndex.value.push(paramIndex);

fe/src/utils/post/seo/index.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,30 @@ export const usePostsSEO = (
66
) => {
77
const route = useRoute();
88
const firstPostPage = computed(() => data.value?.pages[0]);
9-
const firstPostPageForum = computed(() => firstPostPage.value?.forum);
9+
const firstPostPageForumName = computed((): string | null => {
10+
if (firstPostPage.value?.forums === undefined)
11+
return null;
12+
13+
const forumNames = Object.values(firstPostPage.value.forums);
14+
if (forumNames.length === 1)
15+
return forumNames[0];
16+
17+
return null;
18+
});
1019
const firstThread = computed(() => firstPostPage.value?.threads[0]);
1120
useHead({
1221
title: computed(() => {
13-
if (firstPostPage.value === undefined)
14-
return '帖子查询';
15-
switch (currentQueryType.value) {
16-
case 'fid':
17-
case 'search':
18-
return `${firstPostPageForum.value?.name}吧 - 帖子查询`;
19-
case 'postID':
20-
return `${firstThread.value?.title} - ${firstPostPageForum.value?.name}吧 - 帖子查询`;
21-
case 'empty':
22+
const titleParts = ['帖子查询'];
23+
if (currentQueryType.value !== 'empty') {
24+
if (['search', 'fid'].includes(currentQueryType.value) && firstPostPageForumName.value !== null)
25+
titleParts.unshift(`${firstPostPageForumName.value}吧`);
26+
if (currentQueryType.value === 'postID' && firstThread.value?.title !== undefined)
27+
titleParts.unshift(firstThread.value.title);
2228
}
2329

24-
return '帖子查询';
30+
return titleParts.join(' - ');
2531
})
2632
});
27-
defineOgImageComponent('Post', { routePath: route.path, firstPostPage, firstPostPageForum, firstThread, currentQueryType });
33+
defineOgImageComponent('Post', { routePath: route.path, firstPostPage, firstPostPageForumName, firstThread, currentQueryType });
2834
usePostsSchemaOrg(data);
2935
};

0 commit comments

Comments
 (0)