Skip to content

Commit

Permalink
* now will cache sitemap xml response for one day @ routes/web.php
Browse files Browse the repository at this point in the history
…& `App\Http\Controllers\ThreadsSitemap`

* fix duplicate previous threads when providing cursor query string by replace `ORDER BY tid DESC` with `ASC`
* rename static field `$maxItems` to `maxUrls`
@ `App\Http\Controllers\ThreadsSitemap`

* prefer static methods over member method on `new Model()`
* remove unnecessary `Collection::toArray()`
* explicitly set `CACHE_STORE=file` to override its default value `database` @ .env.exmaple
@ be
  • Loading branch information
n0099 committed Aug 2, 2024
1 parent 17f07d7 commit 83e82ad
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 24 deletions.
2 changes: 2 additions & 0 deletions be/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=

CACHE_STORE=file

RECAPTCHA_SECRET=
6 changes: 3 additions & 3 deletions be/app/Http/Controllers/PostsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public function query(\Illuminate\Http\Request $request): array
->only(Helper::POST_TYPES_PLURAL)
->flatMap(static fn (Collection $posts) => $posts->pluck('authorUid'))
->concat($latestRepliers->pluck('uid'))
->filter()->unique()->toArray() // remove NULLs
->filter()->unique() // remove NULLs
)->with(['currentForumModerator' => $whereCurrentFid, 'currentAuthorExpGrade' => $whereCurrentFid])
->selectPublicFields()->get()->toArray();
->selectPublicFields()->get();
Debugbar::stopMeasure('queryUsers');

return [
Expand All @@ -75,7 +75,7 @@ public function query(\Illuminate\Http\Request $request): array
...$query->getResultPages(),
...Arr::except($result, ['fid', ...Helper::POST_TYPES_PLURAL])
],
'forum' => Forum::fid($result['fid'])->selectPublicFields()->first()?->toArray(),
'forum' => Forum::fid($result['fid'])->selectPublicFields()->first(),
'threads' => $query->reOrderNestedPosts($query::nestPostsWithParent(...$result)),
'users' => $users,
'latestRepliers' => $latestRepliers
Expand Down
16 changes: 9 additions & 7 deletions be/app/Http/Controllers/ThreadsSitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@
use App\Eloquent\Model\Post\PostFactory;
use App\Helper;
use Illuminate\Http;
use Illuminate\Support\Facades\Cache;

class ThreadsSitemap extends Controller
{
public static int $maxItems = 50000;
public static int $maxUrls = 50000;

public function query(Http\Request $request, int $fid): Http\Response
{ // https://stackoverflow.com/questions/59554777/laravel-how-to-set-default-value-in-validator-at-post-registeration/78707950#78707950
['cursor' => $cursor] = $request->validate([
'cursor' => 'integer'
]) + ['cursor' => 0];
Helper::abortAPIIfNot(40406, (new Forum())->fid($fid)->exists());
Helper::abortAPIIfNot(40406, Forum::fid($fid)->exists());

return Helper::xmlResponse(view('sitemaps.threads', [
'tids' => PostFactory::newThread($fid)
->where('tid', '>', $cursor)->limit(self::$maxItems)
->orderByDesc('tid')->pluck('tid')
]));
return Cache::remember("/sitemaps/forums/$fid/threads?cursor=$cursor", 86400,
static fn () => Helper::xmlResponse(view('sitemaps.threads', [
'tids' => PostFactory::newThread($fid)
->where('tid', '>', $cursor)->limit(self::$maxUrls)
->orderBy('tid')->pluck('tid')
])));
}
}
2 changes: 1 addition & 1 deletion be/app/Http/Controllers/UsersQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function query(\Illuminate\Http\Request $request): array
]);
Helper::abortAPIIf(40402, empty($queryParams));

$queryBuilder = (new User())->newQuery();
$queryBuilder = User::newQuery();

$nullableParams = ['name', 'displayName', 'gender'];
foreach ($nullableParams as $nullableParamName) {
Expand Down
9 changes: 5 additions & 4 deletions be/app/Http/PostsQuery/IndexQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,24 @@ public function query(QueryParams $params, ?string $cursor): self
->only($postTypes)
->transform(static fn (Post $model, string $type) => $model->selectCurrentAndParentPostID());
$getFidByPostIDParam = static function (string $postIDName, int $postID): int {
$fids = Forum::get('fid')->pluck('fid')->toArray();
$counts = collect($fids)
$counts = Forum::get('fid')
->pluck('fid')
->map(static fn (int $fid) =>
PostFactory::getPostModelsByFid($fid)[Helper::POST_ID_TO_TYPE[$postIDName]]
->selectRaw("{$fid} AS fid, COUNT(*) AS count")
->where($postIDName, $postID))
->reduce(static fn (?BuilderContract $acc, EloquentBuilder|QueryBuilder $cur): BuilderContract =>
$acc === null ? $cur : $acc->union($cur))
->get()->where('count', '!=', 0);
->get()
->where('count', '!=', 0);
Helper::abortAPIIf(50001, $counts->count() > 1);
Helper::abortAPIIf(40401, $counts->count() === 0);
return $counts->pluck('fid')->first();
};

if (\array_key_exists('fid', $flatParams)) {
/** @var int $fid */ $fid = $flatParams['fid'];
if ((new Forum())->fid($fid)->exists()) {
if (Forum::fid($fid)->exists()) {
/** @var Collection<string, EloquentBuilder<Post>> $queries key by post type */
$queries = $getQueryBuilders($fid);
} elseif ($hasPostIDParam) { // query by post ID and fid, but the provided fid is invalid
Expand Down
2 changes: 1 addition & 1 deletion be/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use App\Http\Middleware\ReCAPTCHACheck;
use Illuminate\Support\Facades\Route;

Route::get('/forums', static fn () => \App\Eloquent\Model\Forum::all()->toArray());
Route::get('/forums', static fn () => \App\Eloquent\Model\Forum::all());
Route::middleware(ReCAPTCHACheck::class)->group(static function () {
Route::get('/posts', [Controllers\PostsQuery::class, 'query']);
Route::get('/users', [Controllers\UsersQuery::class, 'query']);
Expand Down
17 changes: 9 additions & 8 deletions be/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
use App\Helper;
use App\Http\Controllers;

Route::get('/sitemaps/forums', static fn() => Helper::xmlResponse(view('sitemaps.forums', [
'tidsKeyByFid' => (new Forum())->get('fid')->pluck('fid')->mapWithKeys(fn(int $fid) => [
$fid => DB::query()
->fromSub(PostFactory::newThread($fid)
->selectRaw('ROW_NUMBER() OVER (ORDER BY tid DESC) AS rn, tid'), 't')
->whereRaw('rn % ' . Controllers\ThreadsSitemap::$maxItems . ' = 0')
->pluck('tid')
])])));
Route::get('/sitemaps/forums', static fn () => Cache::remember('/sitemaps/forums', 86400,
static fn () => Helper::xmlResponse(view('sitemaps.forums', [
'tidsKeyByFid' => Forum::get('fid')->pluck('fid')->mapWithKeys(fn (int $fid) => [
$fid => DB::query()
->fromSub(PostFactory::newThread($fid)
->selectRaw('ROW_NUMBER() OVER (ORDER BY tid DESC) AS rn, tid'), 't')
->whereRaw('rn % ' . Controllers\ThreadsSitemap::$maxUrls . ' = 0')
->pluck('tid')
])]))));
Route::get('/sitemaps/forums/{fid}/threads', [Controllers\ThreadsSitemap::class, 'query']);

0 comments on commit 83e82ad

Please sign in to comment.