implement channel handle id map cache

This commit is contained in:
Simon 2023-08-26 17:34:58 +07:00
parent adde4c51c0
commit 7c47c980f3
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
2 changed files with 55 additions and 10 deletions

View File

@ -120,14 +120,14 @@ async function download(url) {
); );
} }
async function subscribe(url) { async function subscribe(url, subscribed) {
return await sendData( return await sendData(
'api/channel/', 'api/channel/',
{ {
data: [ data: [
{ {
channel_id: url, channel_id: url,
channel_subscribed: true, channel_subscribed: subscribed,
}, },
], ],
}, },
@ -141,12 +141,46 @@ async function videoExists(id) {
return Boolean(response.data); 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}`; const path = `api/channel/search/?q=${channelHandle}`;
let response = await sendGet(path); let response = await sendGet(path);
return response.data; 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) { async function cookieStr(cookieLines) {
const path = 'api/cookie/'; const path = 'api/cookie/';
let payload = { let payload = {
@ -204,6 +238,9 @@ type Message =
| { type: 'sendCookie' } | { type: 'sendCookie' }
| { type: 'download', url: string } | { type: 'download', url: string }
| { type: 'subscribe', url: string } | { type: 'subscribe', url: string }
| { type: 'unsubscribe', url: string }
| { type: 'videoExists', id: string }
| { type: 'getChannel', url: string }
*/ */
function handleMessage(request, sender, sendResponse) { function handleMessage(request, sender, sendResponse) {
console.log('message background.js listener got message', request); console.log('message background.js listener got message', request);
@ -226,7 +263,11 @@ function handleMessage(request, sender, sendResponse) {
return await download(request.url); return await download(request.url);
} }
case 'subscribe': { 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': { case 'videoExists': {
return await videoExists(request.videoId); return await videoExists(request.videoId);

View File

@ -220,8 +220,15 @@ function buildChannelSubButton(channelHandle) {
channelSubButton.addEventListener('click', e => { channelSubButton.addEventListener('click', e => {
e.preventDefault(); e.preventDefault();
console.log(`subscribe to: ${channelHandle}`); if (channelSubButton.innerText === 'Subscribe') {
sendUrl(channelHandle, 'subscribe', channelSubButton); 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, { Object.assign(channelSubButton.style, {
padding: '5px', padding: '5px',
@ -234,10 +241,7 @@ function buildChannelSubButton(channelHandle) {
function checkChannelSubscribed(channelSubButton) { function checkChannelSubscribed(channelSubButton) {
function handleResponse(message) { function handleResponse(message) {
if ( if (!message || (typeof message === 'object' && message.channel_subscribed === false)) {
message === false ||
(typeof message === 'object' && message.channel_subscribed === false)
) {
channelSubButton.innerText = 'Subscribe'; channelSubButton.innerText = 'Subscribe';
} else if (typeof message === 'object' && message.channel_subscribed === true) { } else if (typeof message === 'object' && message.channel_subscribed === true) {
channelSubButton.innerText = 'Unsubscribe'; channelSubButton.innerText = 'Unsubscribe';