server.js 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869
  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 communityStations = [];
  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 getCommunityStation(name, cb) {
  116. communityStations.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 createCommunityStation(name, display, private, desc, owner) {
  145. if (CommunityStations.find({name: name}).count() === 0) {
  146. CommunityStations.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. communityStations.push(new CommunityStation(name));
  157. }
  158. });
  159. } else {
  160. return "Community 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 CommunityStation(name) {
  347. var usersObj = {};
  348. CommunityStations.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. CommunityStations.update({name: name}, {$push: {userList: username}});
  359. this.onStop(function () {
  360. usersObj[username]--;
  361. var room = CommunityStations.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. CommunityStations.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 = CommunityStations.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 = CommunityStations.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. CommunityStations.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. CommunityStations.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. CommunityStations.update({name: name}, {$set: {timePaused: 0}});
  423. this.songTimer();
  424. CommunityStations.update({name: name}, {$set: {currentSong: {song: songs[currentSong], started: startedAt}}});
  425. };
  426. CommunityStations.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 = CommunityStations.findOne({name: name}).state;
  442. this.pauseRoom = function () {
  443. if (state !== "paused") {
  444. timer.pause();
  445. CommunityStations.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. CommunityStations.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 privacy = CommunityStations.findOne({name: name}).privacy;
  469. this.privacy = privacy;
  470. this.setPrivacy = function (privacy) {
  471. if (self.privacy !== privacy) {
  472. self.privacy = privacy;
  473. return CommunityStations.update({name: name}, {$set: {"privacy": privacy}});
  474. }
  475. };
  476. this.setPlaylist = function (plName) {
  477. if (PrivatePlaylists.findOne({name: plName, owner: _room.owner}) !== undefined) {
  478. CommunityStations.update({name: name}, {$set: {"playlist": plName}});
  479. _room.playlist = plName;
  480. playlist = PrivatePlaylists.findOne({name: plName, owner: _room.owner});
  481. songs = playlist.songs;
  482. currentSong = 0;
  483. }
  484. };
  485. this.addAllowed = function (allowed) {
  486. if (_room.allowed.indexOf(allowed) === -1 && _room.owner !== allowed) {
  487. CommunityStations.update({name: name}, {$push: {allowed: allowed}});
  488. _room = CommunityStations.findOne({name: name});
  489. }
  490. };
  491. this.removeAllowed = function (allowed) {
  492. if (_room.allowed.indexOf(allowed) !== -1) {
  493. CommunityStations.update({name: name}, {$pull: {allowed: allowed}});
  494. _room = CommunityStations.findOne({name: name});
  495. }
  496. };
  497. this.skipSong();
  498. this.voted = [];
  499. }
  500. function shuffle(array) {
  501. var currentIndex = array.length, temporaryValue, randomIndex;
  502. // While there remain elements to shuffle...
  503. while (0 !== currentIndex) {
  504. // Pick a remaining element...
  505. randomIndex = Math.floor(Math.random() * currentIndex);
  506. currentIndex -= 1;
  507. // And swap it with the current element.
  508. temporaryValue = array[currentIndex];
  509. array[currentIndex] = array[randomIndex];
  510. array[randomIndex] = temporaryValue;
  511. }
  512. return array;
  513. }
  514. function Timer(callback, delay) {
  515. var timerId, start, remaining = delay;
  516. var timeWhenPaused = 0;
  517. var timePaused = new Date();
  518. this.pause = function () {
  519. Meteor.clearTimeout(timerId);
  520. remaining -= new Date() - start;
  521. timePaused = new Date();
  522. };
  523. this.resume = function () {
  524. start = new Date();
  525. Meteor.clearTimeout(timerId);
  526. timerId = Meteor.setTimeout(callback, remaining);
  527. timeWhenPaused += new Date() - timePaused;
  528. };
  529. this.timeWhenPaused = function () {
  530. return timeWhenPaused;
  531. };
  532. this.resume();
  533. }
  534. Meteor.users.deny({
  535. update: function () {
  536. return true;
  537. }
  538. });
  539. Meteor.users.deny({
  540. insert: function () {
  541. return true;
  542. }
  543. });
  544. Meteor.users.deny({
  545. remove: function () {
  546. return true;
  547. }
  548. });
  549. function getSongDuration(query, artistName) {
  550. var duration;
  551. var search = query;
  552. var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + encodeURIComponent(query) + '&type=track');
  553. for (var i in res.data) {
  554. for (var j in res.data[i].items) {
  555. if (search.indexOf(res.data[i].items[j].name) !== -1 && artistName.indexOf(res.data[i].items[j].artists[0].name) !== -1) {
  556. duration = res.data[i].items[j].duration_ms / 1000;
  557. return duration;
  558. }
  559. }
  560. }
  561. return 0;
  562. }
  563. function getSongDataYT(id) {
  564. var res = Meteor.http.get('https://www.googleapis.com/youtube/v3/videos?id=' + encodeURIComponent(id) + '&part=snippet,contentDetails&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY');
  565. if (res.data !== undefined && res.data.items.length !== 0) {
  566. var dur = res.data.items[0].contentDetails.duration;
  567. dur = dur.replace("PT", "");
  568. var durInSec = 0;
  569. dur = dur.replace(/([\d]*)M/, function(v, v2) {
  570. v2 = Number(v2);
  571. durInSec = (v2 * 60)
  572. return "";
  573. });
  574. dur = dur.replace(/([\d]*)S/, function(v, v2) {
  575. v2 = Number(v2);
  576. durInSec += v2;
  577. return "";
  578. });
  579. return {duration: durInSec, title: res.data.items[0].snippet.title};
  580. } else {
  581. return 0;
  582. }
  583. }
  584. function getSongAlbumArt(query, artistName) {
  585. var albumart;
  586. var search = query;
  587. var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + encodeURIComponent(query) + '&type=track');
  588. for (var i in res.data) {
  589. for (var j in res.data[i].items) {
  590. if (search.indexOf(res.data[i].items[j].name) !== -1 && artistName.indexOf(res.data[i].items[j].artists[0].name) !== -1) {
  591. albumart = res.data[i].items[j].album.images[1].url
  592. return albumart;
  593. }
  594. }
  595. }
  596. }
  597. //var room_types = ["edm", "nightcore"];
  598. var songsArr = [];
  599. Rooms.find({}).fetch().forEach(function (room) {
  600. var type = room.type;
  601. if (Playlists.find({type: type}).count() === 0) {
  602. Playlists.insert({type: type, songs: []});
  603. }
  604. if (Playlists.findOne({type: type}).songs.length === 0) {
  605. Playlists.update({type: type}, {$push: {songs: default_song.mid}}, function() {
  606. stations.push(new Station(type));
  607. });
  608. } else {
  609. stations.push(new Station(type));
  610. }
  611. });
  612. CommunityStations.find({}).fetch().forEach(function (room) {
  613. var name = room.name;
  614. communityStations.push(new CommunityStation(name));
  615. });
  616. Accounts.validateNewUser(function (user) {
  617. var username;
  618. if (user.services) {
  619. if (user.services.github) {
  620. username = user.services.github.username;
  621. } else if (user.services.facebook) {
  622. username = user.services.facebook.first_name;
  623. } else if (user.services.password) {
  624. username = user.username;
  625. }
  626. }
  627. if (Meteor.users.find({"profile.usernameL": username.toLowerCase()}).count() !== 0) {
  628. throw new Meteor.Error(403, "An account with that username already exists.");
  629. } else {
  630. return true;
  631. }
  632. });
  633. Accounts.onCreateUser(function (options, user) {
  634. var username;
  635. if (user.services) {
  636. if (user.services.github) {
  637. username = user.services.github.username;
  638. } else if (user.services.facebook) {
  639. username = user.services.facebook.first_name;
  640. } else if (user.services.password) {
  641. username = user.username;
  642. }
  643. }
  644. user.profile = {
  645. username: username,
  646. usernameL: username.toLowerCase(),
  647. rank: "default",
  648. liked: [],
  649. disliked: [],
  650. settings: {showRating: true},
  651. realname: ""
  652. };
  653. return user;
  654. });
  655. Meteor.publish("alerts", function () {
  656. return Alerts.find({active: true})
  657. });
  658. Meteor.publish("allAlerts", function () {
  659. return Alerts.find({})
  660. });
  661. Meteor.publish("news", function () {
  662. return News.find({})
  663. });
  664. Meteor.publish("userData", function (userId) {
  665. if (userId !== undefined) {
  666. return Meteor.users.find(userId, {fields: {"services.github.username": 1, "punishments": 1}});
  667. } else {
  668. return undefined;
  669. }
  670. });
  671. Meteor.publish("usernames", function () {
  672. return Meteor.users.find({}, {fields: {"profile.username": 1, "profile.usernameL": 1}})
  673. });
  674. Meteor.publish("playlists", function () {
  675. return Playlists.find({})
  676. });
  677. Meteor.publish("rooms", function () {
  678. return Rooms.find({});
  679. });
  680. Meteor.publish("community_stations", function () {
  681. var userId = this.userId;
  682. if (userId) {
  683. var user = Meteor.users.findOne(userId);
  684. if (user.profile.rank === "admin" || user.profile.rank === "moderator") {
  685. return CommunityStations.find({});
  686. } else {
  687. return CommunityStations.find({$or: [{owner: userId}, {privacy: "public"}]});
  688. }
  689. } else {
  690. return CommunityStations.find({privacy: "public"});
  691. }
  692. });
  693. Meteor.publish("community_station", function (name) {
  694. var userId = this.userId;
  695. if (userId) {
  696. var user = Meteor.users.findOne(userId);
  697. if (user.profile.rank === "admin" || user.profile.rank === "moderator") {
  698. return CommunityStations.find({name: name});
  699. } else {
  700. return CommunityStations.find({name: name, $or: [{owner: userId}, {privacy: "public"}]});
  701. }
  702. } else {
  703. return CommunityStations.find({name: name, $or: [{privacy: "public"}, {privacy: "unlisted"}]});
  704. }
  705. });
  706. Meteor.publish("private_playlists", function () {
  707. if (this.userId !== undefined) {
  708. return PrivatePlaylists.find({owner: this.userId});
  709. } else {
  710. return null;
  711. }
  712. });
  713. Meteor.publish("songsType", function (type) {
  714. return Songs.find({genres: type});
  715. });
  716. Meteor.publish("songs", function () {
  717. return Songs.find();
  718. });
  719. Meteor.publish("queues", function () {
  720. return Queues.find({});
  721. });
  722. Meteor.publish("reports", function () {
  723. return Reports.find({});
  724. });
  725. Meteor.publish("chat", function () {
  726. return Chat.find({});
  727. });
  728. Meteor.publish("userProfiles", function (username) {
  729. username = username.toLowerCase();
  730. console.log(username);
  731. console.log(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.liked": 1,
  738. "profile.disliked": 1,
  739. "profile.settings": 1,
  740. "profile.realname": 1
  741. }
  742. }).fetch());
  743. var settings = Meteor.users.findOne({"profile.usernameL": username}, {fields: {"profile.settings": 1}});
  744. if (settings !== undefined && settings.profile.settings) {
  745. settings = settings.profile.settings;
  746. if (settings.showRating === true) {
  747. return Meteor.users.find({"profile.usernameL": username}, {
  748. fields: {
  749. "profile.username": 1,
  750. "profile.usernameL": 1,
  751. "profile.rank": 1,
  752. createdAt: 1,
  753. "profile.liked": 1,
  754. "profile.disliked": 1,
  755. "profile.settings": 1,
  756. "profile.realname": 1
  757. }
  758. });
  759. }
  760. }
  761. return Meteor.users.find({"profile.usernameL": username}, {
  762. fields: {
  763. "profile.username": 1,
  764. "profile.usernameL": 1,
  765. "profile.rank": 1,
  766. createdAt: 1,
  767. "profile.settings": 1,
  768. "profile.realname": 1
  769. }
  770. });
  771. });
  772. Meteor.publish("isAdmin", function () {
  773. return Meteor.users.find({_id: this.userId, "profile.rank": "admin"});
  774. });
  775. Meteor.publish("isModerator", function () {
  776. return Meteor.users.find({_id: this.userId, "profile.rank": "moderator"});
  777. });
  778. Meteor.publish("feedback", function(){
  779. return Feedback.find();
  780. })
  781. function isCommunityStationOwner(name) {
  782. var room = CommunityStations.findOne({name: name});
  783. if (Meteor.userId() && room !== undefined && Meteor.userId() === room.owner) {
  784. return true;
  785. } else {
  786. return false;
  787. }
  788. }
  789. function isAdmin() {
  790. var userData = Meteor.users.find(Meteor.userId());
  791. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
  792. return true;
  793. } else {
  794. return false;
  795. }
  796. }
  797. function isModerator() {
  798. var userData = Meteor.users.find(Meteor.userId());
  799. if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "moderator") {
  800. return true;
  801. } else {
  802. return isAdmin();
  803. }
  804. }
  805. function isBanned() {
  806. var userData = Meteor.users.findOne(Meteor.userId());
  807. if (Meteor.userId() && userData !== undefined && userData.punishments !== undefined && userData.punishments.ban !== undefined) {
  808. var ban = userData.punishments.ban;
  809. if (new Date(ban.bannedUntil).getTime() <= new Date().getTime()) {
  810. Meteor.users.update(Meteor.userId(), {$unset: {"punishments.ban": ""}});
  811. return false;
  812. } else {
  813. return true;
  814. }
  815. } else {
  816. return false;
  817. }
  818. }
  819. function isMuted() {
  820. var userData = Meteor.users.findOne(Meteor.userId());
  821. if (Meteor.userId() && userData !== undefined && userData.punishments !== undefined && userData.punishments.mute !== undefined) {
  822. var mute = userData.punishments.mute;
  823. if (new Date(mute.bannedUntil).getTime() <= new Date().getTime()) {
  824. Meteor.users.update(Meteor.userId(), {$unset: {"punishments.mute": ""}});
  825. return false;
  826. } else {
  827. return true;
  828. }
  829. } else {
  830. return false;
  831. }
  832. }
  833. Meteor.updatedMethods({
  834. deleteCommunityStation: function(roomName) {
  835. if ((isAdmin() || isCommunityStationOwner(roomName)) && !isBanned()) {
  836. CommunityStations.remove({name: roomName});
  837. getCommunityStation(roomName, function(room) {
  838. room.cancelTimer();
  839. delete communityStations[communityStations.indexOf(room)];
  840. });
  841. } else {
  842. throw new Meteor.Error(403, "Invalid permissions.");
  843. }
  844. },
  845. changeCommunityStationDescription: function(roomName, newDescription) {
  846. if ((isAdmin() || isCommunityStationOwner(roomName)) && !isBanned()) {
  847. CommunityStations.update({name: roomName}, {$set: {roomDesc: newDescription}});
  848. } else {
  849. throw new Meteor.Error(403, "Invalid permissions.");
  850. }
  851. },
  852. changeCommunityStationDisplayName: function(roomName, newDisplayName) {
  853. if ((isAdmin() || isCommunityStationOwner(roomName)) && !isBanned()) {
  854. CommunityStations.update({name: roomName}, {$set: {displayName: newDisplayName}});
  855. } else {
  856. throw new Meteor.Error(403, "Invalid permissions.");
  857. }
  858. },
  859. changeCommunityStationPrivacy: function(roomName, newPrivacy) {
  860. if ((isAdmin() || isCommunityStationOwner(roomName)) && !isBanned()) {
  861. CommunityStations.update({name: roomName}, {$set: {privacy: newPrivacy}});
  862. } else {
  863. throw new Meteor.Error(403, "Invalid permissions.");
  864. }
  865. },
  866. setCommunityStationPartyMode: function(roomName, partyMode) {
  867. if ((isAdmin() || isCommunityStationOwner(roomName)) && !isBanned()) {
  868. partyMode = (typeof partyMode === "boolean") ? partyMode : false;
  869. CommunityStations.update({name: roomName}, {$set: {partyModeEnabled: partyMode}});
  870. } else {
  871. throw new Meteor.Error(403, "Invalid permissions.");
  872. }
  873. },
  874. addVideoToPrivatePlaylist: {
  875. code: function(name, id) {
  876. var pl = PrivatePlaylists.findOne({owner: Meteor.userId(), name: name});
  877. if (pl !== undefined) {
  878. var copy = false;
  879. pl.songs.forEach(function(song) {
  880. if (song.id === id) {
  881. copy = true;
  882. }
  883. });
  884. if (!copy) {
  885. var data = getSongDataYT(id);
  886. data.id = id;
  887. PrivatePlaylists.update({owner: Meteor.userId(), name: name}, {$push: {songs: data}});
  888. } else {
  889. throw new Meteor.Error(500, "Video is already in playlist.");
  890. }
  891. } else {
  892. throw new Meteor.Error(404, "Playlist not found.");
  893. }
  894. },
  895. requirements: ["login"]
  896. },
  897. removeVideoFromPrivatePlaylist: {
  898. code: function(name, id) {
  899. var pl = PrivatePlaylists.findOne({owner: Meteor.userId(), name: name});
  900. if (pl !== undefined) {
  901. var copy = false;
  902. var data;
  903. pl.songs.forEach(function(song) {
  904. if (song.id === id) {
  905. data = song;
  906. copy = true;
  907. }
  908. });
  909. if (copy) {
  910. PrivatePlaylists.update({owner: Meteor.userId(), name: name}, {$pull: {songs: data}});
  911. } else {
  912. throw new Meteor.Error(500, "Video is not in playlist.");
  913. }
  914. } else {
  915. throw new Meteor.Error(404, "Playlist not found.");
  916. }
  917. },
  918. requirements: ["login"]
  919. },
  920. moveVideoToTopOfPrivatePlaylist: {
  921. code: function(name, id) {
  922. var pl = PrivatePlaylists.findOne({owner: Meteor.userId(), name: name});
  923. if (pl !== undefined) {
  924. var songs = pl.songs;
  925. var song;
  926. songs.forEach(function(currentSong) {
  927. if (id === currentSong.id) {
  928. song = currentSong;
  929. }
  930. });
  931. console.log(song);
  932. if (song !== undefined) {
  933. var index = songs.indexOf(song);
  934. console.log(index);
  935. console.log(songs.length - 1);
  936. if (index > 0 && songs.length > 1) {
  937. PrivatePlaylists.update({owner: Meteor.userId(), name: name}, {$pull: {songs: song}});
  938. PrivatePlaylists.update({owner: Meteor.userId(), name: name}, {$push: {
  939. songs: {
  940. $each: [song],
  941. $position: 0
  942. }
  943. }});
  944. return true;
  945. } else {
  946. return false;
  947. }
  948. } else {
  949. throw new Meteor.Error(404, "Song not found.");
  950. }
  951. } else {
  952. throw new Meteor.Error(404, "Playlist not found.");
  953. }
  954. },
  955. requirements: ["login"]
  956. },
  957. moveVideoToBottomOfPrivatePlaylist: {
  958. code: function(name, id) {
  959. var pl = PrivatePlaylists.findOne({owner: Meteor.userId(), name: name});
  960. if (pl !== undefined) {
  961. var songs = pl.songs;
  962. var song;
  963. songs.forEach(function(currentSong) {
  964. if (id === currentSong.id) {
  965. song = currentSong;
  966. }
  967. });
  968. if (song !== undefined) {
  969. var index = songs.indexOf(song);
  970. if (index < (songs.length - 1) && index !== -1) {
  971. PrivatePlaylists.update({owner: Meteor.userId(), name: name}, {$pull: {songs: song}});
  972. PrivatePlaylists.update({owner: Meteor.userId(), name: name}, {$push: {songs: song}});
  973. return true;
  974. }
  975. } else {
  976. throw new Meteor.Error(404, "Song not found.");
  977. }
  978. } else {
  979. throw new Meteor.Error(404, "Playlist not found.");
  980. }
  981. },
  982. requirements: ["login"]
  983. },
  984. deletePrivatePlaylist: {
  985. code: function(name) {
  986. var pl = PrivatePlaylists.findOne({owner: Meteor.userId(), name: name});
  987. if (pl !== undefined) {
  988. PrivatePlaylists.remove({owner: Meteor.userId(), name: name});
  989. Deleted.insert({type: "PrivatePlaylist", data: pl});
  990. } else {
  991. throw new Meteor.Error(404, "Playlist not found.");
  992. }
  993. },
  994. requirements: ["login"]
  995. },
  996. renamePrivatePlaylistDisplayName: {
  997. code: function(name, newDisplayName) {
  998. var pl = PrivatePlaylists.findOne({owner: Meteor.userId(), name: name});
  999. if (pl !== undefined) {
  1000. if (PrivatePlaylists.findOne({owner: Meteor.userId(), displayName: newDisplayName}) === undefined) {
  1001. PrivatePlaylists.update({owner: Meteor.userId(), name: name}, {$set: {displayName: newDisplayName}});
  1002. } else {
  1003. throw new Meteor.Error(500, "A playlist with that display name already exists.");
  1004. }
  1005. } else {
  1006. throw new Meteor.Error(404, "Playlist not found.");
  1007. }
  1008. },
  1009. requirements: ["login"]
  1010. },
  1011. renamePrivatePlaylistName: {
  1012. code: function(name, newName) {
  1013. var pl = PrivatePlaylists.findOne({owner: Meteor.userId(), name: name});
  1014. if (pl !== undefined) {
  1015. if (PrivatePlaylists.findOne({owner: Meteor.userId(), name: newName}) === undefined) {
  1016. PrivatePlaylists.update({owner: Meteor.userId(), name: name}, {$set: {name: newName}});
  1017. } else {
  1018. throw new Meteor.Error(500, "A playlist with that name already exists.");
  1019. }
  1020. } else {
  1021. throw new Meteor.Error(404, "Playlist not found.");
  1022. }
  1023. },
  1024. requirements: ["login"]
  1025. },
  1026. activateAlert: {
  1027. code: function(id) {
  1028. Alerts.update(id, {$set: {active: true}});
  1029. },
  1030. requirements: ["admin"]
  1031. },
  1032. deactivateAlert: {
  1033. code: function(id) {
  1034. Alerts.update(id, {$set: {active: false}});
  1035. },
  1036. requirements: ["admin"]
  1037. },
  1038. deleteAlert: {
  1039. code: function(id) {
  1040. Alerts.remove(id);
  1041. },
  1042. requirements: ["admin"]
  1043. },
  1044. fetchSong: {
  1045. code: function(type) {
  1046. getStation(type, function (station) {
  1047. station.fetchSongs();
  1048. });
  1049. },
  1050. requirements: ["admin"]
  1051. },
  1052. removeSongs: {
  1053. code: function(type) {
  1054. getStation(type, function (station) {
  1055. station.removeSongs();
  1056. });
  1057. },
  1058. requirements: ["admin"]
  1059. },
  1060. lockRoom: {
  1061. code: function (type) {
  1062. getStation(type, function (station) {
  1063. station.lock();
  1064. });
  1065. },
  1066. requirements: ["admin"]
  1067. },
  1068. unlockRoom: {
  1069. code: function (type) {
  1070. getStation(type, function (station) {
  1071. station.unlock();
  1072. });
  1073. },
  1074. requirements: ["admin"]
  1075. },
  1076. banUser: {
  1077. code: function (username, period, reason) {
  1078. var user = Meteor.user();
  1079. var bannedUser = Meteor.users.findOne({"profile.usernameL": username.toLowerCase()});
  1080. var bannedUntil = (new Date).getTime() + (period * 1000);
  1081. if (bannedUntil > 8640000000000000) {
  1082. bannedUntil = 8640000000000000;
  1083. }
  1084. bannedUntil = new Date(bannedUntil);
  1085. var banObject = {
  1086. bannedBy: user.profile.usernameL,
  1087. bannedAt: new Date(Date.now()),
  1088. bannedReason: reason,
  1089. bannedUntil: bannedUntil
  1090. };
  1091. Meteor.users.update({"profile.usernameL": bannedUser.profile.usernameL}, {$set: {"punishments.ban": banObject}});
  1092. Meteor.users.update({"profile.usernameL": bannedUser.profile.usernameL}, {$push: {"punishments.bans": banObject}});
  1093. },
  1094. requirements: ["admin"]
  1095. },
  1096. muteUser: {
  1097. code: function (username, period) {
  1098. var user = Meteor.user();
  1099. var mutedUser = Meteor.users.findOne({"profile.usernameL": username.toLowerCase()});
  1100. if (period === undefined || Number(period) === 0) {
  1101. mutedUntil = 8640000000000000;
  1102. } else {
  1103. var mutedUntil = (new Date).getTime() + (period * 1000);
  1104. if (mutedUntil > 8640000000000000) {
  1105. mutedUntil = 8640000000000000;
  1106. }
  1107. }
  1108. mutedUntil = new Date(mutedUntil);
  1109. var muteObject = {mutedBy: user.profile.usernameL, mutedAt: new Date(Date.now()), mutedUntil: mutedUntil};
  1110. Meteor.users.update({"profile.usernameL": mutedUser.profile.usernameL}, {$set: {"punishments.mute": muteObject}});
  1111. Meteor.users.update({"profile.usernameL": mutedUser.profile.usernameL}, {$push: {"punishments.mutes": muteObject}});
  1112. },
  1113. requirements: ["admin"]
  1114. },
  1115. unbanUser: {
  1116. code: function (username) {
  1117. Meteor.users.update({"profile.usernameL": username.toLowerCase()}, {$unset: "punishments.ban"});
  1118. },
  1119. requirements: ["admin"]
  1120. },
  1121. unsilenceUser: {
  1122. code: function (username) {
  1123. Meteor.users.update({"profile.usernameL": username.toLowerCase()}, {$unset: "punishments.mute"});
  1124. },
  1125. requirements: ["admin"]
  1126. },
  1127. isBanned: function () {
  1128. return Meteor.isBanned();
  1129. },
  1130. isMuted: function () {
  1131. return isMuted();
  1132. },
  1133. updateSettings: {
  1134. code: function (showRating) {
  1135. var user = Meteor.user();
  1136. if (showRating !== true && showRating !== false) {
  1137. showRating = true;
  1138. }
  1139. if (user.profile.settings) {
  1140. Meteor.users.update({"profile.username": user.profile.username}, {$set: {"profile.settings.showRating": showRating}});
  1141. } else {
  1142. Meteor.users.update({"profile.username": user.profile.username}, {$set: {"profile.settings": {showRating: showRating}}});
  1143. }
  1144. },
  1145. requirements: ["login"]
  1146. },
  1147. addAlert: {
  1148. code: function (description) {
  1149. var username = Meteor.user().profile.username;
  1150. description = htmlEntities(description);
  1151. Alerts.insert({description: description, active: true, createdBy: username});
  1152. return true;
  1153. },
  1154. requirements: ["admin"]
  1155. },
  1156. sendMessage: {
  1157. code: function (type, message) {
  1158. var user = Meteor.user();
  1159. var time = new Date();
  1160. var rawrank = user.profile.rank;
  1161. var username = user.profile.username;
  1162. var profanity = false;
  1163. message = htmlEntities(message);
  1164. if (!message.replace(/\s/g, "").length > 0) {
  1165. throw new Meteor.Error(406, "Message length cannot be 0.");
  1166. }
  1167. if (message.length > 300) {
  1168. throw new Meteor.Error(406, "Message length cannot be more than 300 characters long..");
  1169. }
  1170. else if (user.profile.rank === "admin") {
  1171. /*HTTP.call("GET", "http://www.wdyl.com/profanity?q=" + encodeURIComponent(message), function (err, res) {
  1172. if (res.content.indexOf("true") > -1) {
  1173. return true;
  1174. } else {*/
  1175. Chat.insert({
  1176. type: type,
  1177. rawrank: rawrank,
  1178. rank: "[A]",
  1179. message: message,
  1180. time: time,
  1181. username: username
  1182. });
  1183. /*}
  1184. });*/
  1185. return true;
  1186. }
  1187. else if (user.profile.rank === "moderator") {
  1188. /*HTTP.call("GET", "http://www.wdyl.com/profanity?q=" + encodeURIComponent(message), function (err, res) {
  1189. if (res.content.indexOf("true") > -1) {
  1190. return true;
  1191. } else {*/
  1192. Chat.insert({
  1193. type: type,
  1194. rawrank: rawrank,
  1195. rank: "[M]",
  1196. message: message,
  1197. time: time,
  1198. username: username
  1199. });
  1200. /*}
  1201. });*/
  1202. return true;
  1203. }
  1204. else {
  1205. /*HTTP.call("GET", "http://www.wdyl.com/profanity?q=" + encodeURIComponent(message), function (err, res) {
  1206. if (res.content.indexOf("true") > -1) {
  1207. return true;
  1208. } else {*/
  1209. Chat.insert({
  1210. type: type,
  1211. rawrank: rawrank,
  1212. rank: "",
  1213. message: message,
  1214. time: time,
  1215. username: username
  1216. });
  1217. /*}
  1218. });*/
  1219. return true;
  1220. }
  1221. },
  1222. requirements: ["login", "noMute"]
  1223. },
  1224. likeSong: {
  1225. code: function (mid) {
  1226. var user = Meteor.user();
  1227. if (user.profile.liked.indexOf(mid) === -1) {
  1228. Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.liked": mid}});
  1229. Songs.update({mid: mid}, {$inc: {"likes": 1}})
  1230. } else {
  1231. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
  1232. Songs.update({mid: mid}, {$inc: {likes: -1}})
  1233. }
  1234. if (user.profile.disliked.indexOf(mid) !== -1) {
  1235. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
  1236. Songs.update({mid: mid}, {$inc: {dislikes: -1}})
  1237. }
  1238. return true;
  1239. },
  1240. requirements: ["login"]
  1241. },
  1242. dislikeSong: {
  1243. code: function (mid) {
  1244. var user = Meteor.user();
  1245. if (user.profile.disliked.indexOf(mid) === -1) {
  1246. Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.disliked": mid}});
  1247. Songs.update({mid: mid}, {$inc: {dislikes: 1}});
  1248. } else {
  1249. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
  1250. Songs.update({mid: mid}, {$inc: {dislikes: -1}});
  1251. }
  1252. if (user.profile.liked.indexOf(mid) !== -1) {
  1253. Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
  1254. Songs.update({mid: mid}, {$inc: {likes: -1}});
  1255. }
  1256. return true;
  1257. },
  1258. requirements: ["login"]
  1259. },
  1260. voteSkip: {
  1261. code: function (type) {
  1262. var user = Meteor.user();
  1263. getStation(type, function (station) {
  1264. if (station.voted.indexOf(user.profile.username) === -1) {
  1265. station.voted.push(user.profile.username);
  1266. Rooms.update({type: type}, {$set: {votes: station.voted.length}});
  1267. if (station.voted.length === 3) {
  1268. station.skipSong();
  1269. }
  1270. } else {
  1271. throw new Meteor.Error(401, "Already voted.");
  1272. }
  1273. });
  1274. },
  1275. requirements: ["login"]
  1276. },
  1277. votePrivateSkip: {
  1278. code: function (name) {
  1279. var user = Meteor.user();
  1280. getCommunityStation(name, function (station) {
  1281. if (station.voted.indexOf(user.profile.username) === -1) {
  1282. station.voted.push(user.profile.username);
  1283. CommunityStations.update({name: name}, {$set: {votes: station.voted.length}});
  1284. if (station.voted.length === 3) {
  1285. station.skipSong();
  1286. }
  1287. } else {
  1288. throw new Meteor.Error(401, "Already voted.");
  1289. }
  1290. });
  1291. },
  1292. requirements: ["login"]
  1293. },
  1294. submitReport: {
  1295. code: function (room, reportData) {
  1296. room = room.toLowerCase();
  1297. if (Rooms.find({type: room}).count() === 1) {
  1298. if (Reports.find({room: room}).count() === 0) {
  1299. Reports.insert({room: room, report: []});
  1300. }
  1301. if (reportData !== undefined) {
  1302. Reports.update({room: room}, {
  1303. $push: {
  1304. report: {
  1305. song: reportData.song,
  1306. type: reportData.type,
  1307. reason: reportData.reason,
  1308. other: reportData.other
  1309. }
  1310. }
  1311. });
  1312. return true;
  1313. } else {
  1314. throw new Meteor.Error(403, "Invalid data.");
  1315. }
  1316. } else {
  1317. throw new Meteor.Error(403, "Invalid genre.");
  1318. }
  1319. },
  1320. requirements: ["login"]
  1321. },
  1322. shufflePlaylist: {
  1323. code: function (type) {
  1324. getStation(type, function (station) {
  1325. if (station === undefined) {
  1326. throw new Meteor.Error(404, "Station not found.");
  1327. } else {
  1328. station.cancelTimer();
  1329. station.shufflePlaylist();
  1330. }
  1331. });
  1332. },
  1333. requirements: ["admin"]
  1334. },
  1335. skipSong: {
  1336. code: function (type) {
  1337. getStation(type, function (station) {
  1338. if (station === undefined) {
  1339. throw new Meteor.Error(404, "Station not found.");
  1340. } else {
  1341. station.skipSong();
  1342. }
  1343. });
  1344. },
  1345. requirements: ["admin"]
  1346. },
  1347. pauseRoom: {
  1348. code: function (type) {
  1349. getStation(type, function (station) {
  1350. if (station === undefined) {
  1351. throw new Meteor.Error(403, "Room doesn't exist.");
  1352. } else {
  1353. station.pauseRoom();
  1354. }
  1355. });
  1356. },
  1357. requirements: ["admin"]
  1358. },
  1359. resumeRoom: {
  1360. code: function (type) {
  1361. getStation(type, function (station) {
  1362. if (station === undefined) {
  1363. throw new Meteor.Error(403, "Room doesn't exist.");
  1364. } else {
  1365. station.resumeRoom();
  1366. }
  1367. });
  1368. },
  1369. requirements: ["admin"]
  1370. },
  1371. skipPrivateSong: function (name) {
  1372. if ((isAdmin() || isCommunityStationOwner(name)) && !isBanned()) {
  1373. getCommunityStation(name, function (station) {
  1374. if (station === undefined) {
  1375. throw new Meteor.Error(404, "Station not found.");
  1376. } else {
  1377. station.skipSong();
  1378. }
  1379. });
  1380. }
  1381. },
  1382. pauseCommunityStation: function (name) {
  1383. if ((isAdmin() || isCommunityStationOwner(name)) && !isBanned()) {
  1384. getCommunityStation(name, function (station) {
  1385. if (station === undefined) {
  1386. throw new Meteor.Error(403, "Room doesn't exist.");
  1387. } else {
  1388. station.pauseRoom();
  1389. }
  1390. });
  1391. } else {
  1392. throw new Meteor.Error(403, "Invalid permissions.");
  1393. }
  1394. },
  1395. resumeCommunityStation: function (name) {
  1396. if ((isAdmin() || isCommunityStationOwner(name)) && !isBanned()) {
  1397. getCommunityStation(name, function (station) {
  1398. if (station === undefined) {
  1399. throw new Meteor.Error(403, "Room doesn't exist.");
  1400. } else {
  1401. station.resumeRoom();
  1402. }
  1403. });
  1404. } else {
  1405. throw new Meteor.Error(403, "Invalid permissions.");
  1406. }
  1407. },
  1408. addAllowedToCommunityStation: function (name, allowed) {
  1409. if (isCommunityStationOwner(name) && !isBanned()) {
  1410. getCommunityStation(name, function (station) {
  1411. if (station === undefined) {
  1412. throw new Meteor.Error(403, "Room doesn't exist.");
  1413. } else {
  1414. allowed = allowed.toLowerCase();
  1415. allowed = Meteor.users.findOne({"profile.usernameL": allowed})._id;
  1416. station.addAllowed(allowed);
  1417. }
  1418. });
  1419. } else {
  1420. throw new Meteor.Error(403, "Invalid permissions.");
  1421. }
  1422. },
  1423. removeAllowedFromCommunityStation: function (name, allowed) {
  1424. if (isCommunityStationOwner(name) && !isBanned()) {
  1425. getCommunityStation(name, function (station) {
  1426. if (station === undefined) {
  1427. throw new Meteor.Error(403, "Room doesn't exist.");
  1428. } else {
  1429. station.removeAllowed(allowed);
  1430. }
  1431. });
  1432. } else {
  1433. throw new Meteor.Error(403, "Invalid permissions.");
  1434. }
  1435. },
  1436. setPlaylistForCommunityStation: function (name, playlist) {
  1437. if ((isCommunityStationOwner(name)) && !isBanned()) {
  1438. getCommunityStation(name, function (station) {
  1439. if (station === undefined) {
  1440. throw new Meteor.Error(403, "Room doesn't exist.");
  1441. } else {
  1442. station.setPlaylist(playlist);
  1443. }
  1444. });
  1445. } else {
  1446. throw new Meteor.Error(403, "Invalid permissions.");
  1447. }
  1448. },
  1449. createUserMethod: {
  1450. code: function (formData, captchaData) {
  1451. var verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(this.connection.clientAddress, captchaData);
  1452. if (!verifyCaptchaResponse.success) {
  1453. throw new Meteor.Error(422, 'reCAPTCHA Failed: ' + verifyCaptchaResponse.error);
  1454. } else {
  1455. Accounts.createUser({
  1456. username: formData.username,
  1457. email: formData.email,
  1458. password: formData.password
  1459. });
  1460. }
  1461. return true;
  1462. },
  1463. requirements: []
  1464. },
  1465. createArticle: {
  1466. code: function(data) {
  1467. var userId = Meteor.userId();
  1468. var requiredProperties = ["title", "content", "anonymous"];
  1469. if (data !== undefined && Object.keys(data).length === requiredProperties.length) {
  1470. for (var property in requiredProperties) {
  1471. if (data[requiredProperties[property]] === undefined) {
  1472. throw new Meteor.Error(403, "Invalid data.");
  1473. }
  1474. }
  1475. if (data.anonymous === false) {
  1476. data.author = Meteor.user().profile.username
  1477. } else {
  1478. data.author = "A Musare Admin";
  1479. }
  1480. delete data.anonymous;
  1481. data.time = new Date();
  1482. data.content = htmlEntities(data.content);
  1483. News.insert(data, function(err, res) {
  1484. if (err) {
  1485. console.log(err);
  1486. throw err.sanitizedError;
  1487. } else {
  1488. return true;
  1489. }
  1490. });
  1491. } else {
  1492. throw new Meteor.Error(403, "Invalid data.");
  1493. }
  1494. },
  1495. requirements: ["moderator"]
  1496. },
  1497. addSongToQueue: {
  1498. code: function (songData) {
  1499. var userId = Meteor.userId();
  1500. var requiredProperties = ["title", "artist", "id", "genres"];
  1501. if (songData !== undefined && Object.keys(songData).length === requiredProperties.length) {
  1502. for (var property in requiredProperties) {
  1503. if (songData[requiredProperties[property]] === undefined) {
  1504. throw new Meteor.Error(403, "Invalid data.");
  1505. }
  1506. }
  1507. songData.duration = Number(getSongDuration(songData.title, songData.artist));
  1508. songData.img = getSongAlbumArt(songData.title, songData.artist) | "";
  1509. songData.skipDuration = 0;
  1510. songData.likes = 0;
  1511. songData.dislikes = 0;
  1512. songData.requestedBy = userId;
  1513. var mid = createUniqueSongId();
  1514. if (mid !== undefined) {
  1515. songData.mid = mid;
  1516. Queues.insert(songData, function(err, res) {
  1517. if (err) {
  1518. console.log(err);
  1519. throw err.sanitizedError;
  1520. } else {
  1521. var songsRequested = (Meteor.user().profile !== undefined && Meteor.user().profile.statistics !== undefined && Meteor.user().profile.statistics.songsRequested !== undefined) ? Meteor.user().profile.statistics.songsRequested : 0;
  1522. songsRequested++;
  1523. Meteor.users.update(Meteor.userId(), {$set: {"profile.statistics.songsRequested": songsRequested}}); // TODO Make mongo query use $inc correctly.
  1524. return true;
  1525. }
  1526. });
  1527. } else {
  1528. throw new Meteor.Error(500, "Am error occured.");
  1529. }
  1530. } else {
  1531. throw new Meteor.Error(403, "Invalid data.");
  1532. }
  1533. },
  1534. requirements: ["login"]
  1535. },
  1536. updateQueueSong: {
  1537. code: function (mid, newSong) {
  1538. Queues.update({mid: mid}, {$set: {
  1539. "title": newSong.title,
  1540. "artist": newSong.artist,
  1541. "id": newSong.id,
  1542. "img": newSong.img,
  1543. "duration" : newSong.duration,
  1544. "skipDuration" : newSong.skipDuration,
  1545. "genres": newSong.genres
  1546. }}, function(err) {
  1547. console.log(err);
  1548. if (err) {
  1549. throw err.sanitizedError;
  1550. } else {
  1551. return true;
  1552. }
  1553. });
  1554. },
  1555. requirements: ["moderator"]
  1556. },
  1557. updatePlaylistSong: {
  1558. code: function (mid, newSong) {
  1559. Songs.update({mid: mid}, {$set: {
  1560. "title": newSong.title,
  1561. "artist": newSong.artist,
  1562. "id": newSong.id,
  1563. "img": newSong.img,
  1564. "duration": newSong.duration,
  1565. "skipDuration": newSong.skipDuration,
  1566. "approvedBy": Meteor.userId(),
  1567. "genres": newSong.genres
  1568. }}, function(err) {
  1569. console.log(err);
  1570. if (err) {
  1571. throw err.sanitizedError;
  1572. } else {
  1573. return true;
  1574. }
  1575. });
  1576. return true;
  1577. },
  1578. requirements: ["moderator"]
  1579. },
  1580. removeSongFromQueue: {
  1581. code: function (mid) {
  1582. Queues.remove({mid: mid});
  1583. },
  1584. requirements: ["moderator"]
  1585. },
  1586. removeSongFromPlaylist: {
  1587. code: function (type, mid) {
  1588. Playlists.update({type: type}, {$pull: {songs: mid}});
  1589. },
  1590. requirements: ["moderator"]
  1591. },
  1592. deleteSong: {
  1593. code: function (mid) {
  1594. Songs.remove({mid: mid})
  1595. },
  1596. requirements: ["moderator"]
  1597. },
  1598. addSongToPlaylist: {
  1599. code: function (songData) {
  1600. var requiredProperties = ["_id", "mid", "id", "title", "artist", "duration", "skipDuration", "img", "likes", "dislikes", "requestedBy", "genres"];
  1601. if (songData !== undefined && Object.keys(songData).length === requiredProperties.length) {
  1602. for (var property in requiredProperties) {
  1603. if (songData[requiredProperties[property]] === undefined) {
  1604. throw new Meteor.Error(403, "Invalid data.");
  1605. }
  1606. }
  1607. delete songData._id;
  1608. songData.approvedBy = Meteor.userId();
  1609. Songs.insert(songData);
  1610. Queues.remove({mid: songData.mid});
  1611. songData.genres.forEach(function(genre) {
  1612. genre = genre.toLowerCase();
  1613. if (Playlists.findOne({type: genre}) === undefined) {
  1614. Playlists.insert({type: genre, songs: [songData.mid]});
  1615. } else {
  1616. Playlists.update({type: genre}, {$push: {songs: songData.mid}});
  1617. }
  1618. });
  1619. return true;
  1620. } else {
  1621. throw new Meteor.Error(403, "Invalid data.");
  1622. }
  1623. },
  1624. requirements: ["moderator"]
  1625. },
  1626. createRoom: {
  1627. code: function (display, tag, private, desc) {
  1628. createRoom(display, tag, private, desc);
  1629. },
  1630. requirements: ["admin"]
  1631. },
  1632. createCommunityStation: {
  1633. code: function (name, display, private, desc) {
  1634. name = name.toLowerCase();
  1635. createCommunityStation(name, display, private, desc, Meteor.userId());
  1636. },
  1637. requirements: ["login"]
  1638. },
  1639. createPrivatePlaylist: {
  1640. code: function (name, display) {
  1641. name = name.toLowerCase();
  1642. if (PrivatePlaylists.findOne({name: name, owner: Meteor.userId()}) === undefined) {
  1643. PrivatePlaylists.insert({name: name, displayName: display, songs: [{id: "60ItHLz5WEA", duration: 213, title: "Alan Walker - Faded"}], owner: Meteor.userId()});
  1644. }
  1645. },
  1646. requirements: ["login"]
  1647. },
  1648. deleteRoom: {
  1649. code: function (type) {
  1650. Rooms.remove({type: type});
  1651. return true;
  1652. },
  1653. requirements: ["admin"]
  1654. },
  1655. getUserNum: {
  1656. code: function () {
  1657. return Object.keys(Meteor.default_server.sessions).length;
  1658. },
  1659. requirements: []
  1660. },
  1661. getTotalUsers: function () {
  1662. return Meteor.users.find().count();
  1663. },
  1664. updateRealName: {
  1665. code: function (realname) {
  1666. var oldName = Meteor.users.findOne(Meteor.userId()).profile.realname;
  1667. Meteor.users.update(Meteor.userId(), {
  1668. $set: {"profile.realname": realname},
  1669. $push: {"profile.realnames": oldName}
  1670. });
  1671. },
  1672. requirements: ["login"]
  1673. },
  1674. updateUserName: {
  1675. code: function (newUserName) {
  1676. var oldUsername = Meteor.users.findOne(Meteor.userId()).profile.username;
  1677. Meteor.users.update(Meteor.userId(), {
  1678. $set: {
  1679. "username": newUserName,
  1680. "profile.username": newUserName,
  1681. "profile.usernameL": newUserName.toLowerCase()
  1682. }, $push: {"profile.usernames": oldUsername}
  1683. });
  1684. },
  1685. requirements: ["login"]
  1686. },
  1687. /*updateUserRank: function(newRank){
  1688. if (Meteor.userId()) {
  1689. Meteor.users.update(Meteor.userId(), {$set: {"profile.rank": newRank}});
  1690. } else {
  1691. throw new Meteor.Error(403, "Invalid permissions.");
  1692. }
  1693. },*/
  1694. deleteAccount: {
  1695. code: function () {
  1696. var user = Meteor.users.findOne(Meteor.userId());
  1697. Meteor.users.remove({_id: Meteor.userId()});
  1698. },
  1699. requirements: ["login"]
  1700. },
  1701. sendFeedback: {
  1702. code: function(message){
  1703. /*HTTP.call("GET", "http://www.wdyl.com/profanity?q=" + encodeURIComponent(message), function (err, res) {
  1704. if (res.content.indexOf("true") > -1) {
  1705. return true;
  1706. } else {*/
  1707. Feedback.insert({
  1708. "username": Meteor.user().profile.username,
  1709. "message": message,
  1710. "upvotes": 0,
  1711. "upvotedBy": []
  1712. })
  1713. /*}
  1714. });*/
  1715. },
  1716. requirements: ["login"]
  1717. },
  1718. upvoteFeedback: {
  1719. code: function(message){
  1720. //TODO Fix feedback upvoting to use Id's
  1721. if(Feedback.findOne({"message": message}).upvotedBy.indexOf(Meteor.user().profile.username) === -1){
  1722. Feedback.update({"message": message}, {$inc: {"upvotes": 1}});
  1723. Feedback.update({"message": message}, {$push: {"upvotedBy": Meteor.user().profile.username}});
  1724. } else{
  1725. Feedback.update({"message": message}, {$inc: {"upvotes": -1}});
  1726. Feedback.update({"message": message}, {$pull: {"upvotedBy": Meteor.user().profile.username}});
  1727. }
  1728. },
  1729. requirements: ["login"]
  1730. },
  1731. deleteFeedback: {
  1732. code: function(message){
  1733. Feedback.remove({"message": message});
  1734. },
  1735. requirements: ["admin"]
  1736. },
  1737. updateFeedback: {
  1738. code: function(oldMessage, newMessage){
  1739. Feedback.update({"message": oldMessage}, {$set: {"message": newMessage}});
  1740. },
  1741. requirements: ["admin"]
  1742. },
  1743. editRoomDesc: {
  1744. code: function(type, description){
  1745. Rooms.update({type: type}, {$set: {"roomDesc": description}});
  1746. },
  1747. requirements: ["admin"]
  1748. },
  1749. removeReport: {
  1750. code: function(query, obj){
  1751. Reports.update(query, {$pull: {"report": obj}});
  1752. },
  1753. requirements: ["admin"]
  1754. }
  1755. });
  1756. Meteor.setInterval(function () {
  1757. checkUsersPR();
  1758. }, 10000);
  1759. Meteor.users.after.insert(function (err, user) {
  1760. Accounts.sendVerificationEmail(user._id);
  1761. });
  1762. function htmlEntities(str) {
  1763. return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
  1764. }