Skip to content

Commit 5d1c773

Browse files
authored
Merge branch 'master' into score-transformer-cleanup
2 parents 3d0aaf7 + 674c156 commit 5d1c773

File tree

8 files changed

+76
-131
lines changed

8 files changed

+76
-131
lines changed

app/Console/Commands/UserNotificationsCleanup.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public function handle()
4848
break;
4949
}
5050

51+
if ($n->notification === null) {
52+
$n->delete();
53+
continue;
54+
}
5155
$notificationIdByUserIds[$n->user_id][] = $n->notification->toIdentityJson();
5256
}
5357

app/Libraries/Search/TeamSearch.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,28 @@ public function __construct(?TeamSearchParams $params = null)
2525

2626
public function getQuery()
2727
{
28-
static $partialMatchFields = [
29-
'description',
30-
'name',
31-
'name.*',
32-
'short_name',
33-
'short_name.*',
34-
];
35-
36-
$value = $this->params->queryString;
37-
$terms = explode(' ', $value);
38-
39-
return new BoolQuery()
40-
->shouldMatch(1)
41-
->should(['term' => ['_id' => ['value' => $value, 'boost' => 100]]])
42-
->should(QueryHelper::queryString($value, $partialMatchFields, 'or', 1 / count($terms)))
43-
->should(QueryHelper::queryString($value, [], 'and'));
28+
$query = new BoolQuery();
29+
30+
if ($this->params->queryString !== null) {
31+
static $partialMatchFields = [
32+
'description',
33+
'name',
34+
'name.*',
35+
'short_name',
36+
'short_name.*',
37+
];
38+
39+
$value = $this->params->queryString;
40+
$terms = explode(' ', $value);
41+
42+
$query
43+
->shouldMatch(1)
44+
->should(['term' => ['_id' => ['value' => $value, 'boost' => 100]]])
45+
->should(QueryHelper::queryString($value, $partialMatchFields, 'or', 1 / count($terms)))
46+
->should(QueryHelper::queryString($value, [], 'and'));
47+
}
48+
49+
return $query;
4450
}
4551

4652
public function records()

app/Models/Traits/Es/ArtistTrackSearch.php

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
namespace App\Models\Traits\Es;
99

10-
use Carbon\Carbon;
11-
1210
trait ArtistTrackSearch
1311
{
1412
use BaseDbIndexable;
@@ -24,26 +22,13 @@ public function esShouldIndex()
2422
return $this->artist->getAttribute('visible') && ($this->album === null || $this->album->getAttribute('visible'));
2523
}
2624

27-
public function toEsJson()
25+
protected function getEsFieldValue(string $field)
2826
{
29-
$mappings = static::esMappings();
30-
31-
$document = [];
32-
foreach ($mappings as $field => $mapping) {
33-
$value = match ($field) {
34-
'album' => $this->album?->title,
35-
'album_romanized' => $this->album?->title_romanized,
36-
'artist' => $this->artist->name,
37-
default => $this->$field,
38-
};
39-
40-
if ($value instanceof Carbon) {
41-
$value = $value->toIso8601String();
42-
}
43-
44-
$document[$field] = $value;
45-
}
46-
47-
return $document;
27+
return match ($field) {
28+
'album' => $this->album?->title,
29+
'album_romanized' => $this->album?->title_romanized,
30+
'artist' => $this->artist->name,
31+
default => $this->$field,
32+
};
4833
}
4934
}

app/Models/Traits/Es/BaseDbIndexable.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,21 @@ public function getEsId()
120120
return $this->getKey();
121121
}
122122

123-
abstract public function toEsJson();
123+
public function toEsJson(): array
124+
{
125+
$mappings = static::esMappings();
126+
127+
$document = [];
128+
foreach ($mappings as $field => $mapping) {
129+
$value = $this->getEsFieldValue($field);
130+
131+
$document[$field] = $value instanceof \DateTimeInterface
132+
? json_time($value)
133+
: $value;
134+
}
135+
136+
return $document;
137+
}
138+
139+
abstract protected function getEsFieldValue(string $field);
124140
}

app/Models/Traits/Es/BeatmapsetSearch.php

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
namespace App\Models\Traits\Es;
77

88
use App\Models\Beatmap;
9-
use Carbon\Carbon;
109

1110
trait BeatmapsetSearch
1211
{
@@ -49,38 +48,14 @@ public function esShouldIndex()
4948
return !$this->trashed() && !present($this->download_disabled_url);
5049
}
5150

