Skip to content

Commit

Permalink
marketplace page complete yay!
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragon1320 committed Sep 6, 2018
1 parent 81cbb5c commit aa50ca8
Show file tree
Hide file tree
Showing 50 changed files with 14,548 additions and 492 deletions.
2 changes: 1 addition & 1 deletion src/db/schemas/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const ScriptSchema = new Schema({
// _id: ObjectId,

// The mongo id of the scripts owner.
author_id: { type: Schema.Types.ObjectId, required: true },
author_id: { type: String, required: true },

// User set parameters. Note that 'thumbnail' should be set to a url of an image.
name: { type: String, required: true },
Expand Down
62 changes: 61 additions & 1 deletion src/web/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fs = require("fs");
const path = require("path");

const jwt = require("jsonwebtoken");
const axios = require("axios");

const schemas = require("../../db");
const Logger = require("../../logger");
Expand Down Expand Up @@ -113,9 +114,68 @@ const fetchUser = discord_id => {
});
}

/*
{
expire,
data
}
*/
const userCache = {};

const cacheTime = 30000;

const getUserData = token => {
return new Promise((resolve, reject) => {

let data = null;
for (let k in userCache) {

if (userCache[k].expire < Date.now()) {

delete userCache[k];
continue;
}

if (k === token) {

data = userCache[k].data;
}
}

if (data === null) {

axios
.get("https://discordapp.com/api/v6/users/@me", {
headers: {
"Authorization": `Bearer ${token}`
}
})
.then(res => {

userCache[token] = {

expire: new Date(Date.now() + cacheTime),
data: res.data
};

resolve(res.data);
})
.catch(error => {

reject(error);
});
} else {

resolve(data);
}
});
}

module.exports = {

jwtVerify,
fetchSession,
fetchUser
fetchUser,

getUserData
};
169 changes: 108 additions & 61 deletions src/web/routes/api/guilds.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,14 @@ router.route("/:discord_id/scripts").get(authUser, (req, res) => {
return res.json({ status: 404 });
}

for (let guildScript of doc.scripts) {

if (guildScript.object_id.equals(object_id)) {

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

schemas.ScriptSchema
.findById(object_id)
.then(doc2 => {
Expand All @@ -483,42 +491,57 @@ router.route("/:discord_id/scripts").get(authUser, (req, res) => {
return res.json({ status: 400 });
}

getUserGuilds(req.session.discord.access_token)
.then(guilds => {
if (doc2.marketplace_enabled === false) {

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

doc2.guild_count++;

doc2
.save()
.then(() => {

getUserGuilds(req.session.discord.access_token)
.then(guilds => {

const current_guild = guilds.find(e => {

return e.id === req.params.discord_id;
});

const current_guild = guilds.find(e => {
if (current_guild === undefined || (current_guild.owner === false && ((current_guild.permissions & 0b1000) !== 0b1000))) {

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

return e.id === req.params.discord_id;
});

if (current_guild === undefined || (current_guild.owner === false && ((current_guild.permissions & 0b1000) !== 0b1000))) {
const script = new schemas.GuildScriptSchema({
...params,
object_id
});

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

const script = new schemas.GuildScriptSchema({
...params,
object_id
});

doc.scripts.push(script);

doc
.save()
.then(() => {

return res.json({ status: 200 });
doc.scripts.push(script);

doc
.save()
.then(() => {

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

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

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

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

});
})
.catch(error => {
Expand Down Expand Up @@ -681,51 +704,75 @@ router.route("/:discord_id/scripts/:object_id").get(authUser, (req, res) => {
return res.json({ status: 404 });
}

getUserGuilds(req.session.discord.access_token)
.then(guilds => {

const current_guild = guilds.find(e => {

return e.id === req.params.discord_id;
});

if (current_guild === undefined || (current_guild.owner === false && ((current_guild.permissions & 0b1000) !== 0b1000))) {

return res.json({ status: 403 });
}
schemas.ScriptSchema
.findById(object_id)
.then(script => {
if (script === null) {

let found = false;
for (let i = 0; i < doc.scripts.length; i++) {

if (doc.scripts[i].object_id.equals(object_id)) {

found = true;

doc.scripts.splice(i, 1);

break;
}
}

if (found === false) {

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

doc

script.guild_count--;

script
.save()
.then(() => {

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

getUserGuilds(req.session.discord.access_token)
.then(guilds => {

const current_guild = guilds.find(e => {

return e.id === req.params.discord_id;
});

if (current_guild === undefined || (current_guild.owner === false && ((current_guild.permissions & 0b1000) !== 0b1000))) {

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

let found = false;
for (let i = 0; i < doc.scripts.length; i++) {

if (doc.scripts[i].object_id.equals(object_id)) {

found = true;

doc.scripts.splice(i, 1);

break;
}
}

if (found === false) {

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

doc
.save()
.then(() => {

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

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

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

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

});
})
.catch(error => {

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

0 comments on commit aa50ca8

Please sign in to comment.