stations.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. 'use strict';
  2. const async = require('async'),
  3. request = require('request'),
  4. config = require('config');
  5. const io = require('../io');
  6. const db = require('../db');
  7. const cache = require('../cache');
  8. const notifications = require('../notifications');
  9. const utils = require('../utils');
  10. const stations = require('../stations');
  11. const songs = require('../songs');
  12. const hooks = require('./hooks');
  13. cache.sub('station.locked', stationId => {
  14. io.io.to(`station.${stationId}`).emit("event:stations.locked");
  15. });
  16. cache.sub('station.unlocked', stationId => {
  17. io.io.to(`station.${stationId}`).emit("event:stations.unlocked");
  18. });
  19. cache.sub('station.pause', stationId => {
  20. io.io.to(`station.${stationId}`).emit("event:stations.pause");
  21. });
  22. cache.sub('station.resume', stationId => {
  23. stations.initializeAndReturnStation(stationId, (err, station) => {
  24. io.io.to(`station.${stationId}`).emit("event:stations.resume", {timePaused: station.timePaused});
  25. });
  26. });
  27. cache.sub('station.create', stationId => {
  28. stations.initializeAndReturnStation(stationId, (err, station) => {
  29. //TODO Emit to homepage and admin station page
  30. if (!err) {
  31. io.io.to('home').emit("event:stations.created", station);
  32. }
  33. });
  34. });
  35. module.exports = {
  36. /**
  37. * Get a list of all the stations
  38. *
  39. * @param session
  40. * @param cb
  41. * @return {{ status: String, stations: Array }}
  42. */
  43. index: (session, cb) => {
  44. // TODO: the logic should be a bit more personalized to the users preferred genres
  45. // and it should probably just a different cache table then 'stations'
  46. cache.hgetall('stations', (err, stations) => {
  47. if (err && err !== true) {
  48. return cb({
  49. status: 'error',
  50. message: 'An error occurred while obtaining the stations'
  51. });
  52. }
  53. let arr = [];
  54. for (let prop in stations) {
  55. arr.push(stations[prop]);
  56. }
  57. cb({ status: 'success', stations: arr });
  58. });
  59. },
  60. /**
  61. * Joins the station by its id
  62. *
  63. * @param sessionId
  64. * @param stationId - the station id
  65. * @param cb
  66. * @return {{ status: String, userCount: Integer }}
  67. */
  68. join: (sessionId, stationId, cb) => {
  69. stations.initializeAndReturnStation(stationId, (err, station) => {
  70. if (err && err !== true) {
  71. return cb({ status: 'error', message: 'An error occurred while joining the station' });
  72. }
  73. if (station) {
  74. //TODO Loop through all sockets, see if socket with same session exists, and if so leave all other station rooms and join this stationRoom
  75. cache.client.hincrby('station.userCounts', stationId, 1, (err, userCount) => {
  76. if (err) return cb({ status: 'error', message: 'An error occurred while joining the station' });
  77. utils.socketJoinRoom(sessionId, `station.${stationId}`);
  78. utils.socketJoinSongRoom(sessionId, `song.${station.currentSong._id}`);
  79. //TODO Emit to cache, listen on cache
  80. songs.getSong(station.currentSong._id, (err, song) => {
  81. if (!err) {
  82. station.currentSong.likes = song.likes;
  83. station.currentSong.dislikes = song.dislikes;
  84. } else {
  85. station.currentSong.likes = -1;
  86. station.currentSong.dislikes = -1;
  87. }
  88. cb({ status: 'success', currentSong: station.currentSong, startedAt: station.startedAt, paused: station.paused, timePaused: station.timePaused });
  89. });
  90. });
  91. }
  92. else {
  93. cb({ status: 'failure', message: `That station doesn't exist` });
  94. }
  95. });
  96. },
  97. /**
  98. * Skips the users current station
  99. *
  100. * @param session
  101. * @param stationId - the station id
  102. * @param cb
  103. * @return {{ status: String, skipCount: Integer }}
  104. */
  105. /*skip: (session, stationId, cb) => {
  106. if (!session) return cb({ status: 'failure', message: 'You must be logged in to skip a song!' });
  107. stations.initializeAndReturnStation(stationId, (err, station) => {
  108. if (err && err !== true) {
  109. return cb({ status: 'error', message: 'An error occurred while skipping the station' });
  110. }
  111. if (station) {
  112. cache.client.hincrby('station.skipCounts', session.stationId, 1, (err, skipCount) => {
  113. session.skippedSong = true;
  114. if (err) return cb({ status: 'error', message: 'An error occurred while skipping the station' });
  115. cache.hget('station.userCounts', session.stationId, (err, userCount) => {
  116. cb({ status: 'success', skipCount });
  117. });
  118. });
  119. }
  120. else {
  121. cb({ status: 'failure', message: `That station doesn't exist` });
  122. }
  123. });
  124. },*/
  125. forceSkip: hooks.adminRequired((session, stationId, cb) => {
  126. //TODO Require admin
  127. stations.initializeAndReturnStation(stationId, (err, station) => {
  128. if (err && err !== true) {
  129. return cb({ status: 'error', message: 'An error occurred while skipping the station' });
  130. }
  131. if (station) {
  132. notifications.unschedule(`stations.nextSong?id=${stationId}`);
  133. notifications.schedule(`stations.nextSong?id=${stationId}`, 100);
  134. }
  135. else {
  136. cb({ status: 'failure', message: `That station doesn't exist` });
  137. }
  138. });
  139. }),
  140. /**
  141. * Leaves the users current station
  142. *
  143. * @param session
  144. * @param cb
  145. * @return {{ status: String, userCount: Integer }}
  146. */
  147. leave: (session, cb) => {
  148. let stationId = "edm";
  149. stations.initializeAndReturnStation(stationId, (err, station) => {
  150. if (err && err !== true) {
  151. return cb({ status: 'error', message: 'An error occurred while leaving the station' });
  152. }
  153. if (session) session.stationId = null;
  154. else if (station) {
  155. cache.client.hincrby('station.userCounts', stationId, -1, (err, userCount) => {
  156. if (err) return cb({ status: 'error', message: 'An error occurred while leaving the station' });
  157. utils.socketLeaveRooms(session);
  158. cb({ status: 'success', userCount });
  159. });
  160. } else {
  161. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  162. }
  163. });
  164. },
  165. lock: hooks.adminRequired((sessionId, stationId, cb) => {
  166. stations.initializeAndReturnStation(stationId, (err, station) => {
  167. if (err && err !== true) {
  168. return cb({ status: 'error', message: 'An error occurred while locking the station' });
  169. } else if (station) {
  170. // Add code to update Mongo and Redis
  171. cb({ status: 'success' });
  172. } else {
  173. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  174. }
  175. });
  176. }),
  177. unlock: hooks.adminRequired((sessionId, stationId, cb) => {
  178. stations.initializeAndReturnStation(stationId, (err, station) => {
  179. if (err && err !== true) {
  180. return cb({ status: 'error', message: 'An error occurred while unlocking the station' });
  181. } else if (station) {
  182. // Add code to update Mongo and Redis
  183. cb({ status: 'success' });
  184. } else {
  185. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  186. }
  187. });
  188. }),
  189. pause: hooks.adminRequired((sessionId, stationId, cb) => {
  190. //TODO Require admin
  191. stations.initializeAndReturnStation(stationId, (err, station) => {
  192. if (err && err !== true) {
  193. return cb({ status: 'error', message: 'An error occurred while pausing the station' });
  194. } else if (station) {
  195. if (!station.paused) {
  196. station.paused = true;
  197. station.pausedAt = Date.now();
  198. cache.hset('stations', stationId, station, (err) => {
  199. if (!err) {
  200. db.models.station.update({_id: stationId}, {$set: {paused: true}}, () => {
  201. cache.pub('station.pause', stationId);
  202. notifications.unschedule(`stations.nextSong?id=${stationId}`);
  203. cb({ status: 'success' });
  204. });
  205. } else {
  206. cb({ status: 'failure', message: 'An error occurred while pausing the station.' });
  207. }
  208. });
  209. } else {
  210. cb({ status: 'failure', message: 'That station was already paused.' });
  211. }
  212. cb({ status: 'success' });
  213. } else {
  214. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  215. }
  216. });
  217. }),
  218. resume: hooks.adminRequired((sessionId, stationId, cb) => {
  219. //TODO Require admin
  220. stations.initializeAndReturnStation(stationId, (err, station) => {
  221. if (err && err !== true) {
  222. return cb({ status: 'error', message: 'An error occurred while resuming the station' });
  223. } else if (station) {
  224. if (station.paused) {
  225. station.paused = false;
  226. station.timePaused += (Date.now() - station.pausedAt);
  227. cache.hset('stations', stationId, station, (err) => {
  228. if (!err) {
  229. db.models.station.update({_id: stationId}, {$set: {paused: false, timePaused: station.timePaused}}, () => {
  230. cache.pub('station.resume', stationId);
  231. cb({ status: 'success' });
  232. });
  233. } else {
  234. cb({ status: 'failure', message: 'An error occurred while resuming the station.' });
  235. }
  236. });
  237. } else {
  238. cb({ status: 'failure', message: 'That station is not paused.' });
  239. }
  240. cb({ status: 'success' });
  241. } else {
  242. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  243. }
  244. });
  245. }),
  246. remove: hooks.adminRequired((sessionId, _id, cb) => {
  247. db.models.station.find({ _id }).remove().exec();
  248. cache.hdel('stations', _id, () => {
  249. return cb({ status: 'success', message: 'Station successfully removed' });
  250. });
  251. }),
  252. create: hooks.adminRequired((sessionId, data, cb) => {
  253. async.waterfall([
  254. (next) => {
  255. return (data) ? next() : cb({ 'status': 'failure', 'message': 'Invalid data' });
  256. },
  257. // check the cache for the station
  258. (next) => cache.hget('stations', data._id, next),
  259. // if the cached version exist
  260. (station, next) => {
  261. if (station) return next({ 'status': 'failure', 'message': 'A station with that id already exists' });
  262. db.models.station.findOne({ _id: data._id }, next);
  263. },
  264. (station, next) => {
  265. if (station) return next({ 'status': 'failure', 'message': 'A station with that id already exists' });
  266. const { _id, displayName, description, genres, playlist } = data;
  267. db.models.station.create({
  268. _id,
  269. displayName,
  270. description,
  271. type: "official",
  272. playlist,
  273. genres,
  274. currentSong: stations.defaultSong
  275. }, next);
  276. }
  277. ], (err, station) => {
  278. if (err) throw err;
  279. stations.calculateSongForStation(station, () => {
  280. cache.pub('station.create', data._id);
  281. return cb(null, { 'status': 'success', 'message': 'Successfully created station.' });
  282. });
  283. });
  284. }),
  285. };