Skip to content

Commit

Permalink
Migrate everything but oauth handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
confused-Techie committed Sep 13, 2023
1 parent 445adf1 commit b65c581
Show file tree
Hide file tree
Showing 11 changed files with 574 additions and 4 deletions.
76 changes: 76 additions & 0 deletions src/controllers/deletePackagesPackageName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
module.exports = {
docs: {

},
endpoint: {
method: "DELETE",
paths: [
"/api/packages/:packageName",
"/api/themes/:packageName"
],
rateLimit: "auth",
successStatus: 204,
options: {
Allow: "DELETE, GET",
"X-Content-Type-Options": "nosniff"
}
},
params: {
auth: (context, req) => { return context.query.auth(req); },
packageName: (context, req) => { return context.query.packageName(req); }
},

async logic(params, context) {
const user = await context.auth.verifyAuth(params.auth, context.database);

if (!user.ok) {
const sso = new context.sso();

return sso.notOk().addContent(user)
.addMessage("Please update your token if you haven't done so recently.")
.addCalls("auth.verifyAuth", user);
}

// Lets also first check to make sure the package exists
const packageExists = await context.database.getPackageByName(params.packageName, true);

if (!packageExists.ok) {
const sso = new context.sso();

return sso.notOk().addContent(packageExists)
.addCalls("auth.verifyAuth", user)
.addCalls("db.getPackageByName", packageExists);
}

// Get `owner/repo` string format from pacakge
const ownerRepo = context.utils.getOwnerRepoFromPackage(packageExists.content.data);

const gitowner = await context.vcs.ownership(user.content, ownerRepo);

if (!gitowner.ok) {
const sso = new context.sso();

return sso.notOk().addContent(gitowner)
.addCalls("auth.verifyAuth", user)
.addCalls("db.getPackageByName", packageExists)
.addCalls("vcs.ownership", gitowner);
}

// Now they are logged in locally, and have permissions over the GitHub repo
const rm = await context.database.removePackageByName(params.packageName);

if (!rm.ok) {
const sso = new context.sso();

return sso.notOk().addContent(rm)
.addCalls("auth.verifyAuth", user)
.addCalls("db.getPackageByName", packageExists)
.addCalls("vcs.ownership", gitowner)
.addCalls("db.removePackageByName", rm);
}

const sso = new context.sso();

return sso.isOk().addContent(false);
}
};
6 changes: 6 additions & 0 deletions src/controllers/endpoints.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
// Exports all the endpoints that need to be required
module.exports = [
require("./deletePackagesPackageName.js"),
require("./deletePackagesPackageNameStar.js"),
require("./deletePackagesPackageNameVersionsVersionName.js"),
require("./getPackagesFeatured.js"),
require("./getPackagesPackageName.js"),
require("./getPackagesPackageNameStargazers.js"),
require("./getPackagesPackageNameVersionsVersionName.js"),
require("./getPackagesPackageNameVersionsVersionNameTarball.js"),
require("./getPackagesSearch.js"),
require("./getRoot.js"),
require("./getStars.js"),
require("./getThemes.js"),
require("./getThemesFeatured.js"),
require("./getThemesSearch.js"),
require("./getUpdates.js"),
require("./getUsers.js"),
require("./getUsersLogin.js"),
require("./getUsersLoginStars.js"),
require("./postPackages.js"),
require("./postPackagesPackageNameStar.js"),
require("./postPackagesPackageNameVersions.js"),
require("./postPackagesPackageNameVersionsVersionNameEventsUninstall.js")
Expand Down
47 changes: 47 additions & 0 deletions src/controllers/getPackages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module.exports = {
docs: {

},
endpoint: {
method: "GET",
paths: [ "/api/packages" ],
rateLimit: "generic",
successStatus: 200,
options: {
Allow: "POST, GET",
"X-Content-Type-Options": "nosniff"
}
},
params: {
page: (context, req) => { return context.query.page(req); },
sort: (context, req) => { return context.query.sort(req); },
direction: (context, req) => { return context.query.dir(req); },
serviceType: (context, req) => { return context.query.serviceType(req); },
service: (context, req) => { return context.query.service(req); },
serviceVersion: (context, req) => { return context.query.serviceVersion(req); },
fileExtension: (context, req) => { return context.query.fileExtension(req); }
},

async logic(params, context) {
const packages = await context.database.getSortedPackages(params);

if (!packages.ok) {
const sso = new context.sso();

return sso.notOk().addContent(packages)
.addCalls("db.getSortedPackages", packages);
}

const packObjShort = await context.utils.constructPackageObjectShort(packages.content);

const packArray = Array.isArray(packObjShort) ? packObjShort : [ packObjShort ];

const ssoP = new context.ssoPaginate();

ssoP.total = packages.pagination.total;
ssoP.limit = packages.pagination.limit;
ssoP.buildLink(`${context.config.server_url}/api/packages`, packages.pagination.page, params);

return ssoP.isOk().addContent(packArray);
}
};
36 changes: 36 additions & 0 deletions src/controllers/getPackagesFeatured.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module.exports = {
docs: {

},
endpoint: {
method: "GET",
paths: [ "/api/packages/featured" ],
rateLimit: "generic",
successStatus: 200,
options: {
Allow: "GET",
"X-Content-Type-Options": "nosniff"
}
},
params: {},
async logic(params, context) {
// TODO: Does not support engine query parameter as of now
const packs = await context.database.getFeaturedPackages();

if (!packs.ok) {
const sso = new context.sso();

return sso.notOk().addContent(packs)
.addCalls("db.getFeaturedPackages", packs);
}

const packObjShort = await context.utils.constructPackageObjectShort(packs.content);

// The endpoint using this ufnction needs an array
const packArray = Array.isArray(packObjShort) ? packObjShort : [ packObjShort ];

const sso = new context.sso();

return sso.isOk().addContent(packArray);
}
};
46 changes: 46 additions & 0 deletions src/controllers/getPackagesPackageName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module.exports = {
docs: {

},
endpoint: {
method: "GET",
paths: [
"/api/packages/:packageName",
"/api/themes/:packageName"
],
rateLimit: "generic",
successStatus: 200,
options: {
Allow: "DELETE, GET",
"X-Content-Type-Options": "nosniff"
}
},
params: {
engine: (context, req) => { return context.query.engine(req.query.engine); },
name: (context, req) => { return context.query.packageName(req); }
},

async logic(params, context) {
let pack = await context.database.getPackageByName(params.name, true);

if (!pack.ok) {
const sso = new context.sso();

return sso.notOk().addContent(pack)
.addCalls("db.getPackageByName", pack);
}

pack = await context.utils.constructPackageObjectFull(pack.content);

if (params.engine !== false) {
// query.engine returns false if no valid query param is found.
// before using engineFilter we need to check the truthiness of it.

pack = await context.utils.engineFilter(pack, params.engine);
}

const sso = new context.sso();

return sso.isOk().addcontent(pack);
}
};
79 changes: 79 additions & 0 deletions src/controllers/getPackagesSearch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
module.exports = {
docs: {

},
endpoint: {
method: "GET",
paths: [ "/api/packages/search" ],
rateLimit: "generic",
successStatus: 200,
options: {
Allow: "GET",
"X-Content-Type-Options": "nosniff"
}
},
params: {
sort: (context, req) => { return context.query.sort(req); },
page: (context, req) => { return context.query.page(req); },
direction: (context, req) => { return context.qeury.dir(req); },
query: (context, req) => { return context.query.query(req); }
},

async logic(params, context) {
// Because the task of implementing the custom search engine is taking longer
// than expected, this will instead use super basic text searching on the DB side.
// This is only an effort to get this working quickly and should be changed later.
// This also means for now, the default sorting method will be downloads, not relevance.

const packs = await context.database.simpleSearch(
params.query,
params.page,
params.direction,
params.sort
);


if (!packs.ok) {
if (packs.short === "not_found") {
// Because getting not found from the search, means the users
// search just had no matches, we will specially handle this to return
// an empty array instead.
// TODO: Re-evaluate if this is needed. The empty result
// returning 'Not Found' has been resolved via the DB.
// But this check still might come in handy, so it'll be left in.

const sso = new context.ssoPaginate();

return sso.isOk().addContent([]);
}

const sso = new context.sso();

return sso.notOk().addContent(packs)
.addCalls("db.simpleSearch", packs);
}

const newPacks = await context.utils.constructPackageObjectShort(packs.content);

let packArray = null;

if (Array.isArray(newPacks)) {
packArray = newPacks;
} else if (Object.keys(newPacks).length < 1) {
packArray = [];
// This also helps protect against misreturned searches. As in getting a 404 rather
// than empty search results.
// See: https://github.com/confused-Techie/atom-backend/issues/59
} else {
packArray = [newPacks];
}

const ssoP = new context.ssoPaginate();

ssoP.total = packs.pagination.total;
ssoP.limit = packs.pagination.limit;
ssoP.buildLink(`${context.config.server_url}/api/packages/search`, packs.pagination.page, params);

return ssoP.isOk().addContent(packArray);
}
};
4 changes: 2 additions & 2 deletions src/controllers/getThemes.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ module.exports = {
const ssoP = new context.ssoPaginate();

ssoP.total = packages.pagination.total;
ssoP.limit = packages.pagination.total;
ssoP.buildLink(`${context.config.server_url}/api/themes`, page, params);
ssoP.limit = packages.pagination.limit;
ssoP.buildLink(`${context.config.server_url}/api/themes`, packages.pagination.page, params);

return ssoP.isOk().addContent(packArray);
}
Expand Down
64 changes: 64 additions & 0 deletions src/controllers/getThemesSearch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module.exports = {
docs: {

},
endpoint: {
method: "GET",
paths: [ "/api/themes/search" ],
rateLimit: "generic",
successStatus: 200,
options: {
Allow: "GET",
"X-Content-Type-Options": "nosniff"
}
},
params: {
sort: (context, req) => { return context.query.sort(req); },
page: (context, req) => { return context.query.page(req); },
direction: (context, req) => { return context.query.dir(req); },
query: (context, req) => { return context.query.query(req); }
},

async logic(params, context) {
const packs = await context.database.simpleSearch(
params.query,
params.page,
params.direction,
params.sort,
true
);

if (!packs.ok) {
if (packs.short === "not_found") {
const sso = new context.ssoPaginate();

return sso.isOk().addContent([]);
}

const sso = new context.sso();

return sso.notOk().addContent(packs)
.addCalls("db.simpleSearch", packs);
}

const newPacks = await context.utils.constructPackageObjectShort(packs.content);

let packArray = null;

if (Array.isArray(newPacks)) {
packArray = newPacks;
} else if (Object.keys(newPacks).length < 1) {
packArray = [];
} else {
packArray = [ newPacks ];
}

const ssoP = new context.ssoPaginate();

ssoP.total = packs.pagination.total;
ssoP.limit = packs.pagination.limit;
ssoP.buildLink(`${context.config.server_url}/api/themes/search`, packs.pagination.page, params);

return ssoP.isOk().addContent(packArray);
}
};
Loading

0 comments on commit b65c581

Please sign in to comment.