stations.js 9.2 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 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', stationId => {
  22. io.io.to(`station.${stationId}`).emit("event:stations.locked");
  23. });
  24. cache.sub('station.unlocked', stationId => {
  25. io.io.to(`station.${stationId}`).emit("event:stations.unlocked");
  26. });
  27. cache.sub('station.pause', stationId => {
  28. io.io.to(`station.${stationId}`).emit("event:stations.pause");
  29. });
  30. cache.sub('station.resume', stationId => {
  31. stations.initializeAndReturnStation(stationId, (err, station) => {
  32. io.io.to(`station.${stationId}`).emit("event:stations.resume", {timePaused: station.timePaused});
  33. });
  34. });
  35. cache.sub('station.create', stationId => {
  36. stations.initializeAndReturnStation(stationId, (err, station) => {
  37. //TODO Emit to homepage and admin station page
  38. if (!err) {
  39. io.io.to('home').emit("event:stations.created", station);
  40. }
  41. });
  42. });
  43. module.exports = {
  44. /**
  45. * Get a list of all the stations
  46. *
  47. * @param session
  48. * @param cb
  49. * @return {{ status: String, stations: Array }}
  50. */
  51. index: (session, cb) => {
  52. // TODO: the logic should be a bit more personalized to the users preferred genres
  53. // and it should probably just a different cache table then 'stations'
  54. cache.hgetall('stations', (err, stations) => {
  55. if (err && err !== true) {
  56. return cb({
  57. status: 'error',
  58. message: 'An error occurred while obtaining the stations'
  59. });
  60. }
  61. let arr = [];
  62. for (let prop in stations) {
  63. arr.push(stations[prop]);
  64. }
  65. cb({ status: 'success', stations: arr });
  66. });
  67. },
  68. /**
  69. * Joins the station by its id
  70. *
  71. * @param sessionId
  72. * @param stationId - the station id
  73. * @param cb
  74. * @return {{ status: String, userCount: Integer }}
  75. */
  76. join: (sessionId, stationId, cb) => {
  77. stations.initializeAndReturnStation(stationId, (err, station) => {
  78. if (err && err !== true) {
  79. return cb({ status: 'error', message: 'An error occurred while joining the station' });
  80. }
  81. if (station) {
  82. //TODO Loop through all sockets, see if socket with same session exists, and if so leave all other station rooms and join this stationRoom
  83. cache.client.hincrby('station.userCounts', stationId, 1, (err, userCount) => {
  84. if (err) return cb({ status: 'error', message: 'An error occurred while joining the station' });
  85. utils.socketJoinRoom(sessionId, `station.${stationId}`);
  86. //TODO Emit to cache, listen on cache
  87. cb({ status: 'success', currentSong: station.currentSong, startedAt: station.startedAt, paused: station.paused, timePaused: station.timePaused });
  88. });
  89. }
  90. else {
  91. cb({ status: 'failure', message: `That station doesn't exist` });
  92. }
  93. });
  94. },
  95. /**
  96. * Skips the users current station
  97. *
  98. * @param session
  99. * @param stationId - the station id
  100. * @param cb
  101. * @return {{ status: String, skipCount: Integer }}
  102. */
  103. skip: (session, stationId, cb) => {
  104. if (!session) return cb({ status: 'failure', message: 'You must be logged in to skip a song!' });
  105. stations.initializeAndReturnStation(stationId, (err, station) => {
  106. if (err && err !== true) {
  107. return cb({ status: 'error', message: 'An error occurred while skipping the station' });
  108. }
  109. if (station) {
  110. cache.client.hincrby('station.skipCounts', session.stationId, 1, (err, skipCount) => {
  111. session.skippedSong = true;
  112. if (err) return cb({ status: 'error', message: 'An error occurred while skipping the station' });
  113. cache.hget('station.userCounts', session.stationId, (err, userCount) => {
  114. cb({ status: 'success', skipCount });
  115. });
  116. });
  117. }
  118. else {
  119. cb({ status: 'failure', message: `That station doesn't exist` });
  120. }
  121. });
  122. },
  123. /**
  124. * Leaves the users current station
  125. *
  126. * @param session
  127. * @param cb
  128. * @return {{ status: String, userCount: Integer }}
  129. */
  130. leave: (session, cb) => {
  131. let stationId = "edm";
  132. stations.initializeAndReturnStation(stationId, (err, station) => {
  133. if (err && err !== true) {
  134. return cb({ status: 'error', message: 'An error occurred while leaving the station' });
  135. }
  136. if (session) session.stationId = null;
  137. else if (station) {
  138. cache.client.hincrby('station.userCounts', stationId, -1, (err, userCount) => {
  139. if (err) return cb({ status: 'error', message: 'An error occurred while leaving the station' });
  140. utils.socketLeaveRooms(session);
  141. cb({ status: 'success', userCount });
  142. });
  143. } else {
  144. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  145. }
  146. });
  147. },
  148. lock: (sessionId, stationId, cb) => {
  149. //TODO Require admin
  150. stations.initializeAndReturnStation(stationId, (err, station) => {
  151. if (err && err !== true) {
  152. return cb({ status: 'error', message: 'An error occurred while locking the station' });
  153. } else if (station) {
  154. // Add code to update Mongo and Redis
  155. cb({ status: 'success' });
  156. } else {
  157. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  158. }
  159. });
  160. },
  161. unlock: (sessionId, stationId, cb) => {
  162. //TODO Require admin
  163. stations.initializeAndReturnStation(stationId, (err, station) => {
  164. if (err && err !== true) {
  165. return cb({ status: 'error', message: 'An error occurred while unlocking the station' });
  166. } else if (station) {
  167. // Add code to update Mongo and Redis
  168. cb({ status: 'success' });
  169. } else {
  170. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  171. }
  172. });
  173. },
  174. pause: (sessionId, stationId, cb) => {
  175. //TODO Require admin
  176. stations.initializeAndReturnStation(stationId, (err, station) => {
  177. if (err && err !== true) {
  178. return cb({ status: 'error', message: 'An error occurred while pausing the station' });
  179. } else if (station) {
  180. if (!station.paused) {
  181. station.paused = true;
  182. station.pausedAt = Date.now();
  183. cache.hset('stations', stationId, station, (err) => {
  184. if (!err) {
  185. db.models.station.update({_id: stationId}, {$set: {paused: true}}, () => {
  186. cache.pub('station.pause', stationId);
  187. cb({ status: 'success' });
  188. });
  189. } else {
  190. cb({ status: 'failure', message: 'An error occurred while pausing the station.' });
  191. }
  192. });
  193. } else {
  194. cb({ status: 'failure', message: 'That station was already paused.' });
  195. }
  196. cb({ status: 'success' });
  197. } else {
  198. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  199. }
  200. });
  201. },
  202. resume: (sessionId, stationId, cb) => {
  203. //TODO Require admin
  204. stations.initializeAndReturnStation(stationId, (err, station) => {
  205. if (err && err !== true) {
  206. return cb({ status: 'error', message: 'An error occurred while resuming the station' });
  207. } else if (station) {
  208. if (station.paused) {
  209. station.paused = false;
  210. station.timePaused += (Date.now() - station.pausedAt);
  211. cache.hset('stations', stationId, station, (err) => {
  212. if (!err) {
  213. db.models.station.update({_id: stationId}, {$set: {paused: false, timePaused: station.timePaused}}, () => {
  214. cache.pub('station.resume', stationId);
  215. cb({ status: 'success' });
  216. });
  217. } else {
  218. cb({ status: 'failure', message: 'An error occurred while resuming the station.' });
  219. }
  220. });
  221. } else {
  222. cb({ status: 'failure', message: 'That station is not paused.' });
  223. }
  224. cb({ status: 'success' });
  225. } else {
  226. cb({ status: 'failure', message: `That station doesn't exist, it may have been deleted` });
  227. }
  228. });
  229. },
  230. remove: (sessionId, stationId, cb) => {
  231. cache.hdel('stations', stationId, () => {
  232. // TODO: Update Mongo
  233. return cb({ status: 'success', message: 'Station successfully removed' });
  234. });
  235. },
  236. create: (sessionId, data, cb) => {
  237. //TODO Require admin
  238. async.waterfall([
  239. (next) => {
  240. return (data) ? next() : cb({ 'status': 'failure', 'message': 'Invalid data' });
  241. },
  242. // check the cache for the station
  243. (next) => cache.hget('stations', data.name, next),
  244. // if the cached version exist
  245. (station, next) => {
  246. if (station) return next({ 'status': 'failure', 'message': 'A station with that name already exists' });
  247. db.models.station.findOne({ _id: data.name }, next);
  248. },
  249. (station, next) => {
  250. if (station) return next({ 'status': 'failure', 'message': 'A station with that name already exists' });
  251. const { _id, displayName, description, genres, playlist } = data;
  252. db.models.station.create({
  253. _id,
  254. displayName,
  255. description,
  256. type: "official",
  257. playlist,
  258. genres,
  259. currentSong: defaultSong
  260. }, next);
  261. }
  262. ], (err, station) => {
  263. if (err) throw err;
  264. stations.calculateSongForStation(station, () => {
  265. cache.pub('station.create', data.name);
  266. return cb(null, { 'status': 'success', 'message': 'Successfully created station.' });
  267. });
  268. });
  269. },
  270. };