server.js 29 KB

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