server.js 60 KB

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