Skip to content

Commit

Permalink
Fix: Delete webradio favorite
Browse files Browse the repository at this point in the history
Upd: Improve select action handler
  • Loading branch information
jcorporation committed Sep 15, 2024
1 parent 6529db6 commit 7d844d1
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 70 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This is a small bug fix release.
### Changelog

- Upd: Russian translation
- Fix: Delete webradio favorite

***

Expand Down
120 changes: 59 additions & 61 deletions htdocs/js/selectActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,37 @@ function getSelectionData(parent, attribute) {
return data;
}

/**
* Returns the attribute for the select action type
* @param {string} type Action type
* @param {string} action action to handle
* @returns {string} Attribute name
*/
function getExecSelectActionAttribute(type, action) {
if (type === 'album') {
switch(action) {
case 'delQueueJukeboxEntry':
return 'pos';
default:
return 'AlbumId';
}
}

switch(action) {
case 'playAfterCurrent':
case 'removeFromQueueIDs':
return'songid';
case 'showMoveToPlaylist':
case 'removeFromPlaylistPositions':
case 'delQueueJukeboxEntry':
return'pos';
case'delRadioFavorites':
return 'name';
default:
return 'uri';
}
}

/**
* Handles the selection actions
* @param {string} type entity type
Expand All @@ -134,88 +165,55 @@ function getSelectionData(parent, attribute) {
//eslint-disable-next-line no-unused-vars
function execSelectAction(type, action) {
const parent = elGetById(app.id + 'List');
const attribute = type === 'album'
? action === 'delQueueJukeboxEntry'
? 'pos'
: 'AlbumId'
: action === 'playAfterCurrent' || action === 'removeFromQueueIDs'
? 'songid'
: action === 'showMoveToPlaylist' || action === 'removeFromPlaylistPositions' || action === 'delQueueJukeboxEntry'
? 'pos'
: 'uri';
const attribute = getExecSelectActionAttribute(type, action);
const data = getSelectionData(parent, attribute);
switch(action) {
case 'appendQueue': {
const uris = getSelectionData(parent, attribute);
appendQueue(type, uris);
case 'appendQueue':
appendQueue(type, data);
break;
}
case 'appendPlayQueue': {
const uris = getSelectionData(parent, attribute);
appendPlayQueue(type, uris);
case 'appendPlayQueue':
appendPlayQueue(type, data);
break;
}
case 'playAfterCurrent': {
const songIds = getSelectionData(parent, attribute);
playAfterCurrent(songIds);
case 'playAfterCurrent':
playAfterCurrent(data);
break;
}
case 'insertAfterCurrentQueue': {
const uris = getSelectionData(parent, attribute);
insertAfterCurrentQueue(type, uris);
case 'insertAfterCurrentQueue':
insertAfterCurrentQueue(type, data);
break;
}
case 'replaceQueue': {
const uris = getSelectionData(parent, attribute);
replaceQueue(type, uris);
case 'replaceQueue':
replaceQueue(type, data);
break;
}
case 'replacePlayQueue': {
const uris = getSelectionData(parent, attribute);
replacePlayQueue(type, uris);
case 'replacePlayQueue':
replacePlayQueue(type, data);
break;
}
case 'removeFromQueueIDs': {
const songIds = getSelectionData(parent, attribute);
removeFromQueueIDs(songIds);
case 'removeFromQueueIDs':
removeFromQueueIDs(data);
break;
}
case 'showAddToPlaylist': {
const uris = getSelectionData(parent, attribute);
showAddToPlaylist(type, uris);
case 'showAddToPlaylist':
showAddToPlaylist(type, data);
break;
}
case 'showMoveToPlaylist': {
const positions = getSelectionData(parent, attribute);
const plist = getData(parent, 'uri');
showMoveToPlaylist(plist, positions);
showMoveToPlaylist(plist, data);
break;
}
case 'removeFromPlaylistPositions': {
const positions = getSelectionData(parent, attribute);
const plist = getData(parent, 'uri');
removeFromPlaylistPositions(plist, positions);
removeFromPlaylistPositions(plist, data);
break;
}
case 'showDelPlaylist': {
const uris = getSelectionData(parent, attribute);
showDelPlaylist(uris);
case 'showDelPlaylist':
showDelPlaylist(data);
break;
}
case 'showCopyPlaylist': {
const plists = getSelectionData(parent, attribute);
showCopyPlaylist(plists);
case 'showCopyPlaylist':
showCopyPlaylist(data);
break;
}
case 'delRadioFavorites': {
const uris = getSelectionData(parent, attribute);
deleteRadioFavorites(uris);
case 'delRadioFavorites':
deleteRadioFavorites(data);
break;
}
case 'delQueueJukeboxEntry': {
const positions = getSelectionData(parent, attribute);
delQueueJukeboxEntries(positions);
case 'delQueueJukeboxEntry':
delQueueJukeboxEntries(data);
break;
}
default:
logError('Invalid select action: ' + action);
}
Expand Down
15 changes: 8 additions & 7 deletions src/mympd_api/mympd_api_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -1681,17 +1681,18 @@ void mympd_api_handler(struct t_mympd_state *mympd_state, struct t_partition_sta
}
case MYMPD_API_WEBRADIO_FAVORITE_RM:
if (webradios_get_write_lock(mympd_state->webradio_favorites) == true) {
struct t_list ids;
list_init(&ids);
if (json_get_array_string(request->data, "$.params.names", &ids, vcb_isname, MPD_COMMANDS_MAX, &parse_error) == true) {
if (ids.length == 0) {
struct t_list names;
list_init(&names);
if (json_get_array_string(request->data, "$.params.names", &names, vcb_isname, MPD_COMMANDS_MAX, &parse_error) == true) {
if (names.length == 0) {
response->data = jsonrpc_respond_message(response->data, request->cmd_id, request->id,
JSONRPC_FACILITY_QUEUE, JSONRPC_SEVERITY_ERROR, "No webradio favorites provided");
}
mympd_api_webradio_favorite_delete(mympd_state->webradio_favorites, &ids);
response->data = jsonrpc_respond_ok(response->data, request->cmd_id, request->id, JSONRPC_FACILITY_DATABASE);
int_buf1 = mympd_api_webradio_favorite_delete(mympd_state->webradio_favorites, &names);
response->data = jsonrpc_respond_with_ok_or_error(response->data, request->cmd_id, request->id, (int_buf1 > 0 ? true : false),
JSONRPC_FACILITY_DATABASE, "Could not delete webradio favorite");
}
list_clear(&ids);
list_clear(&names);
webradios_release_lock(mympd_state->webradio_favorites);
}
else {
Expand Down
5 changes: 4 additions & 1 deletion src/mympd_api/webradio_favorites.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,19 @@ bool mympd_api_webradio_favorite_save(struct t_webradios *webradio_favorites, st
* @param webradio_favorites Webradio favorites struct
* @param names webradio ids to delete
*/
void mympd_api_webradio_favorite_delete(struct t_webradios *webradio_favorites, struct t_list *names) {
int mympd_api_webradio_favorite_delete(struct t_webradios *webradio_favorites, struct t_list *names) {
struct t_list_node *current = names->head;
int deleted = 0;
while (current != NULL) {
void *data = NULL;
if (raxRemove(webradio_favorites->db, (unsigned char *)current->key, sdslen(current->key), &data) == 1) {
deleted++;
struct t_webradio_data *webradio = (struct t_webradio_data *)data;
// remove uri index
raxRemove(webradio_favorites->idx_uris, (unsigned char *)webradio->uris.head->key, sdslen(webradio->uris.head->key), NULL);
webradio_data_free(webradio);
}
current = current->next;
}
return deleted;
}
2 changes: 1 addition & 1 deletion src/mympd_api/webradio_favorites.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
#include <stdbool.h>

bool mympd_api_webradio_favorite_save(struct t_webradios *webradio_favorites, struct t_webradio_data *webradio, sds old_name);
void mympd_api_webradio_favorite_delete(struct t_webradios *webradio_favorites, struct t_list *names);
int mympd_api_webradio_favorite_delete(struct t_webradios *webradio_favorites, struct t_list *names);

#endif

0 comments on commit 7d844d1

Please sign in to comment.