stations.js 9.9 KB

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