server.js 72 KB

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