From d512a285857495dadef526970a858c2ec6360519 Mon Sep 17 00:00:00 2001 From: lachlan Date: Tue, 23 Apr 2024 12:37:05 +1000 Subject: [PATCH] Update PlayerMethod.php make type and state optional --- .../Api/Method/Api4/RecordPlay4Method.php | 2 +- .../Api/Method/Api5/RecordPlay5Method.php | 2 +- src/Module/Api/Method/PlayerMethod.php | 28 +++++++++++-------- src/Module/Api/Method/RecordPlayMethod.php | 2 +- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/Module/Api/Method/Api4/RecordPlay4Method.php b/src/Module/Api/Method/Api4/RecordPlay4Method.php index a5f95627e2..a573d5917b 100644 --- a/src/Module/Api/Method/Api4/RecordPlay4Method.php +++ b/src/Module/Api/Method/Api4/RecordPlay4Method.php @@ -47,7 +47,7 @@ final class RecordPlay4Method * * id = (integer) $object_id * user = (integer|string) $user_id OR $username //optional - * client = (string) $agent //optional + * client = (string) $agent Default: 'api' //optional * date = (integer) UNIXTIME() //optional */ public static function record_play(array $input, User $user): bool diff --git a/src/Module/Api/Method/Api5/RecordPlay5Method.php b/src/Module/Api/Method/Api5/RecordPlay5Method.php index 2c018ff28f..389ef36b6c 100644 --- a/src/Module/Api/Method/Api5/RecordPlay5Method.php +++ b/src/Module/Api/Method/Api5/RecordPlay5Method.php @@ -48,7 +48,7 @@ final class RecordPlay5Method * * id = (integer) $object_id * user = (integer|string) $user_id OR $username //optional - * client = (string) $agent //optional + * client = (string) $agent Default: 'api' //optional * date = (integer) UNIXTIME() //optional */ public static function record_play(array $input, User $user): bool diff --git a/src/Module/Api/Method/PlayerMethod.php b/src/Module/Api/Method/PlayerMethod.php index 051fdcfa4f..fef92ee171 100644 --- a/src/Module/Api/Method/PlayerMethod.php +++ b/src/Module/Api/Method/PlayerMethod.php @@ -46,30 +46,27 @@ final class PlayerMethod /** * player - * MINIMUM_API_VERSION=6.3.2 + * MINIMUM_API_VERSION=6.4.0 * * Inform the server about the state of your client. (Song you are playing, Play/Pause state, etc.) * Return the `now_playing` state when completed * * filter = (integer) $object_id - * type = (string) $object_type ('song', 'podcast_episode', 'video') - * state = (string) 'play', 'stop' + * type = (string) $object_type ('song', 'podcast_episode', 'video'), DEFAULT 'song'//optional + * state = (string) 'play', 'stop', DEFAULT 'play' //optional * time = (integer) current song time in whole seconds, DEFAULT 0 //optional - * client = (string) $agent //optional + * client = (string) $agent, DEFAULT 'api' //optional */ public static function player(array $input, User $user): bool { - if (!Api::check_parameter($input, array('filter', 'type', 'state'), self::ACTION)) { + if (!Api::check_parameter($input, array('filter'), self::ACTION)) { return false; } ob_end_clean(); $object_id = (int)$input['filter']; - $type = $input['type']; - $state = $input['state']; + $type = $input['type'] ?? 'song'; - // validate client string or fall back to 'api' - $agent = scrub_in((string)($input['client'] ?? 'api')); // confirm the correct data if (!in_array(strtolower($type), array('song', 'podcast_episode', 'video'))) { @@ -78,15 +75,15 @@ public static function player(array $input, User $user): bool return false; } - if (!in_array(strtolower($state), array('play', 'pause', 'stop'))) { + + $state = $input['state'] ?? 'play'; + if (!in_array(strtolower($state), array('play', 'stop'))) { /* HINT: Requested object string/id/type ("album", "myusername", "some song title", 1298376) */ Api::error(sprintf('Bad Request: %s', $state), ErrorCodeEnum::BAD_REQUEST, self::ACTION, 'state', $input['api_format']); return false; } - $time = time(); - $position = (array_key_exists('time', $input) && is_numeric(scrub_in((string) $input['time']))) ? (int) scrub_in((string) $input['time']) : 0; //optional $className = ObjectTypeToClassNameMapper::map($type); if ($className === $type || !$object_id) { /* HINT: Requested object string/id/type ("album", "myusername", "some song title", 1298376) */ @@ -104,6 +101,13 @@ public static function player(array $input, User $user): bool return false; } + $time = time(); + $position = (array_key_exists('time', $input) && is_numeric(scrub_in((string) $input['time']))) + ? (int) scrub_in((string) $input['time']) + : 0; + // validate client string or fall back to 'api' + $agent = scrub_in((string)($input['client'] ?? 'api')); + if ($state === 'play') { // make sure the now_playing state is set Stream::garbage_collection(); diff --git a/src/Module/Api/Method/RecordPlayMethod.php b/src/Module/Api/Method/RecordPlayMethod.php index 413d09e7dd..1c9335c65c 100644 --- a/src/Module/Api/Method/RecordPlayMethod.php +++ b/src/Module/Api/Method/RecordPlayMethod.php @@ -49,7 +49,7 @@ final class RecordPlayMethod * * id = (integer) $object_id * user = (integer|string) $user_id OR $username //optional - * client = (string) $agent //optional + * client = (string) $agent Default: 'api' //optional * date = (integer) UNIXTIME() //optional */ public static function record_play(array $input, User $user): bool