|
|
@@ -2,42 +2,69 @@ import AbstractMockApi from './AbstractMockApi.mjs'
|
|
|
|
|
|
class MockChatApi extends AbstractMockApi {
|
|
|
reset() {
|
|
|
- this.projects = {}
|
|
|
+ this.projects = new Map()
|
|
|
}
|
|
|
|
|
|
- getGlobalMessages(req, res) {
|
|
|
- res.json(this.projects[req.params.project_id] || [])
|
|
|
+ getThread(projectId, threadId) {
|
|
|
+ let threads = this.projects.get(projectId)
|
|
|
+ if (threads == null) {
|
|
|
+ threads = new Map()
|
|
|
+ this.projects.set(projectId, threads)
|
|
|
+ }
|
|
|
+ let thread = threads.get(threadId)
|
|
|
+ if (thread == null) {
|
|
|
+ thread = []
|
|
|
+ threads.set(threadId, thread)
|
|
|
+ }
|
|
|
+ return thread
|
|
|
}
|
|
|
|
|
|
- sendGlobalMessage(req, res) {
|
|
|
- const projectId = req.params.project_id
|
|
|
+ sendMessage(projectId, threadId, props) {
|
|
|
const message = {
|
|
|
id: Math.random().toString(),
|
|
|
- content: req.body.content,
|
|
|
+ content: props.content,
|
|
|
timestamp: Date.now(),
|
|
|
- user_id: req.body.user_id,
|
|
|
+ user_id: props.user_id,
|
|
|
}
|
|
|
- this.projects[projectId] = this.projects[projectId] || []
|
|
|
- this.projects[projectId].push(message)
|
|
|
- res.json(Object.assign({ room_id: projectId }, message))
|
|
|
+ const thread = this.getThread(projectId, threadId)
|
|
|
+ thread.push(message)
|
|
|
+ return { room_id: projectId, ...message }
|
|
|
}
|
|
|
|
|
|
- destroyProject(req, res) {
|
|
|
- const projectId = req.params.project_id
|
|
|
- delete this.projects[projectId]
|
|
|
- res.sendStatus(204)
|
|
|
+ destroyProject(projectId) {
|
|
|
+ this.projects.delete(projectId)
|
|
|
}
|
|
|
|
|
|
applyRoutes() {
|
|
|
- this.app.get('/project/:project_id/messages', (req, res) =>
|
|
|
- this.getGlobalMessages(req, res)
|
|
|
- )
|
|
|
- this.app.post('/project/:project_id/messages', (req, res) =>
|
|
|
- this.sendGlobalMessage(req, res)
|
|
|
+ this.app.get('/project/:project_id/messages', (req, res) => {
|
|
|
+ res.json(this.getThread(req.params.project_id, 'global'))
|
|
|
+ })
|
|
|
+ this.app.post('/project/:project_id/messages', (req, res) => {
|
|
|
+ res.json(this.sendMessage(req.params.project_id, 'global', req.body))
|
|
|
+ })
|
|
|
+ this.app.get(
|
|
|
+ '/project/:project_id/thread/:thread_id/messages',
|
|
|
+ (req, res) => {
|
|
|
+ res.json(this.getThread(req.params.project_id, req.params.thread_id))
|
|
|
+ }
|
|
|
)
|
|
|
- this.app.delete('/project/:project_id', (req, res) =>
|
|
|
- this.destroyProject(req, res)
|
|
|
+ this.app.post(
|
|
|
+ '/project/:project_id/thread/:thread_id/messages',
|
|
|
+ (req, res) => {
|
|
|
+ res.json(
|
|
|
+ this.sendMessage(
|
|
|
+ req.params.project_id,
|
|
|
+ req.params.thread_id,
|
|
|
+ req.body
|
|
|
+ )
|
|
|
+ )
|
|
|
+ }
|
|
|
)
|
|
|
+ this.app.delete('/project/:project_id', (req, res) => {
|
|
|
+ const projectId = req.params.project_id
|
|
|
+ this.destroyProject(projectId)
|
|
|
+ res.sendStatus(204)
|
|
|
+ })
|
|
|
}
|
|
|
}
|
|
|
|