stations.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 defaultSong = {
  12. _id: '60ItHLz5WEA',
  13. title: 'Faded',
  14. artists: ['Alan Walker'],
  15. duration: 212,
  16. skipDuration: 0,
  17. thumbnail: 'https://i.scdn.co/image/2ddde58427f632037093857ebb71a67ddbdec34b'
  18. };
  19. cache.sub('station.locked', (stationName) => {
  20. io.to(`station.${stationName}`).emit("event:station.locked");
  21. });
  22. cache.sub('station.unlocked', (stationName) => {
  23. io.to(`station.${stationName}`).emit("event:station.unlocked");
  24. });
  25. cache.sub('station.pause', (stationName) => {
  26. io.to(`station.${stationName}`).emit("event:station.pause");
  27. });
  28. cache.sub('station.resume', (stationName) => {
  29. io.to(`station.${stationName}`).emit("event:station.resume");
  30. });
  31. cache.sub('station.create', (stationId) => {
  32. stations.initializeAndReturnStation(stationId, () => {
  33. //TODO Emit to homepage and admin station page
  34. });
  35. });
  36. module.exports = {
  37. /**
  38. * Get a list of all the stations
  39. *
  40. * @param session
  41. * @param cb
  42. * @return {{ status: String, stations: Array }}
  43. */
  44. index: (session, cb) => {
  45. // TODO: the logic should be a bit more personalized to the users preferred genres
  46. // and it should probably just a different cache table then 'stations'
  47. cache.hgetall('stations', (err, stations) => {
  48. if (err && err !== true) {
  49. return cb({
  50. status: 'error',
  51. message: 'An error occurred while obtaining the stations'
  52. });
  53. }
  54. let arr = [];
  55. for (let prop in stations) {
  56. arr.push(stations[prop]);
  57. }
  58. cb({ status: 'success', stations: arr });
  59. });
  60. },
  61. /**
  62. * Joins the station by its id
  63. *
  64. * @param sessionId
  65. * @param stationId - the station id
  66. * @param cb
  67. * @return {{ status: String, userCount: Integer }}
  68. */
  69. join: (sessionId, stationId, cb) => {
  70. stations.initializeAndReturnStation(stationId, (err, station) => {
  71. if (err && err !== true) {
  72. return cb({ status: 'error', message: 'An error occurred while joining the station' });
  73. }
  74. if (station) {
  75. //TODO Loop through all sockets, see if socket with same session exists, and if so leave all other station rooms and join this stationRoom
  76. cache.client.hincrby('station.userCounts', stationId, 1, (err, userCount) => {
  77. if (err) return cb({ status: 'error', message: 'An error occurred while joining the station' });
  78. utils.socketJoinRoom(sessionId, `station.${stationId}`);
  79. //TODO Emit to cache, listen on cache
  80. cb({ status: 'success', currentSong: station.currentSong, startedAt: station.startedAt, paused: station.paused, timePaused: station.timePaused });
  81. });
  82. }
  83. else {
  84. cb({ status: 'failure', message: `That station doesn't exist` });
  85. }
  86. });
  87. },
  88. /**
  89. * Skips the users current station
  90. *
  91. * @param session
  92. * @param stationId - the station id
  93. * @param cb
  94. * @return {{ status: String, skipCount: Integer }}
  95. */
  96. skip: (session, stationId, cb) => {
  97. if (!session) return cb({ status: 'failure', message: 'You must be logged in to skip a song!' });
  98. stations.initializeAndReturnStation(stationId, (err, station) => {
  99. if (err && err !== true) {
  100. return cb({ status: 'error', message: 'An error occurred while skipping the station' });
  101. }
  102. if (station) {
  103. cache.client.hincrby('station.skipCounts', session.stationId, 1, (err, skipCount) => {
  104. session.skippedSong = true;
  105. if (err) return cb({ status: 'error', message: 'An error occurred while skipping the station' });
  106. cache.hget('station.userCounts', session.stationId, (err, userCount) => {
  107. cb({ status: 'success', skipCount });
  108. });
  109. });
  110. }
  111. else {
  112. cb({ status: 'failure', message: `That station doesn't exist` });
  113. }
  114. });
  115. },
  116. /**
  117. * Leaves the users current station
  118. *
  119. * @param session
  120. * @param cb
  121. * @return {{ status: String, userCount: Integer }}
  122. */
  123. leave: (session, cb) => {
  124. let stationId = "edm";
  125. stations.initializeAndReturnStation(stationId, (err, station) => {
  126. if (err && err !== true) {
  127. return cb({ status: 'error', message: 'An error occurred while leaving the station' });
  128. }
  129. if (session) session.stationId = null;
  130. else if (station) {
  131. cache.client.hincrby('station.userCounts', stationId, -1, (err, userCount) => {
  132. if (err) return cb({ status: 'error', message: 'An error occurred while leaving the station' });
  133. utils.socketLeaveRooms(session);
  134. cb({ status: 'success', userCount });
  135. });
  136. } else {
  137. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  138. }
  139. });
  140. },
  141. lock: (session, stationId, cb) => {
  142. //TODO Require admin
  143. stations.initializeAndReturnStation(stationId, (err, station) => {
  144. if (err && err !== true) {
  145. return cb({ status: 'error', message: 'An error occurred while locking the station' });
  146. } else if (station) {
  147. // Add code to update Mongo and Redis
  148. cb({ status: 'success' });
  149. } else {
  150. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  151. }
  152. });
  153. },
  154. unlock: (session, stationId, cb) => {
  155. //TODO Require admin
  156. stations.initializeAndReturnStation(stationId, (err, station) => {
  157. if (err && err !== true) {
  158. return cb({ status: 'error', message: 'An error occurred while unlocking the station' });
  159. } else if (station) {
  160. // Add code to update Mongo and Redis
  161. cb({ status: 'success' });
  162. } else {
  163. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  164. }
  165. });
  166. },
  167. create: (sessionId, data, cb) => {
  168. //TODO Require admin
  169. async.waterfall([
  170. (next) => {
  171. return (data) ? next() : cb({ 'status': 'failure', 'message': 'Invalid data.' });
  172. },
  173. // check the cache for the station
  174. (next) => cache.hget('stations', data.name, next),
  175. // if the cached version exist
  176. (station, next) => {
  177. if (station) return next({ 'status': 'failure', 'message': 'A station with that name already exists.' });
  178. db.models.station.findOne({ _id: data.name }, next);
  179. },
  180. (station, next) => {
  181. if (station) return next({ 'status': 'failure', 'message': 'A station with that name already exists.' });
  182. db.models.station.create({
  183. _id: data.name,
  184. displayName: data.displayName,
  185. description: data.description,
  186. type: "official",
  187. playlist: [defaultSong._id],
  188. genres: ["edm"],
  189. locked: true,
  190. currentSong: defaultSong
  191. }, next);
  192. }
  193. ], (err, station) => {
  194. console.log(err, 123986);
  195. if (err) return cb(err);
  196. stations.calculateSongForStation(station);
  197. cache.pub('station.create', data.name);
  198. return cb(null, { 'status': 'success', 'message': 'Successfully created station.' });
  199. });
  200. },
  201. };