server.js 68 KB

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