server.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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("reports", function() {
  364. return Reports.find({});
  365. });
  366. Meteor.publish("chat", function() {
  367. return Chat.find({});
  368. });
  369. Meteor.publish("userProfiles", function() {
  370. return Meteor.users.find({}, {fields: {profile: 1, createdAt: 1}});
  371. });
  372. Meteor.publish("isAdmin", function() {
  373. return Meteor.users.find({_id: this.userId, "profile.rank": "admin"});
  374. });
  375. function isAdmin() {
  376. var userData = Meteor.users.find(Meteor.userId());
  377. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
  378. return true;
  379. } else {
  380. return false;
  381. }
  382. }
  383. Meteor.methods({
  384. removeAlerts: function() {
  385. if (isAdmin()) {
  386. Alerts.update({active: true}, {$set: {active: false}}, { multi: true });
  387. } else {
  388. throw Meteor.Error(403, "Invalid permissions.");
  389. }
  390. },
  391. addAlert: function(description, priority) {
  392. if (isAdmin()) {
  393. if (description.length > 0 && description.length < 400) {
  394. var username = Meteor.user().profile.username;
  395. if (["danger", "warning", "success", "primary"].indexOf(priority) === -1) {
  396. priority = "warning";
  397. }
  398. Alerts.insert({description: description, priority: priority, active: true, createdBy: username});
  399. return true;
  400. } else {
  401. throw Meteor.Error(403, "Invalid description length.");
  402. }
  403. } else {
  404. throw Meteor.Error(403, "Invalid permissions.");
  405. }
  406. },
  407. sendMessage: function(type, message) {
  408. if (Meteor.userId()) {
  409. var user = Meteor.user();
  410. var time = new Date();
  411. var rawrank = user.profile.rank;
  412. var username = user.profile.username;
  413. var rank = user.profile.rank;
  414. if (message.length === 0) {
  415. throw new Meteor.Error(406, "Message length cannot be 0.");
  416. }
  417. if (message.length > 300) {
  418. throw new Meteor.Error(406, "Message length cannot be more than 300 characters long..");
  419. }
  420. if (user.profile.rank = "admin") {
  421. Chat.insert({type: type, rawrank: rawrank, rank: "[A]", message: message, time: time, username: username});
  422. return true;
  423. } else if (user.profile.rank = "mod") {
  424. Chat.insert({type: type, rank: "[M]", message: message, time: time, username: username});
  425. return true;
  426. }
  427. else {
  428. Chat.insert({type: type, rawrank: rawrank, message: message, time: time, username: username});
  429. return true;
  430. }
  431. } else {
  432. throw new Meteor.Error(403, "Invalid permissions.");
  433. }
  434. },
  435. likeSong: function(mid) {
  436. if (Meteor.userId()) {
  437. var user = Meteor.user();
  438. if (user.profile.liked.indexOf(mid) === -1) {
  439. Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.liked": mid}});
  440. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": 1}})
  441. } else {
  442. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
  443. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": -1}})
  444. }
  445. if (user.profile.disliked.indexOf(mid) !== -1) {
  446. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
  447. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": -1}})
  448. }
  449. return true;
  450. } else {
  451. throw new Meteor.Error(403, "Invalid permissions.");
  452. }
  453. },
  454. dislikeSong: function(mid) {
  455. if (Meteor.userId()) {
  456. var user = Meteor.user();
  457. if (user.profile.disliked.indexOf(mid) === -1) {
  458. Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.disliked": mid}});
  459. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": 1}});
  460. } else {
  461. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
  462. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": -1}});
  463. }
  464. if (user.profile.liked.indexOf(mid) !== -1) {
  465. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
  466. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": -1}});
  467. }
  468. return true;
  469. } else {
  470. throw new Meteor.Error(403, "Invalid permissions.");
  471. }
  472. },
  473. submitReport: function(report, id) {
  474. var obj = report;
  475. obj.id = id;
  476. Reports.insert(obj);
  477. },
  478. shufflePlaylist: function(type) {
  479. if (isAdmin()) {
  480. getStation(type, function(station) {
  481. if (station === undefined) {
  482. throw new Meteor.Error(404, "Station not found.");
  483. } else {
  484. station.cancelTimer();
  485. station.shufflePlaylist();
  486. }
  487. });
  488. }
  489. },
  490. skipSong: function(type) {
  491. if (isAdmin()) {
  492. getStation(type, function(station) {
  493. if (station === undefined) {
  494. throw new Meteor.Error(404, "Station not found.");
  495. } else {
  496. station.skipSong();
  497. }
  498. });
  499. }
  500. },
  501. pauseRoom: function(type) {
  502. if (isAdmin()) {
  503. getStation(type, function(station) {
  504. if (station === undefined) {
  505. throw new Meteor.Error(403, "Room doesn't exist.");
  506. } else {
  507. station.pauseRoom();
  508. }
  509. });
  510. } else {
  511. throw new Meteor.Error(403, "Invalid permissions.");
  512. }
  513. },
  514. resumeRoom: function(type) {
  515. if (isAdmin()) {
  516. getStation(type, function(station) {
  517. if (station === undefined) {
  518. throw new Meteor.Error(403, "Room doesn't exist.");
  519. } else {
  520. station.resumeRoom();
  521. }
  522. });
  523. } else {
  524. throw new Meteor.Error(403, "Invalid permissions.");
  525. }
  526. },
  527. createUserMethod: function(formData, captchaData) {
  528. var verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(this.connection.clientAddress, captchaData);
  529. if (!verifyCaptchaResponse.success) {
  530. console.log('reCAPTCHA check failed!', verifyCaptchaResponse);
  531. throw new Meteor.Error(422, 'reCAPTCHA Failed: ' + verifyCaptchaResponse.error);
  532. } else {
  533. console.log('reCAPTCHA verification passed!');
  534. Accounts.createUser({
  535. username: formData.username,
  536. email: formData.email,
  537. password: formData.password
  538. });
  539. }
  540. return true;
  541. },
  542. addSongToQueue: function(type, songData) {
  543. if (Meteor.userId()) {
  544. type = type.toLowerCase();
  545. if (Rooms.find({type: type}).count() === 1) {
  546. if (Queues.find({type: type}).count() === 0) {
  547. Queues.insert({type: type, songs: []});
  548. }
  549. if (songData !== undefined && Object.keys(songData).length === 5 && songData.type !== undefined && songData.title !== undefined && songData.title !== undefined && songData.artist !== undefined && songData.img !== undefined) {
  550. songData.duration = getSongDuration(songData.title, songData.artist);
  551. songData.img = getSongAlbumArt(songData.title, songData.artist);
  552. songData.skipDuration = 0;
  553. var mid = createUniqueSongId();
  554. if (mid !== undefined) {
  555. songData.mid = mid;
  556. Queues.update({type: type}, {
  557. $push: {
  558. songs: {
  559. id: songData.id,
  560. mid: songData.mid,
  561. title: songData.title,
  562. artist: songData.artist,
  563. duration: songData.duration,
  564. skipDuration: songData.skipDuration,
  565. img: songData.img,
  566. type: songData.type
  567. }
  568. }
  569. });
  570. return true;
  571. } else {
  572. throw new Meteor.Error(500, "Am error occured.");
  573. }
  574. } else {
  575. throw new Meteor.Error(403, "Invalid data.");
  576. }
  577. } else {
  578. throw new Meteor.Error(403, "Invalid genre.");
  579. }
  580. } else {
  581. throw new Meteor.Error(403, "Invalid permissions.");
  582. }
  583. },
  584. updateQueueSong: function(genre, oldSong, newSong) {
  585. if (isAdmin()) {
  586. newSong.mid = oldSong.mid;
  587. Queues.update({type: genre, "songs": oldSong}, {$set: {"songs.$": newSong}});
  588. return true;
  589. } else {
  590. throw new Meteor.Error(403, "Invalid permissions.");
  591. }
  592. },
  593. updatePlaylistSong: function(genre, oldSong, newSong) {
  594. if (isAdmin()) {
  595. newSong.mid = oldSong.mid;
  596. Playlists.update({type: genre, "songs": oldSong}, {$set: {"songs.$": newSong}});
  597. return true;
  598. } else {
  599. throw new Meteor.Error(403, "Invalid permissions.");
  600. }
  601. },
  602. removeSongFromQueue: function(type, mid) {
  603. if (isAdmin()) {
  604. type = type.toLowerCase();
  605. Queues.update({type: type}, {$pull: {songs: {mid: mid}}});
  606. } else {
  607. throw new Meteor.Error(403, "Invalid permissions.");
  608. }
  609. },
  610. removeSongFromPlaylist: function(type, mid) {
  611. if (isAdmin()) {
  612. type = type.toLowerCase();
  613. Playlists.update({type: type}, {$pull: {songs: {mid: mid}}});
  614. } else {
  615. throw new Meteor.Error(403, "Invalid permissions.");
  616. }
  617. },
  618. addSongToPlaylist: function(type, songData) {
  619. if (isAdmin()) {
  620. type = type.toLowerCase();
  621. if (Rooms.find({type: type}).count() === 1) {
  622. if (Playlists.find({type: type}).count() === 0) {
  623. Playlists.insert({type: type, songs: []});
  624. }
  625. console.log(songData);
  626. 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) {
  627. songData.likes = 0;
  628. songData.dislikes = 0
  629. Playlists.update({type: type}, {
  630. $push: {
  631. songs: {
  632. id: songData.id,
  633. mid: songData.mid,
  634. title: songData.title,
  635. artist: songData.artist,
  636. duration: songData.duration,
  637. skipDuration: songData.skipDuration,
  638. img: songData.img,
  639. type: songData.type,
  640. likes: songData.likes,
  641. dislikes: songData.dislikes
  642. }
  643. }
  644. });
  645. Queues.update({type: type}, {$pull: {songs: {mid: songData.mid}}});
  646. return true;
  647. } else {
  648. throw new Meteor.Error(403, "Invalid data.");
  649. }
  650. } else {
  651. throw new Meteor.Error(403, "Invalid genre.");
  652. }
  653. } else {
  654. throw new Meteor.Error(403, "Invalid permissions.");
  655. }
  656. },
  657. createRoom: function(display, tag) {
  658. if (isAdmin()) {
  659. createRoom(display, tag);
  660. } else {
  661. throw new Meteor.Error(403, "Invalid permissions.");
  662. }
  663. },
  664. deleteRoom: function(type){
  665. if (isAdmin()) {
  666. Rooms.remove({type: type});
  667. Playlists.remove({type: type});
  668. Queues.remove({type: type});
  669. return true;
  670. } else {
  671. throw new Meteor.Error(403, "Invalid permissions.");
  672. }
  673. },
  674. getUserNum: function(){
  675. return Object.keys(Meteor.default_server.sessions).length;
  676. }
  677. });
  678. Meteor.setInterval(function() {
  679. checkUsersPR();
  680. }, 10000);