server.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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. var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + encodeURIComponent(query) + '&type=track');
  240. for(var i in res.data){
  241. for(var j in res.data[i].items){
  242. if(search.indexOf(res.data[i].items[j].name) !== -1 && artistName.indexOf(res.data[i].items[j].artists[0].name) !== -1){
  243. duration = res.data[i].items[j].duration_ms / 1000;
  244. return duration;
  245. }
  246. }
  247. }
  248. }
  249. function getSongAlbumArt(query, artistName){
  250. var albumart;
  251. var search = query;
  252. var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + encodeURIComponent(query) + '&type=track');
  253. for(var i in res.data){
  254. for(var j in res.data[i].items){
  255. if(search.indexOf(res.data[i].items[j].name) !== -1 && artistName.indexOf(res.data[i].items[j].artists[0].name) !== -1){
  256. albumart = res.data[i].items[j].album.images[1].url
  257. return albumart;
  258. }
  259. }
  260. }
  261. }
  262. //var room_types = ["edm", "nightcore"];
  263. var songsArr = [];
  264. function getSongsByType(type) {
  265. if (type === "edm") {
  266. return [
  267. {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"},
  268. {id: "aHjpOzsQ9YI", mid: "goG88g", title: "Crystallize", artist: "Lindsey Stirling", duration: getSongDuration("Crystallize", "Lindsey Stirling"), type: "YouTube", img: "https://i.scdn.co/image/b0c1ccdd0cd7bcda741ccc1c3e036f4ed2e52312"}
  269. ];
  270. } else if (type === "nightcore") {
  271. 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"}];
  272. } else {
  273. 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"}];
  274. }
  275. }
  276. Rooms.find({}).fetch().forEach(function(room) {
  277. var type = room.type;
  278. if (Playlists.find({type: type}).count() === 0) {
  279. if (type === "edm") {
  280. Playlists.insert({type: type, songs: getSongsByType(type)});
  281. } else if (type === "nightcore") {
  282. Playlists.insert({type: type, songs: getSongsByType(type)});
  283. } else {
  284. Playlists.insert({type: type, songs: getSongsByType(type)});
  285. }
  286. }
  287. if (Playlists.findOne({type: type}).songs.length === 0) {
  288. // Add a global video to Playlist so it can proceed
  289. } else {
  290. stations.push(new Station(type));
  291. }
  292. });
  293. Accounts.validateNewUser(function(user) {
  294. var username;
  295. if (user.services) {
  296. if (user.services.github) {
  297. username = user.services.github.username;
  298. } else if (user.services.facebook) {
  299. username = user.services.facebook.first_name;
  300. } else if (user.services.password) {
  301. username = user.username;
  302. }
  303. }
  304. if (Meteor.users.find({"profile.usernameL": username.toLowerCase()}).count() !== 0) {
  305. throw new Meteor.Error(403, "An account with that username already exists.");
  306. } else {
  307. return true;
  308. }
  309. });
  310. Accounts.onCreateUser(function(options, user) {
  311. var username;
  312. if (user.services) {
  313. if (user.services.github) {
  314. username = user.services.github.username;
  315. } else if (user.services.facebook) {
  316. username = user.services.facebook.first_name;
  317. } else if (user.services.password) {
  318. username = user.username;
  319. }
  320. }
  321. user.profile = {username: username, usernameL: username.toLowerCase(), rank: "default", liked: [], disliked: []};
  322. return user;
  323. });
  324. ServiceConfiguration.configurations.remove({
  325. service: "facebook"
  326. });
  327. ServiceConfiguration.configurations.insert({
  328. service: "facebook",
  329. appId: "1496014310695890",
  330. secret: "9a039f254a08a1488c08bb0737dbd2a6"
  331. });
  332. ServiceConfiguration.configurations.remove({
  333. service: "github"
  334. });
  335. ServiceConfiguration.configurations.insert({
  336. service: "github",
  337. clientId: "dcecd720f47c0e4001f7",
  338. secret: "375939d001ef1a0ca67c11dbf8fb9aeaa551e01b"
  339. });
  340. Meteor.publish("alerts", function() {
  341. return Alerts.find({active: true})
  342. });
  343. Meteor.publish("allAlerts", function() {
  344. return Alerts.find({active: false})
  345. });
  346. Meteor.publish("playlists", function() {
  347. return Playlists.find({})
  348. });
  349. Meteor.publish("rooms", function() {
  350. return Rooms.find({});
  351. });
  352. Meteor.publish("queues", function() {
  353. return Queues.find({});
  354. });
  355. Meteor.publish("reports", function() {
  356. return Reports.find({});
  357. });
  358. Meteor.publish("chat", function() {
  359. return Chat.find({});
  360. });
  361. Meteor.publish("userProfiles", function() {
  362. return Meteor.users.find({}, {fields: {profile: 1, createdAt: 1}});
  363. });
  364. Meteor.publish("isAdmin", function() {
  365. return Meteor.users.find({_id: this.userId, "profile.rank": "admin"});
  366. });
  367. function isAdmin() {
  368. var userData = Meteor.users.find(Meteor.userId());
  369. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
  370. return true;
  371. } else {
  372. return false;
  373. }
  374. }
  375. Meteor.methods({
  376. resetRating: function() {
  377. if (isAdmin()) {
  378. stations.forEach(function (station) {
  379. var type = station.type;
  380. var temp_songs = Playlists.findOne({type: type}).songs;
  381. Playlists.update({type: type}, {$set: {"songs": []}});
  382. temp_songs.forEach(function (song) {
  383. song.likes = 0;
  384. song.dislikes = 0;
  385. Playlists.update({type: type}, {$push: {"songs": song}});
  386. });
  387. });
  388. Meteor.users.update({}, {$set: {"profile.liked": [], "profile.disliked": []}}, {multi: true});
  389. } else {
  390. throw Meteor.Error(403, "Invalid permissions.");
  391. }
  392. },
  393. removeAlerts: function() {
  394. if (isAdmin()) {
  395. Alerts.update({active: true}, {$set: {active: false}}, { multi: true });
  396. } else {
  397. throw Meteor.Error(403, "Invalid permissions.");
  398. }
  399. },
  400. addAlert: function(description, priority) {
  401. if (isAdmin()) {
  402. if (description.length > 0 && description.length < 400) {
  403. var username = Meteor.user().profile.username;
  404. if (["danger", "warning", "success", "primary"].indexOf(priority) === -1) {
  405. priority = "warning";
  406. }
  407. Alerts.insert({description: description, priority: priority, active: true, createdBy: username});
  408. return true;
  409. } else {
  410. throw Meteor.Error(403, "Invalid description length.");
  411. }
  412. } else {
  413. throw Meteor.Error(403, "Invalid permissions.");
  414. }
  415. },
  416. sendMessage: function(type, message) {
  417. if (Meteor.userId()) {
  418. var user = Meteor.user();
  419. console.log(user);
  420. var time = new Date();
  421. var rawrank = user.profile.rank;
  422. var username = user.profile.username;
  423. if (!message.replace(/\s/g, "").length > 0) {
  424. throw new Meteor.Error(406, "Message length cannot be 0.");
  425. }
  426. if (message.length > 300) {
  427. throw new Meteor.Error(406, "Message length cannot be more than 300 characters long..");
  428. }
  429. else if (user.profile.rank === "admin") {
  430. console.log("Log admin");
  431. Chat.insert({type: type, rawrank: rawrank, rank: "[A]", message: message, time: time, username: username});
  432. return true;
  433. }
  434. else if (user.profile.rank === "mod") {
  435. console.log("Log mod");
  436. Chat.insert({type: type, rawrank: rawrank, rank: "[M]", message: message, time: time, username: username});
  437. return true;
  438. }
  439. else {
  440. console.log("Log " + rawrank);
  441. Chat.insert({type: type, rawrank: rawrank, message: message, time: time, username: username});
  442. return true;
  443. }
  444. } else {
  445. throw new Meteor.Error(403, "Invalid permissions.");
  446. }
  447. },
  448. likeSong: function(mid) {
  449. if (Meteor.userId()) {
  450. var user = Meteor.user();
  451. if (user.profile.liked.indexOf(mid) === -1) {
  452. Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.liked": mid}});
  453. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": 1}})
  454. } else {
  455. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
  456. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": -1}})
  457. }
  458. if (user.profile.disliked.indexOf(mid) !== -1) {
  459. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
  460. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": -1}})
  461. }
  462. return true;
  463. } else {
  464. throw new Meteor.Error(403, "Invalid permissions.");
  465. }
  466. },
  467. dislikeSong: function(mid) {
  468. if (Meteor.userId()) {
  469. var user = Meteor.user();
  470. if (user.profile.disliked.indexOf(mid) === -1) {
  471. Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.disliked": mid}});
  472. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": 1}});
  473. } else {
  474. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
  475. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": -1}});
  476. }
  477. if (user.profile.liked.indexOf(mid) !== -1) {
  478. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
  479. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": -1}});
  480. }
  481. return true;
  482. } else {
  483. throw new Meteor.Error(403, "Invalid permissions.");
  484. }
  485. },
  486. voteSkip: function(type){
  487. if(Meteor.userId()){
  488. var user = Meteor.user();
  489. getStation(type, function(station){
  490. if(station.voted.indexOf(user.profile.username) === -1){
  491. station.voted.push(user.profile.username);
  492. Rooms.update({type: type}, {$set: {votes: station.voted.length}});
  493. if(station.voted.length === 3){
  494. station.skipSong();
  495. }
  496. } else{
  497. throw new Meteor.Error(401, "Already voted.");
  498. }
  499. })
  500. }
  501. },
  502. submitReport: function(report, id) {
  503. var obj = report;
  504. obj.id = id;
  505. Reports.insert(obj);
  506. },
  507. shufflePlaylist: function(type) {
  508. if (isAdmin()) {
  509. getStation(type, function(station) {
  510. if (station === undefined) {
  511. throw new Meteor.Error(404, "Station not found.");
  512. } else {
  513. station.cancelTimer();
  514. station.shufflePlaylist();
  515. }
  516. });
  517. }
  518. },
  519. skipSong: function(type) {
  520. if (isAdmin()) {
  521. getStation(type, function(station) {
  522. if (station === undefined) {
  523. throw new Meteor.Error(404, "Station not found.");
  524. } else {
  525. station.skipSong();
  526. }
  527. });
  528. }
  529. },
  530. pauseRoom: function(type) {
  531. if (isAdmin()) {
  532. getStation(type, function(station) {
  533. if (station === undefined) {
  534. throw new Meteor.Error(403, "Room doesn't exist.");
  535. } else {
  536. station.pauseRoom();
  537. }
  538. });
  539. } else {
  540. throw new Meteor.Error(403, "Invalid permissions.");
  541. }
  542. },
  543. resumeRoom: function(type) {
  544. if (isAdmin()) {
  545. getStation(type, function(station) {
  546. if (station === undefined) {
  547. throw new Meteor.Error(403, "Room doesn't exist.");
  548. } else {
  549. station.resumeRoom();
  550. }
  551. });
  552. } else {
  553. throw new Meteor.Error(403, "Invalid permissions.");
  554. }
  555. },
  556. createUserMethod: function(formData, captchaData) {
  557. var verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(this.connection.clientAddress, captchaData);
  558. if (!verifyCaptchaResponse.success) {
  559. console.log('reCAPTCHA check failed!', verifyCaptchaResponse);
  560. throw new Meteor.Error(422, 'reCAPTCHA Failed: ' + verifyCaptchaResponse.error);
  561. } else {
  562. console.log('reCAPTCHA verification passed!');
  563. Accounts.createUser({
  564. username: formData.username,
  565. email: formData.email,
  566. password: formData.password
  567. });
  568. }
  569. return true;
  570. },
  571. addSongToQueue: function(type, songData) {
  572. if (Meteor.userId()) {
  573. type = type.toLowerCase();
  574. if (Rooms.find({type: type}).count() === 1) {
  575. if (Queues.find({type: type}).count() === 0) {
  576. Queues.insert({type: type, songs: []});
  577. }
  578. if (songData !== undefined && Object.keys(songData).length === 5 && songData.type !== undefined && songData.title !== undefined && songData.artist !== undefined && songData.img !== undefined) {
  579. songData.duration = getSongDuration(songData.title, songData.artist) || 0;
  580. songData.img = getSongAlbumArt(songData.title, songData.artist) || "";
  581. songData.skipDuration = 0;
  582. songData.likes = 0;
  583. songData.dislikes = 0;
  584. var mid = createUniqueSongId();
  585. if (mid !== undefined) {
  586. songData.mid = mid;
  587. Queues.update({type: type}, {
  588. $push: {
  589. songs: {
  590. id: songData.id,
  591. mid: songData.mid,
  592. title: songData.title,
  593. artist: songData.artist,
  594. duration: songData.duration,
  595. skipDuration: songData.skipDuration,
  596. likes: songData.likes,
  597. dislikes: songData.dislikes,
  598. img: songData.img,
  599. type: songData.type
  600. }
  601. }
  602. });
  603. return true;
  604. } else {
  605. throw new Meteor.Error(500, "Am error occured.");
  606. }
  607. } else {
  608. throw new Meteor.Error(403, "Invalid data.");
  609. }
  610. } else {
  611. throw new Meteor.Error(403, "Invalid genre.");
  612. }
  613. } else {
  614. throw new Meteor.Error(403, "Invalid permissions.");
  615. }
  616. },
  617. updateQueueSong: function(genre, oldSong, newSong) {
  618. if (isAdmin()) {
  619. newSong.mid = oldSong.mid;
  620. Queues.update({type: genre, "songs": oldSong}, {$set: {"songs.$": newSong}});
  621. return true;
  622. } else {
  623. throw new Meteor.Error(403, "Invalid permissions.");
  624. }
  625. },
  626. updatePlaylistSong: function(genre, oldSong, newSong) {
  627. if (isAdmin()) {
  628. newSong.mid = oldSong.mid;
  629. Playlists.update({type: genre, "songs": oldSong}, {$set: {"songs.$": newSong}});
  630. return true;
  631. } else {
  632. throw new Meteor.Error(403, "Invalid permissions.");
  633. }
  634. },
  635. removeSongFromQueue: function(type, mid) {
  636. if (isAdmin()) {
  637. type = type.toLowerCase();
  638. Queues.update({type: type}, {$pull: {songs: {mid: mid}}});
  639. } else {
  640. throw new Meteor.Error(403, "Invalid permissions.");
  641. }
  642. },
  643. removeSongFromPlaylist: function(type, mid) {
  644. if (isAdmin()) {
  645. type = type.toLowerCase();
  646. Playlists.update({type: type}, {$pull: {songs: {mid: mid}}});
  647. } else {
  648. throw new Meteor.Error(403, "Invalid permissions.");
  649. }
  650. },
  651. addSongToPlaylist: function(type, songData) {
  652. if (isAdmin()) {
  653. type = type.toLowerCase();
  654. if (Rooms.find({type: type}).count() === 1) {
  655. if (Playlists.find({type: type}).count() === 0) {
  656. Playlists.insert({type: type, songs: []});
  657. }
  658. var requiredProperties = ["type", "mid", "id", "title", "artist", "duration", "skipDuration", "img", "likes", "dislikes"];
  659. if (songData !== undefined && Object.keys(songData).length === requiredProperties.length) {
  660. for (var property in requiredProperties) {
  661. if (songData[requiredProperties[property]] === undefined) {
  662. throw new Meteor.Error(403, "Invalid data.");
  663. }
  664. }
  665. Playlists.update({type: type}, {
  666. $push: {
  667. songs: {
  668. id: songData.id,
  669. mid: songData.mid,
  670. title: songData.title,
  671. artist: songData.artist,
  672. duration: songData.duration,
  673. skipDuration: songData.skipDuration,
  674. img: songData.img,
  675. type: songData.type,
  676. likes: Number(songData.likes),
  677. dislikes: Number(songData.dislikes)
  678. }
  679. }
  680. });
  681. Queues.update({type: type}, {$pull: {songs: {mid: songData.mid}}});
  682. return true;
  683. } else {
  684. throw new Meteor.Error(403, "Invalid data.");
  685. }
  686. } else {
  687. throw new Meteor.Error(403, "Invalid genre.");
  688. }
  689. } else {
  690. throw new Meteor.Error(403, "Invalid permissions.");
  691. }
  692. },
  693. createRoom: function(display, tag) {
  694. if (isAdmin()) {
  695. createRoom(display, tag);
  696. } else {
  697. throw new Meteor.Error(403, "Invalid permissions.");
  698. }
  699. },
  700. deleteRoom: function(type){
  701. if (isAdmin()) {
  702. Rooms.remove({type: type});
  703. Playlists.remove({type: type});
  704. Queues.remove({type: type});
  705. return true;
  706. } else {
  707. throw new Meteor.Error(403, "Invalid permissions.");
  708. }
  709. },
  710. getUserNum: function(){
  711. return Object.keys(Meteor.default_server.sessions).length;
  712. }
  713. });
  714. Meteor.setInterval(function() {
  715. checkUsersPR();
  716. }, 10000);