diff options
Diffstat (limited to 'together/src/types')
-rw-r--r-- | together/src/types/Session.ts | 39 | ||||
-rw-r--r-- | together/src/types/User.ts | 44 | ||||
-rw-r--r-- | together/src/types/Video.ts | 41 |
3 files changed, 124 insertions, 0 deletions
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<Video> { + let res = await superagent.get("https://peh-internal.minteck.org/api/video?id=" + id) + .set("Cookie", "PEH2_SESSION_TOKEN=" + this.apiToken) + .send(); + + return new Video(id, res.body["title"], res.body["author"], res.body["duration"], res.body["duration_pretty"], res.body["url"], res.body["poster"]); + } + + toAPIUser(): APIUser { + return { + id: this.id + } + } +} + +export interface APIUser { + id: string; +}
\ No newline at end of file diff --git a/together/src/types/Video.ts b/together/src/types/Video.ts new file mode 100644 index 0000000..9bba13f --- /dev/null +++ b/together/src/types/Video.ts @@ -0,0 +1,41 @@ +export default class Video { + public id: string; + public title: string; + public author: string; + public duration: number; + public duration_pretty: string; + public url: string; + public thumbnail: string; + + constructor(id, title, author, duration, duration_pretty, url, thumbnail) { + this.id = id; + this.title = title; + this.author = author; + this.duration = duration; + this.duration_pretty = duration_pretty + this.url = url; + this.thumbnail = thumbnail; + } + + toOngoingVideo() { + return new OngoingVideo(this.id, this.title, this.author, this.duration, this.duration_pretty, this.url, this.thumbnail); + } +} + +export class OngoingVideo extends Video { + public state: VideoState; + public position: number; + + constructor(id, title, author, duration, duration_pretty, url, thumbnail) { + super(id, title, author, duration, duration_pretty, url, thumbnail); + this.state = VideoState.Paused; + this.position = 0; + } +} + +export enum VideoState { + Paused, + Playing, + Buffering, + Loading +}
\ No newline at end of file |