server.js 76 KB

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