server.js 73 KB

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