const watchrooms = require('../../utils/watchrooms');
const broadcast = require('../broadcast');
module.exports = function(user_id, video_id) {
// set video_id in case it was undefined
video_id = watchrooms.leave(user_id, video_id);
if (video_id) {
let watchroom = watchrooms.get(video_id);
if (watchroom) {
broadcast({
command: "announce",
data: {
target: "watchroom",
type: "user-left",
user_id: user_id
}
}, watchroom.viewers);
} else {
broadcast({
command: "announce",
data: {
target: "all",
type: "watchroom-closed",
video_id: video_id
}
});
}
}
};