server.js 28 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. 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. 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.artist !== undefined && songData.img !== undefined) {
  575. songData.duration = getSongDuration(songData.title, songData.artist) || 0;
  576. songData.img = getSongAlbumArt(songData.title, songData.artist) || "";
  577. songData.skipDuration = 0;
  578. songData.likes = 0;
  579. songData.dislikes = 0;
  580. var mid = createUniqueSongId();
  581. if (mid !== undefined) {
  582. songData.mid = mid;
  583. Queues.update({type: type}, {
  584. $push: {
  585. songs: {
  586. id: songData.id,
  587. mid: songData.mid,
  588. title: songData.title,
  589. artist: songData.artist,
  590. duration: songData.duration,
  591. skipDuration: songData.skipDuration,
  592. likes: songData.likes,
  593. dislikes: songData.dislikes,
  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: Number(songData.likes),
  673. dislikes: Number(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);