stations.js 9.9 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.getStation(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.getStation(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. getPlaylist: (session, stationId, cb) => {
  61. let playlist = [];
  62. stations.getStation(stationId, (err, station) => {
  63. for (let s = 1; s < station.playlist.length; s++) {
  64. songs.getSong(station.playlist[s], (err, song) => {
  65. playlist.push(song);
  66. });
  67. }
  68. });
  69. cb({ status: 'success', data: playlist })
  70. },
  71. /**
  72. * Joins the station by its id
  73. *
  74. * @param session
  75. * @param stationId - the station id
  76. * @param cb
  77. * @return {{ status: String, userCount: Integer }}
  78. */
  79. join: (session, stationId, cb) => {
  80. stations.getStation(stationId, (err, station) => {
  81. if (err && err !== true) {
  82. return cb({ status: 'error', message: 'An error occurred while joining the station' });
  83. }
  84. if (station) {
  85. //TODO Loop through all sockets, see if socket with same session exists, and if so leave all other station rooms and join this stationRoom
  86. /*cache.client.hincrby('station.userCounts', stationId, 1, (err, userCount) => {
  87. if (err) return cb({ status: 'error', message: 'An error occurred while joining the station' });*/
  88. utils.socketJoinRoom(session.socketId, `station.${stationId}`);
  89. utils.socketJoinSongRoom(session.socketId, `song.${station.currentSong._id}`);
  90. //TODO Emit to cache, listen on cache
  91. songs.getSong(station.currentSong._id, (err, song) => {
  92. if (!err && song) {
  93. station.currentSong.likes = song.likes;
  94. station.currentSong.dislikes = song.dislikes;
  95. } else {
  96. station.currentSong.likes = -1;
  97. station.currentSong.dislikes = -1;
  98. }
  99. cb({ status: 'success', currentSong: station.currentSong, startedAt: station.startedAt, paused: station.paused, timePaused: station.timePaused });
  100. });
  101. //});
  102. }
  103. else {
  104. cb({ status: 'failure', message: `That station doesn't exist` });
  105. }
  106. });
  107. },
  108. /**
  109. * Skips the users current station
  110. *
  111. * @param session
  112. * @param stationId - the station id
  113. * @param cb
  114. * @return {{ status: String, skipCount: Integer }}
  115. */
  116. /*skip: (session, stationId, cb) => {
  117. if (!session) return cb({ status: 'failure', message: 'You must be logged in to skip a song!' });
  118. stations.getStation(stationId, (err, station) => {
  119. if (err && err !== true) {
  120. return cb({ status: 'error', message: 'An error occurred while skipping the station' });
  121. }
  122. if (station) {
  123. cache.client.hincrby('station.skipCounts', session.stationId, 1, (err, skipCount) => {
  124. session.skippedSong = true;
  125. if (err) return cb({ status: 'error', message: 'An error occurred while skipping the station' });
  126. cache.hget('station.userCounts', session.stationId, (err, userCount) => {
  127. cb({ status: 'success', skipCount });
  128. });
  129. });
  130. }
  131. else {
  132. cb({ status: 'failure', message: `That station doesn't exist` });
  133. }
  134. });
  135. },*/
  136. forceSkip: hooks.adminRequired((session, stationId, cb) => {
  137. stations.getStation(stationId, (err, station) => {
  138. if (err && err !== true) {
  139. return cb({ status: 'error', message: 'An error occurred while skipping the station' });
  140. }
  141. if (station) {
  142. notifications.unschedule(`stations.nextSong?id=${stationId}`);
  143. notifications.schedule(`stations.nextSong?id=${stationId}`, 100);
  144. }
  145. else {
  146. cb({ status: 'failure', message: `That station doesn't exist` });
  147. }
  148. });
  149. }),
  150. /**
  151. * Leaves the users current station
  152. *
  153. * @param session
  154. * @param stationId
  155. * @param cb
  156. * @return {{ status: String, userCount: Integer }}
  157. */
  158. leave: (session, stationId, cb) => {
  159. stations.getStation(stationId, (err, station) => {
  160. if (err && err !== true) {
  161. return cb({ status: 'error', message: 'An error occurred while leaving the station' });
  162. }
  163. if (session) session.stationId = null;
  164. else if (station) {
  165. cache.client.hincrby('station.userCounts', stationId, -1, (err, userCount) => {
  166. if (err) return cb({ status: 'error', message: 'An error occurred while leaving the station' });
  167. utils.socketLeaveRooms(session);
  168. cb({ status: 'success', userCount });
  169. });
  170. } else {
  171. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  172. }
  173. });
  174. },
  175. lock: hooks.adminRequired((session, stationId, cb) => {
  176. stations.getStation(stationId, (err, station) => {
  177. if (err && err !== true) {
  178. return cb({ status: 'error', message: 'An error occurred while locking the station' });
  179. } else if (station) {
  180. // Add code to update Mongo and Redis
  181. cb({ status: 'success' });
  182. } else {
  183. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  184. }
  185. });
  186. }),
  187. unlock: hooks.adminRequired((session, stationId, cb) => {
  188. stations.getStation(stationId, (err, station) => {
  189. if (err && err !== true) {
  190. return cb({ status: 'error', message: 'An error occurred while unlocking the station' });
  191. } else if (station) {
  192. // Add code to update Mongo and Redis
  193. cb({ status: 'success' });
  194. } else {
  195. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  196. }
  197. });
  198. }),
  199. pause: hooks.adminRequired((session, stationId, cb) => {
  200. stations.getStation(stationId, (err, station) => {
  201. if (err && err !== true) {
  202. return cb({ status: 'error', message: 'An error occurred while pausing the station' });
  203. } else if (station) {
  204. if (!station.paused) {
  205. station.paused = true;
  206. station.pausedAt = Date.now();
  207. db.models.station.update({_id: stationId}, {$set: {paused: true, pausedAt: Date.now()}}, () => {
  208. if (err) return cb({ status: 'failure', message: 'An error occurred while pausing the station.' });
  209. stations.updateStation(stationId, () => {
  210. cache.pub('station.pause', stationId);
  211. notifications.unschedule(`stations.nextSong?id=${stationId}`);
  212. cb({ status: 'success' });
  213. });
  214. });
  215. } else {
  216. cb({ status: 'failure', message: 'That station was already paused.' });
  217. }
  218. cb({ status: 'success' });
  219. } else {
  220. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  221. }
  222. });
  223. }),
  224. resume: hooks.adminRequired((session, stationId, cb) => {
  225. stations.getStation(stationId, (err, station) => {
  226. if (err && err !== true) {
  227. return cb({ status: 'error', message: 'An error occurred while resuming the station' });
  228. } else if (station) {
  229. if (station.paused) {
  230. station.paused = false;
  231. station.timePaused += (Date.now() - station.pausedAt);
  232. db.models.station.update({_id: stationId}, {$set: {paused: false}, $inc: {timePaused: Date.now() - station.pausedAt}}, () => {
  233. stations.updateStation(stationId, (err, station) => {
  234. cache.pub('station.resume', stationId);
  235. cb({ status: 'success' });
  236. });
  237. });
  238. } else {
  239. cb({ status: 'failure', message: 'That station is not paused.' });
  240. }
  241. } else {
  242. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  243. }
  244. });
  245. }),
  246. remove: hooks.adminRequired((session, stationId, cb) => {
  247. db.models.station.remove({ _id: stationId });
  248. cache.hdel('stations', stationId, () => {
  249. return cb({ status: 'success', message: 'Station successfully removed' });
  250. });
  251. }),
  252. create: hooks.adminRequired((session, 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. cache.pub('station.create', data._id);
  280. return cb(null, { 'status': 'success', 'message': 'Successfully created station.' });
  281. });
  282. }),
  283. };