Skip to content

Commit aca3bb9

Browse files
authored
Fix ordering by converting to proper type (#3975)
1 parent b3df54e commit aca3bb9

File tree

11 files changed

+50
-40
lines changed

11 files changed

+50
-40
lines changed

app/Actions/Photo/Timeline.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function do(): Builder
6262
unlocked_album_ids: $unlocked_album_ids,
6363
origin: null,
6464
include_nsfw: !$this->config_manager->getValueAsBool('hide_nsfw_in_timeline')
65-
)->orderBy($order->value, OrderSortingType::DESC->value);
65+
)->orderBy($order->toColumn(), OrderSortingType::DESC->value);
6666
}
6767

6868
/**
@@ -99,8 +99,8 @@ public function countYoungerFromDate(Carbon $date): int
9999

100100
return $this->photo_query_policy->applySearchabilityFilter(
101101
query: Photo::query()
102-
->where($order->value, '>=', $date_offset)
103-
->whereNotNull($order->value),
102+
->where($order->toColumn(), '>=', $date_offset)
103+
->whereNotNull($order->toColumn()),
104104
user: $user,
105105
unlocked_album_ids: $unlocked_album_ids,
106106
origin: null,
@@ -133,13 +133,13 @@ public function countYoungerFromPhoto(Photo $photo): int
133133
return $this->photo_query_policy->applySearchabilityFilter(
134134
query: Photo::query()
135135
->joinSub(
136-
query: Photo::query()->select($order->value)->where('id', $photo->id),
136+
query: Photo::query()->select($order->toColumn())->where('id', $photo->id),
137137
as: 'sub',
138-
first: 'sub.' . $order->value,
138+
first: 'sub.' . $order->toColumn(),
139139
operator: '<',
140-
second: 'photos.' . $order->value
140+
second: 'photos.' . $order->toColumn()
141141
)
142-
->whereNotNull('photos.' . $order->value),
142+
->whereNotNull('photos.' . $order->toColumn()),
143143
user: $user,
144144
unlocked_album_ids: $unlocked_album_ids,
145145
origin: null,
@@ -188,8 +188,8 @@ public function dates(): Collection
188188
return $this->photo_query_policy->applySearchabilityFilter(
189189
query: Photo::query()
190190

191-
->selectRaw(sprintf($formatter, $order->value, $date_format) . ' as date')
192-
->whereNotNull($order->value),
191+
->selectRaw(sprintf($formatter, $order->toColumn(), $date_format) . ' as date')
192+
->whereNotNull($order->toColumn()),
193193
user: $user,
194194
unlocked_album_ids: $unlocked_album_ids,
195195
origin: null,

app/Enum/ColumnSortingAlbumType.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,13 @@ public function toColumnSortingType(): ColumnSortingType
3636
{
3737
return ColumnSortingType::from($this->value);
3838
}
39+
40+
public function toColumn(): string
41+
{
42+
return match ($this) {
43+
self::TITLE_STRICT => 'title',
44+
self::DESCRIPTION_STRICT => 'description',
45+
default => $this->value,
46+
};
47+
}
3948
}

app/Enum/ColumnSortingPhotoType.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,16 @@ public function toColumnSortingType(): ColumnSortingType
4141
{
4242
return ColumnSortingType::from($this->value);
4343
}
44+
45+
/**
46+
* Convert into actual column name.
47+
*/
48+
public function toColumn(): string
49+
{
50+
return match ($this) {
51+
self::TITLE_STRICT => 'title',
52+
self::DESCRIPTION_STRICT => 'description',
53+
default => $this->value,
54+
};
55+
}
4456
}

app/Enum/ColumnSortingType.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,16 @@ enum ColumnSortingType: string
3232
case TAKEN_AT = 'taken_at';
3333
case IS_STARRED = 'is_starred';
3434
case TYPE = 'type';
35+
36+
/**
37+
* Convert into actual column name.
38+
*/
39+
public function toColumn(): string
40+
{
41+
return match ($this) {
42+
self::TITLE_STRICT => 'title',
43+
self::DESCRIPTION_STRICT => 'description',
44+
default => $this->value,
45+
};
46+
}
3547
}

app/Jobs/RecomputeAlbumStatsJob.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ private function getPhotoIdForUser(Album $album, ?User $user, bool $is_nsfw_cont
264264
origin: $album,
265265
include_nsfw: $is_nsfw_context)
266266
->orderBy('photos.is_starred', 'desc')
267-
->orderBy($sorting->column->value, $sorting->order->value)
267+
->orderBy($sorting->column->toColumn(), $sorting->order->value)
268268
->select('photos.id')
269269
->first();
270270

