Skip to content

Commit

Permalink
script marketplace beginningzz
Browse files Browse the repository at this point in the history
  • Loading branch information
MattheousDT committed Jul 4, 2018
1 parent a80e7e3 commit 2f7b715
Show file tree
Hide file tree
Showing 12 changed files with 646 additions and 111 deletions.
Binary file modified .vs/slnx.sqlite
Binary file not shown.
16 changes: 15 additions & 1 deletion src/db/schemas/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ const mongoose = require("mongoose");

const Schema = mongoose.Schema;

const thumbnails = [
"https://cdn.discordapp.com/embed/avatars/0.png",
"https://cdn.discordapp.com/embed/avatars/1.png",
"https://cdn.discordapp.com/embed/avatars/2.png",
"https://cdn.discordapp.com/embed/avatars/3.png",
"https://cdn.discordapp.com/embed/avatars/4.png"
];

const def_thumbnail = () => {

return thumbnails[Math.floor(Math.random() * thumbnails.length)];
}

const ScriptSchema = new Schema({

// _id: ObjectId,
Expand All @@ -17,7 +30,8 @@ const ScriptSchema = new Schema({
code: { type: String, required: false },
featured: { type: Boolean, default: false },
upvotes: { type: Number, default: 0 },
downvotes: { type: Number, default: 0 }
downvotes: { type: Number, default: 0 },
thumbnail: { type: String, default: def_thumbnail}
});

module.exports = mongoose.model("Script", ScriptSchema);
30 changes: 27 additions & 3 deletions src/web/routes/api/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ router.route("/").get(authUser, async (req, res) => {
const featured = req.body.featured === undefined ? false : req.body.featured;
const upvotes = req.body.upvotes === undefined ? 0 : req.body.upvotes;
const downvotes = req.body.downvotes === undefined ? 0 : req.body.downvotes;
const thumbnail = req.query.thumbnail === undefined ? null : req.query.thumbnail;

if (local === null || description === null || name === null || type === null || permissions === null || match === null) {
return res.json({ status: 400, message: "Bad Request", error: "Not enough data specified" });
Expand Down Expand Up @@ -122,6 +123,10 @@ router.route("/").get(authUser, async (req, res) => {
return res.json({ status: 400, message: "Bad Request", error: "Downvotes needs to be a number" });
}

if (thumbnail !== null && typeof thumbnail !== "string") {
return res.json({ status: 400, message: "Bad Request", error: "Thumbnail needs to be a string" });
}

const script = new schemas.ScriptSchema({
local,
name,
Expand All @@ -133,7 +138,8 @@ router.route("/").get(authUser, async (req, res) => {
code,
featured,
upvotes,
downvotes
downvotes,
...(thumbnail === null ? {} : { thumbnail })
});

try {
Expand Down Expand Up @@ -193,6 +199,7 @@ router.route("/@me").get(authUser, async (req, res) => {
const match = req.body.match === undefined ? null : req.body.match;
const match_type = req.body.match_type === undefined ? null : req.body.match_type;
const code = req.body.code === undefined ? null : req.body.code;
const thumbnail = req.query.thumbnail === undefined ? null : req.query.thumbnail;

if (name === null || description === null || type === null || permissions === null || match === null || code === null) {
return res.json({ status: 400, message: "Bad Request", error: "Not enough data specified" });
Expand All @@ -213,6 +220,10 @@ router.route("/@me").get(authUser, async (req, res) => {
if (match_type !== "command" && match_type !== "startswith" && match_type !== "contains" && match_type !== "exactmatch") {
return res.json({ status: 400, message: "Bad Request", error: "Match type needs to be either command, startswith, contains or exactmatch" });
}

if (thumbnail !== null && typeof thumbnail !== "string") {
return res.json({ status: 400, message: "Bad Request", error: "Thumbnail needs to be a string" });
}

const script = new schemas.ScriptSchema({
local,
Expand All @@ -222,7 +233,8 @@ router.route("/@me").get(authUser, async (req, res) => {
permissions,
match,
match_type,
code
code,
...(thumbnail === null ? {} : { thumbnail })
});

let user_script;
Expand Down Expand Up @@ -324,6 +336,7 @@ router.route("/@me/:object_id").get(authUser, async (req, res) => {
const match = req.body.match === undefined ? null : req.body.match;
const match_type = req.body.match_type === undefined ? null : req.body.match_type;
const code = req.body.code === undefined ? null : req.body.code;
const thumbnail = req.query.thumbnail === undefined ? null : req.query.thumbnail;

if (type !== null && (type !== "js" && type !== "basic")) {
return res.json({ status: 400, message: "Bad Request", error: "Type needs to be either js or basic" });
Expand All @@ -341,6 +354,10 @@ router.route("/@me/:object_id").get(authUser, async (req, res) => {
return res.json({ status: 400, message: "Bad Request", error: "Match type needs to be either command, startswith, contains or exactmatch" });
}

if (thumbnail !== null && typeof thumbnail !== "string") {
return res.json({ status: 400, message: "Bad Request", error: "Thumbnail needs to be a string" });
}

let upd_script;
try {

Expand All @@ -355,6 +372,7 @@ router.route("/@me/:object_id").get(authUser, async (req, res) => {
...(match === null ? {} : { match }),
...(match_type === null ? {} : { match_type }),
...(code === null ? {} : { code }),
...(thumbnail === null ? {} : { thumbnail })
});
} catch(error) {

Expand Down Expand Up @@ -454,6 +472,7 @@ router.route("/:object_id").get(authAdmin, async (req, res) => {
const featured = req.body.featured === undefined ? null : req.body.featured;
const upvotes = req.body.upvotes === undefined ? null : req.body.upvotes;
const downvotes = req.body.downvotes === undefined ? null : req.body.downvotes;
const thumbnail = req.query.thumbnail === undefined ? null : req.query.thumbnail;

if (local !== null && (local !== true && local !== false)) {
return res.json({ status: 400, message: "Bad Request", error: "Local needs to be a boolean" });
Expand Down Expand Up @@ -491,6 +510,10 @@ router.route("/:object_id").get(authAdmin, async (req, res) => {
return res.json({ status: 400, message: "Bad Request", error: "Downvotes needs to be a number" });
}

if (thumbnail !== null && typeof thumbnail !== "string") {
return res.json({ status: 400, message: "Bad Request", error: "Thumbnail needs to be a string" });
}

let upd_script;
try {

Expand All @@ -508,7 +531,8 @@ router.route("/:object_id").get(authAdmin, async (req, res) => {
...(code === null ? {} : { code }),
...(featured === null ? {} : { featured }),
...(upvotes === null ? {} : { upvotes }),
...(downvotes === null ? {} : { downvotes })
...(downvotes === null ? {} : { downvotes }),
...(thumbnail === null ? {} : { thumbnail })
});
} catch(error) {

Expand Down
63 changes: 59 additions & 4 deletions src/web/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,71 @@ router.get("/commands", (req, res) => {
res.render("commands", { md: text => { return converter.makeHtml(text); }, user: {}});
});

router.get("/dashboard", (req, res) => {
res.render("dashboard/main");
router.get("/dashboard", authUser, async (req, res) => {

let user_res;
try {

user_res = await axios({
method: "get",
url: "https://discordapp.com/api/v6/users/@me",
headers: {
"Authorization": `Bearer ${req.session.discord.access_token}`
}
});
} catch(error) {

apiLogger.error(error);
return res.json({ error: "error fetching discord data lol" });
}

res.render("dashboard/main", { user_data: user_res.data });
});

router.get("/dashboard/scripts/editor", authUser, (req, res) => {
res.render("dashboard/editor");
});

router.get("/dashboard/scripts/me", authUser, (req, res) => {
res.render("dashboard/userscripts");
router.get("/dashboard/scripts/marketplace", authUser, async (req, res) => {

let user_res;
try {

user_res = await axios({
method: "get",
url: "https://discordapp.com/api/v6/users/@me",
headers: {
"Authorization": `Bearer ${req.session.discord.access_token}`
}
});
} catch(error) {

apiLogger.error(error);
return res.json({ error: "error fetching discord data lol" });
}

res.render("dashboard/marketplace", { user_data: user_res.data });
});

router.get("/dashboard/scripts/me", authUser, async (req, res) => {

let user_res;
try {

user_res = await axios({
method: "get",
url: "https://discordapp.com/api/v6/users/@me",
headers: {
"Authorization": `Bearer ${req.session.discord.access_token}`
}
});
} catch(error) {

apiLogger.error(error);
return res.json({ error: "error fetching discord data lol" });
}

res.render("dashboard/userscripts", { user_data: user_res.data });
});

router.get("/token", authAdmin, (req, res) => {
Expand Down
Binary file added src/web/static/audio/metalgear.mp3
Binary file not shown.
39 changes: 39 additions & 0 deletions src/web/static/js/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict";

const searchScripts = options => {
return new Promise(async (resolve, reject) => {

let url_encode = "";
for (let opt in options) {
if (url_encode === "") {

url_encode += "?";
} else {

url_encode += "&";
}

url_encode += `${opt}=${options[opt]}`;
}

const req = new XMLHttpRequest;
req.open("GET", `/api/v3/scripts${url_encode}`);

req.onreadystatechange = () => {
if (req.readyState === 4) {

const res_json = JSON.parse(req.response);

if (res_json instanceof Array === true) {

resolve(res_json);
} else {

reject(res_json);
}
}
}

req.send();
});
}

0 comments on commit 2f7b715

Please sign in to comment.