server.js 28 KB

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