Skip to content

Commit

Permalink
+ server/api/__sitemap__/urls.ts to fetch all threads id of each forum
Browse files Browse the repository at this point in the history
+ exported type `ApiForumThreadsID` @ api/types.ts
* set sitemap url sources and enable multiple sitemaps @ nuxt.config.ts
* fix nuxt/nuxt#23101 (comment) @ server/tsconfig.json
@ fe

* fix `CursorPaginator->nextCursor()` might return null when it's the last page
* rename variable `primaryKey` to `cursorKey & `paginators` to `paginator`
@ `App\Http\ControllersThreadsIDQuery->query()`

* move class `App\Http\Controllers\Topic\BilibiliVote` to `App\Http\BilibiliVoteQuery` for preventing duplicated class name with `App\Eloquent\Model\BilibiliVote`
* change the default value of config `session.driver` from `database` to `file` @ config/session.php
@ be
  • Loading branch information
n0099 committed Jul 4, 2024
1 parent ac1e0e9 commit fe56ab4
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Controllers\Topic;
namespace App\Http;

use App\Eloquent\Model\BilibiliVote as BilibiliVoteModel;
use App\Helper;
Expand All @@ -9,14 +9,7 @@
use Illuminate\Validation\Rule;
use Spatie\Regex\Regex;

/**
* Class BilibiliVote
*
* Controller for /api/bilibiliVote
*
* @package App\Http\Controllers\Topic
*/
class BilibiliVote
class BilibiliVoteQuery
{
/**
* Generate a query builder, which returns top $candidateCount candidates based on valid votes
Expand Down
10 changes: 5 additions & 5 deletions be/app/Http/Controllers/ThreadsIDQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public function query(\Illuminate\Http\Request $request, int $fid): array
]) + ['cursor' => 0];
Helper::abortAPIIfNot(40406, (new Forum())->fid($fid)->exists());
$thread = PostFactory::newThread($fid);
$primaryKey = $thread->getTable() . '.tid';
$paginators = $thread->cursorPaginate(1000, columns: 'tid', cursor: new Cursor([$primaryKey => $cursor]));
$cursorKey = $thread->getTable() . '.tid';
$paginator = $thread->cursorPaginate(1000, columns: 'tid', cursor: new Cursor([$cursorKey => $cursor]));

return [
'pages' => [
'currentCursor' => $paginators->cursor()->parameter($primaryKey),
'nextCursor' => $paginators->nextCursor()->parameter($primaryKey)
'currentCursor' => $paginator->cursor()->parameter($cursorKey),
'nextCursor' => $paginator->nextCursor()?->parameter($cursorKey)
],
'tid' => array_column($paginators->items(), 'tid')
'tid' => array_column($paginator->items(), 'tid')
];
}
}
2 changes: 1 addition & 1 deletion be/config/session.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
|
*/

'driver' => env('SESSION_DRIVER', 'database'),
'driver' => env('SESSION_DRIVER', 'file'),

/*
|--------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions fe/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export default defineNuxtConfig({
name: `open-tbm @ ${process.env.NUXT_PUBLIC_INSTANCE_NAME}`,
defaultLocale: 'zh'
},
sitemap: {
sources: ['/api/__sitemap__/urls'],
sitemaps: true
},
ogImage: {
fonts: ['Noto Sans SC']
},
Expand Down
3 changes: 3 additions & 0 deletions fe/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ export type ApiPosts = Api<CursorPagination & {
}>,
users: User[]
}, { query: JsonString }>;
export type ApiForumThreadsID = Api<CursorPagination & {
tid: Tid[]
}, { cursor: Tid }>;
32 changes: 32 additions & 0 deletions fe/src/server/api/__sitemap__/urls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { ApiForumThreadsID, ApiForums } from '@/api/types';
import type { Tid } from '@/utils';

export default defineSitemapEventHandler(async event => {
const baseUrlWithDomain = useSiteConfig(event).url;
const endpointPrefix = useRuntimeConfig().public.apiEndpointPrefix;
const fetchOptions = {
headers: {
Accept: 'application/json',
Authorization: event.headers.get('Authorization') ?? ''
}
};
const forums = await $fetch<ApiForums['response']>(`${endpointPrefix}/forums`, fetchOptions);

return (await Promise.all(forums.map(async ({ fid }) => {
const tids: Tid[] = [];
let hasMore = true;
let cursor = 0;
while (hasMore) {
// eslint-disable-next-line no-await-in-loop
const threads = await $fetch<ApiForumThreadsID['response']>(
`${endpointPrefix}/forums/${fid}/threads/tid`,
{ query: { cursor }, ...fetchOptions }
);
tids.push(...threads.tid);
hasMore = threads.pages.nextCursor !== null;
cursor = threads.pages.nextCursor ?? Infinity;
}

return tids.map(tid => asSitemapUrl({ loc: `${baseUrlWithDomain}/posts/tid/${tid}` }));
}))).flat();
});
2 changes: 1 addition & 1 deletion fe/src/server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "../.nuxt/tsconfig.server.json"
"extends": "../../.nuxt/tsconfig.server.json"
}

0 comments on commit fe56ab4

Please sign in to comment.