From 7c47c980f3475b02e3fbf8233aa53d0f247caf06 Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 26 Aug 2023 17:34:58 +0700 Subject: [PATCH] implement channel handle id map cache --- extension/background.js | 49 +++++++++++++++++++++++++++++++++++++---- extension/script.js | 16 +++++++++----- 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/extension/background.js b/extension/background.js index f649b25..2e69992 100644 --- a/extension/background.js +++ b/extension/background.js @@ -120,14 +120,14 @@ async function download(url) { ); } -async function subscribe(url) { +async function subscribe(url, subscribed) { return await sendData( 'api/channel/', { data: [ { channel_id: url, - channel_subscribed: true, + channel_subscribed: subscribed, }, ], }, @@ -141,12 +141,46 @@ async function videoExists(id) { return Boolean(response.data); } -async function getChannel(channelHandle) { +async function getChannelCache() { + let cache = await browserType.storage.local.get('cache'); + return cache || { cache: {} }; +} + +async function setChannel(channelHandler, channelId) { + let cache = await getChannelCache(); + cache.cache[channelHandler] = { id: channelId, timestamp: Date.now() }; + browserType.storage.local.set(cache); +} + +async function getChannelId(channelHandle) { + let cache = await getChannelCache(); + + if (cache.cache[channelHandle]) { + return cache.cache[channelHandle]?.id; + } + + let channel = await searchChannel(channelHandle); + if (channel) setChannel(channelHandle, channel.channel_id); + + return channel.channel_id; +} + +async function searchChannel(channelHandle) { const path = `api/channel/search/?q=${channelHandle}`; let response = await sendGet(path); return response.data; } +async function getChannel(channelHandle) { + let channelId = await getChannelId(channelHandle); + if (!channelId) return; + + const path = `api/channel/${channelId}/`; + let response = await sendGet(path); + + return response.data; +} + async function cookieStr(cookieLines) { const path = 'api/cookie/'; let payload = { @@ -204,6 +238,9 @@ type Message = | { type: 'sendCookie' } | { type: 'download', url: string } | { type: 'subscribe', url: string } + | { type: 'unsubscribe', url: string } + | { type: 'videoExists', id: string } + | { type: 'getChannel', url: string } */ function handleMessage(request, sender, sendResponse) { console.log('message background.js listener got message', request); @@ -226,7 +263,11 @@ function handleMessage(request, sender, sendResponse) { return await download(request.url); } case 'subscribe': { - return await subscribe(request.url); + return await subscribe(request.url, true); + } + case 'unsubscribe': { + let channelId = await getChannelId(request.url); + return await subscribe(channelId, false); } case 'videoExists': { return await videoExists(request.videoId); diff --git a/extension/script.js b/extension/script.js index 6d8f846..8aad209 100644 --- a/extension/script.js +++ b/extension/script.js @@ -220,8 +220,15 @@ function buildChannelSubButton(channelHandle) { channelSubButton.addEventListener('click', e => { e.preventDefault(); - console.log(`subscribe to: ${channelHandle}`); - sendUrl(channelHandle, 'subscribe', channelSubButton); + if (channelSubButton.innerText === 'Subscribe') { + console.log(`subscribe to: ${channelHandle}`); + sendUrl(channelHandle, 'subscribe', channelSubButton); + } else if (channelSubButton.innerText === 'Unsubscribe') { + console.log(`unsubscribe from: ${channelHandle}`); + sendUrl(channelHandle, 'unsubscribe', channelSubButton); + } else { + console.log('Unknown state'); + } }); Object.assign(channelSubButton.style, { padding: '5px', @@ -234,10 +241,7 @@ function buildChannelSubButton(channelHandle) { function checkChannelSubscribed(channelSubButton) { function handleResponse(message) { - if ( - message === false || - (typeof message === 'object' && message.channel_subscribed === false) - ) { + if (!message || (typeof message === 'object' && message.channel_subscribed === false)) { channelSubButton.innerText = 'Subscribe'; } else if (typeof message === 'object' && message.channel_subscribed === true) { channelSubButton.innerText = 'Unsubscribe';