app/Models/Extensions/SortingDecorator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function __construct(Builder $base_builder)
100100
public function orderBy(ColumnSortingType $column, OrderSortingType $direction): SortingDecorator
101101
{
102102
$this->order_by[] = [
103-
'column' => $column->value,
103+
'column' => $column->toColumn(),
104104
'direction' => $direction->value,
105105
];
106106

@@ -125,7 +125,7 @@ public function orderBy(ColumnSortingType $column, OrderSortingType $direction):
125125
public function orderPhotosBy(ColumnSortingType $column, OrderSortingType $direction): SortingDecorator
126126
{
127127
$this->order_by[] = [
128-
'column' => 'photos.' . $column->value,
128+
'column' => 'photos.' . $column->toColumn(),
129129
'direction' => $direction->value,
130130
];
131131

app/Models/Extensions/Thumb.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static function createFromQueryable(Relation|Builder $photo_queryable, So
7676
$cover = $photo_queryable
7777
->withOnly(['size_variants' => (fn ($r) => self::sizeVariantsFilter($r))])
7878
->orderBy('photos.' . ColumnSortingPhotoType::IS_STARRED->value, OrderSortingType::DESC->value)
79-
->orderBy('photos.' . $sorting->column->value, $sorting->order->value)
79+
->orderBy('photos.' . $sorting->column->toColumn(), $sorting->order->value)
8080
->select(['photos.id', 'photos.type'])
8181
->first();
8282

app/Relations/HasManyChildAlbums.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function match(array $models, Collection $results, $relation): array
125125
$children_of_model = $this->getRelationValue($dictionary, $key, 'many');
126126
$sorting = $model->getEffectiveAlbumSorting();
127127
$children_of_model = $children_of_model
128-
->sortBy($sorting->column->value, SORT_NATURAL | SORT_FLAG_CASE, $sorting->order === OrderSortingType::DESC)
128+
->sortBy($sorting->column->toColumn(), SORT_NATURAL | SORT_FLAG_CASE, $sorting->order === OrderSortingType::DESC)
129129
->values();
130130
$model->setRelation($relation, $children_of_model);
131131
// This is the newly added code which sets this method apart

app/Relations/HasManyChildPhotos.php

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public function match(array $models, Collection $results, $relation): array
156156
$sorting = $model->getEffectivePhotoSorting();
157157
$children_of_model = $children_of_model
158158
->sortBy(
159-
$sorting->column->value,
159+
$sorting->column->toColumn(),
160160
in_array($sorting->column, SortingDecorator::POSTPONE_COLUMNS, true) ? SORT_NATURAL | SORT_FLAG_CASE : SORT_REGULAR,
161161
$sorting->order === OrderSortingType::DESC
162162
)
@@ -166,29 +166,6 @@ public function match(array $models, Collection $results, $relation): array
166166
$relation, $children_of_model
167167
);
168168
}
169-
170-
// if (isset($dictionary[$key])) {
171-
172-
// // if (isset($dictionary[$key = $this->getDictionaryKey($model->getAttribute($this->localKey))])) {
173-
// /** @var Collection<int,Photo> $children_of_model */
174-
// $children_of_model = $this->getRelationValue($dictionary, $key, 'many');
175-
// $sorting = $model->getEffectivePhotoSorting();
176-
// $children_of_model = $children_of_model
177-
// ->sortBy(
178-
// $sorting->column->value,
179-
// in_array($sorting->column, SortingDecorator::POSTPONE_COLUMNS, true) ? SORT_NATURAL | SORT_FLAG_CASE : SORT_REGULAR,
180-
// $sorting->order === OrderSortingType::DESC
181-
// )
182-
// ->values();
183-
// $model->setRelation($relation, $children_of_model);
184-
// // This is the newly added code which sets this method apart
185-
// // from the original method and additionally sets the
186-
// // reverse link
187-
188-
// // foreach ($children_of_model as $child_model) {
189-
// // $child_model->setRelation($this->foreign_method_name, $model);
190-
// // }
191-
// }
192169
}
193170

194171
return $models;

app/Relations/HasManyPhotosByTag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public function match(array $albums, Collection $photos, $relation): array
168168
$sorting = $album->getEffectivePhotoSorting();
169169

170170
$photos = $photos->sortBy(
171-
$sorting->column->value,
171+
$sorting->column->toColumn(),
172172
in_array($sorting->column, SortingDecorator::POSTPONE_COLUMNS, true) ? SORT_NATURAL | SORT_FLAG_CASE : SORT_REGULAR,
173173
$sorting->order === OrderSortingType::DESC
174174
)->values();

0 commit comments

Comments
 (0)