server.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. Meteor.startup(function() {
  2. reCAPTCHA.config({
  3. privatekey: '6LcVxg0TAAAAAI2fgIEEWHFxwNXeVIs8mzq5cfRM'
  4. });
  5. var stations = [{tag: "edm", display: "EDM"}, {tag: "pop", display: "Pop"}]; //Rooms to be set on server startup
  6. for(var i in stations){
  7. if(Rooms.find({type: stations[i]}).count() === 0){
  8. createRoom(stations[i].display, stations[i].tag);
  9. }
  10. }
  11. });
  12. Alerts.update({active: true}, {$set: {active: false}}, { multi: true });
  13. var stations = [];
  14. var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_";
  15. function createUniqueSongId() {
  16. var code = "";
  17. for (var i = 0; i < 6; i++) {
  18. code += chars[Math.floor(Math.random() * chars.length)];
  19. }
  20. if (Playlists.find({"songs.mid": code}).count() > 0) {
  21. return createUniqueSongId();
  22. } else {
  23. return code;
  24. }
  25. }
  26. function checkUsersPR() {
  27. var output = {};
  28. var connections = Meteor.server.stream_server.open_sockets;
  29. _.each(connections,function(connection){
  30. // named subscriptions
  31. var subs = connection._meteorSession._namedSubs;
  32. //var ip = connection.remoteAddress;
  33. var used_subs = [];
  34. for(var sub in subs){
  35. var mySubName = subs[sub]._name;
  36. if(subs[sub]._params.length>0){
  37. mySubName += subs[sub]._params[0]; // assume one id parameter for now
  38. }
  39. if (used_subs.indexOf(mySubName) === -1) {
  40. used_subs.push(mySubName);
  41. if(!output[mySubName]){
  42. output[mySubName] = 1;
  43. }else{
  44. output[mySubName] += 1;
  45. }
  46. }
  47. }
  48. // there are also these 'universal subscriptions'
  49. //not sure what these are, i count none in my tests
  50. //var usubs = connection._meteorSession._universalSubs;
  51. });
  52. for (var key in output) {
  53. getStation(key, function() {
  54. Rooms.update({type: key}, {$set: {users: output[key]}});
  55. });
  56. }
  57. return output;
  58. }
  59. function getStation(type, cb) {
  60. stations.forEach(function(station) {
  61. if (station.type === type) {
  62. cb(station);
  63. return;
  64. }
  65. });
  66. }
  67. function createRoom(display, tag) {
  68. var type = tag;
  69. if (Rooms.find({type: type}).count() === 0) {
  70. Rooms.insert({display: display, type: type, users: 0}, function(err) {
  71. if (err) {
  72. throw err;
  73. } else {
  74. if (Playlists.find({type: type}).count() === 1) {
  75. stations.push(new Station(type));
  76. } else {
  77. Playlists.insert({type: type, songs: getSongsByType(type)}, function (err2) {
  78. if (err2) {
  79. throw err2;
  80. } else {
  81. stations.push(new Station(type));
  82. }
  83. });
  84. }
  85. }
  86. });
  87. } else {
  88. return "Room already exists";
  89. }
  90. }
  91. function Station(type) {
  92. Meteor.publish(type, function() {
  93. return undefined;
  94. });
  95. var _this = this;
  96. var startedAt = Date.now();
  97. var playlist = Playlists.findOne({type: type});
  98. var songs = playlist.songs;
  99. if (playlist.lastSong === undefined) {
  100. Playlists.update({type: type}, {$set: {lastSong: 0}});
  101. playlist = Playlists.findOne({type: type});
  102. songs = playlist.songs;
  103. }
  104. var currentSong = playlist.lastSong;
  105. if (currentSong < (songs.length - 1)) {
  106. currentSong++;
  107. } else currentSong = 0;
  108. var currentTitle = songs[currentSong].title;
  109. Rooms.update({type: type}, {$set: {currentSong: {song: songs[currentSong], started: startedAt}, users: 0}});
  110. this.skipSong = function() {
  111. songs = Playlists.findOne({type: type}).songs;
  112. songs.forEach(function(song, index) {
  113. if (song.title === currentTitle) {
  114. currentSong = index;
  115. }
  116. });
  117. if (currentSong < (songs.length - 1)) {
  118. currentSong++;
  119. } else currentSong = 0;
  120. if (songs);
  121. if (currentSong === 0) {
  122. this.shufflePlaylist();
  123. } else {
  124. if (songs[currentSong].mid === undefined) {
  125. var newSong = songs[currentSong];
  126. newSong.mid = createUniqueSongId();
  127. songs[currentSong].mid = newSong.mid;
  128. Playlists.update({type: type, "songs": songs[currentSong]}, {$set: {"songs.$": newSong}});
  129. }
  130. if (songs[currentSong].likes === undefined) {
  131. var newSong = songs[currentSong];
  132. newSong.likes = 0;
  133. Playlists.update({type: type, "songs": newSong}, {$set: {"songs.$": newSong}});
  134. }
  135. if (songs[currentSong].dislikes === undefined) {
  136. var newSong = songs[currentSong];
  137. newSong.dislikes = 0;
  138. Playlists.update({type: type, "songs": newSong}, {$set: {"songs.$": newSong}});
  139. }
  140. currentTitle = songs[currentSong].title;
  141. Playlists.update({type: type}, {$set: {lastSong: currentSong}});
  142. Rooms.update({type: type}, {$set: {timePaused: 0}});
  143. this.songTimer();
  144. Rooms.update({type: type}, {$set: {currentSong: {song: songs[currentSong], started: startedAt}}});
  145. }
  146. };
  147. this.shufflePlaylist = function() {
  148. songs = Playlists.findOne({type: type}).songs;
  149. currentSong = 0;
  150. Playlists.update({type: type}, {$set: {"songs": []}});
  151. songs = shuffle(songs);
  152. songs.forEach(function(song) {
  153. if (song.mid === undefined) {
  154. song.mid = createUniqueSongId();
  155. }
  156. if (song.likes === undefined) {
  157. song.likes = 0;
  158. }
  159. if (song.dislikes === undefined) {
  160. song.dislikes = 0;
  161. }
  162. Playlists.update({type: type}, {$push: {"songs": song}});
  163. });
  164. currentTitle = songs[currentSong].title;
  165. Playlists.update({type: type}, {$set: {lastSong: currentSong}});
  166. Rooms.update({type: type}, {$set: {timePaused: 0}});
  167. this.songTimer();
  168. Rooms.update({type: type}, {$set: {currentSong: {song: songs[currentSong], started: startedAt}}});
  169. };
  170. Rooms.update({type: type}, {$set: {timePaused: 0}});
  171. var timer;
  172. this.songTimer = function() {
  173. startedAt = Date.now();
  174. if (timer !== undefined) {
  175. timer.pause();
  176. }
  177. timer = new Timer(function() {
  178. _this.skipSong();
  179. }, songs[currentSong].duration * 1000);
  180. };
  181. var state = Rooms.findOne({type: type}).state;
  182. this.pauseRoom = function() {
  183. if (state !== "paused") {
  184. timer.pause();
  185. Rooms.update({type: type}, {$set: {state: "paused"}});
  186. state = "paused";
  187. }
  188. };
  189. this.resumeRoom = function() {
  190. if (state !== "playing") {
  191. timer.resume();
  192. Rooms.update({type: type}, {$set: {state: "playing", timePaused: timer.timeWhenPaused()}});
  193. state = "playing";
  194. }
  195. };
  196. this.cancelTimer = function() {
  197. timer.pause();
  198. };
  199. this.getState = function() {
  200. return state;
  201. };
  202. this.type = type;
  203. this.songTimer();
  204. }
  205. function shuffle(array) {
  206. var currentIndex = array.length, temporaryValue, randomIndex ;
  207. // While there remain elements to shuffle...
  208. while (0 !== currentIndex) {
  209. // Pick a remaining element...
  210. randomIndex = Math.floor(Math.random() * currentIndex);
  211. currentIndex -= 1;
  212. // And swap it with the current element.
  213. temporaryValue = array[currentIndex];
  214. array[currentIndex] = array[randomIndex];
  215. array[randomIndex] = temporaryValue;
  216. }
  217. return array;
  218. }
  219. function Timer(callback, delay) {
  220. var timerId, start, remaining = delay;
  221. var timeWhenPaused = 0;
  222. var timePaused = new Date();
  223. this.pause = function() {
  224. Meteor.clearTimeout(timerId);
  225. remaining -= new Date() - start;
  226. timePaused = new Date();
  227. };
  228. this.resume = function() {
  229. start = new Date();
  230. Meteor.clearTimeout(timerId);
  231. timerId = Meteor.setTimeout(callback, remaining);
  232. timeWhenPaused += new Date() - timePaused;
  233. };
  234. this.timeWhenPaused = function() {
  235. return timeWhenPaused;
  236. };
  237. this.resume();
  238. }
  239. Meteor.users.deny({update: function () { return true; }});
  240. Meteor.users.deny({insert: function () { return true; }});
  241. Meteor.users.deny({remove: function () { return true; }});
  242. function getSongDuration(query, artistName){
  243. var duration;
  244. var search = query;
  245. query = query.toLowerCase().split(" ").join("%20");
  246. var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + query + '&type=track');
  247. for(var i in res.data){
  248. for(var j in res.data[i].items){
  249. if(search.indexOf(res.data[i].items[j].name) !== -1 && artistName.indexOf(res.data[i].items[j].artists[0].name) !== -1){
  250. duration = res.data[i].items[j].duration_ms / 1000;
  251. return duration;
  252. }
  253. }
  254. }
  255. }
  256. function getSongAlbumArt(query, artistName){
  257. var albumart;
  258. var search = query;
  259. query = query.toLowerCase().split(" ").join("%20");
  260. var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + query + '&type=track');
  261. for(var i in res.data){
  262. for(var j in res.data[i].items){
  263. if(search.indexOf(res.data[i].items[j].name) !== -1 && artistName.indexOf(res.data[i].items[j].artists[0].name) !== -1){
  264. albumart = res.data[i].items[j].album.images[1].url
  265. return albumart;
  266. }
  267. }
  268. }
  269. }
  270. //var room_types = ["edm", "nightcore"];
  271. var songsArr = [];
  272. function getSongsByType(type) {
  273. if (type === "edm") {
  274. return [
  275. {id: "aE2GCa-_nyU", mid: "fh6_Gf", title: "Radioactive", duration: getSongDuration("Radioactive - Lindsey Stirling and Pentatonix", "Lindsey Stirling, Pentatonix"), artist: "Lindsey Stirling, Pentatonix", type: "YouTube", img: "https://i.scdn.co/image/62167a9007cef2e8ef13ab1d93019312b9b03655"},
  276. {id: "aHjpOzsQ9YI", mid: "goG88g", title: "Crystallize", artist: "Lindsey Stirling", duration: getSongDuration("Crystallize", "Lindsey Stirling"), type: "YouTube", img: "https://i.scdn.co/image/b0c1ccdd0cd7bcda741ccc1c3e036f4ed2e52312"}
  277. ];
  278. } else if (type === "nightcore") {
  279. return [{id: "f7RKOP87tt4", mid: "5pGGog", title: "Monster (DotEXE Remix)", duration: getSongDuration("Monster (DotEXE Remix)", "Meg & Dia"), artist: "Meg & Dia", type: "YouTube", img: "https://i.scdn.co/image/35ecdfba9c31a6c54ee4c73dcf1ad474c560cd00"}];
  280. } else {
  281. return [{id: "dQw4w9WgXcQ", mid: "6_fdr4", title: "Never Gonna Give You Up", duration: getSongDuration("Never Gonna Give You Up", "Rick Astley"), artist: "Rick Astley", type: "YouTube", img: "https://i.scdn.co/image/5246898e19195715e65e261899baba890a2c1ded"}];
  282. }
  283. }
  284. Rooms.find({}).fetch().forEach(function(room) {
  285. var type = room.type;
  286. if (Playlists.find({type: type}).count() === 0) {
  287. if (type === "edm") {
  288. Playlists.insert({type: type, songs: getSongsByType(type)});
  289. } else if (type === "nightcore") {
  290. Playlists.insert({type: type, songs: getSongsByType(type)});
  291. } else {
  292. Playlists.insert({type: type, songs: getSongsByType(type)});
  293. }
  294. }
  295. if (Playlists.findOne({type: type}).songs.length === 0) {
  296. // Add a global video to Playlist so it can proceed
  297. } else {
  298. stations.push(new Station(type));
  299. }
  300. });
  301. Accounts.validateNewUser(function(user) {
  302. var username;
  303. if (user.services) {
  304. if (user.services.github) {
  305. username = user.services.github.username;
  306. } else if (user.services.facebook) {
  307. username = user.services.facebook.first_name;
  308. } else if (user.services.password) {
  309. username = user.username;
  310. }
  311. }
  312. if (Meteor.users.find({"profile.usernameL": username.toLowerCase()}).count() !== 0) {
  313. throw new Meteor.Error(403, "An account with that username already exists.");
  314. } else {
  315. return true;
  316. }
  317. });
  318. Accounts.onCreateUser(function(options, user) {
  319. var username;
  320. if (user.services) {
  321. if (user.services.github) {
  322. username = user.services.github.username;
  323. } else if (user.services.facebook) {
  324. username = user.services.facebook.first_name;
  325. } else if (user.services.password) {
  326. username = user.username;
  327. }
  328. }
  329. user.profile = {username: username, usernameL: username.toLowerCase(), rank: "default", liked: [], disliked: []};
  330. return user;
  331. });
  332. ServiceConfiguration.configurations.remove({
  333. service: "facebook"
  334. });
  335. ServiceConfiguration.configurations.insert({
  336. service: "facebook",
  337. appId: "1496014310695890",
  338. secret: "9a039f254a08a1488c08bb0737dbd2a6"
  339. });
  340. ServiceConfiguration.configurations.remove({
  341. service: "github"
  342. });
  343. ServiceConfiguration.configurations.insert({
  344. service: "github",
  345. clientId: "dcecd720f47c0e4001f7",
  346. secret: "375939d001ef1a0ca67c11dbf8fb9aeaa551e01b"
  347. });
  348. Meteor.publish("alerts", function() {
  349. return Alerts.find({active: true})
  350. });
  351. Meteor.publish("allAlerts", function() {
  352. return Alerts.find({active: false})
  353. });
  354. Meteor.publish("playlists", function() {
  355. return Playlists.find({})
  356. });
  357. Meteor.publish("rooms", function() {
  358. return Rooms.find({});
  359. });
  360. Meteor.publish("queues", function() {
  361. return Queues.find({});
  362. });
  363. Meteor.publish("chat", function() {
  364. return Chat.find({});
  365. });
  366. Meteor.publish("userProfiles", function() {
  367. return Meteor.users.find({}, {fields: {profile: 1, createdAt: 1}});
  368. });
  369. Meteor.publish("isAdmin", function() {
  370. return Meteor.users.find({_id: this.userId, "profile.rank": "admin"});
  371. });
  372. function isAdmin() {
  373. var userData = Meteor.users.find(Meteor.userId());
  374. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
  375. return true;
  376. } else {
  377. return false;
  378. }
  379. }
  380. Meteor.methods({
  381. removeAlerts: function() {
  382. if (isAdmin()) {
  383. Alerts.update({active: true}, {$set: {active: false}}, { multi: true });
  384. } else {
  385. throw Meteor.Error(403, "Invalid permissions.");
  386. }
  387. },
  388. addAlert: function(description, priority) {
  389. if (isAdmin()) {
  390. if (description.length > 0 && description.length < 400) {
  391. var username = Meteor.user().profile.username;
  392. if (["danger", "warning", "success", "primary"].indexOf(priority) === -1) {
  393. priority = "warning";
  394. }
  395. Alerts.insert({description: description, priority: priority, active: true, createdBy: username});
  396. return true;
  397. } else {
  398. throw Meteor.Error(403, "Invalid description length.");
  399. }
  400. } else {
  401. throw Meteor.Error(403, "Invalid permissions.");
  402. }
  403. },
  404. sendMessage: function(type, message) {
  405. if (Meteor.userId()) {
  406. var user = Meteor.user();
  407. var time = new Date();
  408. var rawrank = user.profile.rank;
  409. var username = user.profile.username;
  410. var rank = user.profile.rank;
  411. if (message.length === 0) {
  412. throw new Meteor.Error(406, "Message length cannot be 0.");
  413. }
  414. if (message.length > 300) {
  415. throw new Meteor.Error(406, "Message length cannot be more than 300 characters long..");
  416. }
  417. if (user.profile.rank = "admin") {
  418. Chat.insert({type: type, rawrank: rawrank, rank: "[A]", message: message, time: time, username: username});
  419. return true;
  420. } else if (user.profile.rank = "mod") {
  421. Chat.insert({type: type, rank: "[M]", message: message, time: time, username: username});
  422. return true;
  423. }
  424. else {
  425. Chat.insert({type: type, rawrank: rawrank, message: message, time: time, username: username});
  426. return true;
  427. }
  428. } else {
  429. throw new Meteor.Error(403, "Invalid permissions.");
  430. }
  431. },
  432. likeSong: function(mid) {
  433. if (Meteor.userId()) {
  434. var user = Meteor.user();
  435. if (user.profile.liked.indexOf(mid) === -1) {
  436. Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.liked": mid}});
  437. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": 1}})
  438. } else {
  439. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
  440. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": -1}})
  441. }
  442. if (user.profile.disliked.indexOf(mid) !== -1) {
  443. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
  444. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": -1}})
  445. }
  446. return true;
  447. } else {
  448. throw new Meteor.Error(403, "Invalid permissions.");
  449. }
  450. },
  451. dislikeSong: function(mid) {
  452. if (Meteor.userId()) {
  453. var user = Meteor.user();
  454. if (user.profile.disliked.indexOf(mid) === -1) {
  455. Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.disliked": mid}});
  456. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": 1}});
  457. } else {
  458. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
  459. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": -1}});
  460. }
  461. if (user.profile.liked.indexOf(mid) !== -1) {
  462. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
  463. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": -1}});
  464. }
  465. return true;
  466. } else {
  467. throw new Meteor.Error(403, "Invalid permissions.");
  468. }
  469. },
  470. submitReport: function(report, id) {
  471. var obj = report;
  472. obj.id = id;
  473. Reports.insert(obj);
  474. },
  475. shufflePlaylist: function(type) {
  476. if (isAdmin()) {
  477. getStation(type, function(station) {
  478. if (station === undefined) {
  479. throw new Meteor.Error(404, "Station not found.");
  480. } else {
  481. station.cancelTimer();
  482. station.shufflePlaylist();
  483. }
  484. });
  485. }
  486. },
  487. skipSong: function(type) {
  488. if (isAdmin()) {
  489. getStation(type, function(station) {
  490. if (station === undefined) {
  491. throw new Meteor.Error(404, "Station not found.");
  492. } else {
  493. station.skipSong();
  494. }
  495. });
  496. }
  497. },
  498. pauseRoom: function(type) {
  499. if (isAdmin()) {
  500. getStation(type, function(station) {
  501. if (station === undefined) {
  502. throw new Meteor.Error(403, "Room doesn't exist.");
  503. } else {
  504. station.pauseRoom();
  505. }
  506. });
  507. } else {
  508. throw new Meteor.Error(403, "Invalid permissions.");
  509. }
  510. },
  511. resumeRoom: function(type) {
  512. if (isAdmin()) {
  513. getStation(type, function(station) {
  514. if (station === undefined) {
  515. throw new Meteor.Error(403, "Room doesn't exist.");
  516. } else {
  517. station.resumeRoom();
  518. }
  519. });
  520. } else {
  521. throw new Meteor.Error(403, "Invalid permissions.");
  522. }
  523. },
  524. createUserMethod: function(formData, captchaData) {
  525. var verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(this.connection.clientAddress, captchaData);
  526. if (!verifyCaptchaResponse.success) {
  527. console.log('reCAPTCHA check failed!', verifyCaptchaResponse);
  528. throw new Meteor.Error(422, 'reCAPTCHA Failed: ' + verifyCaptchaResponse.error);
  529. } else {
  530. console.log('reCAPTCHA verification passed!');
  531. Accounts.createUser({
  532. username: formData.username,
  533. email: formData.email,
  534. password: formData.password
  535. });
  536. }
  537. return true;
  538. },
  539. addSongToQueue: function(type, songData) {
  540. if (Meteor.userId()) {
  541. type = type.toLowerCase();
  542. if (Rooms.find({type: type}).count() === 1) {
  543. if (Queues.find({type: type}).count() === 0) {
  544. Queues.insert({type: type, songs: []});
  545. }
  546. if (songData !== undefined && Object.keys(songData).length === 5 && songData.type !== undefined && songData.title !== undefined && songData.title !== undefined && songData.artist !== undefined && songData.img !== undefined) {
  547. songData.duration = getSongDuration(songData.title, songData.artist);
  548. songData.img = getSongAlbumArt(songData.title, songData.artist);
  549. songData.skipDuration = 0;
  550. var mid = createUniqueSongId();
  551. if (mid !== undefined) {
  552. songData.mid = mid;
  553. Queues.update({type: type}, {
  554. $push: {
  555. songs: {
  556. id: songData.id,
  557. mid: songData.mid,
  558. title: songData.title,
  559. artist: songData.artist,
  560. duration: songData.duration,
  561. skipDuration: songData.skipDuration,
  562. img: songData.img,
  563. type: songData.type
  564. }
  565. }
  566. });
  567. return true;
  568. } else {
  569. throw new Meteor.Error(500, "Am error occured.");
  570. }
  571. } else {
  572. throw new Meteor.Error(403, "Invalid data.");
  573. }
  574. } else {
  575. throw new Meteor.Error(403, "Invalid genre.");
  576. }
  577. } else {
  578. throw new Meteor.Error(403, "Invalid permissions.");
  579. }
  580. },
  581. updateQueueSong: function(genre, oldSong, newSong) {
  582. if (isAdmin()) {
  583. newSong.mid = oldSong.mid;
  584. Queues.update({type: genre, "songs": oldSong}, {$set: {"songs.$": newSong}});
  585. return true;
  586. } else {
  587. throw new Meteor.Error(403, "Invalid permissions.");
  588. }
  589. },
  590. updatePlaylistSong: function(genre, oldSong, newSong) {
  591. if (isAdmin()) {
  592. newSong.mid = oldSong.mid;
  593. Playlists.update({type: genre, "songs": oldSong}, {$set: {"songs.$": newSong}});
  594. return true;
  595. } else {
  596. throw new Meteor.Error(403, "Invalid permissions.");
  597. }
  598. },
  599. removeSongFromQueue: function(type, mid) {
  600. if (isAdmin()) {
  601. type = type.toLowerCase();
  602. Queues.update({type: type}, {$pull: {songs: {mid: mid}}});
  603. } else {
  604. throw new Meteor.Error(403, "Invalid permissions.");
  605. }
  606. },
  607. removeSongFromPlaylist: function(type, mid) {
  608. if (isAdmin()) {
  609. type = type.toLowerCase();
  610. Playlists.update({type: type}, {$pull: {songs: {mid: mid}}});
  611. } else {
  612. throw new Meteor.Error(403, "Invalid permissions.");
  613. }
  614. },
  615. addSongToPlaylist: function(type, songData) {
  616. if (isAdmin()) {
  617. type = type.toLowerCase();
  618. if (Rooms.find({type: type}).count() === 1) {
  619. if (Playlists.find({type: type}).count() === 0) {
  620. Playlists.insert({type: type, songs: []});
  621. }
  622. console.log(songData);
  623. if (songData !== undefined && Object.keys(songData).length === 8 && songData.type !== undefined && songData.mid !== undefined && songData.title !== undefined && songData.title !== undefined && songData.artist !== undefined && songData.duration !== undefined && songData.skipDuration !== undefined && songData.img !== undefined) {
  624. songData.likes = 0;
  625. songData.dislikes = 0
  626. Playlists.update({type: type}, {
  627. $push: {
  628. songs: {
  629. id: songData.id,
  630. mid: songData.mid,
  631. title: songData.title,
  632. artist: songData.artist,
  633. duration: songData.duration,
  634. skipDuration: songData.skipDuration,
  635. img: songData.img,
  636. type: songData.type,
  637. likes: songData.likes,
  638. dislikes: songData.dislikes
  639. }
  640. }
  641. });
  642. Queues.update({type: type}, {$pull: {songs: {mid: songData.mid}}});
  643. return true;
  644. } else {
  645. throw new Meteor.Error(403, "Invalid data.");
  646. }
  647. } else {
  648. throw new Meteor.Error(403, "Invalid genre.");
  649. }
  650. } else {
  651. throw new Meteor.Error(403, "Invalid permissions.");
  652. }
  653. },
  654. createRoom: function(display, tag) {
  655. if (isAdmin()) {
  656. createRoom(display, tag);
  657. } else {
  658. throw new Meteor.Error(403, "Invalid permissions.");
  659. }
  660. },
  661. deleteRoom: function(type){
  662. if (isAdmin()) {
  663. Rooms.remove({type: type});
  664. Playlists.remove({type: type});
  665. Queues.remove({type: type});
  666. return true;
  667. } else {
  668. throw new Meteor.Error(403, "Invalid permissions.");
  669. }
  670. },
  671. getUserNum: function(){
  672. return Object.keys(Meteor.default_server.sessions).length;
  673. }
  674. });
  675. Meteor.setInterval(function() {
  676. checkUsersPR();
  677. }, 10000);