Skip to content

Commit

Permalink
Update PlayerMethod.php
Browse files Browse the repository at this point in the history
make type and state optional
  • Loading branch information
lachlan-00 committed Apr 23, 2024
1 parent 96327e6 commit d512a28
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Module/Api/Method/Api4/RecordPlay4Method.php
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Module/Api/Method/Api5/RecordPlay5Method.php
Expand Up @@ -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
Expand Down
28 changes: 16 additions & 12 deletions src/Module/Api/Method/PlayerMethod.php
Expand Up @@ -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'))) {
Expand All @@ -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) */
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/Module/Api/Method/RecordPlayMethod.php
Expand Up @@ -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
Expand Down

0 comments on commit d512a28

Please sign in to comment.