123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287 |
- /* Yolo Swag 420 */
- Meteor.startup(function () {
- reCAPTCHA.config({
- privatekey: '6LcVxg0TAAAAAI2fgIEEWHFxwNXeVIs8mzq5cfRM'
- });
- Avatar.setOptions({
- fallbackType: "initials",
- defaultImageUrl: "http://static.boredpanda.com/blog/wp-content/uploads/2014/04/amazing-fox-photos-182.jpg",
- 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);
- }
- Rooms.update({}, {$set: {userList: []}});
- });
- 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"]
- };
- Alerts.update({active: true}, {$set: {active: false}}, {multi: true});
- var stations = [];
- 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 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 Station(type) {
- console.log(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}});
- }
- Meteor.publish(type, function () {
- var username = Meteor.users.findOne(this.userId).profile.username;
- if (username !== undefined) {
- Rooms.update({type: type}, {$push: {userList: username}});
- this.onStop(function() {
- 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;
- }
- console.log(startedAt);
- console.log(song);
- var res = Rooms.update({type: type}, {
- $set: {
- currentSong: {song: song, started: startedAt},
- users: 0
- }
- });
- console.log(res);
- 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;
- this.songTimer = function () {
- startedAt = Date.now();
- if (timer !== undefined) {
- timer.pause();
- }
- 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") {
- 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 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 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));
- }
- });
- 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("allAlerts", function () {
- return Alerts.find({active: false})
- });
- Meteor.publish("playlists", function () {
- return Playlists.find({})
- });
- Meteor.publish("rooms", function () {
- return Rooms.find({});
- });
- 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) {
- 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 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.methods({
- activateAlert: function(id) {
- if (isAdmin() && !isBanned()) {
- Alerts.update(id, {$set: {active: true}});
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- deactivateAlert: function(id) {
- if (isAdmin() && !isBanned()) {
- Alerts.update(id, {$set: {active: false}});
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- deleteAlert: function(id) {
- if (isAdmin() && !isBanned()) {
- Alerts.remove(id);
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- fetchSong: function(type) {
- if (isAdmin() && !isBanned()) {
- getStation(type, function (station) {
- station.fetchSongs();
- });
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- removeSongs: function(type) {
- if (isAdmin() && !isBanned()) {
- getStation(type, function (station) {
- station.removeSongs();
- });
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- lockRoom: function (type) {
- if (isAdmin() && !isBanned()) {
- getStation(type, function (station) {
- station.lock();
- });
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- unlockRoom: function (type) {
- if (isAdmin() && !isBanned()) {
- getStation(type, function (station) {
- station.unlock();
- });
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- banUser: function (username, period, reason) {
- if (isAdmin() && !isBanned()) {
- 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}});
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- muteUser: function (username, period) {
- if (isAdmin() && !isBanned()) {
- 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}});
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- unbanUser: function (username) {
- if (isAdmin() && !isBanned()) {
- Meteor.users.update({"profile.usernameL": username.toLowerCase()}, {$unset: "punishments.ban"});
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- unsilenceUser: function (username) {
- if (isAdmin() && !isBanned()) {
- Meteor.users.update({"profile.usernameL": username.toLowerCase()}, {$unset: "punishments.mute"});
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- isBanned: function () {
- return isBanned();
- },
- isMuted: function () {
- return isMuted();
- },
- updateSettings: function (showRating) {
- if (Meteor.userId() && !isBanned()) {
- 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}}});
- }
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- addAlert: function (description) {
- if (isAdmin()) {
- var username = Meteor.user().profile.username;
- Alerts.insert({description: description, active: true, createdBy: username});
- return true;
- } else {
- throw Meteor.Error(403, "Invalid permissions.");
- }
- },
- sendMessage: function (type, message) {
- if (Meteor.userId() && !isBanned() && !isMuted()) {
- var user = Meteor.user();
- var time = new Date();
- var rawrank = user.profile.rank;
- var username = user.profile.username;
- var profanity = false
- 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;
- }
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- likeSong: function (mid) {
- if (Meteor.userId() && !isBanned()) {
- 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;
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- dislikeSong: function (mid) {
- if (Meteor.userId() && !isBanned()) {
- 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;
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- voteSkip: function (type) {
- if (Meteor.userId() && !isBanned()) {
- 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.");
- }
- })
- }
- },
- submitReport: function (room, reportData) {
- if (Meteor.userId() && !isBanned()) {
- 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.");
- }
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- shufflePlaylist: function (type) {
- if (isAdmin() && !isBanned()) {
- getStation(type, function (station) {
- if (station === undefined) {
- throw new Meteor.Error(404, "Station not found.");
- } else {
- station.cancelTimer();
- station.shufflePlaylist();
- }
- });
- }
- },
- skipSong: function (type) {
- if (isAdmin() && !isBanned()) {
- getStation(type, function (station) {
- if (station === undefined) {
- throw new Meteor.Error(404, "Station not found.");
- } else {
- station.skipSong();
- }
- });
- }
- },
- pauseRoom: function (type) {
- if (isAdmin() && !isBanned()) {
- getStation(type, 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.");
- }
- },
- resumeRoom: function (type) {
- if (isAdmin() && !isBanned()) {
- getStation(type, 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.");
- }
- },
- createUserMethod: function (formData, captchaData) {
- if (!isBanned()) {
- 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;
- }
- },
- createArticle: function(data) {
- if (!isBanned() && isModerator()) {
- 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();
- 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.");
- }
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- addSongToQueue: function (songData) {
- if (Meteor.userId() && !isBanned()) {
- 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.");
- }
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- updateQueueSong: function (mid, newSong) {
- if (isModerator() && !isBanned()) {
- 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;
- }
- });
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- updatePlaylistSong: function (mid, newSong) {
- if (isModerator() && !isBanned()) {
- 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;
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- removeSongFromQueue: function (mid) {
- if (isModerator() && !isBanned()) {
- Queues.remove({mid: mid});
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- removeSongFromPlaylist: function (type, mid) {
- if (isModerator() && !isBanned()) {
- Playlists.update({type: type}, {$pull: {songs: mid}});
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- deleteSong: function (mid) {
- if (isModerator() && !isBanned()) {
- Songs.remove({mid: mid})
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- deleteSong: function (mid) {
- if (isModerator() && !isBanned()) {
- Songs.remove({mid: mid})
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- addSongToPlaylist: function (songData) {
- if (isModerator() && !isBanned()) {
- 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.");
- }
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- createRoom: function (display, tag, private, desc) {
- if (isAdmin() && !isBanned()) {
- createRoom(display, tag, private, desc);
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- deleteRoom: function (type) {
- if (isAdmin() && !isBanned()) {
- Rooms.remove({type: type});
- return true;
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- getUserNum: function () {
- if (!isBanned()) {
- return Object.keys(Meteor.default_server.sessions).length;
- }
- },
- getTotalUsers: function () {
- return Meteor.users.find().count();
- },
- updateRealName: function (realname) {
- if (Meteor.userId()) {
- var oldName = Meteor.users.findOne(Meteor.userId()).profile.realname;
- Meteor.users.update(Meteor.userId(), {
- $set: {"profile.realname": realname},
- $push: {"profile.realnames": oldName}
- });
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- updateUserName: function (newUserName) {
- if (Meteor.userId()) {
- 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}
- });
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- /*updateUserRank: function(newRank){
- if (Meteor.userId()) {
- Meteor.users.update(Meteor.userId(), {$set: {"profile.rank": newRank}});
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },*/
- deleteAccount: function () {
- if (Meteor.userId()) {
- var user = Meteor.users.findOne(Meteor.userId());
- Meteor.users.remove({_id: Meteor.userId()});
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- sendFeedback: function(message){
- if(Meteor.userId() && !isBanned()) {
- 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": []
- })
- }
- });
- }
- },
- upvoteFeedback: function(message){
- if(Meteor.userId() && !isBanned()){
- console.log(Feedback.findOne({"message": message}));
- 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}});
- }
- }
- },
- deleteFeedback: function(message){
- if(isAdmin() && !isBanned()){
- Feedback.remove({"message": message});
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- updateFeedback: function(oldMessage, newMessage){
- if(isAdmin() && !isBanned()){
- Feedback.update({"message": oldMessage}, {$set: {"message": newMessage}});
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- editRoomDesc: function(type, description){
- if(isAdmin() && !isBanned()){
- Rooms.update({type: type}, {$set: {"roomDesc": description}});
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- },
- removeReport: function(query, obj){
- if(isAdmin() && !isBanned()){
- Reports.update(query, {$pull: {"report": obj}});
- } else {
- throw new Meteor.Error(403, "Invalid permissions.");
- }
- }
- });
- Meteor.setInterval(function () {
- checkUsersPR();
- }, 10000);
- Meteor.users.after.insert(function (err, user) {
- Accounts.sendVerificationEmail(user._id);
- });
|