12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877 |
- Meteor.startup(function () {
- reCAPTCHA.config({
- privatekey: '6LcVxg0TAAAAAI2fgIEEWHFxwNXeVIs8mzq5cfRM'
- });
- Avatar.setOptions({
- fallbackType: "initials",
- defaultImageUrl: "/notes.png",
- generateCSS: true,
- imageSizes: {
- 'header': 40
- }
- });
- var stations = [{tag: "edm", display: "EDM"}, {tag: "pop", display: "Pop"}]; //Rooms to be set on server startup
- for (var i in stations) {
- if (Rooms.find({type: stations[i]}).count() === 0) {
- createRoom(stations[i].display, stations[i].tag, false, "Room description goes here.");
- }
- }
- emojione.ascii = true;
- Accounts.config({
- sendVerificationEmail: true
- });
- if (Songs.find().count() === 0 || Songs.find({mid: default_song.mid}).count() === 0) {
- Songs.insert(default_song);
- }
- });
- var default_song = {
- id: "xKVcVSYmesU",
- mid: "ABCDEF",
- likes: 0,
- dislikes: 0,
- title: "Immortals",
- artist: "Fall Out Boy",
- img: "http://c.directlyrics.com/img/upload/fall-out-boy-sixth-album-cover.jpg",
- type: "YouTube",
- duration: 181,
- skipDuration: 0,
- requestedBy: "NONE",
- approvedBy: "GOD",
- genres: ["edm", "pop"]
- };
- var default_private_playlist = {
- name: "default",
- displayName: "Default Playlist",
- songs: [{id: "60ItHLz5WEA", duration: 213, title: "Alan Walker - Faded"}],
- owner: "NONE"
- };
- Alerts.update({active: true}, {$set: {active: false}}, {multi: true});
- var stations = [];
- var communityStations = [];
- var voteNum = 0;
- var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_";
- function createUniqueSongId() {
- var code = "";
- for (var i = 0; i < 6; i++) {
- code += chars[Math.floor(Math.random() * chars.length)];
- }
- if (Playlists.find({"songs.mid": code}).count() > 0) {
- return createUniqueSongId();
- } else {
- return code;
- }
- }
- function checkUsersPR() {
- var output = {};
- var connections = Meteor.server.stream_server.open_sockets;
- _.each(connections, function (connection) {
- // named subscriptions
- if (connection._meteorSession !== undefined && connection._meteorSession !== null) {
- var subs = connection._meteorSession._namedSubs;
- //var ip = connection.remoteAddress;
- var used_subs = [];
- for (var sub in subs) {
- var mySubName = subs[sub]._name;
- if (subs[sub]._params.length > 0) {
- mySubName += subs[sub]._params[0]; // assume one id parameter for now
- }
- if (used_subs.indexOf(mySubName) === -1) {
- used_subs.push(mySubName);
- if (!output[mySubName]) {
- output[mySubName] = 1;
- } else {
- output[mySubName] += 1;
- }
- }
- }
- }
- // there are also these 'universal subscriptions'
- //not sure what these are, i count none in my tests
- //var usubs = connection._meteorSession._universalSubs;
- });
- var emptyStations = [];
- stations.forEach(function (station) {
- emptyStations.push(station);
- });
- for (var key in output) {
- getStation(key, function (station) {
- emptyStations.splice(emptyStations.indexOf(station), 1);
- Rooms.update({type: key}, {$set: {users: output[key]}});
- });
- }
- emptyStations.forEach(function (emptyStation) {
- Rooms.update({type: emptyStation.type}, {$set: {users: 0}});
- });
- return output;
- }
- function getStation(type, cb) {
- stations.forEach(function (station) {
- if (station.type === type) {
- cb(station);
- return;
- }
- });
- }
- function getCommunityStation(name, cb) {
- communityStations.forEach(function (station) {
- if (station.name === name) {
- cb(station);
- return;
- }
- });
- }
- function createRoom(display, tag, private, desc) {
- var type = tag;
- if (Rooms.find({type: type}).count() === 0) {
- Rooms.insert({
- display: display,
- type: type,
- users: 0,
- private: private,
- currentSong: {song: default_song, started: 0},
- roomDesc: desc
- }, function (err) {
- if (err) {
- throw err;
- } else {
- stations.push(new Station(type));
- }
- });
- } else {
- return "Room already exists";
- }
- }
- function createCommunityStation(name, display, private, desc, owner) {
- if (CommunityStations.find({name: name}).count() === 0) {
- CommunityStations.insert({
- name: name,
- displayName: display,
- private: private,
- roomDesc: desc,
- owner: owner
- }, function (err) {
- if (err) {
- throw err;
- } else {
- communityStations.push(new CommunityStation(name));
- }
- });
- } else {
- return "Community room with that name already exists";
- }
- }
- function Station(type) {
- if (Playlists.find({type: type}).count() === 0) {
- Playlists.insert({type: type, songs: [default_song.mid], lastSong: 0});
- }
- if (Songs.find({genres: type}).count() > 0) {
- var list = Songs.find({genres: type}).fetch();
- list.forEach(function(song){
- if (Playlists.findOne({type: type, songs: song.mid}) === undefined) {
- Playlists.update({type: type}, {$push: {songs: song.mid}});
- }
- });
- }
- if (Playlists.findOne({type: type}).songs.length === 0) {
- Playlists.update({type: type}, {$push: {songs: default_song.mid}});
- }
- var usersObj = {};
- Rooms.update({type: type}, {$set: {userList: []}});
- Meteor.publish(type, function () {
- var user = Meteor.users.findOne(this.userId);
- if (this.userId !== undefined && user !== undefined && user.profile !== undefined && user.profile.username !== undefined) {
- var username = user.profile.username;
- if (usersObj[username] === undefined) {
- usersObj[username] = 1;
- } else {
- usersObj[username]++;
- }
- Rooms.update({type: type}, {$push: {userList: username}});
- this.onStop(function() {
- usersObj[username]--;
- var list = Rooms.findOne({type: type}).userList;
- var index = list.indexOf(username);
- if (index >= 0) {
- list.splice(index, 1);
- Rooms.update({type: type}, {$set: {userList: list}});
- }
- });
- }
- return undefined;
- });
- var self = this;
- var startedAt = Date.now();
- var playlist = Playlists.findOne({type: type});
- var songs = playlist.songs;
- var currentSong = playlist.lastSong;
- if (currentSong < (songs.length - 1)) {
- currentSong++;
- } else currentSong = 0;
- var currentMid = songs[currentSong];
- var song = Songs.findOne({mid: currentMid});
- if (song === undefined) {
- Playlists.remove({}, {$pull: {songs: currentMid}});
- song = default_song;
- }
- var res = Rooms.update({type: type}, {
- $set: {
- currentSong: {song: song, started: startedAt},
- users: 0
- }
- });
- this.skipSong = function () {
- self.voted = [];
- voteNum = 0;
- Rooms.update({type: type}, {$set: {votes: 0}});
- songs = Playlists.findOne({type: type}).songs;
- songs.forEach(function (mid, index) {
- if (mid === currentMid) {
- currentSong = index;
- }
- });
- if (currentSong < (songs.length - 1)) {
- currentSong++;
- } else currentSong = 0;
- if (songs);
- if (currentSong === 0) {
- this.shufflePlaylist();
- } else {
- currentMid = songs[currentSong];
- Playlists.update({type: type}, {$set: {lastSong: currentSong}});
- Rooms.update({type: type}, {$set: {timePaused: 0}});
- this.songTimer();
- Rooms.update({type: type}, {$set: {currentSong: {song: Songs.findOne({mid: songs[currentSong]}), started: startedAt}}});
- }
- };
- this.shufflePlaylist = function () {
- voteNum = 0;
- Rooms.update({type: type}, {$set: {votes: 0}});
- self.voted = [];
- songs = Playlists.findOne({type: type}).songs;
- currentSong = 0;
- Playlists.update({type: type}, {$set: {"songs": []}});
- songs = shuffle(songs);
- songs.forEach(function (song) {
- Playlists.update({type: type}, {$push: {"songs": song}});
- });
- currentMid = songs[currentSong];
- Playlists.update({type: type}, {$set: {lastSong: currentSong}});
- Rooms.update({type: type}, {$set: {timePaused: 0}});
- this.songTimer();
- Rooms.update({type: type}, {$set: {currentSong: {song: Songs.findOne({mid: songs[currentSong]}), started: startedAt}}});
- };
- Rooms.update({type: type}, {$set: {timePaused: 0}});
- var timer;
- var timerInitialised = false;
- this.songTimer = function () {
- if (state !== "paused") {
- startedAt = Date.now();
- if (timer !== undefined) {
- timer.pause();
- }
- timerInitialised = true;
- timer = new Timer(function () {
- self.skipSong();
- }, Songs.findOne({mid: songs[currentSong]}).duration * 1000);
- }
- };
- var state = Rooms.findOne({type: type}).state;
- this.pauseRoom = function () {
- if (state !== "paused") {
- timer.pause();
- Rooms.update({type: type}, {$set: {state: "paused"}});
- state = "paused";
- }
- };
- this.resumeRoom = function () {
- if (state !== "playing") {
- if (!timerInitialised) {
- timer = new Timer(function () {
- self.skipSong();
- }, Songs.findOne({mid: songs[currentSong]}).duration * 1000);
- }
- timer.resume();
- Rooms.update({type: type}, {$set: {state: "playing", timePaused: timer.timeWhenPaused()}});
- state = "playing";
- }
- };
- this.cancelTimer = function () {
- timer.pause();
- };
- this.getState = function () {
- return state;
- };
- this.type = type;
- var private = Rooms.findOne({type: type}).private;
- if (typeof private !== "boolean") {
- Rooms.update({type: type}, {$set: {"private": false}});
- private = false;
- }
- this.private = private;
- this.unlock = function () {
- if (self.private) {
- self.private = false;
- Rooms.update({type: type}, {$set: {"private": false}});
- }
- };
- this.lock = function () {
- if (!self.private) {
- self.private = true;
- Rooms.update({type: type}, {$set: {"private": true}});
- }
- };
- /* 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 */
- this.fetchSongs = function() {
- var genreSongs = Songs.find({genres: type}).fetch();
- genreSongs.forEach(function(song) {
- if (songs.indexOf(song.mid) === -1) {
- Playlists.update({type: type}, {$push: {songs: song.mid}});
- }
- });
- };
- /* This function removes all songs that are in the playlist but do not have the type of the playlist in their genre */
- this.removeSongs = function() {
- songs.forEach(function(mid) {
- var song = Songs.findOne({mid: mid});
- if (song === undefined || song.genres.indexOf(type) === -1) {
- Playlists.update({type: type}, {$pull: {songs: mid}});
- }
- });
- };
- this.removeSongs();
- this.fetchSongs();
- this.skipSong();
- this.voted = [];
- }
- function CommunityStation(name) {
- var usersObj = {};
- CommunityStations.update({name: name}, {$set: {userList: []}});
- Meteor.publish("pr_" + name, function () {
- var user = Meteor.users.findOne(this.userId);
- if (this.userId !== undefined && user !== undefined && user.profile !== undefined && user.profile.username !== undefined) {
- var username = user.profile.username;
- if (usersObj[username] === undefined) {
- usersObj[username] = 1;
- } else {
- usersObj[username]++;
- }
- CommunityStations.update({name: name}, {$push: {userList: username}});
- this.onStop(function () {
- usersObj[username]--;
- var room = CommunityStations.findOne({name: name});
- if (room !== undefined) {
- var list = room.userList;
- var index = list.indexOf(username);
- if (index >= 0) {
- list.splice(index, 1);
- CommunityStations.update({name: name}, {$set: {userList: list}});
- }
- }
- });
- }
- return undefined;
- });
- var self = this;
- var startedAt = Date.now();
- var _room = CommunityStations.findOne({name: name});
- var playlist;
- if (_room !== undefined) {
- playlist = PrivatePlaylists.findOne({name: _room.playlist, owner: _room.owner});
- }
- if (playlist === undefined) {
- playlist = default_private_playlist;
- }
- var songs = playlist.songs;
- var currentSong = 0;
- if (currentSong < (songs.length - 1)) {
- currentSong++;
- } else currentSong = 0;
- var currentId = songs[currentSong];
- var song = songs[songs.indexOf(currentId)];
- if (song === undefined) {
- song = {id: "60ItHLz5WEA", duration: 213, title: "Alan Walker - Faded"};
- }
- var res = CommunityStations.update({name: name}, {
- $set: {
- currentSong: {song: song, started: startedAt},
- users: 0
- }
- });
- this.skipSong = function () {
- self.voted = [];
- voteNum = 0;
- CommunityStations.update({name: name}, {$set: {votes: 0}});
- playlist = PrivatePlaylists.findOne({name: _room.playlist, owner: _room.owner});
- if (playlist === undefined) {
- playlist = default_private_playlist;
- }
- if (playlist !== undefined && playlist.songs.length === 0) {
- CommunityStations.update({name: name}, {$unset: {"playlist": 1}});
- playlist = default_private_playlist;
- }
- songs = playlist.songs;
- songs.forEach(function (id, index) {
- if (id === currentId) {
- currentSong = index;
- }
- });
- if (currentSong < (songs.length - 1)) {
- currentSong++;
- } else currentSong = 0;
- currentId = songs[currentSong];
- CommunityStations.update({name: name}, {$set: {timePaused: 0}});
- this.songTimer();
- CommunityStations.update({name: name}, {$set: {currentSong: {song: songs[currentSong], started: startedAt}}});
- };
- CommunityStations.update({name: name}, {$set: {timePaused: 0}});
- var timer;
- var timerInitialised = false;
- this.songTimer = function () {
- if (state !== "paused") {
- startedAt = Date.now();
- if (timer !== undefined) {
- timer.pause();
- }
- timerInitialised = true;
- timer = new Timer(function () {
- self.skipSong();
- }, songs[currentSong].duration * 1000);
- }
- };
- var state = CommunityStations.findOne({name: name}).state;
- this.pauseRoom = function () {
- if (state !== "paused") {
- timer.pause();
- CommunityStations.update({name: name}, {$set: {state: "paused"}});
- state = "paused";
- }
- };
- this.resumeRoom = function () {
- if (state !== "playing") {
- if (!timerInitialised) {
- timer = new Timer(function () {
- self.skipSong();
- }, songs[currentSong] * 1000);
- }
- timer.resume();
- CommunityStations.update({name: name}, {$set: {state: "playing", timePaused: timer.timeWhenPaused()}});
- state = "playing";
- }
- };
- this.cancelTimer = function () {
- timer.pause();
- };
- this.getState = function () {
- return state;
- };
- this.name = name;
- var privacy = CommunityStations.findOne({name: name}).privacy;
- this.privacy = privacy;
- this.setPrivacy = function (privacy) {
- if (self.privacy !== privacy) {
- self.privacy = privacy;
- return CommunityStations.update({name: name}, {$set: {"privacy": privacy}});
- }
- };
- this.setPlaylist = function (plName) {
- if (PrivatePlaylists.findOne({name: plName, owner: _room.owner}) !== undefined) {
- CommunityStations.update({name: name}, {$set: {"playlist": plName}});
- _room.playlist = plName;
- playlist = PrivatePlaylists.findOne({name: plName, owner: _room.owner});
- songs = playlist.songs;
- currentSong = 0;
- }
- };
- this.addAllowed = function (allowed) {
- if (_room.allowed.indexOf(allowed) === -1 && _room.owner !== allowed) {
- CommunityStations.update({name: name}, {$push: {allowed: allowed}});
- _room = CommunityStations.findOne({name: name});
- }
- };
- this.removeAllowed = function (allowed) {
- if (_room.allowed.indexOf(allowed) !== -1) {
- CommunityStations.update({name: name}, {$pull: {allowed: allowed}});
- _room = CommunityStations.findOne({name: name});
- }
- };
- this.skipSong();
- this.voted = [];
- }
- function shuffle(array) {
- var currentIndex = array.length, temporaryValue, randomIndex;
- // While there remain elements to shuffle...
- while (0 !== currentIndex) {
- // Pick a remaining element...
- randomIndex = Math.floor(Math.random() * currentIndex);
- currentIndex -= 1;
- // And swap it with the current element.
- temporaryValue = array[currentIndex];
- array[currentIndex] = array[randomIndex];
- array[randomIndex] = temporaryValue;
- }
- return array;
- }
- function Timer(callback, delay) {
- var timerId, start, remaining = delay;
- var timeWhenPaused = 0;
- var timePaused = new Date();
- this.pause = function () {
- Meteor.clearTimeout(timerId);
- remaining -= new Date() - start;
- timePaused = new Date();
- };
- this.resume = function () {
- start = new Date();
- Meteor.clearTimeout(timerId);
- timerId = Meteor.setTimeout(callback, remaining);
- timeWhenPaused += new Date() - timePaused;
- };
- this.timeWhenPaused = function () {
- return timeWhenPaused;
- };
- this.resume();
- }
- Meteor.users.deny({
- update: function () {
- return true;
- }
- });
- Meteor.users.deny({
- insert: function () {
- return true;
- }
- });
- Meteor.users.deny({
- remove: function () {
- return true;
- }
- });
- function getSongDuration(query, artistName) {
- var duration;
- var search = query;
- var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + encodeURIComponent(query) + '&type=track');
- for (var i in res.data) {
- for (var j in res.data[i].items) {
- if (search.indexOf(res.data[i].items[j].name) !== -1 && artistName.indexOf(res.data[i].items[j].artists[0].name) !== -1) {
- duration = res.data[i].items[j].duration_ms / 1000;
- return duration;
- }
- }
- }
- return 0;
- }
- function getSongDataYT(id) {
- var res = Meteor.http.get('https://www.googleapis.com/youtube/v3/videos?id=' + encodeURIComponent(id) + '&part=snippet,contentDetails&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY');
- if (res.data !== undefined && res.data.items.length !== 0) {
- var dur = res.data.items[0].contentDetails.duration;
- dur = dur.replace("PT", "");
- var durInSec = 0;
- dur = dur.replace(/([\d]*)M/, function(v, v2) {
- v2 = Number(v2);
- durInSec = (v2 * 60)
- return "";
- });
- dur = dur.replace(/([\d]*)S/, function(v, v2) {
- v2 = Number(v2);
- durInSec += v2;
- return "";
- });
- return {duration: durInSec, title: res.data.items[0].snippet.title};
- } else {
- return 0;
- }
- }
- function getSongAlbumArt(query, artistName) {
- var albumart;
- var search = query;
- var res = Meteor.http.get('https://api.spotify.com/v1/search?q=' + encodeURIComponent(query) + '&type=track');
- for (var i in res.data) {
- for (var j in res.data[i].items) {
- if (search.indexOf(res.data[i].items[j].name) !== -1 && artistName.indexOf(res.data[i].items[j].artists[0].name) !== -1) {
- albumart = res.data[i].items[j].album.images[1].url
- return albumart;
- }
- }
- }
- }
- //var room_types = ["edm", "nightcore"];
- var songsArr = [];
- Rooms.find({}).fetch().forEach(function (room) {
- var type = room.type;
- if (Playlists.find({type: type}).count() === 0) {
- Playlists.insert({type: type, songs: []});
- }
- if (Playlists.findOne({type: type}).songs.length === 0) {
- Playlists.update({type: type}, {$push: {songs: default_song.mid}}, function() {
- stations.push(new Station(type));
- });
- } else {
- stations.push(new Station(type));
- }
- });
- CommunityStations.find({}).fetch().forEach(function (room) {
- var name = room.name;
- communityStations.push(new CommunityStation(name));
- });
- Accounts.validateNewUser(function (user) {
- var username;
- if (user.services) {
- if (user.services.github) {
- username = user.services.github.username;
- } else if (user.services.facebook) {
- username = user.services.facebook.first_name;
- } else if (user.services.password) {
- username = user.username;
- }
- }
- if (Meteor.users.find({"profile.usernameL": username.toLowerCase()}).count() !== 0) {
- throw new Meteor.Error(403, "An account with that username already exists.");
- } else {
- return true;
- }
- });
- Accounts.onCreateUser(function (options, user) {
- var username;
- if (user.services) {
- if (user.services.github) {
- username = user.services.github.username;
- } else if (user.services.facebook) {
- username = user.services.facebook.first_name;
- } else if (user.services.password) {
- username = user.username;
- }
- }
- user.profile = {
- username: username,
- usernameL: username.toLowerCase(),
- rank: "default",
- liked: [],
- disliked: [],
- settings: {showRating: true},
- realname: ""
- };
- return user;
- });
- Meteor.publish("alerts", function () {
- return Alerts.find({active: true})
- });
- Meteor.publish("allAlerts", function () {
- return Alerts.find({})
- });
- Meteor.publish("news", function () {
- return News.find({})
- });
- Meteor.publish("userData", function (userId) {
- if (userId !== undefined) {
- return Meteor.users.find(userId, {fields: {"services.github.username": 1, "punishments": 1}});
- } else {
- return undefined;
- }
- });
- Meteor.publish("usernames", function () {
- return Meteor.users.find({}, {fields: {"profile.username": 1, "profile.usernameL": 1}})
- });
- Meteor.publish("playlists", function () {
- return Playlists.find({})
- });
- Meteor.publish("rooms", function () {
- return Rooms.find({});
- });
- Meteor.publish("community_stations", function () {
- var userId = this.userId;
- if (userId) {
- var user = Meteor.users.findOne(userId);
- if (user.profile.rank === "admin" || user.profile.rank === "moderator") {
- return CommunityStations.find({});
- } else {
- return CommunityStations.find({$or: [{owner: userId}, {privacy: "public"}]});
- }
- } else {
- return CommunityStations.find({privacy: "public"});
- }
- });
- Meteor.publish("community_station", function (name) {
- var userId = this.userId;
- if (userId) {
- var user = Meteor.users.findOne(userId);
- if (user.profile.rank === "admin" || user.profile.rank === "moderator") {
- return CommunityStations.find({name: name});
- } else {
- return CommunityStations.find({name: name, $or: [{owner: userId}, {privacy: "public"}]});
- }
- } else {
- return CommunityStations.find({name: name, $or: [{privacy: "public"}, {privacy: "unlisted"}]});
- }
- });
- Meteor.publish("private_playlists", function () {
- if (this.userId !== undefined) {
- return PrivatePlaylists.find({owner: this.userId});
- } else {
- return null;
- }
- });
- Meteor.publish("songsType", function (type) {
- return Songs.find({genres: type});
- });
- Meteor.publish("songs", function () {
- return Songs.find();
- });
- Meteor.publish("queues", function () {
- return Queues.find({});
- });
- Meteor.publish("reports", function () {
- return Reports.find({});
- });
- Meteor.publish("chat", function () {
- return Chat.find({});
- });
- Meteor.publish("userProfiles", function (username) {
- username = username.toLowerCase();
- console.log(username);
- console.log(Meteor.users.find({"profile.usernameL": username}, {
- fields: {
- "profile.username": 1,
- "profile.usernameL": 1,
- "profile.rank": 1,
- createdAt: 1,
- "profile.liked": 1,
- "profile.disliked": 1,
- "profile.settings": 1,
- "profile.realname": 1
- }
- }).fetch());
- var settings = Meteor.users.findOne({"profile.usernameL": username}, {fields: {"profile.settings": 1}});
- if (settings !== undefined && settings.profile.settings) {
- settings = settings.profile.settings;
- if (settings.showRating === true) {
- return Meteor.users.find({"profile.usernameL": username}, {
- fields: {
- "profile.username": 1,
- "profile.usernameL": 1,
- "profile.rank": 1,
- createdAt: 1,
- "profile.liked": 1,
- "profile.disliked": 1,
- "profile.settings": 1,
- "profile.realname": 1
- }
- });
- }
- }
- return Meteor.users.find({"profile.usernameL": username}, {
- fields: {
- "profile.username": 1,
- "profile.usernameL": 1,
- "profile.rank": 1,
- createdAt: 1,
- "profile.settings": 1,
- "profile.realname": 1
- }
- });
- });
- Meteor.publish("isAdmin", function () {
- return Meteor.users.find({_id: this.userId, "profile.rank": "admin"});
- });
- Meteor.publish("isModerator", function () {
- return Meteor.users.find({_id: this.userId, "profile.rank": "moderator"});
- });
- Meteor.publish("feedback", function(){
- return Feedback.find();
- })
- function isCommunityStationOwner(name) {
- var room = CommunityStations.findOne({name: name});
- if (Meteor.userId() && room !== undefined && Meteor.userId() === room.owner) {
- return true;
- } else {
- return false;
- }
- }
- function isAdmin() {
- var userData = Meteor.users.find(Meteor.userId());
- if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "admin") {
- return true;
- } else {
- return false;
- }
- }
- function isModerator() {
- var userData = Meteor.users.find(Meteor.userId());
- if (Meteor.userId() && userData.count !== 0 && userData.fetch()[0].profile.rank === "moderator") {
- return true;
- } else {
- return isAdmin();
- }
- }
- function isBanned() {
- var userData = Meteor.users.findOne(Meteor.userId());
- if (Meteor.userId() && userData !== undefined && userData.punishments !== undefined && userData.punishments.ban !== undefined) {
- var ban = userData.punishments.ban;
- if (new Date(ban.bannedUntil).getTime() <= new Date().getTime()) {
- Meteor.users.update(Meteor.userId(), {$unset: {"punishments.ban": ""}});
- return false;
- } else {
- return true;
- }
- } else {
- return false;
- }
- }
- function isMuted() {
- var userData = Meteor.users.findOne(Meteor.userId());
- if (Meteor.userId() && userData !== undefined && userData.punishments !== undefined && userData.punishments.mute !== undefined) {
- var mute = userData.punishments.mute;
- if (new Date(mute.bannedUntil).getTime() <= new Date().getTime()) {
- Meteor.users.update(Meteor.userId(), {$unset: {"punishments.mute": ""}});
- return false;
- } else {
- return true;
- }
- } else {
- return false;
- }
- }
- Meteor.updatedMethods({
- deleteCommunityStation: function(roomName) {
- if ((isAdmin() || isCommunityStationOwner(roomName)) && !isBanned()) {
- CommunityStations.remove({name: roomName});
- getCommunityStation(roomName, function(room) {
- room.cancelTimer();
- delete communityStations[communityStations.indexOf(room)];
- });
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- changeCommunityStationDescription: function(roomName, newDescription) {
- if ((isAdmin() || isCommunityStationOwner(roomName)) && !isBanned()) {
- CommunityStations.update({name: roomName}, {$set: {roomDesc: newDescription}});
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- changeCommunityStationDisplayName: function(roomName, newDisplayName) {
- if ((isAdmin() || isCommunityStationOwner(roomName)) && !isBanned()) {
- CommunityStations.update({name: roomName}, {$set: {displayName: newDisplayName}});
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- changeCommunityStationPrivacy: function(roomName, newPrivacy) {
- if ((isAdmin() || isCommunityStationOwner(roomName)) && !isBanned()) {
- CommunityStations.update({name: roomName}, {$set: {privacy: newPrivacy}});
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- addVideoToPrivatePlaylist: {
- code: function(name, id) {
- var pl = PrivatePlaylists.findOne({owner: Meteor.userId(), name: name});
- if (pl !== undefined) {
- var copy = false;
- pl.songs.forEach(function(song) {
- if (song.id === id) {
- copy = true;
- }
- });
- if (!copy) {
- var data = getSongDataYT(id);
- data.id = id;
- PrivatePlaylists.update({owner: Meteor.userId(), name: name}, {$push: {songs: data}});
- } else {
- throw new Meteor.Error(500, "Video is already in playlist.");
- }
- } else {
- throw new Meteor.Error(404, "Playlist not found.");
- }
- },
- requirements: ["login"]
- },
- removeVideoFromPrivatePlaylist: {
- code: function(name, id) {
- var pl = PrivatePlaylists.findOne({owner: Meteor.userId(), name: name});
- if (pl !== undefined) {
- var copy = false;
- var data;
- pl.songs.forEach(function(song) {
- if (song.id === id) {
- data = song;
- copy = true;
- }
- });
- if (copy) {
- PrivatePlaylists.update({owner: Meteor.userId(), name: name}, {$pull: {songs: data}});
- } else {
- throw new Meteor.Error(500, "Video is not in playlist.");
- }
- } else {
- throw new Meteor.Error(404, "Playlist not found.");
- }
- },
- requirements: ["login"]
- },
- moveVideoToTopOfPrivatePlaylist: {
- code: function(name, id) {
- var pl = PrivatePlaylists.findOne({owner: Meteor.userId(), name: name});
- if (pl !== undefined) {
- var songs = pl.songs;
- var song;
- songs.forEach(function(currentSong) {
- if (id === currentSong.id) {
- song = currentSong;
- }
- });
- console.log(song);
- if (song !== undefined) {
- var index = songs.indexOf(song);
- console.log(index);
- console.log(songs.length - 1);
- if (index > 0 && songs.length > 1) {
- PrivatePlaylists.update({owner: Meteor.userId(), name: name}, {$pull: {songs: song}});
- PrivatePlaylists.update({owner: Meteor.userId(), name: name}, {$push: {
- songs: {
- $each: [song],
- $position: 0
- }
- }});
- return true;
- } else {
- return false;
- }
- } else {
- throw new Meteor.Error(404, "Song not found.");
- }
- } else {
- throw new Meteor.Error(404, "Playlist not found.");
- }
- },
- requirements: ["login"]
- },
- moveVideoToBottomOfPrivatePlaylist: {
- code: function(name, id) {
- var pl = PrivatePlaylists.findOne({owner: Meteor.userId(), name: name});
- if (pl !== undefined) {
- var songs = pl.songs;
- var song;
- songs.forEach(function(currentSong) {
- if (id === currentSong.id) {
- song = currentSong;
- }
- });
- if (song !== undefined) {
- var index = songs.indexOf(song);
- if (index < (songs.length - 1) && index !== -1) {
- PrivatePlaylists.update({owner: Meteor.userId(), name: name}, {$pull: {songs: song}});
- PrivatePlaylists.update({owner: Meteor.userId(), name: name}, {$push: {songs: song}});
- return true;
- }
- } else {
- throw new Meteor.Error(404, "Song not found.");
- }
- } else {
- throw new Meteor.Error(404, "Playlist not found.");
- }
- },
- requirements: ["login"]
- },
- deletePrivatePlaylist: {
- code: function(name) {
- var pl = PrivatePlaylists.findOne({owner: Meteor.userId(), name: name});
- if (pl !== undefined) {
- PrivatePlaylists.remove({owner: Meteor.userId(), name: name});
- Deleted.insert({type: "PrivatePlaylist", data: pl});
- } else {
- throw new Meteor.Error(404, "Playlist not found.");
- }
- },
- requirements: ["login"]
- },
- renamePrivatePlaylistDisplayName: {
- code: function(name, newDisplayName) {
- var pl = PrivatePlaylists.findOne({owner: Meteor.userId(), name: name});
- if (pl !== undefined) {
- if (PrivatePlaylists.findOne({owner: Meteor.userId(), displayName: newDisplayName}) === undefined) {
- PrivatePlaylists.update({owner: Meteor.userId(), name: name}, {$set: {displayName: newDisplayName}});
- } else {
- throw new Meteor.Error(500, "A playlist with that display name already exists.");
- }
- } else {
- throw new Meteor.Error(404, "Playlist not found.");
- }
- },
- requirements: ["login"]
- },
- renamePrivatePlaylistName: {
- code: function(name, newName) {
- var pl = PrivatePlaylists.findOne({owner: Meteor.userId(), name: name});
- if (pl !== undefined) {
- if (PrivatePlaylists.findOne({owner: Meteor.userId(), name: newName}) === undefined) {
- PrivatePlaylists.update({owner: Meteor.userId(), name: name}, {$set: {name: newName}});
- } else {
- throw new Meteor.Error(500, "A playlist with that name already exists.");
- }
- } else {
- throw new Meteor.Error(404, "Playlist not found.");
- }
- },
- requirements: ["login"]
- },
- activateAlert: {
- code: function(id) {
- Alerts.update(id, {$set: {active: true}});
- },
- requirements: ["admin"]
- },
- deactivateAlert: {
- code: function(id) {
- Alerts.update(id, {$set: {active: false}});
- },
- requirements: ["admin"]
- },
- deleteAlert: {
- code: function(id) {
- Alerts.remove(id);
- },
- requirements: ["admin"]
- },
- fetchSong: {
- code: function(type) {
- getStation(type, function (station) {
- station.fetchSongs();
- });
- },
- requirements: ["admin"]
- },
- removeSongs: {
- code: function(type) {
- getStation(type, function (station) {
- station.removeSongs();
- });
- },
- requirements: ["admin"]
- },
- lockRoom: {
- code: function (type) {
- getStation(type, function (station) {
- station.lock();
- });
- },
- requirements: ["admin"]
- },
- unlockRoom: {
- code: function (type) {
- getStation(type, function (station) {
- station.unlock();
- });
- },
- requirements: ["admin"]
- },
- banUser: {
- code: function (username, period, reason) {
- var user = Meteor.user();
- var bannedUser = Meteor.users.findOne({"profile.usernameL": username.toLowerCase()});
- var bannedUntil = (new Date).getTime() + (period * 1000);
- if (bannedUntil > 8640000000000000) {
- bannedUntil = 8640000000000000;
- }
- bannedUntil = new Date(bannedUntil);
- var banObject = {
- bannedBy: user.profile.usernameL,
- bannedAt: new Date(Date.now()),
- bannedReason: reason,
- bannedUntil: bannedUntil
- };
- Meteor.users.update({"profile.usernameL": bannedUser.profile.usernameL}, {$set: {"punishments.ban": banObject}});
- Meteor.users.update({"profile.usernameL": bannedUser.profile.usernameL}, {$push: {"punishments.bans": banObject}});
- },
- requirements: ["admin"]
- },
- muteUser: {
- code: function (username, period) {
- var user = Meteor.user();
- var mutedUser = Meteor.users.findOne({"profile.usernameL": username.toLowerCase()});
- if (period === undefined || Number(period) === 0) {
- mutedUntil = 8640000000000000;
- } else {
- var mutedUntil = (new Date).getTime() + (period * 1000);
- if (mutedUntil > 8640000000000000) {
- mutedUntil = 8640000000000000;
- }
- }
- mutedUntil = new Date(mutedUntil);
- var muteObject = {mutedBy: user.profile.usernameL, mutedAt: new Date(Date.now()), mutedUntil: mutedUntil};
- Meteor.users.update({"profile.usernameL": mutedUser.profile.usernameL}, {$set: {"punishments.mute": muteObject}});
- Meteor.users.update({"profile.usernameL": mutedUser.profile.usernameL}, {$push: {"punishments.mutes": muteObject}});
- },
- requirements: ["admin"]
- },
- unbanUser: {
- code: function (username) {
- Meteor.users.update({"profile.usernameL": username.toLowerCase()}, {$unset: "punishments.ban"});
- },
- requirements: ["admin"]
- },
- unsilenceUser: {
- code: function (username) {
- Meteor.users.update({"profile.usernameL": username.toLowerCase()}, {$unset: "punishments.mute"});
- },
- requirements: ["admin"]
- },
- isBanned: function () {
- return Meteor.isBanned();
- },
- isMuted: function () {
- return isMuted();
- },
- updateSettings: {
- code: function (showRating) {
- var user = Meteor.user();
- if (showRating !== true && showRating !== false) {
- showRating = true;
- }
- if (user.profile.settings) {
- Meteor.users.update({"profile.username": user.profile.username}, {$set: {"profile.settings.showRating": showRating}});
- } else {
- Meteor.users.update({"profile.username": user.profile.username}, {$set: {"profile.settings": {showRating: showRating}}});
- }
- },
- requirements: ["login"]
- },
- addAlert: {
- code: function (description) {
- var username = Meteor.user().profile.username;
- description = htmlEntities(description);
- Alerts.insert({description: description, active: true, createdBy: username});
- return true;
- },
- requirements: ["admin"]
- },
- sendMessage: {
- code: function (type, message) {
- var user = Meteor.user();
- var time = new Date();
- var rawrank = user.profile.rank;
- var username = user.profile.username;
- var profanity = false;
- message = htmlEntities(message);
- if (!message.replace(/\s/g, "").length > 0) {
- throw new Meteor.Error(406, "Message length cannot be 0.");
- }
- if (message.length > 300) {
- throw new Meteor.Error(406, "Message length cannot be more than 300 characters long..");
- }
- else if (user.profile.rank === "admin") {
- /*HTTP.call("GET", "http://www.wdyl.com/profanity?q=" + encodeURIComponent(message), function (err, res) {
- if (res.content.indexOf("true") > -1) {
- return true;
- } else {*/
- Chat.insert({
- type: type,
- rawrank: rawrank,
- rank: "[A]",
- message: message,
- time: time,
- username: username
- });
- /*}
- });*/
- return true;
- }
- else if (user.profile.rank === "moderator") {
- /*HTTP.call("GET", "http://www.wdyl.com/profanity?q=" + encodeURIComponent(message), function (err, res) {
- if (res.content.indexOf("true") > -1) {
- return true;
- } else {*/
- Chat.insert({
- type: type,
- rawrank: rawrank,
- rank: "[M]",
- message: message,
- time: time,
- username: username
- });
- /*}
- });*/
- return true;
- }
- else {
- /*HTTP.call("GET", "http://www.wdyl.com/profanity?q=" + encodeURIComponent(message), function (err, res) {
- if (res.content.indexOf("true") > -1) {
- return true;
- } else {*/
- Chat.insert({
- type: type,
- rawrank: rawrank,
- rank: "",
- message: message,
- time: time,
- username: username
- });
- /*}
- });*/
- return true;
- }
- },
- requirements: ["login", "noMute"]
- },
- likeSong: {
- code: function (mid) {
- var user = Meteor.user();
- if (user.profile.liked.indexOf(mid) === -1) {
- Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.liked": mid}});
- Songs.update({mid: mid}, {$inc: {"likes": 1}})
- } else {
- Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
- Songs.update({mid: mid}, {$inc: {likes: -1}})
- }
- if (user.profile.disliked.indexOf(mid) !== -1) {
- Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
- Songs.update({mid: mid}, {$inc: {dislikes: -1}})
- }
- return true;
- },
- requirements: ["login"]
- },
- dislikeSong: {
- code: function (mid) {
- var user = Meteor.user();
- if (user.profile.disliked.indexOf(mid) === -1) {
- Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.disliked": mid}});
- Songs.update({mid: mid}, {$inc: {dislikes: 1}});
- } else {
- Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
- Songs.update({mid: mid}, {$inc: {dislikes: -1}});
- }
- if (user.profile.liked.indexOf(mid) !== -1) {
- Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
- Songs.update({mid: mid}, {$inc: {likes: -1}});
- }
- return true;
- },
- requirements: ["login"]
- },
- voteSkip: {
- code: function (type) {
- var user = Meteor.user();
- getStation(type, function (station) {
- if (station.voted.indexOf(user.profile.username) === -1) {
- station.voted.push(user.profile.username);
- Rooms.update({type: type}, {$set: {votes: station.voted.length}});
- if (station.voted.length === 3) {
- station.skipSong();
- }
- } else {
- throw new Meteor.Error(401, "Already voted.");
- }
- });
- },
- requirements: ["login"]
- },
- votePrivateSkip: {
- code: function (name) {
- var user = Meteor.user();
- getCommunityStation(name, function (station) {
- if (station.voted.indexOf(user.profile.username) === -1) {
- station.voted.push(user.profile.username);
- CommunityStations.update({name: name}, {$set: {votes: station.voted.length}});
- if (station.voted.length === 3) {
- station.skipSong();
- }
- } else {
- throw new Meteor.Error(401, "Already voted.");
- }
- });
- },
- requirements: ["login"]
- },
- submitReport: {
- code: function (room, reportData) {
- room = room.toLowerCase();
- if (Rooms.find({type: room}).count() === 1) {
- if (Reports.find({room: room}).count() === 0) {
- Reports.insert({room: room, report: []});
- }
- if (reportData !== undefined) {
- Reports.update({room: room}, {
- $push: {
- report: {
- song: reportData.song,
- type: reportData.type,
- reason: reportData.reason,
- other: reportData.other
- }
- }
- });
- return true;
- } else {
- throw new Meteor.Error(403, "Invalid data.");
- }
- } else {
- throw new Meteor.Error(403, "Invalid genre.");
- }
- },
- requirements: ["login"]
- },
- shufflePlaylist: {
- code: function (type) {
- getStation(type, function (station) {
- if (station === undefined) {
- throw new Meteor.Error(404, "Station not found.");
- } else {
- station.cancelTimer();
- station.shufflePlaylist();
- }
- });
- },
- requirements: ["admin"]
- },
- skipSong: {
- code: function (type) {
- getStation(type, function (station) {
- if (station === undefined) {
- throw new Meteor.Error(404, "Station not found.");
- } else {
- station.skipSong();
- }
- });
- },
- requirements: ["admin"]
- },
- pauseRoom: {
- code: function (type) {
- getStation(type, function (station) {
- if (station === undefined) {
- throw new Meteor.Error(403, "Room doesn't exist.");
- } else {
- station.pauseRoom();
- }
- });
- },
- requirements: ["admin"]
- },
- resumeRoom: {
- code: function (type) {
- getStation(type, function (station) {
- if (station === undefined) {
- throw new Meteor.Error(403, "Room doesn't exist.");
- } else {
- station.resumeRoom();
- }
- });
- },
- requirements: ["admin"]
- },
- skipPrivateSong: function (name) {
- if ((isAdmin() || isCommunityStationOwner(name)) && !isBanned()) {
- getCommunityStation(name, function (station) {
- if (station === undefined) {
- throw new Meteor.Error(404, "Station not found.");
- } else {
- station.skipSong();
- }
- });
- }
- },
- pauseCommunityStation: function (name) {
- if ((isAdmin() || isCommunityStationOwner(name)) && !isBanned()) {
- getCommunityStation(name, function (station) {
- if (station === undefined) {
- throw new Meteor.Error(403, "Room doesn't exist.");
- } else {
- station.pauseRoom();
- }
- });
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- resumeCommunityStation: function (name) {
- if ((isAdmin() || isCommunityStationOwner(name)) && !isBanned()) {
- getCommunityStation(name, function (station) {
- if (station === undefined) {
- throw new Meteor.Error(403, "Room doesn't exist.");
- } else {
- station.resumeRoom();
- }
- });
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- addAllowedToCommunityStation: function (name, allowed) {
- if (isCommunityStationOwner(name) && !isBanned()) {
- getCommunityStation(name, function (station) {
- if (station === undefined) {
- throw new Meteor.Error(403, "Room doesn't exist.");
- } else {
- allowed = allowed.toLowerCase();
- allowed = Meteor.users.findOne({"profile.usernameL": allowed})._id;
- station.addAllowed(allowed);
- }
- });
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- removeAllowedFromCommunityStation: function (name, allowed) {
- if (isCommunityStationOwner(name) && !isBanned()) {
- getCommunityStation(name, function (station) {
- if (station === undefined) {
- throw new Meteor.Error(403, "Room doesn't exist.");
- } else {
- station.removeAllowed(allowed);
- }
- });
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- setPlaylistForCommunityStation: function (name, playlist) {
- if ((isCommunityStationOwner(name)) && !isBanned()) {
- getCommunityStation(name, function (station) {
- if (station === undefined) {
- throw new Meteor.Error(403, "Room doesn't exist.");
- } else {
- station.setPlaylist(playlist);
- }
- });
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- createUserMethod: {
- code: function (formData, captchaData) {
- var verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(this.connection.clientAddress, captchaData);
- if (!verifyCaptchaResponse.success) {
- throw new Meteor.Error(422, 'reCAPTCHA Failed: ' + verifyCaptchaResponse.error);
- } else {
- Accounts.createUser({
- username: formData.username,
- email: formData.email,
- password: formData.password
- });
- }
- return true;
- },
- requirements: []
- },
- promoteUserMethod: {
- code: function(user) {
- Meteor.users.find({"profile.usernameL": username.toLowerCase()})
- }
- },
- createArticle: {
- code: function(data) {
- var userId = Meteor.userId();
- var requiredProperties = ["title", "content", "anonymous"];
- if (data !== undefined && Object.keys(data).length === requiredProperties.length) {
- for (var property in requiredProperties) {
- if (data[requiredProperties[property]] === undefined) {
- throw new Meteor.Error(403, "Invalid data.");
- }
- }
- if (data.anonymous === false) {
- data.author = Meteor.user().profile.username
- } else {
- data.author = "A Musare Admin";
- }
- delete data.anonymous;
- data.time = new Date();
- data.content = htmlEntities(data.content);
- News.insert(data, function(err, res) {
- if (err) {
- console.log(err);
- throw err.sanitizedError;
- } else {
- return true;
- }
- });
- } else {
- throw new Meteor.Error(403, "Invalid data.");
- }
- },
- requirements: ["moderator"]
- },
- addSongToQueue: {
- code: function (songData) {
- var userId = Meteor.userId();
- var requiredProperties = ["title", "artist", "id", "genres"];
- if (songData !== undefined && Object.keys(songData).length === requiredProperties.length) {
- for (var property in requiredProperties) {
- if (songData[requiredProperties[property]] === undefined) {
- throw new Meteor.Error(403, "Invalid data.");
- }
- }
- songData.duration = Number(getSongDuration(songData.title, songData.artist));
- songData.img = getSongAlbumArt(songData.title, songData.artist) | "";
- songData.skipDuration = 0;
- songData.likes = 0;
- songData.dislikes = 0;
- songData.requestedBy = userId;
- var mid = createUniqueSongId();
- if (mid !== undefined) {
- songData.mid = mid;
- Queues.insert(songData, function(err, res) {
- if (err) {
- console.log(err);
- throw err.sanitizedError;
- } else {
- var songsRequested = (Meteor.user().profile !== undefined && Meteor.user().profile.statistics !== undefined && Meteor.user().profile.statistics.songsRequested !== undefined) ? Meteor.user().profile.statistics.songsRequested : 0;
- songsRequested++;
- Meteor.users.update(Meteor.userId(), {$set: {"profile.statistics.songsRequested": songsRequested}}); // TODO Make mongo query use $inc correctly.
- return true;
- }
- });
- } else {
- throw new Meteor.Error(500, "Am error occured.");
- }
- } else {
- throw new Meteor.Error(403, "Invalid data.");
- }
- },
- requirements: ["login"]
- },
- updateQueueSong: {
- code: function (mid, newSong) {
- Queues.update({mid: mid}, {$set: {
- "title": newSong.title,
- "artist": newSong.artist,
- "id": newSong.id,
- "img": newSong.img,
- "duration" : newSong.duration,
- "skipDuration" : newSong.skipDuration,
- "genres": newSong.genres
- }}, function(err) {
- console.log(err);
- if (err) {
- throw err.sanitizedError;
- } else {
- return true;
- }
- });
- },
- requirements: ["moderator"]
- },
- updatePlaylistSong: {
- code: function (mid, newSong) {
- Songs.update({mid: mid}, {$set: {
- "title": newSong.title,
- "artist": newSong.artist,
- "id": newSong.id,
- "img": newSong.img,
- "duration": newSong.duration,
- "skipDuration": newSong.skipDuration,
- "approvedBy": Meteor.userId(),
- "genres": newSong.genres
- }}, function(err) {
- console.log(err);
- if (err) {
- throw err.sanitizedError;
- } else {
- return true;
- }
- });
- return true;
- },
- requirements: ["moderator"]
- },
- removeSongFromQueue: {
- code: function (mid) {
- Queues.remove({mid: mid});
- },
- requirements: ["moderator"]
- },
- removeSongFromPlaylist: {
- code: function (type, mid) {
- Playlists.update({type: type}, {$pull: {songs: mid}});
- },
- requirements: ["moderator"]
- },
- deleteSong: {
- code: function (mid) {
- Songs.remove({mid: mid})
- },
- requirements: ["moderator"]
- },
- addSongToPlaylist: {
- code: function (songData) {
- var requiredProperties = ["_id", "mid", "id", "title", "artist", "duration", "skipDuration", "img", "likes", "dislikes", "requestedBy", "genres"];
- if (songData !== undefined && Object.keys(songData).length === requiredProperties.length) {
- for (var property in requiredProperties) {
- if (songData[requiredProperties[property]] === undefined) {
- throw new Meteor.Error(403, "Invalid data.");
- }
- }
- delete songData._id;
- songData.approvedBy = Meteor.userId();
- Songs.insert(songData);
- Queues.remove({mid: songData.mid});
- songData.genres.forEach(function(genre) {
- genre = genre.toLowerCase();
- if (Playlists.findOne({type: genre}) === undefined) {
- Playlists.insert({type: genre, songs: [songData.mid]});
- } else {
- Playlists.update({type: genre}, {$push: {songs: songData.mid}});
- }
- });
- return true;
- } else {
- throw new Meteor.Error(403, "Invalid data.");
- }
- },
- requirements: ["moderator"]
- },
- createRoom: {
- code: function (display, tag, private, desc) {
- createRoom(display, tag, private, desc);
- },
- requirements: ["admin"]
- },
- createCommunityStation: {
- code: function (name, display, private, desc) {
- name = name.toLowerCase();
- createCommunityStation(name, display, private, desc, Meteor.userId());
- },
- requirements: ["login"]
- },
- createPrivatePlaylist: {
- code: function (name, display) {
- name = name.toLowerCase();
- if (PrivatePlaylists.findOne({name: name, owner: Meteor.userId()}) === undefined) {
- PrivatePlaylists.insert({name: name, displayName: display, songs: [{id: "60ItHLz5WEA", duration: 213, title: "Alan Walker - Faded"}], owner: Meteor.userId()});
- }
- },
- requirements: ["login"]
- },
- deleteRoom: {
- code: function (type) {
- Rooms.remove({type: type});
- return true;
- },
- requirements: ["admin"]
- },
- getUserNum: {
- code: function () {
- return Object.keys(Meteor.default_server.sessions).length;
- },
- requirements: []
- },
- getTotalUsers: function () {
- return Meteor.users.find().count();
- },
- updateRealName: {
- code: function (realname) {
- var oldName = Meteor.users.findOne(Meteor.userId()).profile.realname;
- Meteor.users.update(Meteor.userId(), {
- $set: {"profile.realname": realname},
- $push: {"profile.realnames": oldName}
- });
- },
- requirements: ["login"]
- },
- updateUserName: {
- code: function (newUserName) {
- var oldUsername = Meteor.users.findOne(Meteor.userId()).profile.username;
- Meteor.users.update(Meteor.userId(), {
- $set: {
- "username": newUserName,
- "profile.username": newUserName,
- "profile.usernameL": newUserName.toLowerCase()
- }, $push: {"profile.usernames": oldUsername}
- });
- },
- requirements: ["login"]
- },
- promoteUserRank: {
- code: function(username){
- if (Meteor.userId()) {
- Meteor.users.update({"username": username}, {$set: {"profile.rank": "admin"}});
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- }
- },
- demoteUserRank: {
- code: function(username){
- if (Meteor.userId()) {
- Meteor.users.update({"username": username}, {$set: {"profile.rank": "default"}});
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- }
- },
- deleteAccount: {
- code: function () {
- var user = Meteor.users.findOne(Meteor.userId());
- Meteor.users.remove({_id: Meteor.userId()});
- },
- requirements: ["login"]
- },
- sendFeedback: {
- code: function(message){
- /*HTTP.call("GET", "http://www.wdyl.com/profanity?q=" + encodeURIComponent(message), function (err, res) {
- if (res.content.indexOf("true") > -1) {
- return true;
- } else {*/
- Feedback.insert({
- "username": Meteor.user().profile.username,
- "message": message,
- "upvotes": 0,
- "upvotedBy": []
- })
- /*}
- });*/
- },
- requirements: ["login"]
- },
- upvoteFeedback: {
- code: function(message){
- //TODO Fix feedback upvoting to use Id's
- if(Feedback.findOne({"message": message}).upvotedBy.indexOf(Meteor.user().profile.username) === -1){
- Feedback.update({"message": message}, {$inc: {"upvotes": 1}});
- Feedback.update({"message": message}, {$push: {"upvotedBy": Meteor.user().profile.username}});
- } else{
- Feedback.update({"message": message}, {$inc: {"upvotes": -1}});
- Feedback.update({"message": message}, {$pull: {"upvotedBy": Meteor.user().profile.username}});
- }
- },
- requirements: ["login"]
- },
- deleteFeedback: {
- code: function(message){
- Feedback.remove({"message": message});
- },
- requirements: ["admin"]
- },
- updateFeedback: {
- code: function(oldMessage, newMessage){
- Feedback.update({"message": oldMessage}, {$set: {"message": newMessage}});
- },
- requirements: ["admin"]
- },
- editRoomDesc: {
- code: function(type, description){
- Rooms.update({type: type}, {$set: {"roomDesc": description}});
- },
- requirements: ["admin"]
- },
- removeReport: {
- code: function(query, obj){
- Reports.update(query, {$pull: {"report": obj}});
- },
- requirements: ["admin"]
- }
- });
- Meteor.setInterval(function () {
- checkUsersPR();
- }, 10000);
- Meteor.users.after.insert(function (err, user) {
- Accounts.sendVerificationEmail(user._id);
- });
- function htmlEntities(str) {
- return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
- }
|