stations.js 9.0 KB

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