Skip to content

Commit

Permalink
Make label return cid
Browse files Browse the repository at this point in the history
  • Loading branch information
hanydd committed Nov 3, 2024
1 parent aae3cfb commit 75bdacb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/routes/getVideoLabel.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
import { Request, Response } from "express";
import { getSegmentsFromDBByHash, getSegmentsFromDBByVideoID } from "../dao/skipSegment";
import { SBRecord } from "../types/lib.model";
import { ActionType, DBSegment, HiddenType, Segment, Service, VideoData, VideoID, VideoIDHash } from "../types/segments.model";
import { ActionType, DBSegment, HiddenType, Service, VideoID, VideoIDHash, VideoLabel, VideoLabelData } from "../types/segments.model";
import { getService } from "../utils/getService";
import { Logger } from "../utils/logger";

function transformDBSegments(segments: DBSegment[]): Segment[] {
function transformDBSegments(segments: DBSegment[]): VideoLabel[] {
return segments.map((chosenSegment) => ({
cid: chosenSegment.cid,
category: chosenSegment.category,
actionType: chosenSegment.actionType,
segment: [chosenSegment.startTime, chosenSegment.endTime],
UUID: chosenSegment.UUID,
locked: chosenSegment.locked,
votes: chosenSegment.votes,
videoDuration: chosenSegment.videoDuration,
userID: chosenSegment.userID,
description: chosenSegment.description
}));
}

async function getLabelsByVideoID(videoID: VideoID, service: Service): Promise<Segment[]> {
async function getLabelsByVideoID(videoID: VideoID, service: Service): Promise<VideoLabel[]> {
try {
const segments: DBSegment[] = await getSegmentsFromDBByVideoID(videoID, service);
return chooseSegment(segments);
Expand All @@ -32,8 +28,8 @@ async function getLabelsByVideoID(videoID: VideoID, service: Service): Promise<S
}
}

async function getLabelsByHash(hashedVideoIDPrefix: VideoIDHash, service: Service): Promise<SBRecord<VideoID, VideoData>> {
const segments: SBRecord<VideoID, VideoData> = {};
async function getLabelsByHash(hashedVideoIDPrefix: VideoIDHash, service: Service): Promise<SBRecord<VideoID, VideoLabelData>> {
const segments: SBRecord<VideoID, VideoLabelData> = {};

try {
type SegmentWithHashPerVideoID = SBRecord<VideoID, { hash: VideoIDHash, segments: DBSegment[] }>;
Expand All @@ -52,7 +48,7 @@ async function getLabelsByHash(hashedVideoIDPrefix: VideoIDHash, service: Servic
}, {});

for (const [videoID, videoData] of Object.entries(segmentPerVideoID)) {
const data: VideoData = {
const data: VideoLabelData = {
segments: chooseSegment(videoData.segments),
};

Expand All @@ -68,7 +64,7 @@ async function getLabelsByHash(hashedVideoIDPrefix: VideoIDHash, service: Servic
}
}

function chooseSegment<T extends DBSegment>(choices: T[]): Segment[] {
function chooseSegment<T extends DBSegment>(choices: T[]): VideoLabel[] {
// filter out -2 segments
choices = choices.filter(segment => segment.actionType == ActionType.Full && segment.votes > -2 && segment.hidden == HiddenType.Show);
const results = [];
Expand Down Expand Up @@ -96,7 +92,7 @@ function chooseSegment<T extends DBSegment>(choices: T[]): Segment[] {
return transformDBSegments(results);
}

async function handleGetLabel(req: Request, res: Response): Promise<Segment[] | false> {
async function handleGetLabel(req: Request, res: Response): Promise<VideoLabel[] | false> {
const videoID = req.query.videoID as VideoID;
if (!videoID) {
res.status(400).send("videoID not specified");
Expand Down
13 changes: 13 additions & 0 deletions src/types/segments.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ export interface IncomingSegment {
ignoreSegment?: boolean;
}

export interface VideoLabel {
cid: string;
category: Category;
UUID: SegmentUUID;
videoDuration: VideoDuration;
locked: boolean;
votes: number;
}

export interface Segment {
cid: string;
category: Category;
Expand Down Expand Up @@ -114,6 +123,10 @@ export interface VotableObjectWithWeight extends VotableObject {
weight: number;
}

export interface VideoLabelData {
segments: VideoLabel[];
}

export interface VideoData {
segments: Segment[];
}
Expand Down

0 comments on commit 75bdacb

Please sign in to comment.