server.js 76 KB

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