server.js 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789
  1. Meteor.startup(function () {
  2. reCAPTCHA.config({
  3. privatekey: '6LcVxg0TAAAAAI2fgIEEWHFxwNXeVIs8mzq5cfRM'
  4. });
  5. Avatar.setOptions({
  6. fallbackType: "initials",
  7. defaultImageUrl: "/notes.png",
  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, "Room description goes here.");
  17. }
  18. }
  19. emojione.ascii = true;
  20. Accounts.config({
  21. sendVerificationEmail: true
  22. });
  23. if (Songs.find().count() === 0 || Songs.find({mid: default_song.mid}).count() === 0) {
  24. Songs.insert(default_song);
  25. }
  26. });
  27. var default_song = {
  28. id: "xKVcVSYmesU",
  29. mid: "ABCDEF",
  30. likes: 0,
  31. dislikes: 0,
  32. title: "Immortals",
  33. artist: "Fall Out Boy",
  34. img: "http://c.directlyrics.com/img/upload/fall-out-boy-sixth-album-cover.jpg",
  35. type: "YouTube",
  36. duration: 181,
  37. skipDuration: 0,
  38. requestedBy: "NONE",
  39. approvedBy: "GOD",
  40. genres: ["edm", "pop"]
  41. };
  42. var default_private_playlist = {
  43. name: "default",
  44. displayName: "Default Playlist",
  45. songs: [{id: "60ItHLz5WEA", duration: 213, title: "Alan Walker - Faded"}],
  46. owner: "NONE"
  47. };
  48. Alerts.update({active: true}, {$set: {active: false}}, {multi: true});
  49. var stations = [];
  50. var privateStations = [];
  51. var voteNum = 0;
  52. var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_";
  53. function createUniqueSongId() {
  54. var code = "";
  55. for (var i = 0; i < 6; i++) {
  56. code += chars[Math.floor(Math.random() * chars.length)];
  57. }
  58. if (Playlists.find({"songs.mid": code}).count() > 0) {
  59. return createUniqueSongId();
  60. } else {
  61. return code;
  62. }
  63. }
  64. function checkUsersPR() {
  65. var output = {};
  66. var connections = Meteor.server.stream_server.open_sockets;
  67. _.each(connections, function (connection) {
  68. // named subscriptions
  69. if (connection._meteorSession !== undefined && connection._meteorSession !== null) {
  70. var subs = connection._meteorSession._namedSubs;
  71. //var ip = connection.remoteAddress;
  72. var used_subs = [];
  73. for (var sub in subs) {
  74. var mySubName = subs[sub]._name;
  75. if (subs[sub]._params.length > 0) {
  76. mySubName += subs[sub]._params[0]; // assume one id parameter for now
  77. }
  78. if (used_subs.indexOf(mySubName) === -1) {
  79. used_subs.push(mySubName);
  80. if (!output[mySubName]) {
  81. output[mySubName] = 1;
  82. } else {
  83. output[mySubName] += 1;
  84. }
  85. }
  86. }
  87. }
  88. // there are also these 'universal subscriptions'
  89. //not sure what these are, i count none in my tests
  90. //var usubs = connection._meteorSession._universalSubs;
  91. });
  92. var emptyStations = [];
  93. stations.forEach(function (station) {
  94. emptyStations.push(station);
  95. });
  96. for (var key in output) {
  97. getStation(key, function (station) {
  98. emptyStations.splice(emptyStations.indexOf(station), 1);
  99. Rooms.update({type: key}, {$set: {users: output[key]}});
  100. });
  101. }
  102. emptyStations.forEach(function (emptyStation) {
  103. Rooms.update({type: emptyStation.type}, {$set: {users: 0}});
  104. });
  105. return output;
  106. }
  107. function getStation(type, cb) {
  108. stations.forEach(function (station) {
  109. if (station.type === type) {
  110. cb(station);
  111. return;
  112. }
  113. });
  114. }
  115. function getPrivateStation(name, cb) {
  116. privateStations.forEach(function (station) {
  117. if (station.name === name) {
  118. cb(station);
  119. return;
  120. }
  121. });
  122. }
  123. function createRoom(display, tag, private, desc) {
  124. var type = tag;
  125. if (Rooms.find({type: type}).count() === 0) {
  126. Rooms.insert({
  127. display: display,
  128. type: type,
  129. users: 0,
  130. private: private,
  131. currentSong: {song: default_song, started: 0},
  132. roomDesc: desc
  133. }, function (err) {
  134. if (err) {
  135. throw err;
  136. } else {
  137. stations.push(new Station(type));
  138. }
  139. });
  140. } else {
  141. return "Room already exists";
  142. }
  143. }
  144. function createPrivateRoom(name, display, private, desc, owner) {
  145. if (PrivateRooms.find({name: name}).count() === 0) {
  146. PrivateRooms.insert({
  147. name: name,
  148. displayName: display,
  149. private: private,
  150. roomDesc: desc,
  151. owner: owner,
  152. }, function (err) {
  153. if (err) {
  154. throw err;
  155. } else {
  156. privateStations.push(new PrivateStation(name));
  157. }
  158. });
  159. } else {
  160. return "Private room with that name already exists";
  161. }
  162. }
  163. function Station(type) {
  164. if (Playlists.find({type: type}).count() === 0) {
  165. Playlists.insert({type: type, songs: [default_song.mid], lastSong: 0});
  166. }
  167. if (Songs.find({genres: type}).count() > 0) {
  168. var list = Songs.find({genres: type}).fetch();
  169. list.forEach(function(song){
  170. if (Playlists.findOne({type: type, songs: song.mid}) === undefined) {
  171. Playlists.update({type: type}, {$push: {songs: song.mid}});
  172. }
  173. });
  174. }
  175. if (Playlists.findOne({type: type}).songs.length === 0) {
  176. Playlists.update({type: type}, {$push: {songs: default_song.mid}});
  177. }
  178. var usersObj = {};
  179. Rooms.update({type: type}, {$set: {userList: []}});
  180. Meteor.publish(type, function () {
  181. var user = Meteor.users.findOne(this.userId);
  182. if (this.userId !== undefined && user !== undefined && user.profile !== undefined && user.profile.username !== undefined) {
  183. var username = user.profile.username;
  184. if (usersObj[username] === undefined) {
  185. usersObj[username] = 1;
  186. } else {
  187. usersObj[username]++;
  188. }
  189. Rooms.update({type: type}, {$push: {userList: username}});
  190. this.onStop(function() {
  191. usersObj[username]--;
  192. var list = Rooms.findOne({type: type}).userList;
  193. var index = list.indexOf(username);
  194. if (index >= 0) {
  195. list.splice(index, 1);
  196. Rooms.update({type: type}, {$set: {userList: list}});
  197. }
  198. });
  199. }
  200. return undefined;
  201. });
  202. var self = this;
  203. var startedAt = Date.now();
  204. var playlist = Playlists.findOne({type: type});
  205. var songs = playlist.songs;
  206. var currentSong = playlist.lastSong;
  207. if (currentSong < (songs.length - 1)) {
  208. currentSong++;
  209. } else currentSong = 0;
  210. var currentMid = songs[currentSong];
  211. var song = Songs.findOne({mid: currentMid});
  212. if (song === undefined) {
  213. Playlists.remove({}, {$pull: {songs: currentMid}});
  214. song = default_song;
  215. }
  216. var res = Rooms.update({type: type}, {
  217. $set: {
  218. currentSong: {song: song, started: startedAt},
  219. users: 0
  220. }
  221. });
  222. this.skipSong = function () {
  223. self.voted = [];
  224. voteNum = 0;
  225. Rooms.update({type: type}, {$set: {votes: 0}});
  226. songs = Playlists.findOne({type: type}).songs;
  227. songs.forEach(function (mid, index) {
  228. if (mid === currentMid) {
  229. currentSong = index;
  230. }
  231. });
  232. if (currentSong < (songs.length - 1)) {
  233. currentSong++;
  234. } else currentSong = 0;
  235. if (songs);
  236. if (currentSong === 0) {
  237. this.shufflePlaylist();
  238. } else {
  239. currentMid = songs[currentSong];
  240. Playlists.update({type: type}, {$set: {lastSong: currentSong}});
  241. Rooms.update({type: type}, {$set: {timePaused: 0}});
  242. this.songTimer();
  243. Rooms.update({type: type}, {$set: {currentSong: {song: Songs.findOne({mid: songs[currentSong]}), started: startedAt}}});
  244. }
  245. };
  246. this.shufflePlaylist = function () {
  247. voteNum = 0;
  248. Rooms.update({type: type}, {$set: {votes: 0}});
  249. self.voted = [];
  250. songs = Playlists.findOne({type: type}).songs;
  251. currentSong = 0;
  252. Playlists.update({type: type}, {$set: {"songs": []}});
  253. songs = shuffle(songs);
  254. songs.forEach(function (song) {
  255. Playlists.update({type: type}, {$push: {"songs": song}});
  256. });
  257. currentMid = songs[currentSong];
  258. Playlists.update({type: type}, {$set: {lastSong: currentSong}});
  259. Rooms.update({type: type}, {$set: {timePaused: 0}});
  260. this.songTimer();
  261. Rooms.update({type: type}, {$set: {currentSong: {song: Songs.findOne({mid: songs[currentSong]}), started: startedAt}}});
  262. };
  263. Rooms.update({type: type}, {$set: {timePaused: 0}});
  264. var timer;
  265. var timerInitialised = false;
  266. this.songTimer = function () {
  267. if (state !== "paused") {
  268. startedAt = Date.now();
  269. if (timer !== undefined) {
  270. timer.pause();
  271. }
  272. timerInitialised = true;
  273. timer = new Timer(function () {
  274. self.skipSong();
  275. }, Songs.findOne({mid: songs[currentSong]}).duration * 1000);
  276. }
  277. };
  278. var state = Rooms.findOne({type: type}).state;
  279. this.pauseRoom = function () {
  280. if (state !== "paused") {
  281. timer.pause();
  282. Rooms.update({type: type}, {$set: {state: "paused"}});
  283. state = "paused";
  284. }
  285. };
  286. this.resumeRoom = function () {
  287. if (state !== "playing") {
  288. if (!timerInitialised) {
  289. timer = new Timer(function () {
  290. self.skipSong();
  291. }, Songs.findOne({mid: songs[currentSong]}).duration * 1000);
  292. }
  293. timer.resume();
  294. Rooms.update({type: type}, {$set: {state: "playing", timePaused: timer.timeWhenPaused()}});
  295. state = "playing";
  296. }
  297. };
  298. this.cancelTimer = function () {
  299. timer.pause();
  300. };
  301. this.getState = function () {
  302. return state;
  303. };
  304. this.type = type;
  305. var private = Rooms.findOne({type: type}).private;
  306. if (typeof private !== "boolean") {
  307. Rooms.update({type: type}, {$set: {"private": false}});
  308. private = false;
  309. }
  310. this.private = private;
  311. this.unlock = function () {
  312. if (self.private) {
  313. self.private = false;
  314. Rooms.update({type: type}, {$set: {"private": false}});
  315. }
  316. };
  317. this.lock = function () {
  318. if (!self.private) {
  319. self.private = true;
  320. Rooms.update({type: type}, {$set: {"private": true}});
  321. }
  322. };
  323. /* This function fetches all songs with the genre of this room that isn't in the playlist yet, and then puts it in the playlist */
  324. this.fetchSongs = function() {
  325. var genreSongs = Songs.find({genres: type}).fetch();
  326. genreSongs.forEach(function(song) {
  327. if (songs.indexOf(song.mid) === -1) {
  328. Playlists.update({type: type}, {$push: {songs: song.mid}});
  329. }
  330. });
  331. };
  332. /* This function removes all songs that are in the playlist but do not have the type of the playlist in their genre */
  333. this.removeSongs = function() {
  334. songs.forEach(function(mid) {
  335. var song = Songs.findOne({mid: mid});
  336. if (song === undefined || song.genres.indexOf(type) === -1) {
  337. Playlists.update({type: type}, {$pull: {songs: mid}});
  338. }
  339. });
  340. };
  341. this.removeSongs();
  342. this.fetchSongs();
  343. this.skipSong();
  344. this.voted = [];
  345. }
  346. function PrivateStation(name) {
  347. var usersObj = {};
  348. PrivateRooms.update({name: name}, {$set: {userList: []}});
  349. Meteor.publish("pr_" + name, function () {
  350. var user = Meteor.users.findOne(this.userId);
  351. if (this.userId !== undefined && user !== undefined && user.profile !== undefined && user.profile.username !== undefined) {
  352. var username = user.profile.username;
  353. if (usersObj[username] === undefined) {
  354. usersObj[username] = 1;
  355. } else {
  356. usersObj[username]++;
  357. }
  358. PrivateRooms.update({name: name}, {$push: {userList: username}});
  359. this.onStop(function () {
  360. usersObj[username]--;
  361. var room = PrivateRooms.findOne({name: name});
  362. if (room !== undefined) {
  363. var list = room.userList;
  364. var index = list.indexOf(username);
  365. if (index >= 0) {
  366. list.splice(index, 1);
  367. PrivateRooms.update({name: name}, {$set: {userList: list}});
  368. }
  369. }
  370. });
  371. }
  372. return undefined;
  373. });
  374. var self = this;
  375. var startedAt = Date.now();
  376. var _room = PrivateRooms.findOne({name: name});
  377. var playlist;
  378. if (_room !== undefined) {
  379. playlist = PrivatePlaylists.findOne({name: _room.playlist, owner: _room.owner});
  380. }
  381. if (playlist === undefined) {
  382. playlist = default_private_playlist;
  383. }
  384. var songs = playlist.songs;
  385. var currentSong = 0;
  386. if (currentSong < (songs.length - 1)) {
  387. currentSong++;
  388. } else currentSong = 0;
  389. var currentId = songs[currentSong];
  390. var song = songs[songs.indexOf(currentId)];
  391. if (song === undefined) {
  392. song = {id: "60ItHLz5WEA", duration: 213, title: "Alan Walker - Faded"};
  393. }
  394. var res = PrivateRooms.update({name: name}, {
  395. $set: {
  396. currentSong: {song: song, started: startedAt},
  397. users: 0
  398. }
  399. });
  400. this.skipSong = function () {
  401. self.voted = [];
  402. voteNum = 0;
  403. PrivateRooms.update({name: name}, {$set: {votes: 0}});
  404. playlist = PrivatePlaylists.findOne({name: _room.playlist, owner: _room.owner});
  405. if (playlist === undefined) {
  406. playlist = default_private_playlist;
  407. }
  408. if (playlist !== undefined && playlist.songs.length === 0) {
  409. PrivateRooms.update({name: name}, {$unset: {"playlist": 1}});
  410. playlist = default_private_playlist;
  411. }
  412. songs = playlist.songs;
  413. songs.forEach(function (id, index) {
  414. if (id === currentId) {
  415. currentSong = index;
  416. }
  417. });
  418. if (currentSong < (songs.length - 1)) {
  419. currentSong++;
  420. } else currentSong = 0;
  421. currentId = songs[currentSong];
  422. PrivateRooms.update({name: name}, {$set: {timePaused: 0}});
  423. this.songTimer();
  424. PrivateRooms.update({name: name}, {$set: {currentSong: {song: songs[currentSong], started: startedAt}}});
  425. };
  426. PrivateRooms.update({name: name}, {$set: {timePaused: 0}});
  427. var timer;
  428. var timerInitialised = false;
  429. this.songTimer = function () {
  430. if (state !== "paused") {
  431. startedAt = Date.now();
  432. if (timer !== undefined) {
  433. timer.pause();
  434. }
  435. timerInitialised = true;
  436. timer = new Timer(function () {
  437. self.skipSong();
  438. }, songs[currentSong].duration * 1000);
  439. }
  440. };
  441. var state = PrivateRooms.findOne({name: name}).state;
  442. this.pauseRoom = function () {
  443. if (state !== "paused") {
  444. timer.pause();
  445. PrivateRooms.update({name: name}, {$set: {state: "paused"}});
  446. state = "paused";
  447. }
  448. };
  449. this.resumeRoom = function () {
  450. if (state !== "playing") {
  451. if (!timerInitialised) {
  452. timer = new Timer(function () {
  453. self.skipSong();
  454. }, songs[currentSong] * 1000);
  455. }
  456. timer.resume();
  457. PrivateRooms.update({name: name}, {$set: {state: "playing", timePaused: timer.timeWhenPaused()}});
  458. state = "playing";
  459. }
  460. };
  461. this.cancelTimer = function () {
  462. timer.pause();
  463. };
  464. this.getState = function () {
  465. return state;
  466. };
  467. this.name = name;
  468. var private = PrivateRooms.findOne({name: name}).private;
  469. if (typeof private !== "boolean") {
  470. PrivateRooms.update({name: name}, {$set: {"private": false}});
  471. private = false;
  472. }
  473. this.private = private;
  474. this.unlock = function () {
  475. if (self.private) {
  476. self.private = false;
  477. PrivateRooms.update({name: name}, {$set: {"private": false}});
  478. }
  479. };
  480. this.lock = function () {
  481. if (!self.private) {
  482. self.private = true;
  483. PrivateRooms.update({name: name}, {$set: {"private": true}});
  484. }
  485. };
  486. this.setPlaylist = function (plName) {
  487. if (PrivatePlaylists.findOne({name: plName, owner: _room.owner}) !== undefined) {
  488. PrivateRooms.update({name: name}, {$set: {"playlist": plName}});
  489. _room.playlist = plName;
  490. playlist = PrivatePlaylists.findOne({name: plName, owner: _room.owner});
  491. songs = playlist.songs;
  492. currentSong = 0;
  493. }
  494. };
  495. this.addAllowed = function (allowed) {
  496. if (_room.allowed.indexOf(allowed) === -1 && _room.owner !== allowed) {
  497. PrivateRooms.update({name: name}, {$push: {allowed: allowed}});
  498. _room = PrivateRooms.findOne({name: name});
  499. }
  500. };
  501. this.removeAllowed = function (allowed) {
  502. if (_room.allowed.indexOf(allowed) !== -1) {
  503. PrivateRooms.update({name: name}, {$pull: {allowed: allowed}});
  504. _room = PrivateRooms.findOne({name: name});
  505. }
  506. };
  507. this.skipSong();
  508. this.voted = [];
  509. }
  510. function shuffle(array) {
  511. var currentIndex = array.length, temporaryValue, randomIndex;
  512. // While there remain elements to shuffle...
  513. while (0 !== currentIndex) {
  514. // Pick a remaining element...
  515. randomIndex = Math.floor(Math.random() * currentIndex);
  516. currentIndex -= 1;
  517. // And swap it with the current element.
  518. temporaryValue = array[currentIndex];
  519. array[currentIndex] = array[randomIndex];
  520. array[randomIndex] = temporaryValue;
  521. }
  522. return array;
  523. }
  524. function Timer(callback, delay) {
  525. var timerId, start, remaining = delay;
  526. var timeWhenPaused = 0;
  527. var timePaused = new Date();
  528. this.pause = function () {
  529. Meteor.clearTimeout(timerId);
  530. remaining -= new Date() - start;
  531. timePaused = new Date();
  532. };
  533. this.resume = function () {
  534. start = new Date();
  535. Meteor.clearTimeout(timerId);
  536. timerId = Meteor.setTimeout(callback, remaining);
  537. timeWhenPaused += new Date() - timePaused;
  538. };
  539. this.timeWhenPaused = function () {
  540. return timeWhenPaused;
  541. };
  542. this.resume();
  543. }
  544. Meteor.users.deny({
  545. update: function () {
  546. return true;
  547. }
  548. });
  549. Meteor.users.deny({
  550. insert: function () {
  551. return true;
  552. }
  553. });
  554. Meteor.users.deny({
  555. remove: function () {
  556. return true;
  557. }
  558. });
  559. function getSongDuration(query, artistName) {
  560. var duration;
  561. var search = query;
  562. var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + encodeURIComponent(query) + '&type=track');
  563. for (var i in res.data) {
  564. for (var j in res.data[i].items) {
  565. if (search.indexOf(res.data[i].items[j].name) !== -1 && artistName.indexOf(res.data[i].items[j].artists[0].name) !== -1) {
  566. duration = res.data[i].items[j].duration_ms / 1000;
  567. return duration;
  568. }
  569. }
  570. }
  571. return 0;
  572. }
  573. function getSongDataYT(id) {
  574. var res = Meteor.http.get('https://www.googleapis.com/youtube/v3/videos?id=' + encodeURIComponent(id) + '&part=snippet,contentDetails&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY');
  575. if (res.data !== undefined && res.data.items.length !== 0) {
  576. var dur = res.data.items[0].contentDetails.duration;
  577. dur = dur.replace("PT", "");
  578. var durInSec = 0;
  579. dur = dur.replace(/([\d]*)M/, function(v, v2) {
  580. v2 = Number(v2);
  581. durInSec = (v2 * 60)
  582. return "";
  583. });
  584. dur = dur.replace(/([\d]*)S/, function(v, v2) {
  585. v2 = Number(v2);
  586. durInSec += v2;
  587. return "";
  588. });
  589. return {duration: durInSec, title: res.data.items[0].snippet.title};
  590. } else {
  591. return 0;
  592. }
  593. }
  594. function getSongAlbumArt(query, artistName) {
  595. var albumart;
  596. var search = query;
  597. var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + encodeURIComponent(query) + '&type=track');
  598. for (var i in res.data) {
  599. for (var j in res.data[i].items) {
  600. if (search.indexOf(res.data[i].items[j].name) !== -1 && artistName.indexOf(res.data[i].items[j].artists[0].name) !== -1) {
  601. albumart = res.data[i].items[j].album.images[1].url
  602. return albumart;
  603. }
  604. }
  605. }
  606. }
  607. //var room_types = ["edm", "nightcore"];
  608. var songsArr = [];
  609. Rooms.find({}).fetch().forEach(function (room) {
  610. var type = room.type;
  611. if (Playlists.find({type: type}).count() === 0) {
  612. Playlists.insert({type: type, songs: []});
  613. }
  614. if (Playlists.findOne({type: type}).songs.length === 0) {
  615. Playlists.update({type: type}, {$push: {songs: default_song.mid}}, function() {
  616. stations.push(new Station(type));
  617. });
  618. } else {
  619. stations.push(new Station(type));
  620. }
  621. });
  622. PrivateRooms.find({}).fetch().forEach(function (room) {
  623. var name = room.name;
  624. privateStations.push(new PrivateStation(name));
  625. });
  626. Accounts.validateNewUser(function (user) {
  627. var username;
  628. if (user.services) {
  629. if (user.services.github) {
  630. username = user.services.github.username;
  631. } else if (user.services.facebook) {
  632. username = user.services.facebook.first_name;
  633. } else if (user.services.password) {
  634. username = user.username;
  635. }
  636. }
  637. if (Meteor.users.find({"profile.usernameL": username.toLowerCase()}).count() !== 0) {
  638. throw new Meteor.Error(403, "An account with that username already exists.");
  639. } else {
  640. return true;
  641. }
  642. });
  643. Accounts.onCreateUser(function (options, user) {
  644. var username;
  645. if (user.services) {
  646. if (user.services.github) {
  647. username = user.services.github.username;
  648. } else if (user.services.facebook) {
  649. username = user.services.facebook.first_name;
  650. } else if (user.services.password) {
  651. username = user.username;
  652. }
  653. }
  654. user.profile = {
  655. username: username,
  656. usernameL: username.toLowerCase(),
  657. rank: "default",
  658. liked: [],
  659. disliked: [],
  660. settings: {showRating: true},
  661. realname: ""
  662. };
  663. return user;
  664. });
  665. Meteor.publish("alerts", function () {
  666. return Alerts.find({active: true})
  667. });
  668. Meteor.publish("allAlerts", function () {
  669. return Alerts.find({})
  670. });
  671. Meteor.publish("news", function () {
  672. return News.find({})
  673. });
  674. Meteor.publish("userData", function (userId) {
  675. if (userId !== undefined) {
  676. return Meteor.users.find(userId, {fields: {"services.github.username": 1, "punishments": 1}});
  677. } else {
  678. return undefined;
  679. }
  680. });
  681. Meteor.publish("usernames", function () {
  682. return Meteor.users.find({}, {fields: {"profile.username": 1}})
  683. });
  684. Meteor.publish("playlists", function () {
  685. return Playlists.find({})
  686. });
  687. Meteor.publish("rooms", function () {
  688. return Rooms.find({});
  689. });
  690. Meteor.publish("private_rooms", function () {
  691. return PrivateRooms.find({});
  692. });
  693. Meteor.publish("private_playlists", function () {
  694. if (this.userId !== undefined) {
  695. return PrivatePlaylists.find({owner: this.userId});
  696. } else {
  697. return null;
  698. }
  699. });
  700. Meteor.publish("songs", function () {
  701. return Songs.find({});
  702. });
  703. Meteor.publish("queues", function () {
  704. return Queues.find({});
  705. });
  706. Meteor.publish("reports", function () {
  707. return Reports.find({});
  708. });
  709. Meteor.publish("chat", function () {
  710. return Chat.find({});
  711. });
  712. Meteor.publish("userProfiles", function (username) {
  713. var settings = Meteor.users.findOne({"profile.usernameL": username}, {fields: {"profile.settings": 1}});
  714. if (settings !== undefined && settings.profile.settings) {
  715. settings = settings.profile.settings;
  716. if (settings.showRating === true) {
  717. return Meteor.users.find({"profile.usernameL": username}, {
  718. fields: {
  719. "profile.username": 1,
  720. "profile.usernameL": 1,
  721. "profile.rank": 1,
  722. createdAt: 1,
  723. "profile.liked": 1,
  724. "profile.disliked": 1,
  725. "profile.settings": 1,
  726. "profile.realname": 1
  727. }
  728. });
  729. }
  730. }
  731. return Meteor.users.find({"profile.usernameL": username}, {
  732. fields: {
  733. "profile.username": 1,
  734. "profile.usernameL": 1,
  735. "profile.rank": 1,
  736. createdAt: 1,
  737. "profile.settings": 1,
  738. "profile.realname": 1
  739. }
  740. });
  741. });
  742. Meteor.publish("isAdmin", function () {
  743. return Meteor.users.find({_id: this.userId, "profile.rank": "admin"});
  744. });
  745. Meteor.publish("isModerator", function () {
  746. return Meteor.users.find({_id: this.userId, "profile.rank": "moderator"});
  747. });
  748. Meteor.publish("feedback", function(){
  749. return Feedback.find();
  750. })
  751. function isPrivateRoomOwner(name) {
  752. var room = PrivateRooms.findOne({name: name});
  753. if (Meteor.userId() && room !== undefined && Meteor.userId() === room.owner) {
  754. return true;
  755. } else {
  756. return false;
  757. }
  758. }
  759. function isAdmin() {
  760. var userData = Meteor.users.find(Meteor.userId());
  761. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
  762. return true;
  763. } else {
  764. return false;
  765. }
  766. }
  767. function isModerator() {
  768. var userData = Meteor.users.find(Meteor.userId());
  769. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "moderator") {
  770. return true;
  771. } else {
  772. return isAdmin();
  773. }
  774. }
  775. function isBanned() {
  776. var userData = Meteor.users.findOne(Meteor.userId());
  777. if (Meteor.userId() && userData !== undefined && userData.punishments !== undefined && userData.punishments.ban !== undefined) {
  778. var ban = userData.punishments.ban;
  779. if (new Date(ban.bannedUntil).getTime() <= new Date().getTime()) {
  780. Meteor.users.update(Meteor.userId(), {$unset: {"punishments.ban": ""}});
  781. return false;
  782. } else {
  783. return true;
  784. }
  785. } else {
  786. return false;
  787. }
  788. }
  789. function isMuted() {
  790. var userData = Meteor.users.findOne(Meteor.userId());
  791. if (Meteor.userId() && userData !== undefined && userData.punishments !== undefined && userData.punishments.mute !== undefined) {
  792. var mute = userData.punishments.mute;
  793. if (new Date(mute.bannedUntil).getTime() <= new Date().getTime()) {
  794. Meteor.users.update(Meteor.userId(), {$unset: {"punishments.mute": ""}});
  795. return false;
  796. } else {
  797. return true;
  798. }
  799. } else {
  800. return false;
  801. }
  802. }
  803. Meteor.methods({
  804. deletePrivateRoom: function(roomName) {
  805. if ((isAdmin() || isPrivateRoomOwner(roomName)) && !isBanned()) {
  806. PrivateRooms.remove({name: roomName});
  807. getPrivateStation(roomName, function(room) {
  808. room.cancelTimer();
  809. delete privateStations[privateStations.indexOf(room)];
  810. });
  811. } else {
  812. throw new Meteor.Error(403, "Invalid permissions.");
  813. }
  814. },
  815. changePrivateRoomDescription: function(roomName, newDescription) {
  816. if ((isAdmin() || isPrivateRoomOwner(roomName)) && !isBanned()) {
  817. PrivateRooms.update({name: roomName}, {$set: {roomDesc: newDescription}});
  818. } else {
  819. throw new Meteor.Error(403, "Invalid permissions.");
  820. }
  821. },
  822. changePrivateRoomDisplayName: function(roomName, newDisplayName) {
  823. if ((isAdmin() || isPrivateRoomOwner(roomName)) && !isBanned()) {
  824. PrivateRooms.update({name: roomName}, {$set: {displayName: newDisplayName}});
  825. } else {
  826. throw new Meteor.Error(403, "Invalid permissions.");
  827. }
  828. },
  829. addVideoToPrivatePlaylist: function(name, id) {
  830. if (Meteor.userId() && !isBanned()) {
  831. var pl = PrivatePlaylists.findOne({owner: Meteor.userId(), name: name});
  832. if (pl !== undefined) {
  833. var copy = false;
  834. pl.songs.forEach(function(song) {
  835. if (song.id === id) {
  836. copy = true;
  837. }
  838. });
  839. if (!copy) {
  840. console.log(getSongDataYT(id));
  841. var data = getSongDataYT(id);
  842. data.id = id;
  843. PrivatePlaylists.update({owner: Meteor.userId(), name: name}, {$push: {songs: data}});
  844. } else {
  845. throw new Meteor.Error(500, "Video is already in playlist.");
  846. }
  847. } else {
  848. throw new Meteor.Error(404, "Playlist not found.");
  849. }
  850. } else {
  851. throw new Meteor.Error(403, "Invalid permissions.");
  852. }
  853. },
  854. removeVideoFromPrivatePlaylist: function(name, id) {
  855. if (Meteor.userId() && !isBanned()) {
  856. var pl = PrivatePlaylists.findOne({owner: Meteor.userId(), name: name});
  857. if (pl !== undefined) {
  858. var copy = false;
  859. var data;
  860. pl.songs.forEach(function(song) {
  861. if (song.id === id) {
  862. data = song;
  863. copy = true;
  864. }
  865. });
  866. if (copy) {
  867. PrivatePlaylists.update({owner: Meteor.userId(), name: name}, {$pull: {songs: data}});
  868. } else {
  869. throw new Meteor.Error(500, "Video is not in playlist.");
  870. }
  871. } else {
  872. throw new Meteor.Error(404, "Playlist not found.");
  873. }
  874. } else {
  875. throw new Meteor.Error(403, "Invalid permissions.");
  876. }
  877. },
  878. deletePrivatePlaylist: function(name) {
  879. if (Meteor.userId() && !isBanned()) {
  880. var pl = PrivatePlaylists.findOne({owner: Meteor.userId(), name: name});
  881. if (pl !== undefined) {
  882. PrivatePlaylists.remove({owner: Meteor.userId(), name: name});
  883. Deleted.insert({type: "PrivatePlaylist", data: pl});
  884. } else {
  885. throw new Meteor.Error(404, "Playlist not found.");
  886. }
  887. } else {
  888. throw new Meteor.Error(403, "Invalid permissions.");
  889. }
  890. },
  891. activateAlert: function(id) {
  892. if (isAdmin() && !isBanned()) {
  893. Alerts.update(id, {$set: {active: true}});
  894. } else {
  895. throw new Meteor.Error(403, "Invalid permissions.");
  896. }
  897. },
  898. deactivateAlert: function(id) {
  899. if (isAdmin() && !isBanned()) {
  900. Alerts.update(id, {$set: {active: false}});
  901. } else {
  902. throw new Meteor.Error(403, "Invalid permissions.");
  903. }
  904. },
  905. deleteAlert: function(id) {
  906. if (isAdmin() && !isBanned()) {
  907. Alerts.remove(id);
  908. } else {
  909. throw new Meteor.Error(403, "Invalid permissions.");
  910. }
  911. },
  912. fetchSong: function(type) {
  913. if (isAdmin() && !isBanned()) {
  914. getStation(type, function (station) {
  915. station.fetchSongs();
  916. });
  917. } else {
  918. throw new Meteor.Error(403, "Invalid permissions.");
  919. }
  920. },
  921. removeSongs: function(type) {
  922. if (isAdmin() && !isBanned()) {
  923. getStation(type, function (station) {
  924. station.removeSongs();
  925. });
  926. } else {
  927. throw new Meteor.Error(403, "Invalid permissions.");
  928. }
  929. },
  930. lockRoom: function (type) {
  931. if (isAdmin() && !isBanned()) {
  932. getStation(type, function (station) {
  933. station.lock();
  934. });
  935. } else {
  936. throw new Meteor.Error(403, "Invalid permissions.");
  937. }
  938. },
  939. unlockRoom: function (type) {
  940. if (isAdmin() && !isBanned()) {
  941. getStation(type, function (station) {
  942. station.unlock();
  943. });
  944. } else {
  945. throw new Meteor.Error(403, "Invalid permissions.");
  946. }
  947. },
  948. lockPrivateRoom: function (name) {
  949. if ((isAdmin() || isPrivateRoomOwner(name)) && !isBanned()) {
  950. getPrivateStation(name, function (station) {
  951. station.lock();
  952. });
  953. } else {
  954. throw new Meteor.Error(403, "Invalid permissions.");
  955. }
  956. },
  957. unlockPrivateRoom: function (name) {
  958. if ((isAdmin() || isPrivateRoomOwner(name)) && !isBanned()) {
  959. getPrivateStation(name, function (station) {
  960. station.unlock();
  961. });
  962. } else {
  963. throw new Meteor.Error(403, "Invalid permissions.");
  964. }
  965. },
  966. banUser: function (username, period, reason) {
  967. if (isAdmin() && !isBanned()) {
  968. var user = Meteor.user();
  969. var bannedUser = Meteor.users.findOne({"profile.usernameL": username.toLowerCase()});
  970. var bannedUntil = (new Date).getTime() + (period * 1000);
  971. if (bannedUntil > 8640000000000000) {
  972. bannedUntil = 8640000000000000;
  973. }
  974. bannedUntil = new Date(bannedUntil);
  975. var banObject = {
  976. bannedBy: user.profile.usernameL,
  977. bannedAt: new Date(Date.now()),
  978. bannedReason: reason,
  979. bannedUntil: bannedUntil
  980. };
  981. Meteor.users.update({"profile.usernameL": bannedUser.profile.usernameL}, {$set: {"punishments.ban": banObject}});
  982. Meteor.users.update({"profile.usernameL": bannedUser.profile.usernameL}, {$push: {"punishments.bans": banObject}});
  983. } else {
  984. throw new Meteor.Error(403, "Invalid permissions.");
  985. }
  986. },
  987. muteUser: function (username, period) {
  988. if (isAdmin() && !isBanned()) {
  989. var user = Meteor.user();
  990. var mutedUser = Meteor.users.findOne({"profile.usernameL": username.toLowerCase()});
  991. if (period === undefined || Number(period) === 0) {
  992. mutedUntil = 8640000000000000;
  993. } else {
  994. var mutedUntil = (new Date).getTime() + (period * 1000);
  995. if (mutedUntil > 8640000000000000) {
  996. mutedUntil = 8640000000000000;
  997. }
  998. }
  999. mutedUntil = new Date(mutedUntil);
  1000. var muteObject = {mutedBy: user.profile.usernameL, mutedAt: new Date(Date.now()), mutedUntil: mutedUntil};
  1001. Meteor.users.update({"profile.usernameL": mutedUser.profile.usernameL}, {$set: {"punishments.mute": muteObject}});
  1002. Meteor.users.update({"profile.usernameL": mutedUser.profile.usernameL}, {$push: {"punishments.mutes": muteObject}});
  1003. } else {
  1004. throw new Meteor.Error(403, "Invalid permissions.");
  1005. }
  1006. },
  1007. unbanUser: function (username) {
  1008. if (isAdmin() && !isBanned()) {
  1009. Meteor.users.update({"profile.usernameL": username.toLowerCase()}, {$unset: "punishments.ban"});
  1010. } else {
  1011. throw new Meteor.Error(403, "Invalid permissions.");
  1012. }
  1013. },
  1014. unsilenceUser: function (username) {
  1015. if (isAdmin() && !isBanned()) {
  1016. Meteor.users.update({"profile.usernameL": username.toLowerCase()}, {$unset: "punishments.mute"});
  1017. } else {
  1018. throw new Meteor.Error(403, "Invalid permissions.");
  1019. }
  1020. },
  1021. isBanned: function () {
  1022. return isBanned();
  1023. },
  1024. isMuted: function () {
  1025. return isMuted();
  1026. },
  1027. updateSettings: function (showRating) {
  1028. if (Meteor.userId() && !isBanned()) {
  1029. var user = Meteor.user();
  1030. if (showRating !== true && showRating !== false) {
  1031. showRating = true;
  1032. }
  1033. if (user.profile.settings) {
  1034. Meteor.users.update({"profile.username": user.profile.username}, {$set: {"profile.settings.showRating": showRating}});
  1035. } else {
  1036. Meteor.users.update({"profile.username": user.profile.username}, {$set: {"profile.settings": {showRating: showRating}}});
  1037. }
  1038. } else {
  1039. throw new Meteor.Error(403, "Invalid permissions.");
  1040. }
  1041. },
  1042. addAlert: function (description) {
  1043. if (isAdmin()) {
  1044. var username = Meteor.user().profile.username;
  1045. description = htmlEntities(description);
  1046. Alerts.insert({description: description, active: true, createdBy: username});
  1047. return true;
  1048. } else {
  1049. throw Meteor.Error(403, "Invalid permissions.");
  1050. }
  1051. },
  1052. sendMessage: function (type, message) {
  1053. if (Meteor.userId() && !isBanned() && !isMuted()) {
  1054. var user = Meteor.user();
  1055. var time = new Date();
  1056. var rawrank = user.profile.rank;
  1057. var username = user.profile.username;
  1058. var profanity = false;
  1059. message = htmlEntities(message);
  1060. if (!message.replace(/\s/g, "").length > 0) {
  1061. throw new Meteor.Error(406, "Message length cannot be 0.");
  1062. }
  1063. if (message.length > 300) {
  1064. throw new Meteor.Error(406, "Message length cannot be more than 300 characters long..");
  1065. }
  1066. else if (user.profile.rank === "admin") {
  1067. /*HTTP.call("GET", "http://www.wdyl.com/profanity?q=" + encodeURIComponent(message), function (err, res) {
  1068. if (res.content.indexOf("true") > -1) {
  1069. return true;
  1070. } else {*/
  1071. Chat.insert({
  1072. type: type,
  1073. rawrank: rawrank,
  1074. rank: "[A]",
  1075. message: message,
  1076. time: time,
  1077. username: username
  1078. });
  1079. /*}
  1080. });*/
  1081. return true;
  1082. }
  1083. else if (user.profile.rank === "moderator") {
  1084. /*HTTP.call("GET", "http://www.wdyl.com/profanity?q=" + encodeURIComponent(message), function (err, res) {
  1085. if (res.content.indexOf("true") > -1) {
  1086. return true;
  1087. } else {*/
  1088. Chat.insert({
  1089. type: type,
  1090. rawrank: rawrank,
  1091. rank: "[M]",
  1092. message: message,
  1093. time: time,
  1094. username: username
  1095. });
  1096. /*}
  1097. });*/
  1098. return true;
  1099. }
  1100. else {
  1101. /*HTTP.call("GET", "http://www.wdyl.com/profanity?q=" + encodeURIComponent(message), function (err, res) {
  1102. if (res.content.indexOf("true") > -1) {
  1103. return true;
  1104. } else {*/
  1105. Chat.insert({
  1106. type: type,
  1107. rawrank: rawrank,
  1108. rank: "",
  1109. message: message,
  1110. time: time,
  1111. username: username
  1112. });
  1113. /*}
  1114. });*/
  1115. return true;
  1116. }
  1117. } else {
  1118. throw new Meteor.Error(403, "Invalid permissions.");
  1119. }
  1120. },
  1121. likeSong: function (mid) {
  1122. if (Meteor.userId() && !isBanned()) {
  1123. var user = Meteor.user();
  1124. if (user.profile.liked.indexOf(mid) === -1) {
  1125. Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.liked": mid}});
  1126. Songs.update({mid: mid}, {$inc: {"likes": 1}})
  1127. } else {
  1128. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
  1129. Songs.update({mid: mid}, {$inc: {likes: -1}})
  1130. }
  1131. if (user.profile.disliked.indexOf(mid) !== -1) {
  1132. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
  1133. Songs.update({mid: mid}, {$inc: {dislikes: -1}})
  1134. }
  1135. return true;
  1136. } else {
  1137. throw new Meteor.Error(403, "Invalid permissions.");
  1138. }
  1139. },
  1140. dislikeSong: function (mid) {
  1141. if (Meteor.userId() && !isBanned()) {
  1142. var user = Meteor.user();
  1143. if (user.profile.disliked.indexOf(mid) === -1) {
  1144. Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.disliked": mid}});
  1145. Songs.update({mid: mid}, {$inc: {dislikes: 1}});
  1146. } else {
  1147. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
  1148. Songs.update({mid: mid}, {$inc: {dislikes: -1}});
  1149. }
  1150. if (user.profile.liked.indexOf(mid) !== -1) {
  1151. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
  1152. Songs.update({mid: mid}, {$inc: {likes: -1}});
  1153. }
  1154. return true;
  1155. } else {
  1156. throw new Meteor.Error(403, "Invalid permissions.");
  1157. }
  1158. },
  1159. voteSkip: function (type) {
  1160. if (Meteor.userId() && !isBanned()) {
  1161. var user = Meteor.user();
  1162. getStation(type, function (station) {
  1163. if (station.voted.indexOf(user.profile.username) === -1) {
  1164. station.voted.push(user.profile.username);
  1165. Rooms.update({type: type}, {$set: {votes: station.voted.length}});
  1166. if (station.voted.length === 3) {
  1167. station.skipSong();
  1168. }
  1169. } else {
  1170. throw new Meteor.Error(401, "Already voted.");
  1171. }
  1172. })
  1173. }
  1174. },
  1175. votePrivateSkip: function (name) {
  1176. if (Meteor.userId() && !isBanned()) {
  1177. var user = Meteor.user();
  1178. getPrivateStation(name, function (station) {
  1179. if (station.voted.indexOf(user.profile.username) === -1) {
  1180. station.voted.push(user.profile.username);
  1181. PrivateRooms.update({name: name}, {$set: {votes: station.voted.length}});
  1182. if (station.voted.length === 3) {
  1183. station.skipSong();
  1184. }
  1185. } else {
  1186. throw new Meteor.Error(401, "Already voted.");
  1187. }
  1188. })
  1189. }
  1190. },
  1191. submitReport: function (room, reportData) {
  1192. if (Meteor.userId() && !isBanned()) {
  1193. room = room.toLowerCase();
  1194. if (Rooms.find({type: room}).count() === 1) {
  1195. if (Reports.find({room: room}).count() === 0) {
  1196. Reports.insert({room: room, report: []});
  1197. }
  1198. if (reportData !== undefined) {
  1199. Reports.update({room: room}, {
  1200. $push: {
  1201. report: {
  1202. song: reportData.song,
  1203. type: reportData.type,
  1204. reason: reportData.reason,
  1205. other: reportData.other
  1206. }
  1207. }
  1208. });
  1209. return true;
  1210. } else {
  1211. throw new Meteor.Error(403, "Invalid data.");
  1212. }
  1213. } else {
  1214. throw new Meteor.Error(403, "Invalid genre.");
  1215. }
  1216. } else {
  1217. throw new Meteor.Error(403, "Invalid permissions.");
  1218. }
  1219. },
  1220. shufflePlaylist: function (type) {
  1221. if (isAdmin() && !isBanned()) {
  1222. getStation(type, function (station) {
  1223. if (station === undefined) {
  1224. throw new Meteor.Error(404, "Station not found.");
  1225. } else {
  1226. station.cancelTimer();
  1227. station.shufflePlaylist();
  1228. }
  1229. });
  1230. }
  1231. },
  1232. skipSong: function (type) {
  1233. if (isAdmin() && !isBanned()) {
  1234. getStation(type, function (station) {
  1235. if (station === undefined) {
  1236. throw new Meteor.Error(404, "Station not found.");
  1237. } else {
  1238. station.skipSong();
  1239. }
  1240. });
  1241. }
  1242. },
  1243. pauseRoom: function (type) {
  1244. if (isAdmin() && !isBanned()) {
  1245. getStation(type, function (station) {
  1246. if (station === undefined) {
  1247. throw new Meteor.Error(403, "Room doesn't exist.");
  1248. } else {
  1249. station.pauseRoom();
  1250. }
  1251. });
  1252. } else {
  1253. throw new Meteor.Error(403, "Invalid permissions.");
  1254. }
  1255. },
  1256. resumeRoom: function (type) {
  1257. if (isAdmin() && !isBanned()) {
  1258. getStation(type, function (station) {
  1259. if (station === undefined) {
  1260. throw new Meteor.Error(403, "Room doesn't exist.");
  1261. } else {
  1262. station.resumeRoom();
  1263. }
  1264. });
  1265. } else {
  1266. throw new Meteor.Error(403, "Invalid permissions.");
  1267. }
  1268. },
  1269. skipPrivateSong: function (name) {
  1270. if ((isAdmin() || isPrivateRoomOwner(name)) && !isBanned()) {
  1271. getPrivateStation(name, function (station) {
  1272. if (station === undefined) {
  1273. throw new Meteor.Error(404, "Station not found.");
  1274. } else {
  1275. station.skipSong();
  1276. }
  1277. });
  1278. }
  1279. },
  1280. pausePrivateRoom: function (name) {
  1281. if ((isAdmin() || isPrivateRoomOwner(name)) && !isBanned()) {
  1282. getPrivateStation(name, function (station) {
  1283. if (station === undefined) {
  1284. throw new Meteor.Error(403, "Room doesn't exist.");
  1285. } else {
  1286. station.pauseRoom();
  1287. }
  1288. });
  1289. } else {
  1290. throw new Meteor.Error(403, "Invalid permissions.");
  1291. }
  1292. },
  1293. resumePrivateRoom: function (name) {
  1294. if ((isAdmin() || isPrivateRoomOwner(name)) && !isBanned()) {
  1295. getPrivateStation(name, function (station) {
  1296. if (station === undefined) {
  1297. throw new Meteor.Error(403, "Room doesn't exist.");
  1298. } else {
  1299. station.resumeRoom();
  1300. }
  1301. });
  1302. } else {
  1303. throw new Meteor.Error(403, "Invalid permissions.");
  1304. }
  1305. },
  1306. addAllowedToPrivateRoom: function (name, allowed) {
  1307. if (isPrivateRoomOwner(name) && !isBanned()) {
  1308. getPrivateStation(name, function (station) {
  1309. if (station === undefined) {
  1310. throw new Meteor.Error(403, "Room doesn't exist.");
  1311. } else {
  1312. allowed = allowed.toLowerCase();
  1313. allowed = Meteor.users.findOne({"profile.usernameL": allowed})._id;
  1314. station.addAllowed(allowed);
  1315. }
  1316. });
  1317. } else {
  1318. throw new Meteor.Error(403, "Invalid permissions.");
  1319. }
  1320. },
  1321. removeAllowedFromPrivateRoom: function (name, allowed) {
  1322. if (isPrivateRoomOwner(name) && !isBanned()) {
  1323. getPrivateStation(name, function (station) {
  1324. if (station === undefined) {
  1325. throw new Meteor.Error(403, "Room doesn't exist.");
  1326. } else {
  1327. station.removeAllowed(allowed);
  1328. }
  1329. });
  1330. } else {
  1331. throw new Meteor.Error(403, "Invalid permissions.");
  1332. }
  1333. },
  1334. setPlaylistForPrivateRoom: function (name, playlist) {
  1335. if ((isPrivateRoomOwner(name)) && !isBanned()) {
  1336. getPrivateStation(name, function (station) {
  1337. if (station === undefined) {
  1338. throw new Meteor.Error(403, "Room doesn't exist.");
  1339. } else {
  1340. station.setPlaylist(playlist);
  1341. }
  1342. });
  1343. } else {
  1344. throw new Meteor.Error(403, "Invalid permissions.");
  1345. }
  1346. },
  1347. createUserMethod: function (formData, captchaData) {
  1348. if (!isBanned()) {
  1349. var verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(this.connection.clientAddress, captchaData);
  1350. if (!verifyCaptchaResponse.success) {
  1351. throw new Meteor.Error(422, 'reCAPTCHA Failed: ' + verifyCaptchaResponse.error);
  1352. } else {
  1353. Accounts.createUser({
  1354. username: formData.username,
  1355. email: formData.email,
  1356. password: formData.password
  1357. });
  1358. }
  1359. return true;
  1360. }
  1361. },
  1362. createArticle: function(data) {
  1363. if (!isBanned() && isModerator()) {
  1364. var userId = Meteor.userId();
  1365. var requiredProperties = ["title", "content", "anonymous"];
  1366. if (data !== undefined && Object.keys(data).length === requiredProperties.length) {
  1367. for (var property in requiredProperties) {
  1368. if (data[requiredProperties[property]] === undefined) {
  1369. throw new Meteor.Error(403, "Invalid data.");
  1370. }
  1371. }
  1372. if (data.anonymous === false) {
  1373. data.author = Meteor.user().profile.username
  1374. } else {
  1375. data.author = "A Musare Admin";
  1376. }
  1377. delete data.anonymous;
  1378. data.time = new Date();
  1379. data.content = htmlEntities(data.content);
  1380. News.insert(data, function(err, res) {
  1381. if (err) {
  1382. console.log(err);
  1383. throw err.sanitizedError;
  1384. } else {
  1385. return true;
  1386. }
  1387. });
  1388. } else {
  1389. throw new Meteor.Error(403, "Invalid data.");
  1390. }
  1391. } else {
  1392. throw new Meteor.Error(403, "Invalid permissions.");
  1393. }
  1394. },
  1395. addSongToQueue: function (songData) {
  1396. if (Meteor.userId() && !isBanned()) {
  1397. var userId = Meteor.userId();
  1398. var requiredProperties = ["title", "artist", "id", "genres"];
  1399. if (songData !== undefined && Object.keys(songData).length === requiredProperties.length) {
  1400. for (var property in requiredProperties) {
  1401. if (songData[requiredProperties[property]] === undefined) {
  1402. throw new Meteor.Error(403, "Invalid data.");
  1403. }
  1404. }
  1405. songData.duration = Number(getSongDuration(songData.title, songData.artist));
  1406. songData.img = getSongAlbumArt(songData.title, songData.artist) | "";
  1407. songData.skipDuration = 0;
  1408. songData.likes = 0;
  1409. songData.dislikes = 0;
  1410. songData.requestedBy = userId;
  1411. var mid = createUniqueSongId();
  1412. if (mid !== undefined) {
  1413. songData.mid = mid;
  1414. Queues.insert(songData, function(err, res) {
  1415. if (err) {
  1416. console.log(err);
  1417. throw err.sanitizedError;
  1418. } else {
  1419. var songsRequested = (Meteor.user().profile !== undefined && Meteor.user().profile.statistics !== undefined && Meteor.user().profile.statistics.songsRequested !== undefined) ? Meteor.user().profile.statistics.songsRequested : 0;
  1420. songsRequested++;
  1421. Meteor.users.update(Meteor.userId(), {$set: {"profile.statistics.songsRequested": songsRequested}}); // TODO Make mongo query use $inc correctly.
  1422. return true;
  1423. }
  1424. });
  1425. } else {
  1426. throw new Meteor.Error(500, "Am error occured.");
  1427. }
  1428. } else {
  1429. throw new Meteor.Error(403, "Invalid data.");
  1430. }
  1431. } else {
  1432. throw new Meteor.Error(403, "Invalid permissions.");
  1433. }
  1434. },
  1435. updateQueueSong: function (mid, newSong) {
  1436. if (isModerator() && !isBanned()) {
  1437. Queues.update({mid: mid}, {$set: {
  1438. "title": newSong.title,
  1439. "artist": newSong.artist,
  1440. "id": newSong.id,
  1441. "img": newSong.img,
  1442. "duration" : newSong.duration,
  1443. "skipDuration" : newSong.skipDuration,
  1444. "genres": newSong.genres
  1445. }}, function(err) {
  1446. console.log(err);
  1447. if (err) {
  1448. throw err.sanitizedError;
  1449. } else {
  1450. return true;
  1451. }
  1452. });
  1453. } else {
  1454. throw new Meteor.Error(403, "Invalid permissions.");
  1455. }
  1456. },
  1457. updatePlaylistSong: function (mid, newSong) {
  1458. if (isModerator() && !isBanned()) {
  1459. Songs.update({mid: mid}, {$set: {
  1460. "title": newSong.title,
  1461. "artist": newSong.artist,
  1462. "id": newSong.id,
  1463. "img": newSong.img,
  1464. "duration": newSong.duration,
  1465. "skipDuration": newSong.skipDuration,
  1466. "approvedBy": Meteor.userId(),
  1467. "genres": newSong.genres
  1468. }}, function(err) {
  1469. console.log(err);
  1470. if (err) {
  1471. throw err.sanitizedError;
  1472. } else {
  1473. return true;
  1474. }
  1475. });
  1476. return true;
  1477. } else {
  1478. throw new Meteor.Error(403, "Invalid permissions.");
  1479. }
  1480. },
  1481. removeSongFromQueue: function (mid) {
  1482. if (isModerator() && !isBanned()) {
  1483. Queues.remove({mid: mid});
  1484. } else {
  1485. throw new Meteor.Error(403, "Invalid permissions.");
  1486. }
  1487. },
  1488. removeSongFromPlaylist: function (type, mid) {
  1489. if (isModerator() && !isBanned()) {
  1490. Playlists.update({type: type}, {$pull: {songs: mid}});
  1491. } else {
  1492. throw new Meteor.Error(403, "Invalid permissions.");
  1493. }
  1494. },
  1495. deleteSong: function (mid) {
  1496. if (isModerator() && !isBanned()) {
  1497. Songs.remove({mid: mid})
  1498. } else {
  1499. throw new Meteor.Error(403, "Invalid permissions.");
  1500. }
  1501. },
  1502. deleteSong: function (mid) {
  1503. if (isModerator() && !isBanned()) {
  1504. Songs.remove({mid: mid})
  1505. } else {
  1506. throw new Meteor.Error(403, "Invalid permissions.");
  1507. }
  1508. },
  1509. addSongToPlaylist: function (songData) {
  1510. if (isModerator() && !isBanned()) {
  1511. var requiredProperties = ["_id", "mid", "id", "title", "artist", "duration", "skipDuration", "img", "likes", "dislikes", "requestedBy", "genres"];
  1512. if (songData !== undefined && Object.keys(songData).length === requiredProperties.length) {
  1513. for (var property in requiredProperties) {
  1514. if (songData[requiredProperties[property]] === undefined) {
  1515. throw new Meteor.Error(403, "Invalid data.");
  1516. }
  1517. }
  1518. delete songData._id;
  1519. songData.approvedBy = Meteor.userId();
  1520. Songs.insert(songData);
  1521. Queues.remove({mid: songData.mid});
  1522. songData.genres.forEach(function(genre) {
  1523. genre = genre.toLowerCase();
  1524. if (Playlists.findOne({type: genre}) === undefined) {
  1525. Playlists.insert({type: genre, songs: [songData.mid]});
  1526. } else {
  1527. Playlists.update({type: genre}, {$push: {songs: songData.mid}});
  1528. }
  1529. });
  1530. return true;
  1531. } else {
  1532. throw new Meteor.Error(403, "Invalid data.");
  1533. }
  1534. } else {
  1535. throw new Meteor.Error(403, "Invalid permissions.");
  1536. }
  1537. },
  1538. createRoom: function (display, tag, private, desc) {
  1539. if (isAdmin() && !isBanned()) {
  1540. createRoom(display, tag, private, desc);
  1541. } else {
  1542. throw new Meteor.Error(403, "Invalid permissions.");
  1543. }
  1544. },
  1545. createPrivateRoom: function (name, display, private, desc) {
  1546. if (Meteor.userId() && !isBanned()) {
  1547. name = name.toLowerCase();
  1548. createPrivateRoom(name, display, private, desc, Meteor.userId());
  1549. } else {
  1550. throw new Meteor.Error(403, "Invalid permissions.");
  1551. }
  1552. },
  1553. createPrivatePlaylist: function (name, display) {
  1554. if (Meteor.userId() && !isBanned()) {
  1555. name = name.toLowerCase();
  1556. if (PrivatePlaylists.findOne({name: name, owner: Meteor.userId()}) === undefined) {
  1557. PrivatePlaylists.insert({name: name, displayName: display, songs: [{id: "60ItHLz5WEA", duration: 213, title: "Alan Walker - Faded"}], owner: Meteor.userId()});
  1558. }
  1559. } else {
  1560. throw new Meteor.Error(403, "Invalid permissions.");
  1561. }
  1562. },
  1563. deleteRoom: function (type) {
  1564. if (isAdmin() && !isBanned()) {
  1565. Rooms.remove({type: type});
  1566. return true;
  1567. } else {
  1568. throw new Meteor.Error(403, "Invalid permissions.");
  1569. }
  1570. },
  1571. getUserNum: function () {
  1572. if (!isBanned()) {
  1573. return Object.keys(Meteor.default_server.sessions).length;
  1574. }
  1575. },
  1576. getTotalUsers: function () {
  1577. return Meteor.users.find().count();
  1578. },
  1579. updateRealName: function (realname) {
  1580. if (Meteor.userId()) {
  1581. var oldName = Meteor.users.findOne(Meteor.userId()).profile.realname;
  1582. Meteor.users.update(Meteor.userId(), {
  1583. $set: {"profile.realname": realname},
  1584. $push: {"profile.realnames": oldName}
  1585. });
  1586. } else {
  1587. throw new Meteor.Error(403, "Invalid permissions.");
  1588. }
  1589. },
  1590. updateUserName: function (newUserName) {
  1591. if (Meteor.userId()) {
  1592. var oldUsername = Meteor.users.findOne(Meteor.userId()).profile.username;
  1593. Meteor.users.update(Meteor.userId(), {
  1594. $set: {
  1595. "username": newUserName,
  1596. "profile.username": newUserName,
  1597. "profile.usernameL": newUserName.toLowerCase()
  1598. }, $push: {"profile.usernames": oldUsername}
  1599. });
  1600. } else {
  1601. throw new Meteor.Error(403, "Invalid permissions.");
  1602. }
  1603. },
  1604. /*updateUserRank: function(newRank){
  1605. if (Meteor.userId()) {
  1606. Meteor.users.update(Meteor.userId(), {$set: {"profile.rank": newRank}});
  1607. } else {
  1608. throw new Meteor.Error(403, "Invalid permissions.");
  1609. }
  1610. },*/
  1611. deleteAccount: function () {
  1612. if (Meteor.userId()) {
  1613. var user = Meteor.users.findOne(Meteor.userId());
  1614. Meteor.users.remove({_id: Meteor.userId()});
  1615. } else {
  1616. throw new Meteor.Error(403, "Invalid permissions.");
  1617. }
  1618. },
  1619. sendFeedback: function(message){
  1620. if(Meteor.userId() && !isBanned()) {
  1621. HTTP.call("GET", "http://www.wdyl.com/profanity?q=" + encodeURIComponent(message), function (err, res) {
  1622. if (res.content.indexOf("true") > -1) {
  1623. return true;
  1624. } else {
  1625. Feedback.insert({
  1626. "username": Meteor.user().profile.username,
  1627. "message": message,
  1628. "upvotes": 0,
  1629. "upvotedBy": []
  1630. })
  1631. }
  1632. });
  1633. }
  1634. },
  1635. upvoteFeedback: function(message){
  1636. if(Meteor.userId() && !isBanned()){
  1637. console.log(Feedback.findOne({"message": message}));
  1638. if(Feedback.findOne({"message": message}).upvotedBy.indexOf(Meteor.user().profile.username) === -1){
  1639. Feedback.update({"message": message}, {$inc: {"upvotes": 1}});
  1640. Feedback.update({"message": message}, {$push: {"upvotedBy": Meteor.user().profile.username}});
  1641. } else{
  1642. Feedback.update({"message": message}, {$inc: {"upvotes": -1}});
  1643. Feedback.update({"message": message}, {$pull: {"upvotedBy": Meteor.user().profile.username}});
  1644. }
  1645. }
  1646. },
  1647. deleteFeedback: function(message){
  1648. if(isAdmin() && !isBanned()){
  1649. Feedback.remove({"message": message});
  1650. } else {
  1651. throw new Meteor.Error(403, "Invalid permissions.");
  1652. }
  1653. },
  1654. updateFeedback: function(oldMessage, newMessage){
  1655. if(isAdmin() && !isBanned()){
  1656. Feedback.update({"message": oldMessage}, {$set: {"message": newMessage}});
  1657. } else {
  1658. throw new Meteor.Error(403, "Invalid permissions.");
  1659. }
  1660. },
  1661. editRoomDesc: function(type, description){
  1662. if(isAdmin() && !isBanned()){
  1663. Rooms.update({type: type}, {$set: {"roomDesc": description}});
  1664. } else {
  1665. throw new Meteor.Error(403, "Invalid permissions.");
  1666. }
  1667. },
  1668. removeReport: function(query, obj){
  1669. if(isAdmin() && !isBanned()){
  1670. Reports.update(query, {$pull: {"report": obj}});
  1671. } else {
  1672. throw new Meteor.Error(403, "Invalid permissions.");
  1673. }
  1674. }
  1675. });
  1676. Meteor.setInterval(function () {
  1677. checkUsersPR();
  1678. }, 10000);
  1679. Meteor.users.after.insert(function (err, user) {
  1680. Accounts.sendVerificationEmail(user._id);
  1681. });
  1682. function htmlEntities(str) {
  1683. return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
  1684. }