refactor: extract functions and avoid duplicates in PlaylistSocketHandler
This commit is contained in:
@@ -35,7 +35,13 @@ class ChannelService {
|
||||
// throw new Error('Channel already exists');
|
||||
// }
|
||||
|
||||
const headers = JSON.parse(headersJson);
|
||||
let headers = headersJson;
|
||||
try {
|
||||
//Try to parse headers if not already parsed
|
||||
headers = JSON.parse(headersJson);
|
||||
} catch (error) {
|
||||
}
|
||||
|
||||
const newChannel = new Channel(name, url, avatar, mode, headers, group, playlist, playlistName);
|
||||
this.channels.push(newChannel);
|
||||
if(save) ChannelStorage.save(this.channels);
|
||||
@@ -88,7 +94,7 @@ class ChannelService {
|
||||
}
|
||||
|
||||
async updateChannel(id, updatedAttributes, save = true) {
|
||||
console.log(id);
|
||||
|
||||
const channelIndex = this.channels.findIndex(channel => channel.id === id);
|
||||
if (channelIndex === -1) {
|
||||
throw new Error('Channel does not exist');
|
||||
|
||||
@@ -47,20 +47,6 @@ class PlaylistService {
|
||||
|
||||
async updatePlaylist(playlistUrl, updatedAttributes) {
|
||||
|
||||
if(playlistUrl !== updatedAttributes.playlist) {
|
||||
// Playlist URL has changed - delete channels and fetch again
|
||||
await this.deletePlaylist(playlistUrl);
|
||||
const channels = await this.addPlaylist(
|
||||
updatedAttributes.playlist,
|
||||
updatedAttributes.playlistName,
|
||||
updatedAttributes.mode,
|
||||
updatedAttributes.headers
|
||||
);
|
||||
|
||||
ChannelStorage.save(ChannelService.getChannels());
|
||||
return channels;
|
||||
}
|
||||
|
||||
// Update channels attributes
|
||||
const channels = ChannelService
|
||||
.getChannels()
|
||||
@@ -76,7 +62,7 @@ class PlaylistService {
|
||||
|
||||
async deletePlaylist(playlistUrl) {
|
||||
|
||||
console.log('Adding playlist', playlistUrl);
|
||||
console.log('Deleting playlist', playlistUrl);
|
||||
|
||||
const channels = ChannelService
|
||||
.getChannels()
|
||||
|
||||
@@ -2,52 +2,57 @@ const PlaylistService = require('../services/PlaylistService');
|
||||
const ChannelService = require('../services/ChannelService');
|
||||
const Channel = require('../models/Channel');
|
||||
|
||||
async function handleAddPlaylist({ playlist, playlistName, mode, headers }, io, socket) {
|
||||
try {
|
||||
const channels = await PlaylistService.addPlaylist(playlist, playlistName, mode, headers);
|
||||
|
||||
if (channels) {
|
||||
channels.forEach(channel => {
|
||||
io.emit('channel-added', channel);
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
socket.emit('app-error', { message: err.message });
|
||||
}
|
||||
}
|
||||
|
||||
async function handleUpdatePlaylist({ playlist, updatedAttributes }, io, socket) {
|
||||
try {
|
||||
if (playlist !== updatedAttributes.playlist) {
|
||||
// Playlist URL has changed - delete channels and fetch again
|
||||
await handleDeletePlaylist(playlist, io, socket);
|
||||
await handleAddPlaylist(updatedAttributes, io, socket);
|
||||
return;
|
||||
}
|
||||
|
||||
const channels = await PlaylistService.updatePlaylist(playlist, updatedAttributes);
|
||||
|
||||
channels.forEach(channel => {
|
||||
io.emit('channel-updated', channel);
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
socket.emit('app-error', { message: err.message });
|
||||
}
|
||||
}
|
||||
|
||||
async function handleDeletePlaylist(playlist, io, socket) {
|
||||
try {
|
||||
const channels = await PlaylistService.deletePlaylist(playlist);
|
||||
|
||||
channels.forEach(channel => {
|
||||
io.emit('channel-deleted', channel.id);
|
||||
});
|
||||
io.emit('channel-selected', ChannelService.getCurrentChannel());
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
socket.emit('app-error', { message: err.message });
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = (io, socket) => {
|
||||
|
||||
socket.on('add-playlist', async ({ playlist, playlistName, mode, headersJson }) => {
|
||||
try {
|
||||
const channels = await PlaylistService.addPlaylist(playlist, playlistName, mode, headersJson);
|
||||
|
||||
if (channels) {
|
||||
channels.forEach(channel => {
|
||||
io.emit('channel-added', channel);
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
socket.emit('app-error', { message: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
socket.on('update-playlist', async ({ playlist, updatedAttributes }) => {
|
||||
try {
|
||||
if(playlist !== updatedAttributes.playlist) {
|
||||
//call delete-playlist
|
||||
}
|
||||
|
||||
const channels = await PlaylistService.updatePlaylist(playlist, updatedAttributes);
|
||||
|
||||
channels.forEach(channel => {
|
||||
io.emit('channel-updated', channel);
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
socket.emit('app-error', { message: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
socket.on('delete-playlist', async (playlist) => {
|
||||
try {
|
||||
const channels = await PlaylistService.deletePlaylist(playlist);
|
||||
channels.forEach(channel => {
|
||||
io.emit('channel-deleted', channel.id);
|
||||
});
|
||||
io.emit('channel-selected', ChannelService.getCurrentChannel());
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
socket.emit('app-error', { message: err.message });
|
||||
}
|
||||
});
|
||||
};
|
||||
socket.on('add-playlist', data => handleAddPlaylist(data, io, socket));
|
||||
socket.on('update-playlist', data => handleUpdatePlaylist(data, io, socket));
|
||||
socket.on('delete-playlist', playlist => handleDeletePlaylist(playlist, io, socket));
|
||||
};
|
||||
@@ -39,7 +39,11 @@ function ChannelModal({ onClose, channel }: ChannelModalProps) {
|
||||
setIsEditMode(true);
|
||||
setType('channel');
|
||||
|
||||
if(channel.playlist.startsWith("http")) {
|
||||
if(!channel.playlist) {
|
||||
setInputMethod('url');
|
||||
setPlaylistUrl('');
|
||||
setPlaylistText('');
|
||||
} else if(channel.playlist.startsWith("http")) {
|
||||
setInputMethod('url');
|
||||
setPlaylistUrl(channel.playlist);
|
||||
setPlaylistText('');
|
||||
@@ -131,7 +135,7 @@ function ChannelModal({ onClose, channel }: ChannelModalProps) {
|
||||
playlist: newPlaylist,
|
||||
playlistName: playlistName.trim(),
|
||||
mode: mode,
|
||||
headers: JSON.stringify(headers),
|
||||
headers: headers,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -98,10 +98,10 @@ class SocketService {
|
||||
}
|
||||
|
||||
// Add playlist
|
||||
addPlaylist(playlist: string, playlistName: string, mode: ChannelMode, headersJson: string) {
|
||||
addPlaylist(playlist: string, playlistName: string, mode: ChannelMode, headers: string) {
|
||||
if (!this.socket) throw new Error('Socket is not connected.');
|
||||
|
||||
this.socket.emit('add-playlist', { playlist, playlistName, mode, headersJson });
|
||||
this.socket.emit('add-playlist', { playlist, playlistName, mode, headers });
|
||||
}
|
||||
|
||||
// Update playlist
|
||||
|
||||
Reference in New Issue
Block a user