forked from confused-Techie/atom-backend
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate everything but oauth handlers
- Loading branch information
1 parent
445adf1
commit b65c581
Showing
11 changed files
with
574 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
Oops, something went wrong.