Skip to content

Commit

Permalink
Refresh video label cache after submitting
Browse files Browse the repository at this point in the history
  • Loading branch information
hanydd committed Sep 13, 2024
1 parent ef071c4 commit 650fda3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { PlayerButton } from "./render/PlayerButton";
import SkipNotice from "./render/SkipNotice";
import SubmissionNotice from "./render/SubmissionNotice";
import { asyncRequestToServer } from "./requests/requests";
import { getVideoLabel } from "./requests/videoLabels";
import { setupThumbnailListener, updateAll } from "./thumbnail-utils/thumbnailManagement";
import {
ActionType,
Expand Down Expand Up @@ -2374,6 +2375,8 @@ async function sendSubmitMessage(): Promise<boolean> {
const fullVideoSegment = sponsorTimes.filter((time) => time.actionType === ActionType.Full)[0];
if (fullVideoSegment) {
categoryPill?.setSegment(fullVideoSegment);
// refresh the video labels cache
getVideoLabel(getVideoID(), true);
}

return true;
Expand Down
10 changes: 5 additions & 5 deletions src/requests/videoLabels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ export interface LabelCacheEntry {
const labelCache: Record<string, LabelCacheEntry> = {};
const cacheLimit = 1000;

async function getLabelHashBlock(hashPrefix: string): Promise<LabelCacheEntry | null> {
async function getLabelHashBlock(hashPrefix: string, refreshCache: boolean = false): Promise<LabelCacheEntry | null> {
// Check cache
const cachedEntry = labelCache[hashPrefix];
if (cachedEntry) {
if (cachedEntry && !refreshCache) {
return cachedEntry;
}

const response = await asyncRequestToServer("GET", `/api/videoLabels/${hashPrefix}`);
const response = await asyncRequestToServer("GET", `/api/videoLabels/${hashPrefix}`, {}, refreshCache);
if (response.status !== 200) {
// No video labels or server down
labelCache[hashPrefix] = {
Expand Down Expand Up @@ -54,9 +54,9 @@ async function getLabelHashBlock(hashPrefix: string): Promise<LabelCacheEntry |
}
}

export async function getVideoLabel(videoID: VideoID): Promise<Category | null> {
export async function getVideoLabel(videoID: VideoID, refreshCache: boolean = false): Promise<Category | null> {
const prefix = (await getVideoIDHash(videoID)).slice(0, 3);
const result = await getLabelHashBlock(prefix);
const result = await getLabelHashBlock(prefix, refreshCache);

if (result) {
const category = result.videos[videoID];
Expand Down

0 comments on commit 650fda3

Please sign in to comment.