diff --git a/app/Services/Api/V1/ReleaseItemPayloadBuilder.php b/app/Services/Api/V1/ReleaseItemPayloadBuilder.php index 1fcbb1fd7..dec022c7d 100644 --- a/app/Services/Api/V1/ReleaseItemPayloadBuilder.php +++ b/app/Services/Api/V1/ReleaseItemPayloadBuilder.php @@ -124,7 +124,11 @@ final readonly class ReleaseItemPayloadBuilder $attributes['grabs'] = $this->value($release, 'grabs'); $attributes['comments'] = $this->value($release, 'comments'); $attributes['password'] = $this->value($release, 'passwordstatus'); - $attributes['usenetdate'] = date(DATE_RSS, strtotime((string) $this->value($release, 'postdate'))); + $usenetTimestamp = $this->timestamp($this->value($release, 'postdate')) + ?? $this->timestamp($this->value($release, 'adddate')); + if ($usenetTimestamp !== null) { + $attributes['usenetdate'] = date(DATE_RSS, $usenetTimestamp); + } $groupName = $this->value($release, 'group_name'); if (! empty($groupName)) { @@ -223,4 +227,19 @@ final readonly class ReleaseItemPayloadBuilder return null; } + + private function timestamp(mixed $value): ?int + { + if (\is_int($value)) { + return $value; + } + + if (! \is_string($value) || trim($value) === '') { + return null; + } + + $timestamp = strtotime($value); + + return $timestamp === false ? null : $timestamp; + } } diff --git a/tests/Unit/Api/XMLResponseTest.php b/tests/Unit/Api/XMLResponseTest.php index eba412a80..13fd86a92 100644 --- a/tests/Unit/Api/XMLResponseTest.php +++ b/tests/Unit/Api/XMLResponseTest.php @@ -99,6 +99,21 @@ final class XMLResponseTest extends TestCase self::assertStringContainsString('', $xml); } + public function test_extended_api_falls_back_to_adddate_for_invalid_postdate(): void + { + $release = $this->release([ + 'postdate' => 'not-a-date', + ]); + + $array = $this->response([$release], ['extended' => '1'])->returnArray(); + + self::assertIsArray($array); + self::assertSame( + date(DATE_RSS, strtotime('2026-05-29 12:00:00')), + $array['item'][0]['attr']['usenetdate'] + ); + } + public function test_api_rows_are_normalized_from_supported_payload_shapes(): void { $arrayRow = (array) $this->release([