server.js 72 KB

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