server.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. Meteor.startup(function() {
  2. reCAPTCHA.config({
  3. privatekey: '6LcVxg0TAAAAAI2fgIEEWHFxwNXeVIs8mzq5cfRM'
  4. });
  5. Avatar.setOptions({
  6. fallbackType: "initials",
  7. defaultImageUrl: "http://static.boredpanda.com/blog/wp-content/uploads/2014/04/amazing-fox-photos-182.jpg",
  8. generateCSS: true,
  9. imageSizes: {
  10. 'header': 40
  11. }
  12. });
  13. var stations = [{tag: "edm", display: "EDM"}, {tag: "pop", display: "Pop"}]; //Rooms to be set on server startup
  14. for(var i in stations){
  15. if(Rooms.find({type: stations[i]}).count() === 0){
  16. createRoom(stations[i].display, stations[i].tag, false);
  17. }
  18. }
  19. emojione.ascii = true;
  20. });
  21. Alerts.update({active: true}, {$set: {active: false}}, { multi: true });
  22. var stations = [];
  23. var voteNum = 0;
  24. var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_";
  25. function createUniqueSongId() {
  26. var code = "";
  27. for (var i = 0; i < 6; i++) {
  28. code += chars[Math.floor(Math.random() * chars.length)];
  29. }
  30. if (Playlists.find({"songs.mid": code}).count() > 0) {
  31. return createUniqueSongId();
  32. } else {
  33. return code;
  34. }
  35. }
  36. function checkUsersPR() {
  37. var output = {};
  38. var connections = Meteor.server.stream_server.open_sockets;
  39. _.each(connections,function(connection){
  40. // named subscriptions
  41. if (connection._meteorSession !== undefined && connection._meteorSession !== null) {
  42. var subs = connection._meteorSession._namedSubs;
  43. //var ip = connection.remoteAddress;
  44. var used_subs = [];
  45. for (var sub in subs) {
  46. var mySubName = subs[sub]._name;
  47. if (subs[sub]._params.length > 0) {
  48. mySubName += subs[sub]._params[0]; // assume one id parameter for now
  49. }
  50. if (used_subs.indexOf(mySubName) === -1) {
  51. used_subs.push(mySubName);
  52. if (!output[mySubName]) {
  53. output[mySubName] = 1;
  54. } else {
  55. output[mySubName] += 1;
  56. }
  57. }
  58. }
  59. }
  60. // there are also these 'universal subscriptions'
  61. //not sure what these are, i count none in my tests
  62. //var usubs = connection._meteorSession._universalSubs;
  63. });
  64. for (var key in output) {
  65. getStation(key, function() {
  66. Rooms.update({type: key}, {$set: {users: output[key]}});
  67. });
  68. }
  69. return output;
  70. }
  71. function getStation(type, cb) {
  72. stations.forEach(function(station) {
  73. if (station.type === type) {
  74. cb(station);
  75. return;
  76. }
  77. });
  78. }
  79. function createRoom(display, tag, private) {
  80. var type = tag;
  81. if (Rooms.find({type: type}).count() === 0 && private === false) {
  82. Rooms.insert({display: display, type: type, users: 0}, function(err) {
  83. if (err) {
  84. throw err;
  85. } else {
  86. if (Playlists.find({type: type}).count() === 1) {
  87. stations.push(new Station(type));
  88. } else {
  89. Playlists.insert({type: type, songs: getSongsByType(type)}, function (err2) {
  90. if (err2) {
  91. throw err2;
  92. } else {
  93. stations.push(new Station(type));
  94. }
  95. });
  96. }
  97. }
  98. });
  99. } else if (Rooms.find({type: type}).count() === 0 && private === true) {
  100. Rooms.insert({display: display, type: type, users: 0, private: true}, function(err) {
  101. if (err) {
  102. throw err;
  103. } else {
  104. if (Playlists.find({type: type}).count() === 1) {
  105. stations.push(new Station(type));
  106. } else {
  107. Playlists.insert({type: type, songs: getSongsByType(type)}, function (err2) {
  108. if (err2) {
  109. throw err2;
  110. } else {
  111. stations.push(new Station(type));
  112. }
  113. });
  114. }
  115. }
  116. });
  117. } else {
  118. return "Room already exists";
  119. }
  120. }
  121. function Station(type) {
  122. Meteor.publish(type, function() {
  123. return undefined;
  124. });
  125. var self = this;
  126. var startedAt = Date.now();
  127. var playlist = Playlists.findOne({type: type});
  128. var songs = playlist.songs;
  129. if (playlist.lastSong === undefined) {
  130. Playlists.update({type: type}, {$set: {lastSong: 0}});
  131. playlist = Playlists.findOne({type: type});
  132. songs = playlist.songs;
  133. }
  134. var currentSong = playlist.lastSong;
  135. if (currentSong < (songs.length - 1)) {
  136. currentSong++;
  137. } else currentSong = 0;
  138. var currentTitle = songs[currentSong].title;
  139. Rooms.update({type: type}, {$set: {currentSong: {song: songs[currentSong], started: startedAt}, users: 0}});
  140. this.skipSong = function() {
  141. self.voted = [];
  142. voteNum = 0;
  143. Rooms.update({type: type}, {$set: {votes: 0}});
  144. songs = Playlists.findOne({type: type}).songs;
  145. songs.forEach(function(song, index) {
  146. if (song.title === currentTitle) {
  147. currentSong = index;
  148. }
  149. });
  150. if (currentSong < (songs.length - 1)) {
  151. currentSong++;
  152. } else currentSong = 0;
  153. if (songs);
  154. if (currentSong === 0) {
  155. this.shufflePlaylist();
  156. } else {
  157. if (songs[currentSong].mid === undefined) {
  158. var newSong = songs[currentSong];
  159. newSong.mid = createUniqueSongId();
  160. songs[currentSong].mid = newSong.mid;
  161. Playlists.update({type: type, "songs": songs[currentSong]}, {$set: {"songs.$": newSong}});
  162. }
  163. currentTitle = songs[currentSong].title;
  164. Playlists.update({type: type}, {$set: {lastSong: currentSong}});
  165. Rooms.update({type: type}, {$set: {timePaused: 0}});
  166. this.songTimer();
  167. Rooms.update({type: type}, {$set: {currentSong: {song: songs[currentSong], started: startedAt}}});
  168. }
  169. };
  170. this.shufflePlaylist = function() {
  171. voteNum = 0;
  172. Rooms.update({type: type}, {$set: {votes: 0}});
  173. self.voted = [];
  174. songs = Playlists.findOne({type: type}).songs;
  175. currentSong = 0;
  176. Playlists.update({type: type}, {$set: {"songs": []}});
  177. songs = shuffle(songs);
  178. songs.forEach(function(song) {
  179. if (song.mid === undefined) {
  180. song.mid = createUniqueSongId();
  181. }
  182. Playlists.update({type: type}, {$push: {"songs": song}});
  183. });
  184. currentTitle = songs[currentSong].title;
  185. Playlists.update({type: type}, {$set: {lastSong: currentSong}});
  186. Rooms.update({type: type}, {$set: {timePaused: 0}});
  187. this.songTimer();
  188. Rooms.update({type: type}, {$set: {currentSong: {song: songs[currentSong], started: startedAt}}});
  189. };
  190. Rooms.update({type: type}, {$set: {timePaused: 0}});
  191. var timer;
  192. this.songTimer = function() {
  193. startedAt = Date.now();
  194. if (timer !== undefined) {
  195. timer.pause();
  196. }
  197. timer = new Timer(function() {
  198. self.skipSong();
  199. }, songs[currentSong].duration * 1000);
  200. };
  201. var state = Rooms.findOne({type: type}).state;
  202. this.pauseRoom = function() {
  203. if (state !== "paused") {
  204. timer.pause();
  205. Rooms.update({type: type}, {$set: {state: "paused"}});
  206. state = "paused";
  207. }
  208. };
  209. this.resumeRoom = function() {
  210. if (state !== "playing") {
  211. timer.resume();
  212. Rooms.update({type: type}, {$set: {state: "playing", timePaused: timer.timeWhenPaused()}});
  213. state = "playing";
  214. }
  215. };
  216. this.cancelTimer = function() {
  217. timer.pause();
  218. };
  219. this.getState = function() {
  220. return state;
  221. };
  222. this.type = type;
  223. var private = Rooms.findOne({type: type}).private;
  224. if (typeof private !== "boolean") {
  225. Rooms.update({type: type}, {$set: {"private": false}});
  226. private = false;
  227. }
  228. this.private = private;
  229. this.unlock = function() {
  230. if (self.private) {
  231. self.private = false;
  232. Rooms.update({type: type}, {$set: {"private": false}});
  233. }
  234. };
  235. this.lock = function() {
  236. if (!self.private) {
  237. self.private = true;
  238. Rooms.update({type: type}, {$set: {"private": true}});
  239. }
  240. };
  241. this.songTimer();
  242. this.voted = [];
  243. }
  244. function shuffle(array) {
  245. var currentIndex = array.length, temporaryValue, randomIndex ;
  246. // While there remain elements to shuffle...
  247. while (0 !== currentIndex) {
  248. // Pick a remaining element...
  249. randomIndex = Math.floor(Math.random() * currentIndex);
  250. currentIndex -= 1;
  251. // And swap it with the current element.
  252. temporaryValue = array[currentIndex];
  253. array[currentIndex] = array[randomIndex];
  254. array[randomIndex] = temporaryValue;
  255. }
  256. return array;
  257. }
  258. function Timer(callback, delay) {
  259. var timerId, start, remaining = delay;
  260. var timeWhenPaused = 0;
  261. var timePaused = new Date();
  262. this.pause = function() {
  263. Meteor.clearTimeout(timerId);
  264. remaining -= new Date() - start;
  265. timePaused = new Date();
  266. };
  267. this.resume = function() {
  268. start = new Date();
  269. Meteor.clearTimeout(timerId);
  270. timerId = Meteor.setTimeout(callback, remaining);
  271. timeWhenPaused += new Date() - timePaused;
  272. };
  273. this.timeWhenPaused = function() {
  274. return timeWhenPaused;
  275. };
  276. this.resume();
  277. }
  278. Meteor.users.deny({update: function () { return true; }});
  279. Meteor.users.deny({insert: function () { return true; }});
  280. Meteor.users.deny({remove: function () { return true; }});
  281. function getSongDuration(query, artistName){
  282. var duration;
  283. var search = query;
  284. var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + encodeURIComponent(query) + '&type=track');
  285. for(var i in res.data){
  286. for(var j in res.data[i].items){
  287. if(search.indexOf(res.data[i].items[j].name) !== -1 && artistName.indexOf(res.data[i].items[j].artists[0].name) !== -1){
  288. duration = res.data[i].items[j].duration_ms / 1000;
  289. return duration;
  290. }
  291. }
  292. }
  293. }
  294. function getSongAlbumArt(query, artistName){
  295. var albumart;
  296. var search = query;
  297. var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + encodeURIComponent(query) + '&type=track');
  298. for(var i in res.data){
  299. for(var j in res.data[i].items){
  300. if(search.indexOf(res.data[i].items[j].name) !== -1 && artistName.indexOf(res.data[i].items[j].artists[0].name) !== -1){
  301. albumart = res.data[i].items[j].album.images[1].url
  302. return albumart;
  303. }
  304. }
  305. }
  306. }
  307. //var room_types = ["edm", "nightcore"];
  308. var songsArr = [];
  309. function getSongsByType(type) {
  310. if (type === "edm") {
  311. return [
  312. {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"},
  313. {id: "aHjpOzsQ9YI", mid: "goG88g", title: "Crystallize", artist: "Lindsey Stirling", duration: getSongDuration("Crystallize", "Lindsey Stirling"), type: "YouTube", img: "https://i.scdn.co/image/b0c1ccdd0cd7bcda741ccc1c3e036f4ed2e52312"}
  314. ];
  315. } else if (type === "nightcore") {
  316. 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"}];
  317. } else {
  318. 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"}];
  319. }
  320. }
  321. Rooms.find({}).fetch().forEach(function(room) {
  322. var type = room.type;
  323. if (Playlists.find({type: type}).count() === 0) {
  324. if (type === "edm") {
  325. Playlists.insert({type: type, songs: getSongsByType(type)});
  326. } else if (type === "nightcore") {
  327. Playlists.insert({type: type, songs: getSongsByType(type)});
  328. } else {
  329. Playlists.insert({type: type, songs: getSongsByType(type)});
  330. }
  331. }
  332. if (Playlists.findOne({type: type}).songs.length === 0) {
  333. // Add a global video to Playlist so it can proceed
  334. } else {
  335. stations.push(new Station(type));
  336. }
  337. });
  338. Accounts.validateNewUser(function(user) {
  339. var username;
  340. if (user.services) {
  341. if (user.services.github) {
  342. username = user.services.github.username;
  343. } else if (user.services.facebook) {
  344. username = user.services.facebook.first_name;
  345. } else if (user.services.password) {
  346. username = user.username;
  347. }
  348. }
  349. if (Meteor.users.find({"profile.usernameL": username.toLowerCase()}).count() !== 0) {
  350. throw new Meteor.Error(403, "An account with that username already exists.");
  351. } else {
  352. return true;
  353. }
  354. });
  355. Accounts.onCreateUser(function(options, user) {
  356. var username;
  357. if (user.services) {
  358. if (user.services.github) {
  359. username = user.services.github.username;
  360. } else if (user.services.facebook) {
  361. username = user.services.facebook.first_name;
  362. } else if (user.services.password) {
  363. username = user.username;
  364. }
  365. }
  366. user.profile = {username: username, usernameL: username.toLowerCase(), rank: "default", liked: [], disliked: [], settings: {showRating: false}};
  367. return user;
  368. });
  369. Meteor.publish("alerts", function() {
  370. return Alerts.find({active: true})
  371. });
  372. Meteor.publish("userData", function(userId) {
  373. if (userId !== undefined) {
  374. return Meteor.users.find(userId, {fields: {"services.github.username": 1, "punishments": 1}})
  375. } else {
  376. return undefined;
  377. }
  378. });
  379. Meteor.publish("allAlerts", function() {
  380. return Alerts.find({active: false})
  381. });
  382. Meteor.publish("playlists", function() {
  383. return Playlists.find({})
  384. });
  385. Meteor.publish("rooms", function() {
  386. return Rooms.find({});
  387. });
  388. Meteor.publish("queues", function() {
  389. return Queues.find({});
  390. });
  391. Meteor.publish("reports", function() {
  392. return Reports.find({});
  393. });
  394. Meteor.publish("chat", function() {
  395. return Chat.find({});
  396. });
  397. Meteor.publish("userProfiles", function(username) {
  398. var settings = Meteor.users.findOne({"profile.usernameL": username}, {fields: {"profile.settings": 1}});
  399. if (settings !== undefined && settings.profile.settings) {
  400. settings = settings.profile.settings;
  401. if (settings.showRating === true) {
  402. 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}});
  403. }
  404. }
  405. return Meteor.users.find({"profile.usernameL": username}, {fields: {"profile.username": 1, "profile.usernameL": 1, "profile.rank": 1, createdAt: 1, "profile.settings": 1}});
  406. });
  407. Meteor.publish("isAdmin", function() {
  408. return Meteor.users.find({_id: this.userId, "profile.rank": "admin"});
  409. });
  410. Meteor.publish("isModerator", function() {
  411. return Meteor.users.find({_id: this.userId, "profile.rank": "moderator"});
  412. });
  413. function isAdmin() {
  414. var userData = Meteor.users.find(Meteor.userId());
  415. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
  416. return true;
  417. } else {
  418. return false;
  419. }
  420. }
  421. function isModerator() {
  422. var userData = Meteor.users.find(Meteor.userId());
  423. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "moderator") {
  424. return true;
  425. } else {
  426. return isAdmin();
  427. }
  428. }
  429. function isBanned() {
  430. var userData = Meteor.users.findOne(Meteor.userId());
  431. if (Meteor.userId() && userData !== undefined && userData.punishments !== undefined && userData.punishments.ban !== undefined) {
  432. var ban = userData.punishments.ban;
  433. if (new Date(ban.bannedUntil).getTime() <= new Date().getTime()) {
  434. Meteor.users.update(Meteor.userId(), {$unset: {"punishments.ban": ""}});
  435. return false;
  436. } else {
  437. return true;
  438. }
  439. } else {
  440. return false;
  441. }
  442. }
  443. function isMuted() {
  444. var userData = Meteor.users.findOne(Meteor.userId());
  445. if (Meteor.userId() && userData !== undefined && userData.punishments !== undefined && userData.punishments.mute !== undefined) {
  446. var mute = userData.punishments.mute;
  447. if (new Date(mute.bannedUntil).getTime() <= new Date().getTime()) {
  448. Meteor.users.update(Meteor.userId(), {$unset: {"punishments.mute": ""}});
  449. return false;
  450. } else {
  451. return true;
  452. }
  453. } else {
  454. return false;
  455. }
  456. }
  457. Meteor.methods({
  458. lockRoom: function(type) {
  459. if (isAdmin() && !isBanned()) {
  460. getStation(type, function(station){
  461. station.lock();
  462. });
  463. } else {
  464. throw new Meteor.Error(403, "Invalid permissions.");
  465. }
  466. },
  467. unlockRoom: function(type) {
  468. if (isAdmin() && !isBanned()) {
  469. getStation(type, function(station){
  470. station.unlock();
  471. });
  472. } else {
  473. throw new Meteor.Error(403, "Invalid permissions.");
  474. }
  475. },
  476. banUser: function(username, period, reason) {
  477. if (isAdmin() && !isBanned()) {
  478. var user = Meteor.user();
  479. var bannedUser = Meteor.users.findOne({"profile.usernameL": username.toLowerCase()});
  480. var bannedUntil = (new Date).getTime() + (period * 1000);
  481. if (bannedUntil > 8640000000000000) {
  482. bannedUntil = 8640000000000000;
  483. }
  484. bannedUntil = new Date(bannedUntil);
  485. var banObject = {bannedBy: user.profile.usernameL, bannedAt: new Date(Date.now()), bannedReason: reason, bannedUntil: bannedUntil};
  486. Meteor.users.update({"profile.usernameL": bannedUser.profile.usernameL}, {$set: {"punishments.ban": banObject}});
  487. Meteor.users.update({"profile.usernameL": bannedUser.profile.usernameL}, {$push: {"punishments.bans": banObject}});
  488. } else {
  489. throw new Meteor.Error(403, "Invalid permissions.");
  490. }
  491. },
  492. muteUser: function(username, period) {
  493. if (isAdmin() && !isBanned()) {
  494. var user = Meteor.user();
  495. var mutedUser = Meteor.users.findOne({"profile.usernameL": username.toLowerCase()});
  496. if (period === undefined || Number(period) === 0) {
  497. mutedUntil = 8640000000000000;
  498. } else {
  499. var mutedUntil = (new Date).getTime() + (period * 1000);
  500. if (mutedUntil > 8640000000000000) {
  501. mutedUntil = 8640000000000000;
  502. }
  503. }
  504. mutedUntil = new Date(mutedUntil);
  505. var muteObject = {mutedBy: user.profile.usernameL, mutedAt: new Date(Date.now()), mutedUntil: mutedUntil};
  506. Meteor.users.update({"profile.usernameL": mutedUser.profile.usernameL}, {$set: {"punishments.mute": muteObject}});
  507. Meteor.users.update({"profile.usernameL": mutedUser.profile.usernameL}, {$push: {"punishments.mutes": muteObject}});
  508. } else {
  509. throw new Meteor.Error(403, "Invalid permissions.");
  510. }
  511. },
  512. isBanned: function() {
  513. return isBanned();
  514. },
  515. isMuted: function() {
  516. return isMuted();
  517. },
  518. updateSettings: function(showRating) {
  519. if (Meteor.userId() && !isBanned()) {
  520. var user = Meteor.user();
  521. if (showRating !== true && showRating !== false) {
  522. showRating = false;
  523. }
  524. if (user.profile.settings) {
  525. Meteor.users.update({"profile.username": user.profile.username}, {$set: {"profile.settings.showRating": showRating}});
  526. } else {
  527. Meteor.users.update({"profile.username": user.profile.username}, {$set: {"profile.settings": {showRating: showRating}}});
  528. }
  529. } else {
  530. throw new Meteor.Error(403, "Invalid permissions.");
  531. }
  532. },
  533. resetRating: function() {
  534. if (isAdmin() && !isBanned()) {
  535. stations.forEach(function (station) {
  536. var type = station.type;
  537. var temp_songs = Playlists.findOne({type: type}).songs;
  538. Playlists.update({type: type}, {$set: {"songs": []}});
  539. temp_songs.forEach(function (song) {
  540. song.likes = 0;
  541. song.dislikes = 0;
  542. Playlists.update({type: type}, {$push: {"songs": song}});
  543. });
  544. });
  545. Meteor.users.update({}, {$set: {"profile.liked": [], "profile.disliked": []}}, {multi: true});
  546. } else {
  547. throw Meteor.Error(403, "Invalid permissions.");
  548. }
  549. },
  550. removeAlerts: function() {
  551. if (isAdmin() && !isBanned()) {
  552. Alerts.update({active: true}, {$set: {active: false}}, { multi: true });
  553. } else {
  554. throw Meteor.Error(403, "Invalid permissions.");
  555. }
  556. },
  557. addAlert: function(description, priority) {
  558. if (isAdmin()) {
  559. if (description.length > 0 && description.length < 400) {
  560. var username = Meteor.user().profile.username;
  561. if (["danger", "warning", "success", "primary"].indexOf(priority) === -1) {
  562. priority = "warning";
  563. }
  564. Alerts.insert({description: description, priority: priority, active: true, createdBy: username});
  565. return true;
  566. } else {
  567. throw Meteor.Error(403, "Invalid description length.");
  568. }
  569. } else {
  570. throw Meteor.Error(403, "Invalid permissions.");
  571. }
  572. },
  573. sendMessage: function(type, message) {
  574. if (Meteor.userId() && !isBanned() && !isMuted()) {
  575. var user = Meteor.user();
  576. var time = new Date();
  577. var rawrank = user.profile.rank;
  578. var username = user.profile.username;
  579. var profanity = false;
  580. if (!message.replace(/\s/g, "").length > 0) {
  581. throw new Meteor.Error(406, "Message length cannot be 0.");
  582. }
  583. if (message.length > 300) {
  584. throw new Meteor.Error(406, "Message length cannot be more than 300 characters long..");
  585. }
  586. else if (user.profile.rank === "admin") {
  587. HTTP.call("GET", "http://www.wdyl.com/profanity?q=" + encodeURIComponent(message), function(err,res){
  588. if(res.content.indexOf("true") > -1){
  589. return true;
  590. } else{
  591. Chat.insert({type: type, rawrank: rawrank, rank: "[A]", message: message, time: time, username: username});
  592. }
  593. });
  594. return true;
  595. }
  596. else if (user.profile.rank === "moderator") {
  597. HTTP.call("GET", "http://www.wdyl.com/profanity?q=" + encodeURIComponent(message), function(err,res){
  598. if(res.content.indexOf("true") > -1){
  599. return true;
  600. } else{
  601. Chat.insert({type: type, rawrank: rawrank, rank: "[M]", message: message, time: time, username: username});
  602. }
  603. });
  604. return true;
  605. }
  606. else {
  607. HTTP.call("GET", "http://www.wdyl.com/profanity?q=" + encodeURIComponent(message), function(err,res){
  608. if(res.content.indexOf("true") > -1){
  609. return true;
  610. } else{
  611. Chat.insert({type: type, rawrank: rawrank, rank: "", message: message, time: time, username: username});
  612. }
  613. });
  614. return true;
  615. }
  616. } else {
  617. throw new Meteor.Error(403, "Invalid permissions.");
  618. }
  619. },
  620. likeSong: function(mid) {
  621. if (Meteor.userId() && !isBanned()) {
  622. var user = Meteor.user();
  623. if (user.profile.liked.indexOf(mid) === -1) {
  624. Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.liked": mid}});
  625. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": 1}})
  626. } else {
  627. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
  628. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": -1}})
  629. }
  630. if (user.profile.disliked.indexOf(mid) !== -1) {
  631. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
  632. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": -1}})
  633. }
  634. return true;
  635. } else {
  636. throw new Meteor.Error(403, "Invalid permissions.");
  637. }
  638. },
  639. dislikeSong: function(mid) {
  640. if (Meteor.userId() && !isBanned()) {
  641. var user = Meteor.user();
  642. if (user.profile.disliked.indexOf(mid) === -1) {
  643. Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.disliked": mid}});
  644. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": 1}});
  645. } else {
  646. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
  647. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": -1}});
  648. }
  649. if (user.profile.liked.indexOf(mid) !== -1) {
  650. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
  651. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": -1}});
  652. }
  653. return true;
  654. } else {
  655. throw new Meteor.Error(403, "Invalid permissions.");
  656. }
  657. },
  658. voteSkip: function(type){
  659. if(Meteor.userId() && !isBanned()){
  660. var user = Meteor.user();
  661. getStation(type, function(station){
  662. if(station.voted.indexOf(user.profile.username) === -1){
  663. station.voted.push(user.profile.username);
  664. Rooms.update({type: type}, {$set: {votes: station.voted.length}});
  665. if(station.voted.length === 3){
  666. station.skipSong();
  667. }
  668. } else{
  669. throw new Meteor.Error(401, "Already voted.");
  670. }
  671. })
  672. }
  673. },
  674. submitReport: function(room, reportData) {
  675. if (Meteor.userId() && !isBanned()) {
  676. room = room.toLowerCase();
  677. if (Rooms.find({type: room}).count() === 1) {
  678. if (Reports.find({room: room}).count() === 0) {
  679. Reports.insert({room: room, report: []});
  680. }
  681. if (reportData !== undefined) {
  682. Reports.update({room: room}, {
  683. $push: {
  684. report: {
  685. song: reportData.song,
  686. type: reportData.type,
  687. reason: reportData.reason,
  688. other: reportData.other
  689. }
  690. }
  691. });
  692. return true;
  693. } else {
  694. throw new Meteor.Error(403, "Invalid data.");
  695. }
  696. } else {
  697. throw new Meteor.Error(403, "Invalid genre.");
  698. }
  699. } else {
  700. throw new Meteor.Error(403, "Invalid permissions.");
  701. }
  702. },
  703. shufflePlaylist: function(type) {
  704. if (isAdmin() && !isBanned()) {
  705. getStation(type, function(station) {
  706. if (station === undefined) {
  707. throw new Meteor.Error(404, "Station not found.");
  708. } else {
  709. station.cancelTimer();
  710. station.shufflePlaylist();
  711. }
  712. });
  713. }
  714. },
  715. skipSong: function(type) {
  716. if (isAdmin() && !isBanned()) {
  717. getStation(type, function(station) {
  718. if (station === undefined) {
  719. throw new Meteor.Error(404, "Station not found.");
  720. } else {
  721. station.skipSong();
  722. }
  723. });
  724. }
  725. },
  726. pauseRoom: function(type) {
  727. if (isAdmin() && !isBanned()) {
  728. getStation(type, function(station) {
  729. if (station === undefined) {
  730. throw new Meteor.Error(403, "Room doesn't exist.");
  731. } else {
  732. station.pauseRoom();
  733. }
  734. });
  735. } else {
  736. throw new Meteor.Error(403, "Invalid permissions.");
  737. }
  738. },
  739. resumeRoom: function(type) {
  740. if (isAdmin() && !isBanned()) {
  741. getStation(type, function(station) {
  742. if (station === undefined) {
  743. throw new Meteor.Error(403, "Room doesn't exist.");
  744. } else {
  745. station.resumeRoom();
  746. }
  747. });
  748. } else {
  749. throw new Meteor.Error(403, "Invalid permissions.");
  750. }
  751. },
  752. createUserMethod: function(formData, captchaData) {
  753. if (!isBanned()) {
  754. var verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(this.connection.clientAddress, captchaData);
  755. if (!verifyCaptchaResponse.success) {
  756. throw new Meteor.Error(422, 'reCAPTCHA Failed: ' + verifyCaptchaResponse.error);
  757. } else {
  758. Accounts.createUser({
  759. username: formData.username,
  760. email: formData.email,
  761. password: formData.password
  762. });
  763. }
  764. return true;
  765. }
  766. },
  767. addSongToQueue: function(type, songData) {
  768. if (Meteor.userId() && !isBanned()) {
  769. type = type.toLowerCase();
  770. if (Rooms.find({type: type}).count() === 1) {
  771. if (Queues.find({type: type}).count() === 0) {
  772. Queues.insert({type: type, songs: []});
  773. }
  774. if (songData !== undefined && Object.keys(songData).length === 5 && songData.type !== undefined && songData.title !== undefined && songData.artist !== undefined && songData.img !== undefined) {
  775. songData.duration = getSongDuration(songData.title, songData.artist) || 0;
  776. songData.img = getSongAlbumArt(songData.title, songData.artist) || "";
  777. songData.skipDuration = 0;
  778. songData.likes = 0;
  779. songData.dislikes = 0;
  780. var mid = createUniqueSongId();
  781. if (mid !== undefined) {
  782. songData.mid = mid;
  783. Queues.update({type: type}, {
  784. $push: {
  785. songs: {
  786. id: songData.id,
  787. mid: songData.mid,
  788. title: songData.title,
  789. artist: songData.artist,
  790. duration: songData.duration,
  791. skipDuration: songData.skipDuration,
  792. likes: songData.likes,
  793. dislikes: songData.dislikes,
  794. img: songData.img,
  795. type: songData.type
  796. }
  797. }
  798. });
  799. return true;
  800. } else {
  801. throw new Meteor.Error(500, "Am error occured.");
  802. }
  803. } else {
  804. throw new Meteor.Error(403, "Invalid data.");
  805. }
  806. } else {
  807. throw new Meteor.Error(403, "Invalid genre.");
  808. }
  809. } else {
  810. throw new Meteor.Error(403, "Invalid permissions.");
  811. }
  812. },
  813. updateQueueSong: function(genre, oldSong, newSong) {
  814. if (isModerator() && !isBanned()) {
  815. newSong.mid = oldSong.mid;
  816. Queues.update({type: genre, "songs": oldSong}, {$set: {"songs.$": newSong}});
  817. return true;
  818. } else {
  819. throw new Meteor.Error(403, "Invalid permissions.");
  820. }
  821. },
  822. updatePlaylistSong: function(genre, oldSong, newSong) {
  823. if (isModerator() && !isBanned()) {
  824. newSong.mid = oldSong.mid;
  825. Playlists.update({type: genre, "songs": oldSong}, {$set: {"songs.$": newSong}});
  826. return true;
  827. } else {
  828. throw new Meteor.Error(403, "Invalid permissions.");
  829. }
  830. },
  831. removeSongFromQueue: function(type, mid) {
  832. if (isModerator() && !isBanned()) {
  833. type = type.toLowerCase();
  834. Queues.update({type: type}, {$pull: {songs: {mid: mid}}});
  835. } else {
  836. throw new Meteor.Error(403, "Invalid permissions.");
  837. }
  838. },
  839. removeSongFromPlaylist: function(type, mid) {
  840. if (isModerator() && !isBanned()) {
  841. type = type.toLowerCase();
  842. var songs = Playlists.findOne({type: type}).songs;
  843. var song = undefined;
  844. songs.forEach(function(curr_song) {
  845. if (mid === curr_song.mid) {
  846. song = curr_song;
  847. return;
  848. }
  849. });
  850. Playlists.update({type: type}, {$pull: {songs: {mid: mid}}});
  851. if (song !== undefined) {
  852. song.deletedBy = Meteor.userId();
  853. song.deletedAt = new Date(Date.now());
  854. if (Deleted.find({type: type}).count() === 0) {
  855. Deleted.insert({type: type, songs: [song]});
  856. } else {
  857. Deleted.update({type: type}, {$push: {songs: song}});
  858. }
  859. }
  860. } else {
  861. throw new Meteor.Error(403, "Invalid permissions.");
  862. }
  863. },
  864. addSongToPlaylist: function(type, songData) {
  865. if (isModerator() && !isBanned()) {
  866. type = type.toLowerCase();
  867. if (Rooms.find({type: type}).count() === 1) {
  868. if (Playlists.find({type: type}).count() === 0) {
  869. Playlists.insert({type: type, songs: []});
  870. }
  871. var requiredProperties = ["type", "mid", "id", "title", "artist", "duration", "skipDuration", "img", "likes", "dislikes"];
  872. if (songData !== undefined && Object.keys(songData).length === requiredProperties.length) {
  873. for (var property in requiredProperties) {
  874. if (songData[requiredProperties[property]] === undefined) {
  875. throw new Meteor.Error(403, "Invalid data.");
  876. }
  877. }
  878. Playlists.update({type: type}, {
  879. $push: {
  880. songs: {
  881. id: songData.id,
  882. mid: songData.mid,
  883. title: songData.title,
  884. artist: songData.artist,
  885. duration: songData.duration,
  886. skipDuration: Number(songData.skipDuration),
  887. img: songData.img,
  888. type: songData.type,
  889. likes: Number(songData.likes),
  890. dislikes: Number(songData.dislikes)
  891. }
  892. }
  893. });
  894. Queues.update({type: type}, {$pull: {songs: {mid: songData.mid}}});
  895. return true;
  896. } else {
  897. throw new Meteor.Error(403, "Invalid data.");
  898. }
  899. } else {
  900. throw new Meteor.Error(403, "Invalid genre.");
  901. }
  902. } else {
  903. throw new Meteor.Error(403, "Invalid permissions.");
  904. }
  905. },
  906. createRoom: function(display, tag, private) {
  907. if (isAdmin() && !isBanned()) {
  908. createRoom(display, tag, private);
  909. } else {
  910. throw new Meteor.Error(403, "Invalid permissions.");
  911. }
  912. },
  913. deleteRoom: function(type){
  914. if (isAdmin() && !isBanned()) {
  915. Rooms.remove({type: type});
  916. Playlists.remove({type: type});
  917. Queues.remove({type: type});
  918. return true;
  919. } else {
  920. throw new Meteor.Error(403, "Invalid permissions.");
  921. }
  922. },
  923. getUserNum: function(){
  924. if (!isBanned()) {
  925. return Object.keys(Meteor.default_server.sessions).length;
  926. }
  927. }
  928. });
  929. Meteor.setInterval(function() {
  930. checkUsersPR();
  931. }, 10000);