fix: backend m3u playlist saving
This commit is contained in:
@@ -29,7 +29,7 @@ class ChannelService {
|
||||
return filtered;
|
||||
}
|
||||
|
||||
addChannel({ name, url, avatar, mode, headersJson, group = null, playlist = null, playlistName = null }) {
|
||||
addChannel({ name, url, avatar, mode, headersJson, group = null, playlist = null, playlistName = null }, save = true) {
|
||||
// const existing = this.channels.find(channel => channel.url === url);
|
||||
// if (existing) {
|
||||
// throw new Error('Channel already exists');
|
||||
@@ -38,7 +38,7 @@ class ChannelService {
|
||||
const headers = JSON.parse(headersJson);
|
||||
const newChannel = new Channel(name, url, avatar, mode, headers, group, playlist, playlistName);
|
||||
this.channels.push(newChannel);
|
||||
ChannelStorage.save(this.channels);
|
||||
if(save) ChannelStorage.save(this.channels);
|
||||
|
||||
return newChannel;
|
||||
}
|
||||
@@ -70,7 +70,7 @@ class ChannelService {
|
||||
return this.channels.find(channel => channel.id === id);
|
||||
}
|
||||
|
||||
async deleteChannel(id) {
|
||||
async deleteChannel(id, save = true) {
|
||||
const channelIndex = this.channels.findIndex(channel => channel.id === id);
|
||||
if (channelIndex === -1) {
|
||||
throw new Error('Channel does not exist');
|
||||
@@ -82,12 +82,12 @@ class ChannelService {
|
||||
await this.setCurrentChannel(0);
|
||||
}
|
||||
|
||||
ChannelStorage.save(this.channels);
|
||||
if(save) ChannelStorage.save(this.channels);
|
||||
|
||||
return this.currentChannel;
|
||||
}
|
||||
|
||||
async updateChannel(id, updatedAttributes) {
|
||||
async updateChannel(id, updatedAttributes, save = true) {
|
||||
console.log(id);
|
||||
const channelIndex = this.channels.findIndex(channel => channel.id === id);
|
||||
if (channelIndex === -1) {
|
||||
@@ -110,7 +110,7 @@ class ChannelService {
|
||||
}
|
||||
}
|
||||
|
||||
ChannelStorage.save(this.channels);
|
||||
if(save) ChannelStorage.save(this.channels);
|
||||
|
||||
return channel;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const m3uParser = require('iptv-playlist-parser');
|
||||
const ChannelService = require('./ChannelService');
|
||||
const ChannelStorage = require('./ChannelStorage');
|
||||
|
||||
class PlaylistService {
|
||||
|
||||
@@ -30,7 +31,7 @@ class PlaylistService {
|
||||
group: channel.group.title,
|
||||
playlist: playlist,
|
||||
playlistName: playlistName
|
||||
});
|
||||
}, false);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return null;
|
||||
@@ -38,30 +39,53 @@ class PlaylistService {
|
||||
})
|
||||
.filter(result => result !== null);
|
||||
|
||||
ChannelStorage.save(ChannelService.getChannels());
|
||||
|
||||
return channels;
|
||||
}
|
||||
|
||||
|
||||
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()
|
||||
.filter(channel => channel.playlist === playlistUrl);
|
||||
|
||||
for(let channel of channels) {
|
||||
channel = await ChannelService.updateChannel(channel.id, updatedAttributes);
|
||||
channel = await ChannelService.updateChannel(channel.id, updatedAttributes, false);
|
||||
}
|
||||
ChannelStorage.save(ChannelService.getChannels());
|
||||
|
||||
return channels;
|
||||
}
|
||||
|
||||
async deletePlaylist(playlistUrl) {
|
||||
|
||||
console.log('Adding playlist', playlistUrl);
|
||||
|
||||
const channels = ChannelService
|
||||
.getChannels()
|
||||
.filter(channel => channel.playlist === playlistUrl);
|
||||
|
||||
for(const channel of channels) {
|
||||
await ChannelService.deleteChannel(channel.id);
|
||||
await ChannelService.deleteChannel(channel.id, false);
|
||||
}
|
||||
ChannelStorage.save(ChannelService.getChannels());
|
||||
|
||||
return channels;
|
||||
}
|
||||
|
||||
@@ -23,9 +23,10 @@ module.exports = (io, socket) => {
|
||||
|
||||
socket.on('delete-channel', async (id) => {
|
||||
try {
|
||||
const lastChannel = ChannelService.getCurrentChannel();
|
||||
const current = await ChannelService.deleteChannel(id);
|
||||
io.emit('channel-deleted', id); // Broadcast to all clients
|
||||
io.emit('channel-selected', current);
|
||||
if(lastChannel.id != current.id) io.emit('channel-selected', current);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
socket.emit('app-error', { message: err.message });
|
||||
|
||||
@@ -22,6 +22,10 @@ module.exports = (io, socket) => {
|
||||
|
||||
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 => {
|
||||
|
||||
@@ -127,22 +127,13 @@ function ChannelModal({ onClose, channel }: ChannelModalProps) {
|
||||
});
|
||||
} else if (type === 'playlist') {
|
||||
const newPlaylist = inputMethod === 'url' ? playlistUrl.trim() : playlistText.trim();
|
||||
if(channel!.playlist !== newPlaylist) {
|
||||
socketService.deletePlaylist(channel!.playlist);
|
||||
socketService.addPlaylist(
|
||||
inputMethod === 'url' ? playlistUrl.trim() : playlistText.trim(),
|
||||
playlistName.trim(),
|
||||
mode,
|
||||
JSON.stringify(headers)
|
||||
);
|
||||
} else {
|
||||
socketService.updatePlaylist(channel!.playlist, {
|
||||
playlist: newPlaylist,
|
||||
playlistName: playlistName.trim(),
|
||||
mode: mode,
|
||||
headers: headers,
|
||||
headers: JSON.stringify(headers),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
addToast({
|
||||
type: 'success',
|
||||
|
||||
Reference in New Issue
Block a user