2
0

server.js 76 KB

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