server.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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: [], settings: {showRating: false}};
  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(username) {
  362. var settings = Meteor.users.findOne({"profile.usernameL": username}, {fields: {"profile.settings": 1}});
  363. if (settings !== undefined && settings.profile.settings) {
  364. settings = settings.profile.settings;
  365. if (settings.showRating === true) {
  366. 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}});
  367. }
  368. }
  369. return Meteor.users.find({"profile.usernameL": username}, {fields: {"profile.username": 1, "profile.usernameL": 1, "profile.rank": 1, createdAt: 1, "profile.settings": 1}});
  370. });
  371. Meteor.publish("isAdmin", function() {
  372. return Meteor.users.find({_id: this.userId, "profile.rank": "admin"});
  373. });
  374. function isAdmin() {
  375. var userData = Meteor.users.find(Meteor.userId());
  376. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
  377. return true;
  378. } else {
  379. return false;
  380. }
  381. }
  382. Meteor.methods({
  383. updateSettings: function(showRating) {
  384. if (Meteor.userId()) {
  385. var user = Meteor.user();
  386. console.log(showRating);
  387. if (showRating !== true && showRating !== false) {
  388. showRating = false;
  389. }
  390. console.log(showRating);
  391. if (user.profile.settings) {
  392. Meteor.users.update({"profile.username": user.profile.username}, {$set: {"profile.settings.showRating": showRating}});
  393. } else {
  394. Meteor.users.update({"profile.username": user.profile.username}, {$set: {"profile.settings": {showRating: showRating}}});
  395. }
  396. } else {
  397. throw new Meteor.Error(403, "Invalid permissions.");
  398. }
  399. },
  400. resetRating: function() {
  401. if (isAdmin()) {
  402. stations.forEach(function (station) {
  403. var type = station.type;
  404. var temp_songs = Playlists.findOne({type: type}).songs;
  405. Playlists.update({type: type}, {$set: {"songs": []}});
  406. temp_songs.forEach(function (song) {
  407. song.likes = 0;
  408. song.dislikes = 0;
  409. Playlists.update({type: type}, {$push: {"songs": song}});
  410. });
  411. });
  412. Meteor.users.update({}, {$set: {"profile.liked": [], "profile.disliked": []}}, {multi: true});
  413. } else {
  414. throw Meteor.Error(403, "Invalid permissions.");
  415. }
  416. },
  417. removeAlerts: function() {
  418. if (isAdmin()) {
  419. Alerts.update({active: true}, {$set: {active: false}}, { multi: true });
  420. } else {
  421. throw Meteor.Error(403, "Invalid permissions.");
  422. }
  423. },
  424. addAlert: function(description, priority) {
  425. if (isAdmin()) {
  426. if (description.length > 0 && description.length < 400) {
  427. var username = Meteor.user().profile.username;
  428. if (["danger", "warning", "success", "primary"].indexOf(priority) === -1) {
  429. priority = "warning";
  430. }
  431. Alerts.insert({description: description, priority: priority, active: true, createdBy: username});
  432. return true;
  433. } else {
  434. throw Meteor.Error(403, "Invalid description length.");
  435. }
  436. } else {
  437. throw Meteor.Error(403, "Invalid permissions.");
  438. }
  439. },
  440. sendMessage: function(type, message) {
  441. if (Meteor.userId()) {
  442. var user = Meteor.user();
  443. var time = new Date();
  444. var rawrank = user.profile.rank;
  445. var username = user.profile.username;
  446. var rank = user.profile.rank;
  447. if (!message.replace(/\s/g, "").length > 0) {
  448. throw new Meteor.Error(406, "Message length cannot be 0.");
  449. }
  450. if (message.length > 300) {
  451. throw new Meteor.Error(406, "Message length cannot be more than 300 characters long..");
  452. }
  453. if (user.profile.rank = "admin") {
  454. Chat.insert({type: type, rawrank: rawrank, rank: "[A]", message: message, time: time, username: username});
  455. return true;
  456. } else if (user.profile.rank = "mod") {
  457. Chat.insert({type: type, rank: "[M]", message: message, time: time, username: username});
  458. return true;
  459. }
  460. else {
  461. Chat.insert({type: type, rawrank: rawrank, message: message, time: time, username: username});
  462. return true;
  463. }
  464. } else {
  465. throw new Meteor.Error(403, "Invalid permissions.");
  466. }
  467. },
  468. likeSong: function(mid) {
  469. if (Meteor.userId()) {
  470. var user = Meteor.user();
  471. if (user.profile.liked.indexOf(mid) === -1) {
  472. Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.liked": mid}});
  473. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": 1}})
  474. } else {
  475. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
  476. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": -1}})
  477. }
  478. if (user.profile.disliked.indexOf(mid) !== -1) {
  479. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
  480. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": -1}})
  481. }
  482. return true;
  483. } else {
  484. throw new Meteor.Error(403, "Invalid permissions.");
  485. }
  486. },
  487. dislikeSong: function(mid) {
  488. if (Meteor.userId()) {
  489. var user = Meteor.user();
  490. if (user.profile.disliked.indexOf(mid) === -1) {
  491. Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.disliked": mid}});
  492. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": 1}});
  493. } else {
  494. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
  495. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": -1}});
  496. }
  497. if (user.profile.liked.indexOf(mid) !== -1) {
  498. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
  499. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": -1}});
  500. }
  501. return true;
  502. } else {
  503. throw new Meteor.Error(403, "Invalid permissions.");
  504. }
  505. },
  506. voteSkip: function(type){
  507. if(Meteor.userId()){
  508. var user = Meteor.user();
  509. getStation(type, function(station){
  510. if(station.voted.indexOf(user.profile.username) === -1){
  511. station.voted.push(user.profile.username);
  512. Rooms.update({type: type}, {$set: {votes: station.voted.length}});
  513. if(station.voted.length === 3){
  514. station.skipSong();
  515. }
  516. } else{
  517. throw new Meteor.Error(401, "Already voted.");
  518. }
  519. })
  520. }
  521. },
  522. submitReport: function(report, id) {
  523. var obj = report;
  524. obj.id = id;
  525. Reports.insert(obj);
  526. },
  527. shufflePlaylist: function(type) {
  528. if (isAdmin()) {
  529. getStation(type, function(station) {
  530. if (station === undefined) {
  531. throw new Meteor.Error(404, "Station not found.");
  532. } else {
  533. station.cancelTimer();
  534. station.shufflePlaylist();
  535. }
  536. });
  537. }
  538. },
  539. skipSong: function(type) {
  540. if (isAdmin()) {
  541. getStation(type, function(station) {
  542. if (station === undefined) {
  543. throw new Meteor.Error(404, "Station not found.");
  544. } else {
  545. station.skipSong();
  546. }
  547. });
  548. }
  549. },
  550. pauseRoom: function(type) {
  551. if (isAdmin()) {
  552. getStation(type, function(station) {
  553. if (station === undefined) {
  554. throw new Meteor.Error(403, "Room doesn't exist.");
  555. } else {
  556. station.pauseRoom();
  557. }
  558. });
  559. } else {
  560. throw new Meteor.Error(403, "Invalid permissions.");
  561. }
  562. },
  563. resumeRoom: function(type) {
  564. if (isAdmin()) {
  565. getStation(type, function(station) {
  566. if (station === undefined) {
  567. throw new Meteor.Error(403, "Room doesn't exist.");
  568. } else {
  569. station.resumeRoom();
  570. }
  571. });
  572. } else {
  573. throw new Meteor.Error(403, "Invalid permissions.");
  574. }
  575. },
  576. createUserMethod: function(formData, captchaData) {
  577. var verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(this.connection.clientAddress, captchaData);
  578. if (!verifyCaptchaResponse.success) {
  579. console.log('reCAPTCHA check failed!', verifyCaptchaResponse);
  580. throw new Meteor.Error(422, 'reCAPTCHA Failed: ' + verifyCaptchaResponse.error);
  581. } else {
  582. console.log('reCAPTCHA verification passed!');
  583. Accounts.createUser({
  584. username: formData.username,
  585. email: formData.email,
  586. password: formData.password
  587. });
  588. }
  589. return true;
  590. },
  591. addSongToQueue: function(type, songData) {
  592. if (Meteor.userId()) {
  593. type = type.toLowerCase();
  594. if (Rooms.find({type: type}).count() === 1) {
  595. if (Queues.find({type: type}).count() === 0) {
  596. Queues.insert({type: type, songs: []});
  597. }
  598. if (songData !== undefined && Object.keys(songData).length === 5 && songData.type !== undefined && songData.title !== undefined && songData.artist !== undefined && songData.img !== undefined) {
  599. songData.duration = getSongDuration(songData.title, songData.artist) || 0;
  600. songData.img = getSongAlbumArt(songData.title, songData.artist) || "";
  601. songData.skipDuration = 0;
  602. songData.likes = 0;
  603. songData.dislikes = 0;
  604. var mid = createUniqueSongId();
  605. if (mid !== undefined) {
  606. songData.mid = mid;
  607. Queues.update({type: type}, {
  608. $push: {
  609. songs: {
  610. id: songData.id,
  611. mid: songData.mid,
  612. title: songData.title,
  613. artist: songData.artist,
  614. duration: songData.duration,
  615. skipDuration: songData.skipDuration,
  616. likes: songData.likes,
  617. dislikes: songData.dislikes,
  618. img: songData.img,
  619. type: songData.type
  620. }
  621. }
  622. });
  623. return true;
  624. } else {
  625. throw new Meteor.Error(500, "Am error occured.");
  626. }
  627. } else {
  628. throw new Meteor.Error(403, "Invalid data.");
  629. }
  630. } else {
  631. throw new Meteor.Error(403, "Invalid genre.");
  632. }
  633. } else {
  634. throw new Meteor.Error(403, "Invalid permissions.");
  635. }
  636. },
  637. updateQueueSong: function(genre, oldSong, newSong) {
  638. if (isAdmin()) {
  639. newSong.mid = oldSong.mid;
  640. Queues.update({type: genre, "songs": oldSong}, {$set: {"songs.$": newSong}});
  641. return true;
  642. } else {
  643. throw new Meteor.Error(403, "Invalid permissions.");
  644. }
  645. },
  646. updatePlaylistSong: function(genre, oldSong, newSong) {
  647. if (isAdmin()) {
  648. newSong.mid = oldSong.mid;
  649. Playlists.update({type: genre, "songs": oldSong}, {$set: {"songs.$": newSong}});
  650. return true;
  651. } else {
  652. throw new Meteor.Error(403, "Invalid permissions.");
  653. }
  654. },
  655. removeSongFromQueue: function(type, mid) {
  656. if (isAdmin()) {
  657. type = type.toLowerCase();
  658. Queues.update({type: type}, {$pull: {songs: {mid: mid}}});
  659. } else {
  660. throw new Meteor.Error(403, "Invalid permissions.");
  661. }
  662. },
  663. removeSongFromPlaylist: function(type, mid) {
  664. if (isAdmin()) {
  665. type = type.toLowerCase();
  666. Playlists.update({type: type}, {$pull: {songs: {mid: mid}}});
  667. } else {
  668. throw new Meteor.Error(403, "Invalid permissions.");
  669. }
  670. },
  671. addSongToPlaylist: function(type, songData) {
  672. if (isAdmin()) {
  673. type = type.toLowerCase();
  674. if (Rooms.find({type: type}).count() === 1) {
  675. if (Playlists.find({type: type}).count() === 0) {
  676. Playlists.insert({type: type, songs: []});
  677. }
  678. var requiredProperties = ["type", "mid", "id", "title", "artist", "duration", "skipDuration", "img", "likes", "dislikes"];
  679. if (songData !== undefined && Object.keys(songData).length === requiredProperties.length) {
  680. for (var property in requiredProperties) {
  681. if (songData[requiredProperties[property]] === undefined) {
  682. throw new Meteor.Error(403, "Invalid data.");
  683. }
  684. }
  685. Playlists.update({type: type}, {
  686. $push: {
  687. songs: {
  688. id: songData.id,
  689. mid: songData.mid,
  690. title: songData.title,
  691. artist: songData.artist,
  692. duration: songData.duration,
  693. skipDuration: songData.skipDuration,
  694. img: songData.img,
  695. type: songData.type,
  696. likes: Number(songData.likes),
  697. dislikes: Number(songData.dislikes)
  698. }
  699. }
  700. });
  701. Queues.update({type: type}, {$pull: {songs: {mid: songData.mid}}});
  702. return true;
  703. } else {
  704. throw new Meteor.Error(403, "Invalid data.");
  705. }
  706. } else {
  707. throw new Meteor.Error(403, "Invalid genre.");
  708. }
  709. } else {
  710. throw new Meteor.Error(403, "Invalid permissions.");
  711. }
  712. },
  713. createRoom: function(display, tag) {
  714. if (isAdmin()) {
  715. createRoom(display, tag);
  716. } else {
  717. throw new Meteor.Error(403, "Invalid permissions.");
  718. }
  719. },
  720. deleteRoom: function(type){
  721. if (isAdmin()) {
  722. Rooms.remove({type: type});
  723. Playlists.remove({type: type});
  724. Queues.remove({type: type});
  725. return true;
  726. } else {
  727. throw new Meteor.Error(403, "Invalid permissions.");
  728. }
  729. },
  730. getUserNum: function(){
  731. return Object.keys(Meteor.default_server.sessions).length;
  732. }
  733. });
  734. Meteor.setInterval(function() {
  735. checkUsersPR();
  736. }, 10000);