stations.js 6.9 KB

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