Skip to content

Commit

Permalink
Update protube api for minor bugs (#134)
Browse files Browse the repository at this point in the history
* Update protube api for minor bugs

* Fix code style issues with Prettier

---------

Co-authored-by: Lint Action <[email protected]>
Co-authored-by: ysbrandB <[email protected]>
  • Loading branch information
3 people authored Dec 5, 2023
1 parent a708300 commit 2375b40
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 32 deletions.
64 changes: 34 additions & 30 deletions client/src/views/ProtubeScreen.vue
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ const props = defineProps({
});
const allowedDelta = props.screenCode === -1 ? 2 : 0.12;
let YTPlayerState=-1;
let YTPlayerState = -1;
let bufferTime = -1;
let firstTime=-1;
let firstTime = -1;
let maySync = true;
// Compute the queue with the currently playing video at the front
Expand Down Expand Up @@ -173,32 +173,33 @@ onBeforeMount(() => {
});
onMounted(() => {
player = YoutubePlayer(playerID, {
host: "https://www.youtube-nocookie.com",
videoId: "",
playerVars: {
autoplay: 1,
controls: 0,
modestbranding: 0,
loop: 0,
},
});
player = YoutubePlayer(playerID, {
host: "https://www.youtube-nocookie.com",
videoId: "",
playerVars: {
autoplay: 1,
controls: 0,
modestbranding: 0,
loop: 0,
},
});
// the iframe api player generates any error (unplayable media)
player.on("error", (event) => {
emit("youtube-media-error", event.data);
});
// the iframe api player generates any error (unplayable media)
player.on("error", (event) => {
emit("youtube-media-error", event.data);
});
player.on('stateChange', (event) => {
if(event.data===-1){
return
player.on("stateChange", (event) => {
if (event.data === -1) {
return;
}
if(event.data === 5 && playerState.value.playerType === enums.TYPES.VIDEO){
player.playVideo();
bufferTime=(Date.now()-firstTime)/1000;
bufferTime = (Date.now() - firstTime) / 1000;
}
YTPlayerState = event.data
})
YTPlayerState = event.data;
});
});
watch(
Expand All @@ -216,8 +217,8 @@ onBeforeUnmount(() => {
socket.on("player-update", (newState) => {
if (newState.playerType === enums.TYPES.VIDEO) {
if (newState.playerMode === enums.MODES.PLAYING) {
player.cueVideoById(newState.video.id, newState.timestamp)
firstTime=Date.now()
player.cueVideoById(newState.video.id, newState.timestamp);
firstTime = Date.now();
maySync = true;
} else player.pauseVideo();
} else if (playerState.value.playerType === enums.TYPES.VIDEO)
Expand All @@ -229,24 +230,27 @@ socket.on("player-update", (newState) => {
socket.on("new-video-timestamp", async (newStamp) => {
const playerTime = await player.getCurrentTime();
//if the player is buffering or we can not get a playertime do nothing
if(Number.isNaN(playerTime) || YTPlayerState === 3){
if (Number.isNaN(playerTime) || YTPlayerState === 3) {
return;
}
let delta=newStamp.timestamp - playerTime;
let delta = newStamp.timestamp - playerTime;
totalDuration.value = newStamp.totalDuration;
queueProgress.value =
(newStamp.timestamp / playerState.value.video.duration) * 100;
if(Math.abs(delta) <= allowedDelta){
maySync=false
if (Math.abs(delta) <= allowedDelta) {
maySync = false;
}
if((Math.abs(delta) > allowedDelta && maySync) || Math.abs(delta) > allowedDelta * 10) {
if (
(Math.abs(delta) > allowedDelta && maySync) ||
Math.abs(delta) > allowedDelta * 10
) {
if (Math.abs(delta) > bufferTime * 2) {
setTimeout(async () => {
player.playVideo();
}, bufferTime * 2000);
player.pauseVideo().then(()=>{
player.pauseVideo().then(() => {
player.seekTo(newStamp.timestamp + bufferTime * 2, true);
});
} else {
Expand Down
9 changes: 7 additions & 2 deletions server/modules/api_endpoints/ProtubeApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ this.protubeApi.post("/updateadmin", async function (req, res) {
logger.apiInfo(
`Attempt from ${req.hostname} to update the admin status of a user`
);

// Check if the required data is present and parse it
if (!req.body?.user_id || !req.body?.admin) {
if (
!Object.keys(req.body).includes("user_id") ||
!Object.keys(req.body).includes("admin")
) {
logger.apiInfo("Request had incomplete body");
return res.send({ success: enums.FAIL, message: "Incomplete body" });
}
const userID = parseInt(req.body.user_id);
const isAdmin = parseInt(req.body.admin) === 1;
const isAdmin = +req.body.admin === 1;

// finding and updating the users admin status in the database
const user = await User.findByPk(userID);
Expand Down Expand Up @@ -61,6 +65,7 @@ this.protubeApi.post("/updateadmin", async function (req, res) {

// Endpoint to skip a song
this.protubeApi.post("/skipsong", function (req, res) {
logger.apiInfo(`Attempt from ${req.hostname} to skip a song`);
const wasPlaying = getPlayerMode() !== enums.MODES.IDLE;
try {
playNextVideo();
Expand Down

0 comments on commit 2375b40

Please sign in to comment.