52-
public function toEsJson()
51+
protected function getEsFieldValue(string $field)
5352
{
54-
return [
55-
...$this->esBeatmapsetValues(),
53+
return match ($field) {
5654
'beatmaps' => $this->esBeatmapsValues(),
5755
'difficulties' => $this->esDifficultiesValues(),
58-
];
59-
}
60-
61-
private function esBeatmapsetValues()
62-
{
63-
$mappings = static::esMappings();
64-
65-
$values = [];
66-
foreach ($mappings as $field => $mapping) {
67-
if ($field === 'beatmaps' || $field === 'difficulties') {
68-
continue;
69-
}
70-
71-
$value = match ($field) {
72-
'id' => $this->getKey(),
73-
default => $this->$field,
74-
};
75-
76-
if ($value instanceof Carbon) {
77-
$value = $value->toIso8601String();
78-
}
79-
80-
$values[$field] = $value;
81-
}
82-
83-
return $values;
56+
'id' => $this->getKey(),
57+
default => $this->$field,
58+
};
8459
}
8560

8661
private function esBeatmapsValues()

app/Models/Traits/Es/ForumPostSearch.php

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
namespace App\Models\Traits\Es;
77

88
use App\Models\Forum\Forum;
9-
use Carbon\Carbon;
109

1110
trait ForumPostSearch
1211
{
@@ -45,34 +44,14 @@ public function getEsId()
4544
return "post-{$this->post_id}";
4645
}
4746

48-
public function toEsJson()
47+
protected function getEsFieldValue(string $field)
4948
{
50-
$mappings = static::esMappings();
51-
52-
$document = [];
53-
foreach ($mappings as $field => $mapping) {
54-
switch ($field) {
55-
case 'is_deleted':
56-
$value = $this->trashed() || $this->topic->trashed();
57-
break;
58-
case 'topic_title':
59-
if ($this->topic !== null && $this->topic->topic_first_post_id === $this->getKey()) {
60-
$value = $this->topic->topic_title;
61-
} else {
62-
$value = null;
63-
}
64-
break;
65-
default:
66-
$value = $this[$field];
67-
}
68-
69-
if ($value instanceof Carbon) {
70-
$value = $value->toIso8601String();
71-
}
72-
73-
$document[$field] = $value;
74-
}
75-
76-
return $document;
49+
return match ($field) {
50+
'is_deleted' => $this->trashed() || $this->topic->trashed(),
51+
'topic_title' => $this->topic !== null && $this->topic->topic_first_post_id === $this->getKey()
52+
? $this->topic->topic_title
53+
: null,
54+
default => $this->$field,
55+
};
7756
}
7857
}

app/Models/Traits/Es/TeamSearch.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,11 @@ public static function esIndexingQuery()
1818
return static::query();
1919
}
2020

21-
public function toEsJson()
21+
public function getEsFieldValue(string $field)
2222
{
23-
$document = [];
24-
foreach (static::esMappings() as $field => $mapping) {
25-
$document[$field] = match ($field) {
26-
'description' => BBCodeFromDB::removeBBCodeTags($this->description),
27-
default => $this->$field,
28-
};
29-
}
30-
31-
return $document;
23+
return match ($field) {
24+
'description' => BBCodeFromDB::removeBBCodeTags($this->description),
25+
default => $this->$field,
26+
};
3227
}
3328
}

app/Models/Traits/Es/UserSearch.php

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
namespace App\Models\Traits\Es;
77

8-
use Carbon\Carbon;
9-
108
trait UserSearch
119
{
1210
use BaseDbIndexable;
@@ -27,27 +25,14 @@ public static function esSchemaFile()
2725
return config_path('schemas/users.json');
2826
}
2927

30-
public function toEsJson()
28+
protected function getEsFieldValue(string $field)
3129
{
32-
$mappings = static::esMappings();
33-
34-
$document = [];
35-
foreach ($mappings as $field => $mapping) {
36-
$value = match ($field) {
37-
'id' => $this->getKey(),
38-
'is_old' => $this->isOld(),
39-
'previous_usernames' => $this->previousUsernames(true)->unique()->values(),
40-
'user_lastvisit' => $this->displayed_last_visit,
41-
default => $this->$field,
42-
};
43-
44-
if ($value instanceof Carbon) {
45-
$value = $value->toIso8601String();
46-
}
47-
48-
$document[$field] = $value;
49-
}
50-
51-
return $document;
30+
return match ($field) {
31+
'id' => $this->getKey(),
32+
'is_old' => $this->isOld(),
33+
'previous_usernames' => $this->previousUsernames(true)->unique()->values(),
34+
'user_lastvisit' => $this->displayed_last_visit,
35+
default => $this->$field,
36+
};
5237
}
5338
}

0 commit comments

Comments
 (0)