stations.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. cb({ status: 'success' });
  189. });
  190. } else {
  191. cb({ status: 'failure', message: 'An error occurred while pausing the station.' });
  192. }
  193. });
  194. } else {
  195. cb({ status: 'failure', message: 'That station was already paused.' });
  196. }
  197. cb({ status: 'success' });
  198. } else {
  199. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  200. }
  201. });
  202. },
  203. resume: (sessionId, stationId, cb) => {
  204. //TODO Require admin
  205. stations.initializeAndReturnStation(stationId, (err, station) => {
  206. if (err && err !== true) {
  207. return cb({ status: 'error', message: 'An error occurred while resuming the station' });
  208. } else if (station) {
  209. if (station.paused) {
  210. station.paused = false;
  211. station.timePaused += (Date.now() - station.pausedAt);
  212. cache.hset('stations', stationId, station, (err) => {
  213. if (!err) {
  214. db.models.station.update({_id: stationId}, {$set: {paused: false, timePaused: station.timePaused}}, () => {
  215. cache.pub('station.resume', stationId);
  216. cb({ status: 'success' });
  217. });
  218. } else {
  219. cb({ status: 'failure', message: 'An error occurred while resuming the station.' });
  220. }
  221. });
  222. } else {
  223. cb({ status: 'failure', message: 'That station is not paused.' });
  224. }
  225. cb({ status: 'success' });
  226. } else {
  227. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  228. }
  229. });
  230. },
  231. remove: (sessionId, stationId, cb) => {
  232. cache.hdel('stations', stationId, () => {
  233. // TODO: Update Mongo
  234. return cb({ status: 'success', message: 'Station successfully removed' });
  235. });
  236. },
  237. create: (sessionId, data, cb) => {
  238. //TODO Require admin
  239. async.waterfall([
  240. (next) => {
  241. return (data) ? next() : cb({ 'status': 'failure', 'message': 'Invalid data' });
  242. },
  243. // check the cache for the station
  244. (next) => cache.hget('stations', data.name, next),
  245. // if the cached version exist
  246. (station, next) => {
  247. if (station) return next({ 'status': 'failure', 'message': 'A station with that name already exists' });
  248. db.models.station.findOne({ _id: data.name }, next);
  249. },
  250. (station, next) => {
  251. if (station) return next({ 'status': 'failure', 'message': 'A station with that name already exists' });
  252. const { _id, displayName, description, genres, playlist } = data;
  253. db.models.station.create({
  254. _id,
  255. displayName,
  256. description,
  257. type: "official",
  258. playlist,
  259. genres,
  260. currentSong: stations.defaultSong
  261. }, next);
  262. }
  263. ], (err, station) => {
  264. if (err) throw err;
  265. stations.calculateSongForStation(station, () => {
  266. cache.pub('station.create', data.name);
  267. return cb(null, { 'status': 'success', 'message': 'Successfully created station.' });
  268. });
  269. });
  270. },
  271. };