server.js 71 KB

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