Skip to content

armgit / my-yt

Public Imported from GitHub
0

A clean and minimal youtube frontend, without all the ads and whistles

JavaScript

handle duplicate channel

Christian Fei8a90018346cb
diff --git a/lib/server.js b/lib/server.js
index 5fdd283..b872b5d 100644
--- a/lib/server.js
+++ b/lib/server.js
@@ -5,7 +5,7 @@ import { summarizeVideo } from './subtitles-summary.js'
import { handleSSE, broadcastSSE } from './sse.js'
import { downloadVideo, getVideosFor } from './youtube.js'
-export function createServer ({repo, port = 3000, connections = []}) {
+export function createServer ({repo, connections = []}) {
updateAndPersistVideos(repo, ({name, videos}) => {
broadcastSSE(JSON.stringify({type: 'channel', name, videos}), connections)
})
@@ -26,7 +26,7 @@ export function createServer ({repo, port = 3000, connections = []}) {
}
if (url.pathname === '/channels' && req.method === 'POST')
- return addChannelHandler(req, res)
+ return addChannelHandler(req, res, repo)
if (url.pathname === '/download-video' && req.method === 'POST')
return downloadVideoHandler(req, res, connections)
if (url.pathname === '/summarize-video' && req.method === 'POST')
@@ -60,20 +60,26 @@ export function createServer ({repo, port = 3000, connections = []}) {
})
}
- async function addChannelHandler(req, res) {
+ async function addChannelHandler(req, res, repo) {
const body = await getBody(req)
let { name } = JSON.parse(body)
name = name.trim()
+
+ if (repo.channelExists(name)) {
+ res.writeHead(409, { 'Content-Type': 'text/plain' })
+ return res.end('Channel already added')
+ }
+
const videos = await updateAndPersistVideosForChannel(name, repo)
if (Array.isArray(videos)) {
repo.addChannel(name)
broadcastSSE(JSON.stringify({type: 'channel', name, videos}), connections)
res.writeHead(201, { 'Content-Type': 'text/plain' })
- res.end('Channel added')
- } else {
- res.writeHead(404, { 'Content-Type': 'text/plain' })
- res.end('Channel not found')
+ return res.end('Channel added')
}
+
+ res.writeHead(404, { 'Content-Type': 'text/plain' })
+ return res.end('Channel not found')
}
async function downloadVideoHandler(req, res, connections = []) {