Files
Verso/services/web/test/acceptance/src/mocks/MockChatApi.mjs
T
Eric Mc Sween 201e5c49aa Merge pull request #28019 from overleaf/em-dsmp-comment-event
DS mobile notification for comment added/replied to

GitOrigin-RevId: 602983c679ed415bf8dd8fbf83b328e5ee832e2b
2025-08-26 08:05:20 +00:00

80 lines
1.9 KiB
JavaScript

import AbstractMockApi from './AbstractMockApi.mjs'
class MockChatApi extends AbstractMockApi {
reset() {
this.projects = new Map()
}
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
}
sendMessage(projectId, threadId, props) {
const message = {
id: Math.random().toString(),
content: props.content,
timestamp: Date.now(),
user_id: props.user_id,
}
const thread = this.getThread(projectId, threadId)
thread.push(message)
return { room_id: projectId, ...message }
}
destroyProject(projectId) {
this.projects.delete(projectId)
}
applyRoutes() {
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.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)
})
}
}
export default MockChatApi
// type hint for the inherited `instance` method
/**
* @function instance
* @memberOf MockChatApi
* @static
* @returns {MockChatApi}
*/