Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup discussion related search query and params helper #11499

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions app/Models/BeatmapDiscussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class BeatmapDiscussion extends Model
'review' => 5,
];

const int PER_PAGE = 20;

const RESOLVABLE_TYPES = [1, 2];
const KUDOSUABLE_TYPES = [1, 2];

Expand All @@ -71,14 +73,8 @@ class BeatmapDiscussion extends Model
// FIXME: This and other static search functions should be extracted out.
public static function search($rawParams = [])
{
$pagination = pagination(cursor_from_params($rawParams) ?? $rawParams);

$params = [
'limit' => $pagination['limit'],
'page' => $pagination['page'],
];
[$query, $params] = static::searchQueryAndParams(cursor_from_params($rawParams) ?? $rawParams);

$query = static::limit($params['limit'])->offset($pagination['offset']);
$isModerator = $rawParams['is_moderator'] ?? false;

if (present($rawParams['user'] ?? null)) {
Expand Down
9 changes: 2 additions & 7 deletions app/Models/BeatmapDiscussionPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class BeatmapDiscussionPost extends Model implements Traits\ReportableInterface

const MESSAGE_LIMIT = 16_000; // column limit for 4 bytes utf8
const MESSAGE_LIMIT_TIMELINE = 750;
const int PER_PAGE = 20;

protected $touches = ['beatmapDiscussion'];

Expand All @@ -41,14 +42,8 @@ class BeatmapDiscussionPost extends Model implements Traits\ReportableInterface

public static function search($rawParams = [])
{
$pagination = pagination(cursor_from_params($rawParams) ?? $rawParams);
[$query, $params] = static::searchQueryAndParams(cursor_from_params($rawParams) ?? $rawParams);

$params = [
'limit' => $pagination['limit'],
'page' => $pagination['page'],
];

$query = static::limit($params['limit'])->offset($pagination['offset']);
$isModerator = $rawParams['is_moderator'] ?? false;

if (isset($rawParams['user'])) {
Expand Down
10 changes: 3 additions & 7 deletions app/Models/BeatmapDiscussionVote.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
class BeatmapDiscussionVote extends Model
{
const int PER_PAGE = 20;

protected $touches = ['beatmapDiscussion'];

public static function recentlyReceivedByUser($userId, $timeframeMonths = 3)
Expand Down Expand Up @@ -57,14 +59,8 @@ public static function recentlyGivenByUser($userId, $timeframeMonths = 3)

public static function search($rawParams = [])
{
$pagination = pagination(cursor_from_params($rawParams) ?? $rawParams);

$params = [
'limit' => $pagination['limit'],
'page' => $pagination['page'],
];
[$query, $params] = static::searchQueryAndParams(cursor_from_params($rawParams) ?? $rawParams);

$query = static::limit($params['limit'])->offset($pagination['offset']);
$isModerator = $rawParams['is_moderator'] ?? false;

if (isset($rawParams['user'])) {
Expand Down
10 changes: 3 additions & 7 deletions app/Models/BeatmapsetEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class BeatmapsetEvent extends Model

const BEATMAP_OWNER_CHANGE = 'beatmap_owner_change';

const int PER_PAGE = 20;

public static function log($type, $user, $object, $extraData = [])
{
if ($object instanceof BeatmapDiscussionPost) {
Expand All @@ -83,14 +85,8 @@ public static function log($type, $user, $object, $extraData = [])

public static function search($rawParams = [])
{
$pagination = pagination($rawParams);

$params = [
'limit' => $pagination['limit'],
'page' => $pagination['page'],
];
[$query, $params] = static::searchQueryAndParams($rawParams);

$query = static::limit($params['limit'])->offset($pagination['offset']);
$searchByUser = present($rawParams['user'] ?? null);
$isModerator = $rawParams['is_moderator'] ?? false;

Expand Down
13 changes: 13 additions & 0 deletions app/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ public static function booted()
static::addGlobalScope(new MacroableModelScope());
}

protected static function searchQueryAndParams(array $params)
{
$limit = clamp(get_int($params['limit'] ?? null) ?? static::PER_PAGE, 5, 50);
$page = max(get_int($params['page'] ?? null), 1);

$offset = max_offset($page, $limit);
$page = 1 + $offset / $limit;

$query = static::limit($limit)->offset($offset);

return [$query, compact('limit', 'page')];
}

public function getForeignKey()
{
if ($this->primaryKey === null || $this->primaryKey === 'id') {
Expand Down
11 changes: 0 additions & 11 deletions app/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,17 +651,6 @@ function pack_str($str)
return pack('ccH*', 0x0b, strlen($str), bin2hex($str));
}

function pagination($params, $defaults = null)
{
$limit = clamp(get_int($params['limit'] ?? null) ?? $defaults['limit'] ?? 20, 5, 50);
$page = max(get_int($params['page'] ?? null) ?? 1, 1);

$offset = max_offset($page, $limit);
$page = 1 + $offset / $limit;

return compact('limit', 'page', 'offset');
}

function product_quantity_options($product, $selected = null)
{
if ($product->stock === null) {
Expand Down