server.js 31 KB

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