server.js 27 KB

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