stations.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. //TODO Emit to cache, listen on cache
  77. cb({ status: 'success', currentSong: station.currentSong, startedAt: station.startedAt, paused: station.paused, timePaused: station.timePaused });
  78. });
  79. }
  80. else {
  81. cb({ status: 'failure', message: `That station doesn't exist` });
  82. }
  83. });
  84. },
  85. /**
  86. * Skips the users current station
  87. *
  88. * @param session
  89. * @param stationId - the station id
  90. * @param cb
  91. * @return {{ status: String, skipCount: Integer }}
  92. */
  93. skip: (session, stationId, cb) => {
  94. if (!session) return cb({ status: 'failure', message: 'You must be logged in to skip a song!' });
  95. stations.initializeAndReturnStation(stationId, (err, station) => {
  96. if (err && err !== true) {
  97. return cb({ status: 'error', message: 'An error occurred while skipping the station' });
  98. }
  99. if (station) {
  100. cache.client.hincrby('station.skipCounts', session.stationId, 1, (err, skipCount) => {
  101. session.skippedSong = true;
  102. if (err) return cb({ status: 'error', message: 'An error occurred while skipping the station' });
  103. cache.hget('station.userCounts', session.stationId, (err, userCount) => {
  104. cb({ status: 'success', skipCount });
  105. });
  106. });
  107. }
  108. else {
  109. cb({ status: 'failure', message: `That station doesn't exist` });
  110. }
  111. });
  112. },
  113. /**
  114. * Leaves the users current station
  115. *
  116. * @param session
  117. * @param cb
  118. * @return {{ status: String, userCount: Integer }}
  119. */
  120. leave: (session, cb) => {
  121. let stationId = "edm";
  122. stations.initializeAndReturnStation(stationId, (err, station) => {
  123. if (err && err !== true) {
  124. return cb({ status: 'error', message: 'An error occurred while leaving the station' });
  125. }
  126. if (session) session.stationId = null;
  127. else if (station) {
  128. cache.client.hincrby('station.userCounts', stationId, -1, (err, userCount) => {
  129. if (err) return cb({ status: 'error', message: 'An error occurred while leaving the station' });
  130. utils.socketLeaveRooms(session);
  131. cb({ status: 'success', userCount });
  132. });
  133. } else {
  134. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  135. }
  136. });
  137. },
  138. lock: (sessionId, stationId, cb) => {
  139. //TODO Require admin
  140. stations.initializeAndReturnStation(stationId, (err, station) => {
  141. if (err && err !== true) {
  142. return cb({ status: 'error', message: 'An error occurred while locking the station' });
  143. } else if (station) {
  144. // Add code to update Mongo and Redis
  145. cb({ status: 'success' });
  146. } else {
  147. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  148. }
  149. });
  150. },
  151. unlock: (sessionId, stationId, cb) => {
  152. //TODO Require admin
  153. stations.initializeAndReturnStation(stationId, (err, station) => {
  154. if (err && err !== true) {
  155. return cb({ status: 'error', message: 'An error occurred while unlocking the station' });
  156. } else if (station) {
  157. // Add code to update Mongo and Redis
  158. cb({ status: 'success' });
  159. } else {
  160. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  161. }
  162. });
  163. },
  164. pause: (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 pausing the station' });
  169. } else if (station) {
  170. if (!station.paused) {
  171. station.paused = true;
  172. station.pausedAt = Date.now();
  173. cache.hset('stations', stationId, station, (err) => {
  174. if (!err) {
  175. db.models.station.update({_id: stationId}, {$set: {paused: true}}, () => {
  176. cache.pub('station.pause', stationId);
  177. cb({ status: 'success' });
  178. });
  179. } else {
  180. cb({ status: 'failure', message: 'An error occurred while pausing the station.' });
  181. }
  182. });
  183. } else {
  184. cb({ status: 'failure', message: 'That station was already paused.' });
  185. }
  186. cb({ status: 'success' });
  187. } else {
  188. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  189. }
  190. });
  191. },
  192. resume: (sessionId, stationId, cb) => {
  193. //TODO Require admin
  194. stations.initializeAndReturnStation(stationId, (err, station) => {
  195. if (err && err !== true) {
  196. return cb({ status: 'error', message: 'An error occurred while resuming the station' });
  197. } else if (station) {
  198. if (station.paused) {
  199. station.paused = false;
  200. station.timePaused += (Date.now() - station.pausedAt);
  201. cache.hset('stations', stationId, station, (err) => {
  202. if (!err) {
  203. db.models.station.update({_id: stationId}, {$set: {paused: false, timePaused: station.timePaused}}, () => {
  204. cache.pub('station.resume', stationId);
  205. cb({ status: 'success' });
  206. });
  207. } else {
  208. cb({ status: 'failure', message: 'An error occurred while resuming the station.' });
  209. }
  210. });
  211. } else {
  212. cb({ status: 'failure', message: 'That station is not paused.' });
  213. }
  214. cb({ status: 'success' });
  215. } else {
  216. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  217. }
  218. });
  219. },
  220. remove: (sessionId, stationId, cb) => {
  221. cache.hdel('stations', stationId, () => {
  222. // TODO: Update Mongo
  223. return cb({ status: 'success', message: 'Station successfully removed' });
  224. });
  225. },
  226. create: (sessionId, data, cb) => {
  227. //TODO Require admin
  228. async.waterfall([
  229. (next) => {
  230. return (data) ? next() : cb({ 'status': 'failure', 'message': 'Invalid data' });
  231. },
  232. // check the cache for the station
  233. (next) => cache.hget('stations', data.name, next),
  234. // if the cached version exist
  235. (station, next) => {
  236. if (station) return next({ 'status': 'failure', 'message': 'A station with that name already exists' });
  237. db.models.station.findOne({ _id: data.name }, next);
  238. },
  239. (station, next) => {
  240. if (station) return next({ 'status': 'failure', 'message': 'A station with that name already exists' });
  241. const { _id, displayName, description, genres, playlist } = data;
  242. db.models.station.create({
  243. _id,
  244. displayName,
  245. description,
  246. type: "official",
  247. playlist,
  248. genres,
  249. currentSong: stations.defaultSong
  250. }, next);
  251. }
  252. ], (err, station) => {
  253. if (err) throw err;
  254. stations.calculateSongForStation(station, () => {
  255. cache.pub('station.create', data.name);
  256. return cb(null, { 'status': 'success', 'message': 'Successfully created station.' });
  257. });
  258. });
  259. },
  260. };