123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- 'use strict';
- // nodejs modules
- const path = require('path'),
- fs = require('fs'),
- os = require('os'),
- events = require('events');
- // npm modules
- const config = require('config'),
- request = require('request'),
- waterfall = require('async/waterfall'),
- passport = require('passport');
- // custom modules
- const global = require('./global'),
- stations = require('./stations');
- var eventEmitter = new events.EventEmitter();
- module.exports = {
- // module functions
- on: function (name, cb) {
- eventEmitter.on(name, cb);
- },
- emit: function (name, data) {
- eventEmitter.emit(name, data);
- },
- // core route handlers
- '/users/login': function (user, cb) {
- passport.authenticate('local-login', {
- // successRedirect: cb({ status: 'success', message: 'Successfully logged in' }),
- // failureRedirect: cb({ status: 'error', message: 'Error while trying to log in' })
- });
- },
- '/users/register': function (user, cb) {
- passport.authenticate('local-signup', {
- // successRedirect: cb({ status: 'success', message: 'Successfully signed up' }),
- // failureRedirect: cb({ status: 'error', message: 'Error while trying to sign up' })
- });
- },
- '/stations': function (cb) {
- cb(stations.getStations().map(function (result) {
- return {
- id: result.getId(),
- displayName: result.getDisplayName(),
- description: result.getDescription(),
- users: result.getUsers()
- }
- }));
- },
- '/stations/join/:id': function (id, user, cb) {
- var station = stations.getStation(id);
- if (station) {
- user.stationId = id;
- this.emit('station-joined', {
- user: {
- id: user.id,
- username: user.username
- }
- });
- return cb({
- status: 'joined',
- data: {
- displayName: station.getDisplayName(),
- users: station.getUsers(),
- currentSong: station.getCurrentSong()
- }
- });
- }
- else {
- return cb({ status: 'error', message: 'Room with that ID does not exists' });
- }
- },
- '/stations/search/:query': function (query, cb) {
- var params = [
- 'part=snippet',
- `q=${encodeURIComponent(query)}`,
- `key=${config.get('apis.youtube.key')}`,
- 'type=video',
- 'maxResults=25'
- ].join('&');
- request(`https://www.googleapis.com/youtube/v3/search?${params}`, function (err, res, body) {
- if (err) {
- return cb({ status: 'error', message: 'Failed to make request' });
- }
- else {
- try {
- return cb({ status: 'success', body: JSON.parse(body) });
- }
- catch (e) {
- return cb({ status: 'error', message: 'Non JSON response' });
- }
- }
- });
- }
- };
|