server.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. var subs = connection._meteorSession._namedSubs;
  33. //var ip = connection.remoteAddress;
  34. var used_subs = [];
  35. for(var sub in subs){
  36. var mySubName = subs[sub]._name;
  37. if(subs[sub]._params.length>0){
  38. mySubName += subs[sub]._params[0]; // assume one id parameter for now
  39. }
  40. if (used_subs.indexOf(mySubName) === -1) {
  41. used_subs.push(mySubName);
  42. if(!output[mySubName]){
  43. output[mySubName] = 1;
  44. }else{
  45. output[mySubName] += 1;
  46. }
  47. }
  48. }
  49. // there are also these 'universal subscriptions'
  50. //not sure what these are, i count none in my tests
  51. //var usubs = connection._meteorSession._universalSubs;
  52. });
  53. for (var key in output) {
  54. getStation(key, function() {
  55. Rooms.update({type: key}, {$set: {users: output[key]}});
  56. });
  57. }
  58. return output;
  59. }
  60. function getStation(type, cb) {
  61. stations.forEach(function(station) {
  62. if (station.type === type) {
  63. cb(station);
  64. return;
  65. }
  66. });
  67. }
  68. function createRoom(display, tag) {
  69. var type = tag;
  70. if (Rooms.find({type: type}).count() === 0) {
  71. Rooms.insert({display: display, type: type, users: 0}, function(err) {
  72. if (err) {
  73. throw err;
  74. } else {
  75. if (Playlists.find({type: type}).count() === 1) {
  76. stations.push(new Station(type));
  77. } else {
  78. Playlists.insert({type: type, songs: getSongsByType(type)}, function (err2) {
  79. if (err2) {
  80. throw err2;
  81. } else {
  82. stations.push(new Station(type));
  83. }
  84. });
  85. }
  86. }
  87. });
  88. } else {
  89. return "Room already exists";
  90. }
  91. }
  92. function Station(type) {
  93. Meteor.publish(type, function() {
  94. return undefined;
  95. });
  96. var _this = this;
  97. var startedAt = Date.now();
  98. var playlist = Playlists.findOne({type: type});
  99. var songs = playlist.songs;
  100. if (playlist.lastSong === undefined) {
  101. Playlists.update({type: type}, {$set: {lastSong: 0}});
  102. playlist = Playlists.findOne({type: type});
  103. songs = playlist.songs;
  104. }
  105. var currentSong = playlist.lastSong;
  106. if (currentSong < (songs.length - 1)) {
  107. currentSong++;
  108. } else currentSong = 0;
  109. var currentTitle = songs[currentSong].title;
  110. Rooms.update({type: type}, {$set: {currentSong: {song: songs[currentSong], started: startedAt}, users: 0}});
  111. this.skipSong = function() {
  112. _this.voted = [];
  113. voteNum = 0;
  114. Rooms.update({type: type}, {$set: {votes: 0}});
  115. songs = Playlists.findOne({type: type}).songs;
  116. songs.forEach(function(song, index) {
  117. if (song.title === currentTitle) {
  118. currentSong = index;
  119. }
  120. });
  121. if (currentSong < (songs.length - 1)) {
  122. currentSong++;
  123. } else currentSong = 0;
  124. if (songs);
  125. if (currentSong === 0) {
  126. this.shufflePlaylist();
  127. } else {
  128. if (songs[currentSong].mid === undefined) {
  129. var newSong = songs[currentSong];
  130. newSong.mid = createUniqueSongId();
  131. songs[currentSong].mid = newSong.mid;
  132. Playlists.update({type: type, "songs": songs[currentSong]}, {$set: {"songs.$": newSong}});
  133. }
  134. currentTitle = songs[currentSong].title;
  135. Playlists.update({type: type}, {$set: {lastSong: currentSong}});
  136. Rooms.update({type: type}, {$set: {timePaused: 0}});
  137. this.songTimer();
  138. Rooms.update({type: type}, {$set: {currentSong: {song: songs[currentSong], started: startedAt}}});
  139. }
  140. };
  141. this.shufflePlaylist = function() {
  142. voteNum = 0;
  143. Rooms.update({type: type}, {$set: {votes: 0}});
  144. _this.voted = [];
  145. songs = Playlists.findOne({type: type}).songs;
  146. currentSong = 0;
  147. Playlists.update({type: type}, {$set: {"songs": []}});
  148. songs = shuffle(songs);
  149. songs.forEach(function(song) {
  150. if (song.mid === undefined) {
  151. song.mid = createUniqueSongId();
  152. }
  153. Playlists.update({type: type}, {$push: {"songs": song}});
  154. });
  155. currentTitle = songs[currentSong].title;
  156. Playlists.update({type: type}, {$set: {lastSong: currentSong}});
  157. Rooms.update({type: type}, {$set: {timePaused: 0}});
  158. this.songTimer();
  159. Rooms.update({type: type}, {$set: {currentSong: {song: songs[currentSong], started: startedAt}}});
  160. };
  161. Rooms.update({type: type}, {$set: {timePaused: 0}});
  162. var timer;
  163. this.songTimer = function() {
  164. startedAt = Date.now();
  165. if (timer !== undefined) {
  166. timer.pause();
  167. }
  168. timer = new Timer(function() {
  169. _this.skipSong();
  170. }, songs[currentSong].duration * 1000);
  171. };
  172. var state = Rooms.findOne({type: type}).state;
  173. this.pauseRoom = function() {
  174. if (state !== "paused") {
  175. timer.pause();
  176. Rooms.update({type: type}, {$set: {state: "paused"}});
  177. state = "paused";
  178. }
  179. };
  180. this.resumeRoom = function() {
  181. if (state !== "playing") {
  182. timer.resume();
  183. Rooms.update({type: type}, {$set: {state: "playing", timePaused: timer.timeWhenPaused()}});
  184. state = "playing";
  185. }
  186. };
  187. this.cancelTimer = function() {
  188. timer.pause();
  189. };
  190. this.getState = function() {
  191. return state;
  192. };
  193. this.type = type;
  194. this.songTimer();
  195. this.voted = [];
  196. }
  197. function shuffle(array) {
  198. var currentIndex = array.length, temporaryValue, randomIndex ;
  199. // While there remain elements to shuffle...
  200. while (0 !== currentIndex) {
  201. // Pick a remaining element...
  202. randomIndex = Math.floor(Math.random() * currentIndex);
  203. currentIndex -= 1;
  204. // And swap it with the current element.
  205. temporaryValue = array[currentIndex];
  206. array[currentIndex] = array[randomIndex];
  207. array[randomIndex] = temporaryValue;
  208. }
  209. return array;
  210. }
  211. function Timer(callback, delay) {
  212. var timerId, start, remaining = delay;
  213. var timeWhenPaused = 0;
  214. var timePaused = new Date();
  215. this.pause = function() {
  216. Meteor.clearTimeout(timerId);
  217. remaining -= new Date() - start;
  218. timePaused = new Date();
  219. };
  220. this.resume = function() {
  221. start = new Date();
  222. Meteor.clearTimeout(timerId);
  223. timerId = Meteor.setTimeout(callback, remaining);
  224. timeWhenPaused += new Date() - timePaused;
  225. };
  226. this.timeWhenPaused = function() {
  227. return timeWhenPaused;
  228. };
  229. this.resume();
  230. }
  231. Meteor.users.deny({update: function () { return true; }});
  232. Meteor.users.deny({insert: function () { return true; }});
  233. Meteor.users.deny({remove: function () { return true; }});
  234. function getSongDuration(query, artistName){
  235. var duration;
  236. var search = query;
  237. query = query.toLowerCase().split(" ").join("%20");
  238. var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + query + '&type=track');
  239. for(var i in res.data){
  240. for(var j in res.data[i].items){
  241. if(search.indexOf(res.data[i].items[j].name) !== -1 && artistName.indexOf(res.data[i].items[j].artists[0].name) !== -1){
  242. duration = res.data[i].items[j].duration_ms / 1000;
  243. return duration;
  244. }
  245. }
  246. }
  247. }
  248. function getSongAlbumArt(query, artistName){
  249. var albumart;
  250. var search = query;
  251. query = query.toLowerCase().split(" ").join("%20");
  252. var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + 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. var time = new Date();
  420. var rawrank = user.profile.rank;
  421. var username = user.profile.username;
  422. var rank = user.profile.rank;
  423. if (message.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. if (user.profile.rank = "admin") {
  430. Chat.insert({type: type, rawrank: rawrank, rank: "[A]", message: message, time: time, username: username});
  431. return true;
  432. } else if (user.profile.rank = "mod") {
  433. Chat.insert({type: type, rank: "[M]", message: message, time: time, username: username});
  434. return true;
  435. }
  436. else {
  437. Chat.insert({type: type, rawrank: rawrank, message: message, time: time, username: username});
  438. return true;
  439. }
  440. } else {
  441. throw new Meteor.Error(403, "Invalid permissions.");
  442. }
  443. },
  444. likeSong: function(mid) {
  445. if (Meteor.userId()) {
  446. var user = Meteor.user();
  447. if (user.profile.liked.indexOf(mid) === -1) {
  448. Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.liked": mid}});
  449. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": 1}})
  450. } else {
  451. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
  452. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": -1}})
  453. }
  454. if (user.profile.disliked.indexOf(mid) !== -1) {
  455. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
  456. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": -1}})
  457. }
  458. return true;
  459. } else {
  460. throw new Meteor.Error(403, "Invalid permissions.");
  461. }
  462. },
  463. dislikeSong: function(mid) {
  464. if (Meteor.userId()) {
  465. var user = Meteor.user();
  466. if (user.profile.disliked.indexOf(mid) === -1) {
  467. Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.disliked": mid}});
  468. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": 1}});
  469. } else {
  470. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
  471. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": -1}});
  472. }
  473. if (user.profile.liked.indexOf(mid) !== -1) {
  474. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
  475. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": -1}});
  476. }
  477. return true;
  478. } else {
  479. throw new Meteor.Error(403, "Invalid permissions.");
  480. }
  481. },
  482. voteSkip: function(type){
  483. if(Meteor.userId()){
  484. var user = Meteor.user();
  485. getStation(type, function(station){
  486. if(station.voted.indexOf(user.profile.username) === -1){
  487. station.voted.push(user.profile.username);
  488. Rooms.update({type: type}, {$set: {votes: station.voted.length}});
  489. if(station.voted.length === 3){
  490. station.skipSong();
  491. }
  492. } else{
  493. throw new Meteor.Error(401, "Already voted.");
  494. }
  495. })
  496. }
  497. },
  498. submitReport: function(report, id) {
  499. var obj = report;
  500. obj.id = id;
  501. Reports.insert(obj);
  502. },
  503. shufflePlaylist: function(type) {
  504. if (isAdmin()) {
  505. getStation(type, function(station) {
  506. if (station === undefined) {
  507. throw new Meteor.Error(404, "Station not found.");
  508. } else {
  509. station.cancelTimer();
  510. station.shufflePlaylist();
  511. }
  512. });
  513. }
  514. },
  515. skipSong: function(type) {
  516. if (isAdmin()) {
  517. getStation(type, function(station) {
  518. if (station === undefined) {
  519. throw new Meteor.Error(404, "Station not found.");
  520. } else {
  521. station.skipSong();
  522. }
  523. });
  524. }
  525. },
  526. pauseRoom: function(type) {
  527. if (isAdmin()) {
  528. getStation(type, function(station) {
  529. if (station === undefined) {
  530. throw new Meteor.Error(403, "Room doesn't exist.");
  531. } else {
  532. station.pauseRoom();
  533. }
  534. });
  535. } else {
  536. throw new Meteor.Error(403, "Invalid permissions.");
  537. }
  538. },
  539. resumeRoom: function(type) {
  540. if (isAdmin()) {
  541. getStation(type, function(station) {
  542. if (station === undefined) {
  543. throw new Meteor.Error(403, "Room doesn't exist.");
  544. } else {
  545. station.resumeRoom();
  546. }
  547. });
  548. } else {
  549. throw new Meteor.Error(403, "Invalid permissions.");
  550. }
  551. },
  552. createUserMethod: function(formData, captchaData) {
  553. var verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(this.connection.clientAddress, captchaData);
  554. if (!verifyCaptchaResponse.success) {
  555. console.log('reCAPTCHA check failed!', verifyCaptchaResponse);
  556. throw new Meteor.Error(422, 'reCAPTCHA Failed: ' + verifyCaptchaResponse.error);
  557. } else {
  558. console.log('reCAPTCHA verification passed!');
  559. Accounts.createUser({
  560. username: formData.username,
  561. email: formData.email,
  562. password: formData.password
  563. });
  564. }
  565. return true;
  566. },
  567. addSongToQueue: function(type, songData) {
  568. if (Meteor.userId()) {
  569. type = type.toLowerCase();
  570. if (Rooms.find({type: type}).count() === 1) {
  571. if (Queues.find({type: type}).count() === 0) {
  572. Queues.insert({type: type, songs: []});
  573. }
  574. if (songData !== undefined && Object.keys(songData).length === 5 && songData.type !== undefined && songData.title !== undefined && songData.title !== undefined && songData.artist !== undefined && songData.img !== undefined) {
  575. songData.duration = getSongDuration(songData.title, songData.artist);
  576. songData.img = getSongAlbumArt(songData.title, songData.artist);
  577. songData.skipDuration = 0;
  578. var mid = createUniqueSongId();
  579. if (mid !== undefined) {
  580. songData.mid = mid;
  581. Queues.update({type: type}, {
  582. $push: {
  583. songs: {
  584. id: songData.id,
  585. mid: songData.mid,
  586. title: songData.title,
  587. artist: songData.artist,
  588. duration: songData.duration,
  589. skipDuration: songData.skipDuration,
  590. img: songData.img,
  591. type: songData.type
  592. }
  593. }
  594. });
  595. return true;
  596. } else {
  597. throw new Meteor.Error(500, "Am error occured.");
  598. }
  599. } else {
  600. throw new Meteor.Error(403, "Invalid data.");
  601. }
  602. } else {
  603. throw new Meteor.Error(403, "Invalid genre.");
  604. }
  605. } else {
  606. throw new Meteor.Error(403, "Invalid permissions.");
  607. }
  608. },
  609. updateQueueSong: function(genre, oldSong, newSong) {
  610. if (isAdmin()) {
  611. newSong.mid = oldSong.mid;
  612. Queues.update({type: genre, "songs": oldSong}, {$set: {"songs.$": newSong}});
  613. return true;
  614. } else {
  615. throw new Meteor.Error(403, "Invalid permissions.");
  616. }
  617. },
  618. updatePlaylistSong: function(genre, oldSong, newSong) {
  619. if (isAdmin()) {
  620. newSong.mid = oldSong.mid;
  621. Playlists.update({type: genre, "songs": oldSong}, {$set: {"songs.$": newSong}});
  622. return true;
  623. } else {
  624. throw new Meteor.Error(403, "Invalid permissions.");
  625. }
  626. },
  627. removeSongFromQueue: function(type, mid) {
  628. if (isAdmin()) {
  629. type = type.toLowerCase();
  630. Queues.update({type: type}, {$pull: {songs: {mid: mid}}});
  631. } else {
  632. throw new Meteor.Error(403, "Invalid permissions.");
  633. }
  634. },
  635. removeSongFromPlaylist: function(type, mid) {
  636. if (isAdmin()) {
  637. type = type.toLowerCase();
  638. Playlists.update({type: type}, {$pull: {songs: {mid: mid}}});
  639. } else {
  640. throw new Meteor.Error(403, "Invalid permissions.");
  641. }
  642. },
  643. addSongToPlaylist: function(type, songData) {
  644. if (isAdmin()) {
  645. type = type.toLowerCase();
  646. if (Rooms.find({type: type}).count() === 1) {
  647. if (Playlists.find({type: type}).count() === 0) {
  648. Playlists.insert({type: type, songs: []});
  649. }
  650. console.log(songData);
  651. 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) {
  652. songData.likes = 0;
  653. songData.dislikes = 0
  654. Playlists.update({type: type}, {
  655. $push: {
  656. songs: {
  657. id: songData.id,
  658. mid: songData.mid,
  659. title: songData.title,
  660. artist: songData.artist,
  661. duration: songData.duration,
  662. skipDuration: songData.skipDuration,
  663. img: songData.img,
  664. type: songData.type,
  665. likes: songData.likes,
  666. dislikes: songData.dislikes
  667. }
  668. }
  669. });
  670. Queues.update({type: type}, {$pull: {songs: {mid: songData.mid}}});
  671. return true;
  672. } else {
  673. throw new Meteor.Error(403, "Invalid data.");
  674. }
  675. } else {
  676. throw new Meteor.Error(403, "Invalid genre.");
  677. }
  678. } else {
  679. throw new Meteor.Error(403, "Invalid permissions.");
  680. }
  681. },
  682. createRoom: function(display, tag) {
  683. if (isAdmin()) {
  684. createRoom(display, tag);
  685. } else {
  686. throw new Meteor.Error(403, "Invalid permissions.");
  687. }
  688. },
  689. deleteRoom: function(type){
  690. if (isAdmin()) {
  691. Rooms.remove({type: type});
  692. Playlists.remove({type: type});
  693. Queues.remove({type: type});
  694. return true;
  695. } else {
  696. throw new Meteor.Error(403, "Invalid permissions.");
  697. }
  698. },
  699. getUserNum: function(){
  700. return Object.keys(Meteor.default_server.sessions).length;
  701. }
  702. });
  703. Meteor.setInterval(function() {
  704. checkUsersPR();
  705. }, 10000);