Skip to content

Commit

Permalink
feat(PlaylistManager): add .setName() and .setDescription() functions…
Browse files Browse the repository at this point in the history
… for editing playlists (#498)
  • Loading branch information
jeremyBanks authored Sep 1, 2023
1 parent bff4210 commit 86fb33e
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 5 deletions.
29 changes: 28 additions & 1 deletion docs/API/playlist.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Playlist management class.
* [.addVideos(playlist_id, video_ids)](#addvideos)
* [.removeVideos(playlist_id, video_ids)](#removevideos)
* [.moveVideo(playlist_id, moved_video_id, predecessor_video_id)](#movevideo)
* [.setName(playlist_id, name)](#setname)
* [.setDescription(playlist_id, description)](#setdescription)

<a name="create"></a>
### create(title, video_ids)
Expand Down Expand Up @@ -69,4 +71,29 @@ Moves a video to a new position within a given playlist.
| --- | --- | --- |
| playlist_id | `string` | Playlist id |
| moved_video_id | `string` | the video to be moved |
| predecessor_video_id | `string` | the video present in the target position |
| predecessor_video_id | `string` | the video present in the target position |

<a name="setname"></a>
### setName(playlist_id, name)

Sets the name / title for the given playlist.

**Returns:** `Promise.<ApiResponse>`

| Param | Type | Description |
| --- | --- | --- |
| playlist_id | `string` | Playlist id |
| name | `string` | Name / title |


<a name="setdescription"></a>
### setDescription(playlist_id, description)

Sets the description for the given playlist.

**Returns:** `Promise.<ApiResponse>`

| Param | Type | Description |
| --- | --- | --- |
| playlist_id | `string` | Playlist id |
| description | `string` | Description |
4 changes: 3 additions & 1 deletion src/core/endpoints/browse/EditPlaylistEndpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export function build(opts: EditPlaylistEndpointOptions): IEditPlaylistRequest {
...{
addedVideoId: action.added_video_id,
setVideoId: action.set_video_id,
movedSetVideoIdPredecessor: action.moved_set_video_id_predecessor
movedSetVideoIdPredecessor: action.moved_set_video_id_predecessor,
playlistDescription: action.playlist_description,
playlistName: action.playlist_name
}
}))
};
Expand Down
58 changes: 57 additions & 1 deletion src/core/managers/PlaylistManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,60 @@ export default class PlaylistManager {
action_result: response.data.actions // TODO: implement actions in the parser
};
}
}

/**
* Sets the name (title) for the given playlist.
* @param playlist_id - The playlist ID.
* @param name - The name / title to use for the playlist.
*/
async setName(playlist_id: string, name: string): Promise<{ playlist_id: string; action_result: any; }> {
throwIfMissing({ playlist_id, name });

if (!this.#actions.session.logged_in)
throw new InnertubeError('You must be signed in to perform this operation.');

const payload: EditPlaylistEndpointOptions = { playlist_id, actions: [] };

payload.actions.push({
action: 'ACTION_SET_PLAYLIST_NAME',
playlist_name: name
});

const response = await this.#actions.execute(
EditPlaylistEndpoint.PATH, EditPlaylistEndpoint.build(payload)
);

return {
playlist_id,
action_result: response.data.actions
};
}

/**
* Sets the description for the given playlist.
* @param playlist_id - The playlist ID.
* @param description - The description to use for the playlist.
*/
async setDescription(playlist_id: string, description: string): Promise<{ playlist_id: string; action_result: any; }> {
throwIfMissing({ playlist_id, description });

if (!this.#actions.session.logged_in)
throw new InnertubeError('You must be signed in to perform this operation.');

const payload: EditPlaylistEndpointOptions = { playlist_id, actions: [] };

payload.actions.push({
action: 'ACTION_SET_PLAYLIST_DESCRIPTION',
playlist_description: description
});

const response = await this.#actions.execute(
EditPlaylistEndpoint.PATH, EditPlaylistEndpoint.build(payload)
);

return {
playlist_id,
action_result: response.data.actions
};
}
}
8 changes: 6 additions & 2 deletions src/types/Endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,18 +334,22 @@ export type EditPlaylistEndpointOptions = {
* The changes to make to the playlist.
*/
actions: {
action: 'ACTION_ADD_VIDEO' | 'ACTION_REMOVE_VIDEO' | 'ACTION_MOVE_VIDEO_AFTER';
action: 'ACTION_ADD_VIDEO' | 'ACTION_REMOVE_VIDEO' | 'ACTION_MOVE_VIDEO_AFTER' | 'ACTION_SET_PLAYLIST_DESCRIPTION' | 'ACTION_SET_PLAYLIST_NAME';
added_video_id?: string;
set_video_id?: string;
moved_set_video_id_predecessor?: string;
playlist_description?: string;
playlist_name?: string;
}[];
}

export interface IEditPlaylistRequest extends ObjectSnakeToCamel<EditPlaylistEndpointOptions> {
actions: {
action: 'ACTION_ADD_VIDEO' | 'ACTION_REMOVE_VIDEO' | 'ACTION_MOVE_VIDEO_AFTER';
action: 'ACTION_ADD_VIDEO' | 'ACTION_REMOVE_VIDEO' | 'ACTION_MOVE_VIDEO_AFTER' | 'ACTION_SET_PLAYLIST_DESCRIPTION' | 'ACTION_SET_PLAYLIST_NAME';
addedVideoId?: string;
setVideoId?: string;
movedSetVideoIdPredecessor?: string;
playlistDescription?: string;
playlistName?: string;
}[];
}

0 comments on commit 86fb33e

Please sign in to comment.