-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from paywteam/feature/sharing
Complete implementation of the feature: sharing
- Loading branch information
Showing
5 changed files
with
187 additions
and
2 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
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,86 @@ | ||
import { Response, SimpleHandler } from '@/http/RequestHandler' | ||
import { GlueBoardDoc } from '@@/migrate/schemas/glue-board' | ||
import GlueBoard from '@@/migrate/models/glue-board' | ||
import { FragmentDoc } from '@@/migrate/schemas/fragment' | ||
|
||
interface GetHashResponseBody { | ||
hash: string | ||
} | ||
|
||
interface GetResponseBody { | ||
category: { | ||
name: string | ||
color: string | ||
} | ||
fragments: Array<{ | ||
url: string | ||
selector: string | ||
xPos: number | ||
yPos: number | ||
scale: number | ||
}> | ||
} | ||
|
||
export default class SharingController { | ||
/** | ||
* Get the url hash of the shared GlueBoard | ||
*/ | ||
public static getHash(): SimpleHandler { | ||
return (req, res): Response => { | ||
const glueBoard = res.locals.glueBoard as GlueBoardDoc | ||
|
||
// if sharing option is off, reject this request. | ||
if (glueBoard.sharing === false) { | ||
return res.status(403).json({ | ||
err: { | ||
msg: 'Sharing option for this GlueBoard is off.' | ||
} | ||
}) | ||
} | ||
|
||
const responseBody: GetHashResponseBody = { | ||
hash: glueBoard.id | ||
} | ||
|
||
return res.status(200).json(responseBody) | ||
} | ||
} | ||
|
||
/** | ||
* Access to the shared GlueBoard | ||
*/ | ||
public static get(): SimpleHandler { | ||
return async (req, res): Promise<Response> => { | ||
const glueBoard = (await GlueBoard.findById(res.locals.glueBoard._id, { | ||
category: 1, | ||
fragments: 1 | ||
}) | ||
.lean() | ||
.populate({ | ||
path: 'fragments', | ||
select: '-_id -id' | ||
})) as GlueBoardDoc | ||
|
||
const responseBody: GetResponseBody = { | ||
category: { | ||
name: glueBoard.category.name, | ||
color: glueBoard.category.color | ||
}, | ||
fragments: [] | ||
} | ||
|
||
// compose response body | ||
for (const fragment of glueBoard.fragments as FragmentDoc[]) { | ||
responseBody.fragments.push({ | ||
url: fragment.url, | ||
selector: fragment.selector, | ||
xPos: fragment.xPos, | ||
yPos: fragment.yPos, | ||
scale: fragment.scale | ||
}) | ||
} | ||
|
||
return res.status(200).json(responseBody) | ||
} | ||
} | ||
} |
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 @@ | ||
import { Response, NextHandler } from '@/http/RequestHandler' | ||
import GlueBoard from '@@/migrate/models/glue-board' | ||
import { GlueBoardDoc } from '@@/migrate/schemas/glue-board' | ||
import { checkSchema, ValidationChain } from 'express-validator' | ||
|
||
export default class CheckSharing { | ||
public static validate(): ValidationChain[] { | ||
return checkSchema({ | ||
hash: { | ||
exists: true, | ||
in: 'params', | ||
isString: true, | ||
trim: true, | ||
errorMessage: '`hash` must be a string.' | ||
} | ||
}) | ||
} | ||
|
||
public static handler(): NextHandler { | ||
return async (req, res, next): Promise<Response | void> => { | ||
const glueBoardID = req.params.hash | ||
const glueBoard = (await GlueBoard.findOne({ | ||
id: glueBoardID | ||
})) as GlueBoardDoc | ||
|
||
if (!glueBoard) { | ||
return res.status(404).json({ | ||
err: { | ||
msg: 'Glueboard not found.' | ||
} | ||
}) | ||
} | ||
|
||
if (!glueBoard.sharing) { | ||
return res.status(403).json({ | ||
err: { | ||
msg: 'Unshared GlueBoard.' | ||
} | ||
}) | ||
} | ||
|
||
res.locals.glueBoard = glueBoard | ||
|
||
return next() | ||
} | ||
} | ||
} |
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