server.js 30 KB

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