server.js 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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}, realname: ""};
  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, "profile.realname": 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, "profile.realname": 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. unbanUser: function(username) {
  513. if (isAdmin() && !isBanned()) {
  514. Meteor.users.update({"profile.usernameL": username.toLowerCase()}, {$unset: "punishments.ban"});
  515. } else {
  516. throw new Meteor.Error(403, "Invalid permissions.");
  517. }
  518. },
  519. unsilenceUser: function(username) {
  520. if (isAdmin() && !isBanned()) {
  521. Meteor.users.update({"profile.usernameL": username.toLowerCase()}, {$unset: "punishments.mute"});
  522. } else {
  523. throw new Meteor.Error(403, "Invalid permissions.");
  524. }
  525. },
  526. isBanned: function() {
  527. return isBanned();
  528. },
  529. isMuted: function() {
  530. return isMuted();
  531. },
  532. updateSettings: function(showRating) {
  533. if (Meteor.userId() && !isBanned()) {
  534. var user = Meteor.user();
  535. if (showRating !== true && showRating !== false) {
  536. showRating = false;
  537. }
  538. if (user.profile.settings) {
  539. Meteor.users.update({"profile.username": user.profile.username}, {$set: {"profile.settings.showRating": showRating}});
  540. } else {
  541. Meteor.users.update({"profile.username": user.profile.username}, {$set: {"profile.settings": {showRating: showRating}}});
  542. }
  543. } else {
  544. throw new Meteor.Error(403, "Invalid permissions.");
  545. }
  546. },
  547. resetRating: function() {
  548. if (isAdmin() && !isBanned()) {
  549. stations.forEach(function (station) {
  550. var type = station.type;
  551. var temp_songs = Playlists.findOne({type: type}).songs;
  552. Playlists.update({type: type}, {$set: {"songs": []}});
  553. temp_songs.forEach(function (song) {
  554. song.likes = 0;
  555. song.dislikes = 0;
  556. Playlists.update({type: type}, {$push: {"songs": song}});
  557. });
  558. });
  559. Meteor.users.update({}, {$set: {"profile.liked": [], "profile.disliked": []}}, {multi: true});
  560. } else {
  561. throw Meteor.Error(403, "Invalid permissions.");
  562. }
  563. },
  564. removeAlerts: function() {
  565. if (isAdmin() && !isBanned()) {
  566. Alerts.update({active: true}, {$set: {active: false}}, { multi: true });
  567. } else {
  568. throw Meteor.Error(403, "Invalid permissions.");
  569. }
  570. },
  571. addAlert: function(description, priority) {
  572. if (isAdmin()) {
  573. if (description.length > 0 && description.length < 400) {
  574. var username = Meteor.user().profile.username;
  575. if (["danger", "warning", "success", "primary"].indexOf(priority) === -1) {
  576. priority = "warning";
  577. }
  578. Alerts.insert({description: description, priority: priority, active: true, createdBy: username});
  579. return true;
  580. } else {
  581. throw Meteor.Error(403, "Invalid description length.");
  582. }
  583. } else {
  584. throw Meteor.Error(403, "Invalid permissions.");
  585. }
  586. },
  587. sendMessage: function(type, message) {
  588. if (Meteor.userId() && !isBanned() && !isMuted()) {
  589. var user = Meteor.user();
  590. var time = new Date();
  591. var rawrank = user.profile.rank;
  592. var username = user.profile.username;
  593. var profanity = false;
  594. if (!message.replace(/\s/g, "").length > 0) {
  595. throw new Meteor.Error(406, "Message length cannot be 0.");
  596. }
  597. if (message.length > 300) {
  598. throw new Meteor.Error(406, "Message length cannot be more than 300 characters long..");
  599. }
  600. else if (user.profile.rank === "admin") {
  601. HTTP.call("GET", "http://www.wdyl.com/profanity?q=" + encodeURIComponent(message), function(err,res){
  602. if(res.content.indexOf("true") > -1){
  603. return true;
  604. } else{
  605. Chat.insert({type: type, rawrank: rawrank, rank: "[A]", message: message, time: time, username: username});
  606. }
  607. });
  608. return true;
  609. }
  610. else if (user.profile.rank === "moderator") {
  611. HTTP.call("GET", "http://www.wdyl.com/profanity?q=" + encodeURIComponent(message), function(err,res){
  612. if(res.content.indexOf("true") > -1){
  613. return true;
  614. } else{
  615. Chat.insert({type: type, rawrank: rawrank, rank: "[M]", message: message, time: time, username: username});
  616. }
  617. });
  618. return true;
  619. }
  620. else {
  621. HTTP.call("GET", "http://www.wdyl.com/profanity?q=" + encodeURIComponent(message), function(err,res){
  622. if(res.content.indexOf("true") > -1){
  623. return true;
  624. } else{
  625. Chat.insert({type: type, rawrank: rawrank, rank: "", message: message, time: time, username: username});
  626. }
  627. });
  628. return true;
  629. }
  630. } else {
  631. throw new Meteor.Error(403, "Invalid permissions.");
  632. }
  633. },
  634. likeSong: function(mid) {
  635. if (Meteor.userId() && !isBanned()) {
  636. var user = Meteor.user();
  637. if (user.profile.liked.indexOf(mid) === -1) {
  638. Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.liked": mid}});
  639. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": 1}})
  640. } else {
  641. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
  642. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": -1}})
  643. }
  644. if (user.profile.disliked.indexOf(mid) !== -1) {
  645. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
  646. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": -1}})
  647. }
  648. return true;
  649. } else {
  650. throw new Meteor.Error(403, "Invalid permissions.");
  651. }
  652. },
  653. dislikeSong: function(mid) {
  654. if (Meteor.userId() && !isBanned()) {
  655. var user = Meteor.user();
  656. if (user.profile.disliked.indexOf(mid) === -1) {
  657. Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.disliked": mid}});
  658. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": 1}});
  659. } else {
  660. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
  661. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": -1}});
  662. }
  663. if (user.profile.liked.indexOf(mid) !== -1) {
  664. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
  665. Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": -1}});
  666. }
  667. return true;
  668. } else {
  669. throw new Meteor.Error(403, "Invalid permissions.");
  670. }
  671. },
  672. voteSkip: function(type){
  673. if(Meteor.userId() && !isBanned()){
  674. var user = Meteor.user();
  675. getStation(type, function(station){
  676. if(station.voted.indexOf(user.profile.username) === -1){
  677. station.voted.push(user.profile.username);
  678. Rooms.update({type: type}, {$set: {votes: station.voted.length}});
  679. if(station.voted.length === 3){
  680. station.skipSong();
  681. }
  682. } else{
  683. throw new Meteor.Error(401, "Already voted.");
  684. }
  685. })
  686. }
  687. },
  688. submitReport: function(room, reportData) {
  689. if (Meteor.userId() && !isBanned()) {
  690. room = room.toLowerCase();
  691. if (Rooms.find({type: room}).count() === 1) {
  692. if (Reports.find({room: room}).count() === 0) {
  693. Reports.insert({room: room, report: []});
  694. }
  695. if (reportData !== undefined) {
  696. Reports.update({room: room}, {
  697. $push: {
  698. report: {
  699. song: reportData.song,
  700. type: reportData.type,
  701. reason: reportData.reason,
  702. other: reportData.other
  703. }
  704. }
  705. });
  706. return true;
  707. } else {
  708. throw new Meteor.Error(403, "Invalid data.");
  709. }
  710. } else {
  711. throw new Meteor.Error(403, "Invalid genre.");
  712. }
  713. } else {
  714. throw new Meteor.Error(403, "Invalid permissions.");
  715. }
  716. },
  717. shufflePlaylist: function(type) {
  718. if (isAdmin() && !isBanned()) {
  719. getStation(type, function(station) {
  720. if (station === undefined) {
  721. throw new Meteor.Error(404, "Station not found.");
  722. } else {
  723. station.cancelTimer();
  724. station.shufflePlaylist();
  725. }
  726. });
  727. }
  728. },
  729. skipSong: function(type) {
  730. if (isAdmin() && !isBanned()) {
  731. getStation(type, function(station) {
  732. if (station === undefined) {
  733. throw new Meteor.Error(404, "Station not found.");
  734. } else {
  735. station.skipSong();
  736. }
  737. });
  738. }
  739. },
  740. pauseRoom: function(type) {
  741. if (isAdmin() && !isBanned()) {
  742. getStation(type, function(station) {
  743. if (station === undefined) {
  744. throw new Meteor.Error(403, "Room doesn't exist.");
  745. } else {
  746. station.pauseRoom();
  747. }
  748. });
  749. } else {
  750. throw new Meteor.Error(403, "Invalid permissions.");
  751. }
  752. },
  753. resumeRoom: function(type) {
  754. if (isAdmin() && !isBanned()) {
  755. getStation(type, function(station) {
  756. if (station === undefined) {
  757. throw new Meteor.Error(403, "Room doesn't exist.");
  758. } else {
  759. station.resumeRoom();
  760. }
  761. });
  762. } else {
  763. throw new Meteor.Error(403, "Invalid permissions.");
  764. }
  765. },
  766. createUserMethod: function(formData, captchaData) {
  767. if (!isBanned()) {
  768. var verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(this.connection.clientAddress, captchaData);
  769. if (!verifyCaptchaResponse.success) {
  770. throw new Meteor.Error(422, 'reCAPTCHA Failed: ' + verifyCaptchaResponse.error);
  771. } else {
  772. Accounts.createUser({
  773. username: formData.username,
  774. email: formData.email,
  775. password: formData.password
  776. });
  777. }
  778. return true;
  779. }
  780. },
  781. addSongToQueue: function(type, songData) {
  782. if (Meteor.userId() && !isBanned()) {
  783. type = type.toLowerCase();
  784. if (Rooms.find({type: type}).count() === 1) {
  785. if (Queues.find({type: type}).count() === 0) {
  786. Queues.insert({type: type, songs: []});
  787. }
  788. if (songData !== undefined && Object.keys(songData).length === 5 && songData.type !== undefined && songData.title !== undefined && songData.artist !== undefined && songData.img !== undefined) {
  789. songData.duration = getSongDuration(songData.title, songData.artist) || 0;
  790. songData.img = getSongAlbumArt(songData.title, songData.artist) || "";
  791. songData.skipDuration = 0;
  792. songData.likes = 0;
  793. songData.dislikes = 0;
  794. var mid = createUniqueSongId();
  795. if (mid !== undefined) {
  796. songData.mid = mid;
  797. Queues.update({type: type}, {
  798. $push: {
  799. songs: {
  800. id: songData.id,
  801. mid: songData.mid,
  802. title: songData.title,
  803. artist: songData.artist,
  804. duration: songData.duration,
  805. skipDuration: songData.skipDuration,
  806. likes: songData.likes,
  807. dislikes: songData.dislikes,
  808. img: songData.img,
  809. type: songData.type
  810. }
  811. }
  812. });
  813. return true;
  814. } else {
  815. throw new Meteor.Error(500, "Am error occured.");
  816. }
  817. } else {
  818. throw new Meteor.Error(403, "Invalid data.");
  819. }
  820. } else {
  821. throw new Meteor.Error(403, "Invalid genre.");
  822. }
  823. } else {
  824. throw new Meteor.Error(403, "Invalid permissions.");
  825. }
  826. },
  827. updateQueueSong: function(genre, oldSong, newSong) {
  828. if (isModerator() && !isBanned()) {
  829. newSong.mid = oldSong.mid;
  830. Queues.update({type: genre, "songs": oldSong}, {$set: {"songs.$": newSong}});
  831. return true;
  832. } else {
  833. throw new Meteor.Error(403, "Invalid permissions.");
  834. }
  835. },
  836. updatePlaylistSong: function(genre, oldSong, newSong) {
  837. if (isModerator() && !isBanned()) {
  838. newSong.mid = oldSong.mid;
  839. Playlists.update({type: genre, "songs": oldSong}, {$set: {"songs.$": newSong}});
  840. return true;
  841. } else {
  842. throw new Meteor.Error(403, "Invalid permissions.");
  843. }
  844. },
  845. removeSongFromQueue: function(type, mid) {
  846. if (isModerator() && !isBanned()) {
  847. type = type.toLowerCase();
  848. Queues.update({type: type}, {$pull: {songs: {mid: mid}}});
  849. } else {
  850. throw new Meteor.Error(403, "Invalid permissions.");
  851. }
  852. },
  853. removeSongFromPlaylist: function(type, mid) {
  854. if (isModerator() && !isBanned()) {
  855. type = type.toLowerCase();
  856. var songs = Playlists.findOne({type: type}).songs;
  857. var song = undefined;
  858. songs.forEach(function(curr_song) {
  859. if (mid === curr_song.mid) {
  860. song = curr_song;
  861. return;
  862. }
  863. });
  864. Playlists.update({type: type}, {$pull: {songs: {mid: mid}}});
  865. if (song !== undefined) {
  866. song.deletedBy = Meteor.userId();
  867. song.deletedAt = new Date(Date.now());
  868. if (Deleted.find({type: type}).count() === 0) {
  869. Deleted.insert({type: type, songs: [song]});
  870. } else {
  871. Deleted.update({type: type}, {$push: {songs: song}});
  872. }
  873. }
  874. } else {
  875. throw new Meteor.Error(403, "Invalid permissions.");
  876. }
  877. },
  878. addSongToPlaylist: function(type, songData) {
  879. if (isModerator() && !isBanned()) {
  880. type = type.toLowerCase();
  881. if (Rooms.find({type: type}).count() === 1) {
  882. if (Playlists.find({type: type}).count() === 0) {
  883. Playlists.insert({type: type, songs: []});
  884. }
  885. var requiredProperties = ["type", "mid", "id", "title", "artist", "duration", "skipDuration", "img", "likes", "dislikes"];
  886. if (songData !== undefined && Object.keys(songData).length === requiredProperties.length) {
  887. for (var property in requiredProperties) {
  888. if (songData[requiredProperties[property]] === undefined) {
  889. throw new Meteor.Error(403, "Invalid data.");
  890. }
  891. }
  892. Playlists.update({type: type}, {
  893. $push: {
  894. songs: {
  895. id: songData.id,
  896. mid: songData.mid,
  897. title: songData.title,
  898. artist: songData.artist,
  899. duration: songData.duration,
  900. skipDuration: Number(songData.skipDuration),
  901. img: songData.img,
  902. type: songData.type,
  903. likes: Number(songData.likes),
  904. dislikes: Number(songData.dislikes)
  905. }
  906. }
  907. });
  908. Queues.update({type: type}, {$pull: {songs: {mid: songData.mid}}});
  909. return true;
  910. } else {
  911. throw new Meteor.Error(403, "Invalid data.");
  912. }
  913. } else {
  914. throw new Meteor.Error(403, "Invalid genre.");
  915. }
  916. } else {
  917. throw new Meteor.Error(403, "Invalid permissions.");
  918. }
  919. },
  920. createRoom: function(display, tag, private) {
  921. if (isAdmin() && !isBanned()) {
  922. createRoom(display, tag, private);
  923. } else {
  924. throw new Meteor.Error(403, "Invalid permissions.");
  925. }
  926. },
  927. deleteRoom: function(type){
  928. if (isAdmin() && !isBanned()) {
  929. Rooms.remove({type: type});
  930. Playlists.remove({type: type});
  931. Queues.remove({type: type});
  932. return true;
  933. } else {
  934. throw new Meteor.Error(403, "Invalid permissions.");
  935. }
  936. },
  937. getUserNum: function(){
  938. if (!isBanned()) {
  939. return Object.keys(Meteor.default_server.sessions).length;
  940. }
  941. },
  942. getTotalUsers: function(){
  943. console.log(Meteor.users.find().count());
  944. return Meteor.users.find().count();
  945. },
  946. updateRealName: function(username, realname){
  947. Meteor.users.update({"profile.username": username}, {$set: {"profile.realname": realname}});
  948. },
  949. updateUserName: function(username, newUserName){
  950. Meteor.users.update({"username": username}, {$set: {"username": newUserName, "profile.username": newUserName, "profile.usernameL": newUserName.toLowerCase()}});
  951. },
  952. updateUserRank: function(username, newRank){
  953. Meteor.users.update({"username" : username}, {$set: {"profile.rank": newRank}});
  954. },
  955. deleteAccount: function(userID) {
  956. Meteor.users.remove({_id: userID});
  957. }
  958. });
  959. Meteor.setInterval(function() {
  960. checkUsersPR();
  961. }, 10000);