Skip to content

Commit

Permalink
Merge pull request #243 from pulsar-edit/use-call-stack-everywhere
Browse files Browse the repository at this point in the history
Use `callStack` everywhere
  • Loading branch information
confused-Techie authored Feb 19, 2024
2 parents 18ed042 + 19a487e commit 9b3ed1f
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 44 deletions.
24 changes: 14 additions & 10 deletions src/controllers/deletePackagesPackageName.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,20 @@ module.exports = {
},

async logic(params, context) {
const callStack = new context.callStack();

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

callStack.addCall("auth.verifyAuth", user);

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);
.assignCalls(callStack);
}

// Lets also first check to make sure the package exists
Expand All @@ -49,14 +53,15 @@ module.exports = {
true
);

callStack.addCall("db.getPackageByName", packageExists);

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

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

// Get `owner/repo` string format from pacakge
Expand All @@ -66,30 +71,29 @@ module.exports = {

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

callStack.addCall("vcs.ownership", gitowner);

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);
.assignCalls(callStack);
}

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

callStack.addCall("db.removePackageByName", rm);

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);
.assignCalls(callStack);
}

const sso = new context.sso();
Expand Down
11 changes: 8 additions & 3 deletions src/controllers/deletePackagesPackageNameStar.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,32 @@ module.exports = {
},
},
async logic(params, context) {
const callStack = new context.callStack();

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

callStack.addCall("auth.verifyAuth", user);

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

return sso.notOk().addContent(user).addCalls("auth.verifyAuth", user);
return sso.notOk().addContent(user).assignCalls(callStack);
}

const unstar = await context.database.updateDecrementStar(
user.content,
params.packageName
);

callStack.addCall("db.updateDecrementStar", unstar);

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

return sso
.notOk()
.addContent(unstar)
.addCalls("auth.verifyAuth", user)
.addCalls("db.updateDecrementStar", unstar);
.assignCalls(callStack);
}

const sso = new context.sso();
Expand Down
23 changes: 13 additions & 10 deletions src/controllers/deletePackagesPackageNameVersionsVersionName.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module.exports = {
},

async logic(params, context) {
const callStack = new context.callStack();
// Moving this forward to do the least computationally expensive task first.
// Check version validity
if (params.versionName === false) {
Expand All @@ -52,10 +53,12 @@ module.exports = {
// Verify the user has local and remote permissions
const user = await context.auth.verifyAuth(params.auth, context.database);

callStack.addCall("auth.verifyAuth", user);

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

return sso.notOk().addContent(user).addCalls("auth.verifyAuth", user);
return sso.notOk().addContent(user).assignCalls(callStack);
}

// Lets also first check to make sure the package exists
Expand All @@ -64,30 +67,31 @@ module.exports = {
true
);

callStack.addCall("db.getPackageByName", packageExists);

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

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

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

callStack.addCall("vcs.ownership", gitowner);

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);
.assignCalls(callStack);
}

// Mark the specified version for deletion, if version is valid
Expand All @@ -96,16 +100,15 @@ module.exports = {
params.versionName
);

callStack.addCall("db.rremovePackageVersion", removeVersion);

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

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

const sso = new context.sso();
Expand Down
6 changes: 5 additions & 1 deletion src/controllers/getOwnersOwnerName.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,19 @@ module.exports = {
},

async logic(params, context) {
const callStack = new context.callStack();

const packages = await context.database.getSortedPackages(params);

callStack.addCall("db.getSortedPackages", packages);

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

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

const packObjShort = await context.models.constructPackageObjectShort(
Expand Down
17 changes: 11 additions & 6 deletions src/controllers/getPackagesPackageNameStargazers.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,40 +43,45 @@ module.exports = {
* @returns {sso}
*/
async logic(params, context) {
const callStack = new context.callStack();

// The following can't be executed in user mode because we need the pointer
const pack = await context.database.getPackageByName(params.packageName);

callStack.addCall("db.getPackageByName", pack);

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

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

const stars = await context.database.getStarringUsersByPointer(
pack.content
);

callStack.addCall("db.getStarringUsersByPointer", stars);

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

return sso
.notOk()
.addContent(stars)
.addCalls("db.getPackageByName", pack)
.addCalls("db.getStarringUsersByPointer", stars);
.assignCalls(callStack);
}

const gazers = await context.database.getUserCollectionById(stars.content);

callStack.addCall("db.getUserCollectionById", gazers);

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

return sso
.notOk()
.addContent(gazers)
.addCalls("db.getPackageByName", pack)
.addCalls("db.getStarringUsersByPointer", stars)
.addCalls("db.getUserCollectionById", gazers);
.assignCalls(callStack);
}

const sso = new context.sso();
Expand Down
5 changes: 4 additions & 1 deletion src/controllers/getPackagesPackageNameVersionsVersionName.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module.exports = {
* @returns {sso}
*/
async logic(params, context) {
const callStack = new context.callStack();
// Check the truthiness of the returned query engine
if (params.versionName === false) {
const sso = new context.sso();
Expand All @@ -64,13 +65,15 @@ module.exports = {
params.versionName
);

callStack.addCall("db.getPackageVersionByNameAndVersion", pack);

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

return sso
.notOk()
.addContent(pack)
.addCalls("db.getPackageVersionByNameAndVersion", pack);
.assignCalls(callStack);
}

const packRes = await context.models.constructPackageObjectJSON(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ module.exports = {
* @returns {sso}
*/
async logic(params, context) {
const callStack = new context.callStack();

// First ensure our version is valid
if (params.versionName === false) {
// since query.engine gives false if invalid, we can check the truthiness
Expand All @@ -63,19 +65,23 @@ module.exports = {
params.versionName
);

callStack.addCall("db.getPackageVersionByNameAndVersion", pack);

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

return sso
.notOk()
.addContent(pack)
.addCalls("db.getPackageVersionByNameAndVersion", pack);
.assignCalls(callStack);
}

const save = await context.database.updatePackageIncrementDownloadByName(
params.packageName
);

callStack.addCall("db.updatePackageIncrementDownloadByName", save);

if (!save.ok) {
context.logger.generic(3, "Failed to Update Downloads Count", {
type: "object",
Expand All @@ -101,12 +107,16 @@ module.exports = {
3,
`Malformed tarball URL for version ${params.versionName} of ${params.packageName}`
);

callStack.addCall("URL.hostname", err);

const sso = new context.sso();

return sso
.notOk()
.addContent(err)
.addShort("server_error")
.assignCalls(callStack)
.addMessage(
`The URL to download this package seems invalid: ${tarballURL}.`
);
Expand All @@ -128,6 +138,7 @@ module.exports = {
return sso
.notOk()
.addShort("server_error")
.assignCalls(callStack)
.addMessage(`Invalid Domain for Download Redirect: ${hostname}`);
}

Expand Down
Loading

0 comments on commit 9b3ed1f

Please sign in to comment.