Skip to content

Commit

Permalink
Move vote on port video record to content
Browse files Browse the repository at this point in the history
  • Loading branch information
hanydd committed Dec 11, 2024
1 parent 5453507 commit 491ff8f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
9 changes: 7 additions & 2 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { setMessageNotice, showMessage } from "./render/MessageNotice";
import { PlayerButton } from "./render/PlayerButton";
import SkipNotice from "./render/SkipNotice";
import SubmissionNotice from "./render/SubmissionNotice";
import { getPortVideoByHash } from "./requests/portVideo";
import { getPortVideoByHash, postPortVideoVote } from "./requests/portVideo";
import { asyncRequestToServer } from "./requests/requests";
import { getSegmentsByHash } from "./requests/segments";
import { getVideoLabel } from "./requests/videoLabels";
Expand Down Expand Up @@ -1237,7 +1237,7 @@ function setupCategoryPill() {

function setupDescriptionPill() {
if (!descriptionPill) {
descriptionPill = new DescriptionPortPill(getPortVideo, sponsorsLookup);
descriptionPill = new DescriptionPortPill(getPortVideo, portVideoVote, sponsorsLookup);
}
descriptionPill.setupDecription(getVideoID());
}
Expand All @@ -1261,6 +1261,11 @@ async function getPortVideo(videoId: VideoID, bypassCache = false) {
});
}

async function portVideoVote(UUID: string, bvID: VideoID, voteType: number) {
postPortVideoVote(UUID, bvID, voteType);
await getPortVideo(this.bvID, true);
}

async function sponsorsLookup(keepOldSubmissions = true, ignoreServerCache = false, forceUpdatePreviewBar = false) {
if (lookupWaiting) return;
//there is still no video here
Expand Down
21 changes: 8 additions & 13 deletions src/render/DesciptionPortPill.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@ export class DescriptionPortPill {
portUUID: string;
hasDescription: boolean;
getPortVideo: (videoId: VideoID, bypassCache?: boolean) => void;
portVideoVote: (UUID: string, bvID: VideoID, voteType: number) => void;
sponsorsLookup: (keepOldSubmissions: boolean, ignoreServerCache: boolean, forceUpdatePreviewBar: boolean) => void;

inputContainer: HTMLElement;
buttonContainer: HTMLElement;
ref: React.RefObject<DescriptionPortPillComponent>;
root: Root;

constructor(getPortVideo: (videoId: VideoID, bypassCache?: boolean) => void, sponsorsLookup: () => void) {
constructor(
getPortVideo: (videoId: VideoID, bypassCache?: boolean) => void,
portVideoVote: (UUID: string, bvID: VideoID, voteType: number) => void,
sponsorsLookup: () => void
) {
this.getPortVideo = getPortVideo;
this.portVideoVote = portVideoVote;
this.sponsorsLookup = sponsorsLookup;
}

Expand Down Expand Up @@ -187,18 +193,7 @@ export class DescriptionPortPill {
return;
}

const response = await asyncRequestToServer("POST", "/api/votePort", {
UUID: this.portUUID,
bvID: this.bvID,
userID: Config.config.userID,
type: voteType,
});
if (!response?.ok) {
throw response?.responseText ? response.responseText : "投票失败!";
}

await this.getPortVideo(this.bvID, true);
this.ref.current.setState({ ytbVideoID: this.ytbID, previewYtbID: this.ytbID });
this.portVideoVote(this.portUUID, this.bvID, voteType);
}

private async updateSegments() {
Expand Down
13 changes: 13 additions & 0 deletions src/requests/portVideo.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Config from "../config";
import { VideoID } from "../types";
import { getHash } from "../utils/hash";
import { FetchResponse } from "./background-request-proxy";
Expand Down Expand Up @@ -49,6 +50,18 @@ export async function getPortVideoByHash(bvID: VideoID, options: RequestOptions
throw response;
}

export async function postPortVideoVote(UUID: string, bvID: VideoID, voteType: number) {
const response = await asyncRequestToServer("POST", "/api/votePort", {
UUID: UUID,
bvID: bvID,
userID: Config.config.userID,
type: voteType,
});
if (!response?.ok) {
throw response?.responseText ? response.responseText : "投票失败!";
}
}

export async function updatePortedSegments(bvID: VideoID): Promise<FetchResponse> {
return asyncRequestToServer("POST", "/api/updatePortedSegments", { videoID: bvID });
}

0 comments on commit 491ff8f

Please sign in to comment.