server.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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) {
  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. Meteor.methods({
  444. lockRoom: function(type) {
  445. if (isAdmin() && !isBanned()) {
  446. getStation(type, function(station){
  447. station.lock();
  448. });
  449. } else {
  450. throw new Meteor.Error(403, "Invalid permissions.");
  451. }
  452. },
  453. unlockRoom: function(type) {
  454. if (isAdmin() && !isBanned()) {
  455. getStation(type, function(station){
  456. station.unlock();
  457. });
  458. } else {
  459. throw new Meteor.Error(403, "Invalid permissions.");
  460. }
  461. },
  462. banUser: function(username, period, reason) {
  463. if (isAdmin() && !isBanned()) {
  464. var user = Meteor.user();
  465. var bannedUser = Meteor.users.findOne({"profile.usernameL": username.toLowerCase()});
  466. var bannedUntil = (new Date).getTime() + (period * 1000);
  467. if (bannedUntil > 8640000000000000) {
  468. bannedUntil = 8640000000000000;
  469. }
  470. bannedUntil = new Date(bannedUntil);
  471. var banObject = {bannedBy: user.profile.usernameL, bannedAt: new Date(Date.now()), bannedReason: reason, bannedUntil: bannedUntil};
  472. Meteor.users.update({"profile.usernameL": bannedUser.profile.usernameL}, {$set: {"punishments.ban": banObject}});
  473. Meteor.users.update({"profile.usernameL": bannedUser.profile.usernameL}, {$push: {"punishments.bans": banObject}});
  474. } else {
  475. throw new Meteor.Error(403, "Invalid permissions.");
  476. }
  477. },
  478. isBanned: function() {
  479. return isBanned();
  480. },
  481. updateSettings: function(showRating) {
  482. if (Meteor.userId() && !isBanned()) {
  483. var user = Meteor.user();
  484. if (showRating !== true && showRating !== false) {
  485. showRating = false;
  486. }
  487. if (user.profile.settings) {
  488. Meteor.users.update({"profile.username": user.profile.username}, {$set: {"profile.settings.showRating": showRating}});
  489. } else {
  490. Meteor.users.update({"profile.username": user.profile.username}, {$set: {"profile.settings": {showRating: showRating}}});
  491. }
  492. } else {
  493. throw new Meteor.Error(403, "Invalid permissions.");
  494. }
  495. },
  496. resetRating: function() {
  497. if (isAdmin() && !isBanned()) {
  498. stations.forEach(function (station) {
  499. var type = station.type;
  500. var temp_songs = Playlists.findOne({type: type}).songs;
  501. Playlists.update({type: type}, {$set: {"songs": []}});
  502. temp_songs.forEach(function (song) {
  503. song.likes = 0;
  504. song.dislikes = 0;
  505. Playlists.update({type: type}, {$push: {"songs": song}});
  506. });
  507. });
  508. Meteor.users.update({}, {$set: {"profile.liked": [], "profile.disliked": []}}, {multi: true});
  509. } else {
  510. throw Meteor.Error(403, "Invalid permissions.");
  511. }
  512. },
  513. removeAlerts: function() {
  514. if (isAdmin() && !isBanned()) {
  515. Alerts.update({active: true}, {$set: {active: false}}, { multi: true });
  516. } else {
  517. throw Meteor.Error(403, "Invalid permissions.");
  518. }
  519. },
  520. addAlert: function(description, priority) {
  521. if (isAdmin()) {
  522. if (description.length > 0 && description.length < 400) {
  523. var username = Meteor.user().profile.username;
  524. if (["danger", "warning", "success", "primary"].indexOf(priority) === -1) {
  525. priority = "warning";
  526. }
  527. Alerts.insert({description: description, priority: priority, active: true, createdBy: username});
  528. return true;
  529. } else {
  530. throw Meteor.Error(403, "Invalid description length.");
  531. }
  532. } else {
  533. throw Meteor.Error(403, "Invalid permissions.");
  534. }
  535. },
  536. sendMessage: function(type, message) {
  537. if (Meteor.userId() && !isBanned()) {
  538. var user = Meteor.user();
  539. var time = new Date();
  540. var rawrank = user.profile.rank;
  541. var username = user.profile.username;
  542. var profanity = false;
  543. if (!message.replace(/\s/g, "").length > 0) {
  544. throw new Meteor.Error(406, "Message length cannot be 0.");
  545. }
  546. if (message.length > 300) {
  547. throw new Meteor.Error(406, "Message length cannot be more than 300 characters long..");
  548. }
  549. else if (user.profile.rank === "admin") {
  550. HTTP.call("GET", "http://www.wdyl.com/profanity?q=" + encodeURIComponent(message), function(err,res){
  551. if(res.content.indexOf("true") > -1){
  552. return true;
  553. } else{
  554. Chat.insert({type: type, rawrank: rawrank, rank: "[A]", message: message, time: time, username: username});
  555. }
  556. });
  557. return true;
  558. }
  559. else if (user.profile.rank === "moderator") {
  560. HTTP.call("GET", "http://www.wdyl.com/profanity?q=" + encodeURIComponent(message), function(err,res){
  561. if(res.content.indexOf("true") > -1){
  562. return true;
  563. } else{
  564. Chat.insert({type: type, rawrank: rawrank, rank: "[A]", message: message, time: time, username: username});
  565. }
  566. });
  567. return true;
  568. }
  569. else {
  570. HTTP.call("GET", "http://www.wdyl.com/profanity?q=" + encodeURIComponent(message), function(err,res){
  571. if(res.content.indexOf("true") > -1){
  572. return true;
  573. } else{
  574. Chat.insert({type: type, rawrank: rawrank, rank: "[A]", message: message, time: time, username: username});
  575. }
  576. });
  577. return true;
  578. }
  579. } else {
  580. throw new Meteor.Error(403, "Invalid permissions.");
  581. }
  582. },
  583. likeSong: function(mid) {
  584. if (Meteor.userId() && !isBanned()) {
  585. var user = Meteor.user();
  586. if (user.profile.liked.indexOf(mid) === -1) {
  587. Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.liked": mid}});
  588. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": 1}})
  589. } else {
  590. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
  591. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": -1}})
  592. }
  593. if (user.profile.disliked.indexOf(mid) !== -1) {
  594. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
  595. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": -1}})
  596. }
  597. return true;
  598. } else {
  599. throw new Meteor.Error(403, "Invalid permissions.");
  600. }
  601. },
  602. dislikeSong: function(mid) {
  603. if (Meteor.userId() && !isBanned()) {
  604. var user = Meteor.user();
  605. if (user.profile.disliked.indexOf(mid) === -1) {
  606. Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.disliked": mid}});
  607. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": 1}});
  608. } else {
  609. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
  610. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": -1}});
  611. }
  612. if (user.profile.liked.indexOf(mid) !== -1) {
  613. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
  614. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": -1}});
  615. }
  616. return true;
  617. } else {
  618. throw new Meteor.Error(403, "Invalid permissions.");
  619. }
  620. },
  621. voteSkip: function(type){
  622. if(Meteor.userId() && !isBanned()){
  623. var user = Meteor.user();
  624. getStation(type, function(station){
  625. if(station.voted.indexOf(user.profile.username) === -1){
  626. station.voted.push(user.profile.username);
  627. Rooms.update({type: type}, {$set: {votes: station.voted.length}});
  628. if(station.voted.length === 3){
  629. station.skipSong();
  630. }
  631. } else{
  632. throw new Meteor.Error(401, "Already voted.");
  633. }
  634. })
  635. }
  636. },
  637. submitReport: function(room, reportData) {
  638. if (Meteor.userId() && !isBanned()) {
  639. room = room.toLowerCase();
  640. if (Rooms.find({type: room}).count() === 1) {
  641. if (Reports.find({room: room}).count() === 0) {
  642. Reports.insert({room: room, report: []});
  643. }
  644. if (reportData !== undefined) {
  645. Reports.update({room: room}, {
  646. $push: {
  647. report: {
  648. song: reportData.song,
  649. type: reportData.type,
  650. reason: reportData.reason,
  651. other: reportData.other
  652. }
  653. }
  654. });
  655. return true;
  656. } else {
  657. throw new Meteor.Error(403, "Invalid data.");
  658. }
  659. } else {
  660. throw new Meteor.Error(403, "Invalid genre.");
  661. }
  662. } else {
  663. throw new Meteor.Error(403, "Invalid permissions.");
  664. }
  665. },
  666. shufflePlaylist: function(type) {
  667. if (isAdmin() && !isBanned()) {
  668. getStation(type, function(station) {
  669. if (station === undefined) {
  670. throw new Meteor.Error(404, "Station not found.");
  671. } else {
  672. station.cancelTimer();
  673. station.shufflePlaylist();
  674. }
  675. });
  676. }
  677. },
  678. skipSong: function(type) {
  679. if (isAdmin() && !isBanned()) {
  680. getStation(type, function(station) {
  681. if (station === undefined) {
  682. throw new Meteor.Error(404, "Station not found.");
  683. } else {
  684. station.skipSong();
  685. }
  686. });
  687. }
  688. },
  689. pauseRoom: function(type) {
  690. if (isAdmin() && !isBanned()) {
  691. getStation(type, function(station) {
  692. if (station === undefined) {
  693. throw new Meteor.Error(403, "Room doesn't exist.");
  694. } else {
  695. station.pauseRoom();
  696. }
  697. });
  698. } else {
  699. throw new Meteor.Error(403, "Invalid permissions.");
  700. }
  701. },
  702. resumeRoom: function(type) {
  703. if (isAdmin() && !isBanned()) {
  704. getStation(type, function(station) {
  705. if (station === undefined) {
  706. throw new Meteor.Error(403, "Room doesn't exist.");
  707. } else {
  708. station.resumeRoom();
  709. }
  710. });
  711. } else {
  712. throw new Meteor.Error(403, "Invalid permissions.");
  713. }
  714. },
  715. createUserMethod: function(formData, captchaData) {
  716. if (!isBanned()) {
  717. var verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(this.connection.clientAddress, captchaData);
  718. if (!verifyCaptchaResponse.success) {
  719. throw new Meteor.Error(422, 'reCAPTCHA Failed: ' + verifyCaptchaResponse.error);
  720. } else {
  721. Accounts.createUser({
  722. username: formData.username,
  723. email: formData.email,
  724. password: formData.password
  725. });
  726. }
  727. return true;
  728. }
  729. },
  730. addSongToQueue: function(type, songData) {
  731. if (Meteor.userId() && !isBanned()) {
  732. type = type.toLowerCase();
  733. if (Rooms.find({type: type}).count() === 1) {
  734. if (Queues.find({type: type}).count() === 0) {
  735. Queues.insert({type: type, songs: []});
  736. }
  737. if (songData !== undefined && Object.keys(songData).length === 5 && songData.type !== undefined && songData.title !== undefined && songData.artist !== undefined && songData.img !== undefined) {
  738. songData.duration = getSongDuration(songData.title, songData.artist) || 0;
  739. songData.img = getSongAlbumArt(songData.title, songData.artist) || "";
  740. songData.skipDuration = 0;
  741. songData.likes = 0;
  742. songData.dislikes = 0;
  743. var mid = createUniqueSongId();
  744. if (mid !== undefined) {
  745. songData.mid = mid;
  746. Queues.update({type: type}, {
  747. $push: {
  748. songs: {
  749. id: songData.id,
  750. mid: songData.mid,
  751. title: songData.title,
  752. artist: songData.artist,
  753. duration: songData.duration,
  754. skipDuration: songData.skipDuration,
  755. likes: songData.likes,
  756. dislikes: songData.dislikes,
  757. img: songData.img,
  758. type: songData.type
  759. }
  760. }
  761. });
  762. return true;
  763. } else {
  764. throw new Meteor.Error(500, "Am error occured.");
  765. }
  766. } else {
  767. throw new Meteor.Error(403, "Invalid data.");
  768. }
  769. } else {
  770. throw new Meteor.Error(403, "Invalid genre.");
  771. }
  772. } else {
  773. throw new Meteor.Error(403, "Invalid permissions.");
  774. }
  775. },
  776. updateQueueSong: function(genre, oldSong, newSong) {
  777. if (isModerator() && !isBanned()) {
  778. newSong.mid = oldSong.mid;
  779. Queues.update({type: genre, "songs": oldSong}, {$set: {"songs.$": newSong}});
  780. return true;
  781. } else {
  782. throw new Meteor.Error(403, "Invalid permissions.");
  783. }
  784. },
  785. updatePlaylistSong: function(genre, oldSong, newSong) {
  786. if (isModerator() && !isBanned()) {
  787. newSong.mid = oldSong.mid;
  788. Playlists.update({type: genre, "songs": oldSong}, {$set: {"songs.$": newSong}});
  789. return true;
  790. } else {
  791. throw new Meteor.Error(403, "Invalid permissions.");
  792. }
  793. },
  794. removeSongFromQueue: function(type, mid) {
  795. if (isModerator() && !isBanned()) {
  796. type = type.toLowerCase();
  797. Queues.update({type: type}, {$pull: {songs: {mid: mid}}});
  798. } else {
  799. throw new Meteor.Error(403, "Invalid permissions.");
  800. }
  801. },
  802. removeSongFromPlaylist: function(type, mid) {
  803. if (isModerator() && !isBanned()) {
  804. type = type.toLowerCase();
  805. var songs = Playlists.findOne({type: type}).songs;
  806. var song = undefined;
  807. songs.forEach(function(curr_song) {
  808. if (mid === curr_song.mid) {
  809. song = curr_song;
  810. return;
  811. }
  812. });
  813. Playlists.update({type: type}, {$pull: {songs: {mid: mid}}});
  814. if (song !== undefined) {
  815. song.deletedBy = Meteor.userId();
  816. song.deletedAt = new Date(Date.now());
  817. if (Deleted.find({type: type}).count() === 0) {
  818. Deleted.insert({type: type, songs: [song]});
  819. } else {
  820. Deleted.update({type: type}, {$push: {songs: song}});
  821. }
  822. }
  823. } else {
  824. throw new Meteor.Error(403, "Invalid permissions.");
  825. }
  826. },
  827. addSongToPlaylist: function(type, songData) {
  828. if (isModerator() && !isBanned()) {
  829. type = type.toLowerCase();
  830. if (Rooms.find({type: type}).count() === 1) {
  831. if (Playlists.find({type: type}).count() === 0) {
  832. Playlists.insert({type: type, songs: []});
  833. }
  834. var requiredProperties = ["type", "mid", "id", "title", "artist", "duration", "skipDuration", "img", "likes", "dislikes"];
  835. if (songData !== undefined && Object.keys(songData).length === requiredProperties.length) {
  836. for (var property in requiredProperties) {
  837. if (songData[requiredProperties[property]] === undefined) {
  838. throw new Meteor.Error(403, "Invalid data.");
  839. }
  840. }
  841. Playlists.update({type: type}, {
  842. $push: {
  843. songs: {
  844. id: songData.id,
  845. mid: songData.mid,
  846. title: songData.title,
  847. artist: songData.artist,
  848. duration: songData.duration,
  849. skipDuration: Number(songData.skipDuration),
  850. img: songData.img,
  851. type: songData.type,
  852. likes: Number(songData.likes),
  853. dislikes: Number(songData.dislikes)
  854. }
  855. }
  856. });
  857. Queues.update({type: type}, {$pull: {songs: {mid: songData.mid}}});
  858. return true;
  859. } else {
  860. throw new Meteor.Error(403, "Invalid data.");
  861. }
  862. } else {
  863. throw new Meteor.Error(403, "Invalid genre.");
  864. }
  865. } else {
  866. throw new Meteor.Error(403, "Invalid permissions.");
  867. }
  868. },
  869. createRoom: function(display, tag, private) {
  870. if (isAdmin() && !isBanned()) {
  871. createRoom(display, tag, private);
  872. } else {
  873. throw new Meteor.Error(403, "Invalid permissions.");
  874. }
  875. },
  876. deleteRoom: function(type){
  877. if (isAdmin() && !isBanned()) {
  878. Rooms.remove({type: type});
  879. Playlists.remove({type: type});
  880. Queues.remove({type: type});
  881. return true;
  882. } else {
  883. throw new Meteor.Error(403, "Invalid permissions.");
  884. }
  885. },
  886. getUserNum: function(){
  887. if (!isBanned()) {
  888. return Object.keys(Meteor.default_server.sessions).length;
  889. }
  890. },
  891. updateRealName: function(username, realname){
  892. Meteor.users.update({"profile.username": username}, {$set: {"profile.realname": realname}});
  893. },
  894. updateUserName: function(username, newUserName){
  895. Meteor.users.update({"username": username}, {$set: {"username": newUserName, "profile.username": newUserName, "profile.usernameL": newUserName.toLowerCase()}})
  896. }
  897. });
  898. Meteor.setInterval(function() {
  899. checkUsersPR();
  900. }, 10000);