From a2df9a69dcc14cb70118cda2ded499055e7ee358 Mon Sep 17 00:00:00 2001 From: Minteck Date: Sun, 21 Aug 2022 17:31:56 +0200 Subject: m. update --- together/src/types/Session.ts | 39 ++++++++++++++++++++++++++++++++++++++ together/src/types/User.ts | 44 +++++++++++++++++++++++++++++++++++++++++++ together/src/types/Video.ts | 41 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 together/src/types/Session.ts create mode 100644 together/src/types/User.ts create mode 100644 together/src/types/Video.ts (limited to 'together/src/types') diff --git a/together/src/types/Session.ts b/together/src/types/Session.ts new file mode 100644 index 0000000..5d3e799 --- /dev/null +++ b/together/src/types/Session.ts @@ -0,0 +1,39 @@ +import {v4 as uuidv4} from 'uuid'; +import User from "./User"; +import Video, {OngoingVideo} from "./Video"; +import {generateParyCode} from "../utils/PartyCodes"; + +export default class Session { + public id: string; + public partyCode: string; + public videoQueue: Video[]; + public currentVideo: OngoingVideo; + public users: User[]; + + constructor() { + this.id = uuidv4(); + this.partyCode = generateParyCode(); + this.videoQueue = []; + this.currentVideo = null; + this.users = []; + } + + update() { + if(this.videoQueue.length === 0) this.currentVideo = null; + else this.currentVideo = (this.videoQueue.shift()).toOngoingVideo(); + + this.users.forEach(user => { + user.ws.send(JSON.stringify({ + "task": "VIDEO_UPDATE", + "payload": { + "url": this.currentVideo.url, + "title": this.currentVideo.title, + "author": this.currentVideo.author, + "thumbnail": this.currentVideo.thumbnail, + "state": this.currentVideo.state, + "position": this.currentVideo.position + } + })) + }); + } +} \ No newline at end of file diff --git a/together/src/types/User.ts b/together/src/types/User.ts new file mode 100644 index 0000000..900acb8 --- /dev/null +++ b/together/src/types/User.ts @@ -0,0 +1,44 @@ +import {WebSocket} from "ws"; +import Video from "./Video"; +import superagent from "superagent"; + +export default class User { + public id: string; + public ws: WebSocket; + public videoPositon: number; + private apiToken: string; + + constructor(ws: WebSocket, apiToken: string) { + this.videoPositon = 0; + this.ws = ws; + this.apiToken = apiToken; + + this.getUserData(); + } + + async getUserData() { + let res = await superagent.get("https://peh-internal.minteck.org/api/me") + .set("Cookie", "PEH2_SESSION_TOKEN=" + this.apiToken) + .send(); + + this.id = res.body["id"]; + } + + async getYoutubeVideo(id: string): Promise