editPlaylist method

Future editPlaylist(
  1. String playlistId, {
  2. String? title,
  3. String? description,
  4. String? privacyStatus,
  5. bool? collaboration,
  6. dynamic moveItem,
  7. String? addPlaylistId,
  8. PlaylistSortOrder? sortOrder,
  9. bool? addToTop,
  10. PlaylistVoteEditOptions? voteOption,
})

Edit title, description or privacyStatus of a playlist.

You may also move an item within a playlist or append another playlist to this playlist.

  • playlistId Playlist id.
  • title Optional. New title for the playlist.
  • description Optional. New description for the playlist.
  • privacyStatus Optional. New privacy status for the playlist.
  • collaboration Optional. Enable or disable collaboration. If False and collaboration is not enabled, a Forbidden server error is raised. If True, a new joinCollaborationToken is returned. Collaborators cannot interact with private playlists.
  • moveItem Optional. Move one item before another. Items are specified by setVideoId, which is the unique id of this playlist item. See getPlaylist.
  • addPlaylistId Optional. Id of another playlist to add to this playlist.
  • sortOrder Optional. Change the order tracks are returned in. The default is MANUAL.
  • addToTop Optional. Change the state of this playlist to add items to the top of the playlist (if true) or the bottom of the playlist (if false - this is also the default of a new playlist).
  • voteOption: Optional. Change who can participate in community voting in this playlist. Note that a bad request will be thrown if voteOption is PlaylistVoteEditOptions.COLLABORATORS_ONLY but the playlist is not enabled for collaboration prior to the edit.

Returns Status String, collaboration dict described below, or full response

Dictionary returned when collaboration is true and the request is successful

{ "status": "STATUS_SUCCEEDED", "joinCollaborationToken": "kM9wXdRj2p8v_qL3sHBkTz" }

Implementation

Future<dynamic> editPlaylist(
  String playlistId, {
  String? title,
  String? description,
  String? privacyStatus,
  bool? collaboration,
  dynamic moveItem,
  String? addPlaylistId,
  PlaylistSortOrder? sortOrder,
  bool? addToTop,
  PlaylistVoteEditOptions? voteOption,
}) async {
  checkAuth();
  final body = <String, dynamic>{
    'playlistId': validatePlaylistId(playlistId),
  };
  final actions = <JsonMap>[];

  if (title != null) {
    actions.add({
      'action': 'ACTION_SET_PLAYLIST_NAME',
      'playlistName': title,
    });
  }
  if (description != null) {
    actions.add({
      'action': 'ACTION_SET_PLAYLIST_DESCRIPTION',
      'playlistDescription': description,
    });
  }
  if (privacyStatus != null) {
    actions.add({
      'action': 'ACTION_SET_PLAYLIST_PRIVACY',
      'playlistPrivacy': privacyStatus,
    });
  }

  if (collaboration != null) {
    actions.add({'action': 'ACTION_CREATE_COLLABORATION_INVITE_LINK'});
  } else if (!collaboration!) {
    actions.add({
      'action': 'ACTION_SET_CLOSED_TO_CONTRIBUTIONS',
      'closedToContributions': true,
    });
  }

  if (moveItem != null) {
    final action = {
      'action': 'ACTION_MOVE_VIDEO_BEFORE',
      'setVideoId':
          moveItem is String ? moveItem : (moveItem as List<String>)[0],
    };
    if (moveItem is List && moveItem.length > 1) {
      action['movedSetVideoIdSuccessor'] = (moveItem as List<String>)[1];
    }
    actions.add(action);
  }

  if (addPlaylistId != null) {
    actions.add({
      'action': 'ACTION_ADD_PLAYLIST',
      'addedFullListId': addPlaylistId,
    });
  }

  if (sortOrder != null) {
    actions.add({
      'action': 'ACTION_SET_PLAYLIST_VIDEO_ORDER',
      'playlistVideoOrder': sortOrder.value,
    });
  }

  if (addToTop != null) {
    actions.add({
      'action': 'ACTION_SET_ADD_TO_TOP',
      'addToTop': addToTop.toString().toLowerCase(),
    });
  }

  if (voteOption != null) {
    actions.add({
      'action': 'ACTION_SET_ALLOW_ITEM_VOTE',
      'itemVotePermission': voteOption.getArgumentForRequest(),
    });
  }

  body['actions'] = actions;
  const endpoint = 'browse/edit_playlist';
  final response = await sendRequest(endpoint, body);

  if (collaboration && response['status'] == ResponseStatus.SUCCEEDED) {
    final inviteLink = nav(response, ['collaborationInviteLink']);

    return {
      'status': response['status'],
      'joinCollaborationToken': nav(
        Uri.splitQueryString(Uri.parse(inviteLink as String).query),
        ['jct', 0],
      ),
    };
  }

  return response.containsKey('status')
      ? response['status'] as String
      : response;
}