Skip to content

Commit

Permalink
premium support
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragon1320 committed Oct 31, 2018
1 parent 8aef2b4 commit 57087af
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/bot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ client.on("message", message => {
// bot not enabled on server
return;
}
if (guild.premium === false) {

// bot not enabled on the server
return;
}

schemas.UserSchema
.findOne({
Expand Down
1 change: 1 addition & 0 deletions src/db/schemas/guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const GuildSchema = new Schema({

discord_id: { type: String, required: true, unique: true },
prefix: { type: String, default: "-" },
premium: { type: Boolean, deafult: false },
member_perms: [ memberPermSchema ],
scripts: [ guildScriptSchema ],
});
Expand Down
1 change: 1 addition & 0 deletions src/db/schemas/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const UserSchema = new Schema({
verified: { type: Boolean, default: false },
developer: { type: Boolean, default: false },
tier: { type: String, default: "free" },
premium: [ String ],

xp: { type: Number, default: 0 },
shits: { type: Number, default: 0 },
Expand Down
47 changes: 47 additions & 0 deletions src/web/middlewares/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,52 @@ const authUser = (req, res, next) => {
});
}

const authPremium = (req, res, next) => {

let token;

if (req.cookies !== undefined && req.cookies.session !== undefined) {
token = req.cookies.session;
}

if (req.headers["xxx-access-token"] !== undefined) {
token = req.headers["xxx-access-token"];
}

if (token === undefined) {
return res.json({ status: 401, message: "Unauthorized", error: "There was an issue fetching your session" });
}

fetchSession(token)
.then(session_doc => {

if (session_doc.complete === false) {
return res.json({ status: 401, message: "Unauthorized", error: "Your session was in an incorrect state" });
}

req.session = session_doc;

fetchUser(session_doc.discord.id)
.then(user_doc => {

if (user_doc.tier === "free") {
return res.json({ status: 403, message: "Forbidden", error: "You are not authorised to access this path" });
}

req.user = user_doc
next();
})
.catch(error => {

return res.json({ status: 401, message: "Unauthorized", error });
});
})
.catch(error => {

res.json({ status: 401, message: "Unauthorized", error });
});
}

const authAdmin = (req, res, next) => {

let token;
Expand Down Expand Up @@ -126,5 +172,6 @@ module.exports = {

authSession,
authUser,
authPremium,
authAdmin
};
129 changes: 127 additions & 2 deletions src/web/routes/api/guilds.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const mongoose = require("mongoose");

const schemas = require("../../../db");
const Logger = require("../../../logger");
const { authUser, authAdmin } = require("../../middlewares");
const { authUser, authPremium, authAdmin } = require("../../middlewares");

const router = express.Router();
const apiLogger = new Logger();
Expand Down Expand Up @@ -77,14 +77,134 @@ const getUserGuilds = token => {
});
}

router.route("/").post(authAdmin, (req, res) => {
router.route("/premium/:discord_id").post(authPremium, async (req, res) => {

let limit = 0;

switch(req.user.tier) {
case "partner":
limit = 1;
break;
case "f":
limit = 1;
break;
case "bf":
limit = 2;
break;
case "sbf":
limit = 3;
break;
default:
limit = parseInt(req.user.tier);

if (isNaN(limit)) {

return res.json({ status: 400, message: "invalid tier" });
}
}

const current = await schemas.GuildSchema.find({ discord_id: { $in: req.user.premium } }).map(e => e.discord_id);
req.user.premium.filter(e => current.includes(e));

if (limit <= req.user.premium.length) {

return res.json({ status: 403, tier: req.user.tier, limit, used: req.user.premium.length });
}

schemas.GuildSchema
.findOne({
discord_id: req.params.discord_id
})
.then(doc => {
if (doc === null) {

return res.json({ status: 404 });
}

if (doc.premium === true) {

return res.json({ status: 400, message: "already premium" });
}

const promises = [];
promises.push(doc.update({ premium: true }));
promises.push(req.user.update({ premium: [...req.user.premium, doc.discord_id] }));

Promise.all(promises).then(() => {

res.json({ status: 200 });
}).catch(error => {

apiLogger.error(error);
return res.json({ status: 500 });
});
})
.catch(error => {

apiLogger.error(error);
return res.json({ status: 500 });
});
});

router.route("/premium/:discord_id").delete(authPremium, (req, res) => {

schemas.GuildSchema
.findOne({
discord_id: req.params.discord_id
})
.then(doc => {
if (doc === null) {

return res.json({ status: 404 });
}

const index = req.user.premium.indexOf(req.params.discord_id);

if (index === -1) {

return res.json({ status: 403, message: "you didnt give this guild premium" });
}

if (doc.premium === false) {

return res.json({ status: 400, message: "already not premium" });
}

req.user.premium.splice(index, 1);

const promises = [];
promises.push(doc.update({ premium: false }));
promises.push(req.user.update({ premium: req.user.premium }));

Promise.all(promises).then(() => {

res.json({ status: 200 });
}).catch(error => {

apiLogger.error(error);
return res.json({ status: 500 });
});
})
.catch(error => {

apiLogger.error(error);
return res.json({ status: 500 });
});
});

router.route("/").post(authUser, (req, res) => {

const params = {};
params.discord_id = req.body.discord_id;
params.prefix = req.body.prefix;
params.member_perms = req.body.member_perms;
params.scripts = [];

if (req.user.admin === true) {

params.premium = req.body.premium === undefined ? true : req.body.premium;
}

const guild = new schemas.GuildSchema(params);

guild
Expand Down Expand Up @@ -238,6 +358,11 @@ router.route("/:discord_id").get(authUser, (req, res) => {
params.prefix = req.body.prefix;
params.member_perms = req.body.member_perms;

if (req.user.admin === true) {

params.premium = req.body.premium;
}

schemas.GuildSchema
.findOne({
discord_id: req.params.discord_id
Expand Down
2 changes: 2 additions & 0 deletions src/web/routes/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ router.route("/").get(authUser, (req, res) => {
params.verified = req.body.verified;
params.developer = req.body.developer;
params.tier = req.body.tier;
params.premium = req.body.premium;

params.xp = req.body.xp;
params.shits = req.body.shits;
Expand Down Expand Up @@ -230,6 +231,7 @@ router.route("/:discord_id").get(authUser, (req, res) => {
req.user.verified = req.body.verified === undefined ? req.user.verified : req.body.verified;
req.user.developer = req.body.developer === undefined ? req.user.developer : req.body.developer;
req.user.tier = req.body.tier === undefined ? req.user.tier : req.body.tier;
req.user.premium = req.body.premium === undefined ? req.user.premium : req.body.premium;

req.user.xp = req.body.xp === undefined ? req.user.xp : req.body.xp;
req.user.shits = req.body.shits === undefined ? req.user.shits : req.body.shits;
Expand Down

0 comments on commit 57087af

Please sign in to comment.