stations.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. /**
  125. * Leaves the users current station
  126. *
  127. * @param session
  128. * @param cb
  129. * @return {{ status: String, userCount: Integer }}
  130. */
  131. leave: (session, cb) => {
  132. let stationId = "edm";
  133. stations.initializeAndReturnStation(stationId, (err, station) => {
  134. if (err && err !== true) {
  135. return cb({ status: 'error', message: 'An error occurred while leaving the station' });
  136. }
  137. if (session) session.stationId = null;
  138. else if (station) {
  139. cache.client.hincrby('station.userCounts', stationId, -1, (err, userCount) => {
  140. if (err) return cb({ status: 'error', message: 'An error occurred while leaving the station' });
  141. utils.socketLeaveRooms(session);
  142. cb({ status: 'success', userCount });
  143. });
  144. } else {
  145. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  146. }
  147. });
  148. },
  149. lock: (sessionId, stationId, cb) => {
  150. //TODO Require admin
  151. stations.initializeAndReturnStation(stationId, (err, station) => {
  152. if (err && err !== true) {
  153. return cb({ status: 'error', message: 'An error occurred while locking the station' });
  154. } else if (station) {
  155. // Add code to update Mongo and Redis
  156. cb({ status: 'success' });
  157. } else {
  158. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  159. }
  160. });
  161. },
  162. unlock: (sessionId, stationId, cb) => {
  163. //TODO Require admin
  164. stations.initializeAndReturnStation(stationId, (err, station) => {
  165. if (err && err !== true) {
  166. return cb({ status: 'error', message: 'An error occurred while unlocking the station' });
  167. } else if (station) {
  168. // Add code to update Mongo and Redis
  169. cb({ status: 'success' });
  170. } else {
  171. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  172. }
  173. });
  174. },
  175. pause: (sessionId, stationId, cb) => {
  176. //TODO Require admin
  177. stations.initializeAndReturnStation(stationId, (err, station) => {
  178. if (err && err !== true) {
  179. return cb({ status: 'error', message: 'An error occurred while pausing the station' });
  180. } else if (station) {
  181. if (!station.paused) {
  182. station.paused = true;
  183. station.pausedAt = Date.now();
  184. cache.hset('stations', stationId, station, (err) => {
  185. if (!err) {
  186. db.models.station.update({_id: stationId}, {$set: {paused: true}}, () => {
  187. cache.pub('station.pause', stationId);
  188. notifications.unschedule(stationId);
  189. cb({ status: 'success' });
  190. });
  191. } else {
  192. cb({ status: 'failure', message: 'An error occurred while pausing the station.' });
  193. }
  194. });
  195. } else {
  196. cb({ status: 'failure', message: 'That station was already paused.' });
  197. }
  198. cb({ status: 'success' });
  199. } else {
  200. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  201. }
  202. });
  203. },
  204. resume: (sessionId, stationId, cb) => {
  205. //TODO Require admin
  206. stations.initializeAndReturnStation(stationId, (err, station) => {
  207. if (err && err !== true) {
  208. return cb({ status: 'error', message: 'An error occurred while resuming the station' });
  209. } else if (station) {
  210. if (station.paused) {
  211. station.paused = false;
  212. station.timePaused += (Date.now() - station.pausedAt);
  213. cache.hset('stations', stationId, station, (err) => {
  214. if (!err) {
  215. db.models.station.update({_id: stationId}, {$set: {paused: false, timePaused: station.timePaused}}, () => {
  216. cache.pub('station.resume', stationId);
  217. cb({ status: 'success' });
  218. });
  219. } else {
  220. cb({ status: 'failure', message: 'An error occurred while resuming the station.' });
  221. }
  222. });
  223. } else {
  224. cb({ status: 'failure', message: 'That station is not paused.' });
  225. }
  226. cb({ status: 'success' });
  227. } else {
  228. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  229. }
  230. });
  231. },
  232. remove: (sessionId, _id, cb) => {
  233. db.models.station.find({ _id }).remove().exec();
  234. cache.hdel('stations', _id, () => {
  235. return cb({ status: 'success', message: 'Station successfully removed' });
  236. });
  237. },
  238. create: (sessionId, data, cb) => {
  239. //TODO Require admin
  240. async.waterfall([
  241. (next) => {
  242. return (data) ? next() : cb({ 'status': 'failure', 'message': 'Invalid data' });
  243. },
  244. // check the cache for the station
  245. (next) => cache.hget('stations', data._id, next),
  246. // if the cached version exist
  247. (station, next) => {
  248. if (station) return next({ 'status': 'failure', 'message': 'A station with that id already exists' });
  249. db.models.station.findOne({ _id: data._id }, next);
  250. },
  251. (station, next) => {
  252. if (station) return next({ 'status': 'failure', 'message': 'A station with that id already exists' });
  253. const { _id, displayName, description, genres, playlist } = data;
  254. db.models.station.create({
  255. _id,
  256. displayName,
  257. description,
  258. type: "official",
  259. playlist,
  260. genres,
  261. currentSong: stations.defaultSong
  262. }, next);
  263. }
  264. ], (err, station) => {
  265. if (err) throw err;
  266. stations.calculateSongForStation(station, () => {
  267. cache.pub('station.create', data._id);
  268. return cb(null, { 'status': 'success', 'message': 'Successfully created station.' });
  269. });
  270. });
  271. },
  272. };