server.js 31 KB

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