Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hahahaha #62

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ app.use('/static', express.static(path.join(__dirname, 'public')));

//passport stuff

var session=require('express-session');
var session = require('express-session');
app.use(session({ secret: 'auxen production' })); //changed key.

passport.serializeUser(function(user, done) {
Expand Down
4 changes: 2 additions & 2 deletions routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ module.exports = function(passport) {
res.render('login');
})

router.get('/auth/spotify',
router.get('/auth/spotify',
passport.authenticate('spotify', {
scope: ['user-read-email', 'user-read-private', 'user-modify-playback-state', 'user-read-playback-state']
scope: ['user-read-email', 'user-read-private', 'user-modify-playback-state', 'user-read-playback-state']
}
));

Expand Down
18 changes: 8 additions & 10 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ var User = models.User;
var Room = models.Room;
var existingRoomNames = [];

// testing

module.exports = function() {

/* Check login page. */
Expand All @@ -23,15 +25,9 @@ module.exports = function() {
function ifRedirected(req, res, next){
if(req.user){
if(req.user.premium === 'premium'){
// User.findOne({spotifyId:req.user.spotifyId})
// .then(user => {
// if(!user.active)next()
// else res.redirect('/multipleTabs')
// })
next();
}
else res.redirect('/notPremium');

}
else{
if(req.session){
Expand Down Expand Up @@ -84,7 +80,7 @@ module.exports = function() {
})

/* Get list of available rooms. */
router.get('/getRooms', ifRedirected,function(req, res, next) {
router.get('/getRooms', ifRedirected, function(req, res, next) {
Room.find(function(err, rooms) {
var roomNameArray = [];
roomNameArray = rooms.map(function(room) {
Expand All @@ -107,10 +103,12 @@ module.exports = function() {
djSpotifyId: req.user.spotifyId,
imageURL: req.user.imageURL,
djName: req.user.username
})
});

newRoom.save(function(err, newRoom) {
if (err) {
res.redirect('/error')
console.log("fuck this");
res.redirect('/error');
} else {
res.redirect('/djRoom/' + newRoom._id);
}
Expand Down Expand Up @@ -143,7 +141,7 @@ module.exports = function() {
var euser = room.usersInRoom.find(function(user){
return user.spotifyId === req.user.spotifyId;
})
if(euser)return;
if(euser)res.redirect('/error');
else {
var userObject = {
spotifyId: req.user.spotifyId,
Expand Down
68 changes: 60 additions & 8 deletions routes/socketPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,23 @@ module.exports = function(io) {
})
}

function clearRoom(roomName){
console.log("clearing room and turning all members inactive");
Room.findOne({"roomName":roomName})
.then(room => {
if(room){
inActive(room.djSpotifyId);
for(var i = 0;i<room.usersInRoom.length;i++){
inActive(room.usersInRoom[i].spotifyId);
};
room.remove();
}
})
.catch(err => {
console.log(err);
})
}

///////////////////// PASS DJ ////////////////////////

/* finds the right user from db. sets accestoken of user to dj token */
Expand Down Expand Up @@ -150,7 +167,7 @@ module.exports = function(io) {
.then( user => {
user.active = true;
user.save(function(err, user){
console.log("changed user active status", user);
console.log("changed user active status");
});
})
.catch( err => {
Expand Down Expand Up @@ -185,7 +202,7 @@ module.exports = function(io) {

/* called on disconnect, does not do anything */
socket.on('disconnect', function() {
console.log('user disconnected');
console.log('########user disconnected');
});

///////////////// USER //////////////////////
Expand Down Expand Up @@ -305,6 +322,13 @@ module.exports = function(io) {
}
});

socket.on('ping', function(data){
if(Date.now() - data > 10*1000){
console.log("DISCONNECT SOCKET");
}
else socket.emit('pong', Date.now());
});

////////////////// USER ENDS //////////////////

/////////////////// DJ ///////////////////////
Expand All @@ -324,12 +348,14 @@ module.exports = function(io) {
return getDJData(io.sockets.adapter.rooms[roomName].DJToken, roomName);
} else {
console.log("this room no longer exists");
Room.remove({'roomName': roomName})
.then(() => {
console.log("***********Ben - room successfully removed****************************");
}).catch((error) => {
console.log("error", error);
});
// Room.remove({'roomName': roomName})
// .then(room => {
// console.log("*********** room successfully removed ****************************");
// })
// .catch((error) => {
// console.log("error", error);
// });
clearRoom(roomName);
clearInterval(clearID);
}
}, 5000);
Expand Down Expand Up @@ -373,11 +399,37 @@ module.exports = function(io) {
socket.to(socket.room).emit('sendgrace');
})

/* mantains flame count */
socket.on('getflames', function() {
if(socket.room)socket.emit('getflames', io.sockets.adapter.rooms[socket.room].laflame)

})

// socket.on('startPinging', function(spotifyId){
//
// socket.flag = true;
// socket.spotifyId = spotifyId;
//
// var id = setInterval(function(){
// socket.flag = false;
// socket.emit('ping');
// }, 7000);
//
// var id2 = setInterval(function(){
// console.log("flag", socket.flag);
// if(!socket.flag){
// console.log("THIS IS AMAZING");
// }
// }, 9000);
//
// })
//
// socket.on('pong', function(){
// socket.flag = true;
// })



//////////////////// DJ ENDS ///////////////////
})

Expand Down
12 changes: 10 additions & 2 deletions views/createRoom.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,32 @@ and (max-device-width : 480px) {

<script type="text/javascript">


$.ajax({
url:'/isActive?spotifyId='+localStorage.getItem('spotifyId'),
success: function(response){
console.log("reached here", response.active);
if(response.active === true)window.location.href = '/multipleTabs';
else{
localStorage.setItem('closing', 'false');
localStorage.setItem('active', 'false');
// var a = localStorage.getItem('active');
// if((a && a === 'false') || !localStorage.getItem('closing')){
// localStorage.setItem('closing', 'false');
// localStorage.setItem('active', 'false');
// }
}
},
error: function(response){
console.log(response);
}
})


$('#startRoom').on('click', function() {
$('#roomSubmit').submit();
})

var existingRoomNames = '{{existingRoomNames}}';
existingRoomNames = existingRoomNames.split(' ');
console.log("existingRoomNames", existingRoomNames);

$('#roomSubmit').on('submit', function (event) {
Expand Down
45 changes: 30 additions & 15 deletions views/djRoom.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ var socket = io();
var clearId = -1;
var isDJ = true;
var userDJSpotifyId = '';
var unload = 0; // set to 1 when refresh or duplicated tab is closing so that specialClose does not happen...
new Clipboard('#shareable');


Expand All @@ -122,11 +123,12 @@ if(isSafari){
socket.emit('specialClose', {"roomName": roomName, "roomId": roomId, spotifyId:localStorage.getItem('spotifyId')});
clearInterval(clearId);
localStorage.setItem("closing", "true");
localStorage.setItem('active', 'false');
window.location.href = '/';
}
}
});
window.history.pushState('forward', null, './#forward');
window.history.pushState('forward', null, './#forward');
}
});
}
Expand All @@ -139,40 +141,49 @@ else {
// return "Are you sure you want to leave? This will take you to home page";
});

/* after refresh or close tab*/
/* after refresh or close tab */
$(window).on("unload", function(){
if(localStorage.getItem('closing') === 'false'){
if(localStorage.getItem('closing') === 'false' && unload === 0){
socket.emit('specialClose', {"roomName": roomName, "roomId": roomId, spotifyId: localStorage.getItem('spotifyId')});
localStorage.setItem("closing", "true");
localStorage.setItem('closing', 'true');
localStorage.setItem('active', 'false'); // changed this
clearInterval(clearId);
}
clearInterval(clearId);
});
}

// testing

/* Checking connnection to socket */
socket.on('connect', function(){

//console.log('Connected!');

clearId = setInterval(function () {
socket.emit("toRefresh", localStorage.getItem("refreshToken"));
}, 20*60000);

if(localStorage.getItem('closing') === "true"){
if(localStorage.getItem('active') === 'true'){
unload = 1;
window.location.href = '/multipleTabs';
}
if(localStorage.getItem('closing') === 'true'){
unload = 1;
window.location.href = '/';
}

else{
socket.emit('active',localStorage.getItem('spotifyId'))
else {
clearId = setInterval(function () {
socket.emit("toRefresh", localStorage.getItem("refreshToken"));
}, 20*60000);
socket.emit('active',localStorage.getItem('spotifyId'));
localStorage.setItem('active', 'true');
var djObject = {
roomName: roomName,
accessToken: localStorage.getItem('accessToken')
}
socket.emit('createRoom', djObject);
}

})

// socket.on('disconnect', function(){
// window.location.href = '/error';
// })

/* sets new access token after refresh */
socket.on('setNewAccessToken', function(accessToken){
localStorage.setItem('accessToken', accessToken);
Expand Down Expand Up @@ -293,6 +304,7 @@ socket.on('DJTakeBack', function(){
})

/////////////////////

$('#shareable').on('click', function() {
$('#shareable').text('Copied!');
var link = window.location.href;
Expand All @@ -305,6 +317,7 @@ $('#shareable').on('click', function() {


/* sends message of dj to room */

$('#djtalk').on('click', function() {
var djTalk = $('#djtalkval').val();
$('#djtalkval').val('');
Expand All @@ -331,6 +344,8 @@ $('#djtalkform').on('submit', function(e) {
$('#closeRoom').on('click', function(event){
console.log("reached front end destination");
localStorage.setItem('closing', 'true');
localStorage.setItem('active', 'false');
clearInterval(clearId);
socket.emit('closingRoom', {"roomName": roomName, "roomId": roomId, spotifyId: localStorage.getItem('spotifyId')});
$(window).unbind('beforeunload');
window.location = $(this).attr('data-url');
Expand Down
6 changes: 6 additions & 0 deletions views/getRooms.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ $.ajax({
success: function(response){
console.log("reached here", response);
if(response.active === true)window.location.href = '/multipleTabs';
else {
var a = localStorage.getItem('active');
if((a && a === 'false') || !localStorage.getItem('leaving')){
localStorage.setItem('leaving', "false");
}
}
}
})

Expand Down
15 changes: 10 additions & 5 deletions views/home.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,19 @@ and (max-device-width : 480px) {
var username = '{{username}}';
var accessToken = '{{{accessToken}}}';
var refreshToken = '{{{refreshToken}}}';
localStorage.setItem("spotifyId", spotifyId);
localStorage.setItem("imageURL", imageURL);
localStorage.setItem("username", username);
localStorage.setItem('closing', "false");
localStorage.setItem('leaving', "false");
localStorage.setItem('spotifyId', spotifyId);
localStorage.setItem('imageURL', imageURL);
localStorage.setItem('username', username);
localStorage.setItem('accessToken', accessToken);
localStorage.setItem('refreshToken', refreshToken);
var a = localStorage.getItem('active');

if((a && a === 'false') || !localStorage.getItem('closing'))localStorage.setItem('closing', 'false');

if((a && a === 'false') || !localStorage.getItem('leaving'))localStorage.setItem('leaving', "false");

getPlayback();

$(document).on('click','#songTest' ,function() {

$.ajax({
Expand Down
Loading