Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LEAN-3843-test #181

Open
wants to merge 50 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
e99261a
Update DocumentService.ts
fwaisi Nov 19, 2024
3c9620a
Update package.json
zax-assistant[bot] Nov 19, 2024
3c68ae8
test
fwaisi Nov 26, 2024
644ae7f
Update package.json
zax-assistant[bot] Nov 26, 2024
7404470
Update DocumentService.ts
fwaisi Nov 27, 2024
1b32449
sync with master
atyponci Nov 27, 2024
b645d69
Update package.json
zax-assistant[bot] Nov 27, 2024
d369fd6
test
fwaisi Nov 27, 2024
9e526d9
Update package.json
zax-assistant[bot] Nov 27, 2024
60d5645
test
fwaisi Nov 27, 2024
7bcc276
sync with master
atyponci Nov 28, 2024
9d3fb63
Update package.json
zax-assistant[bot] Nov 28, 2024
6d62962
Update DocumentController.ts
fwaisi Nov 28, 2024
cad2fcf
Merge branch 'LEAN-3843-test' of https://github.com/Atypon-OpenSource…
fwaisi Nov 28, 2024
605ea38
sync with master
atyponci Nov 28, 2024
d125b1b
Update package.json
zax-assistant[bot] Nov 28, 2024
dfa861f
test
fwaisi Nov 28, 2024
f7ad85b
Update package.json
zax-assistant[bot] Nov 28, 2024
b5900a5
Update DocumentController.ts
fwaisi Dec 1, 2024
acf0e81
sync with master
atyponci Dec 1, 2024
1424f28
Update package.json
zax-assistant[bot] Dec 1, 2024
41e68ca
Update DocumentService.ts
fwaisi Dec 1, 2024
e64451e
Merge branch 'LEAN-3843-test' of https://github.com/Atypon-OpenSource…
fwaisi Dec 1, 2024
20a4b62
Update package.json
zax-assistant[bot] Dec 1, 2024
d7e3273
aa
fwaisi Dec 1, 2024
3447278
Merge branch 'LEAN-3843-test' of https://github.com/Atypon-OpenSource…
fwaisi Dec 1, 2024
a7c082d
Update package.json
zax-assistant[bot] Dec 1, 2024
4ce4886
Update DocumentService.ts
fwaisi Dec 1, 2024
ad9281e
Merge branch 'LEAN-3843-test' of https://github.com/Atypon-OpenSource…
fwaisi Dec 1, 2024
716f116
Update package.json
zax-assistant[bot] Dec 1, 2024
796e626
Update DocumentService.ts
fwaisi Dec 1, 2024
02ec2ef
Update DocumentService.ts
fwaisi Dec 1, 2024
a0b352a
Merge branch 'LEAN-3843-test' of https://github.com/Atypon-OpenSource…
fwaisi Dec 1, 2024
798174e
Update package.json
zax-assistant[bot] Dec 1, 2024
5f70d2b
Update DocumentService.ts
fwaisi Dec 1, 2024
beede6c
Merge branch 'LEAN-3843-test' of https://github.com/Atypon-OpenSource…
fwaisi Dec 1, 2024
d83be20
Update package.json
zax-assistant[bot] Dec 1, 2024
2bec327
sss
fwaisi Dec 1, 2024
b247496
Merge branch 'LEAN-3843-test' of https://github.com/Atypon-OpenSource…
fwaisi Dec 1, 2024
b5555e3
Update package.json
zax-assistant[bot] Dec 1, 2024
66a5e21
Update DocumentService.ts
fwaisi Dec 1, 2024
d9a8cca
Merge branch 'LEAN-3843-test' of https://github.com/Atypon-OpenSource…
fwaisi Dec 1, 2024
dac7d7f
Update package.json
zax-assistant[bot] Dec 1, 2024
c00591e
kk
fwaisi Dec 1, 2024
040fb24
Merge branch 'LEAN-3843-test' of https://github.com/Atypon-OpenSource…
fwaisi Dec 1, 2024
6a261e9
Update package.json
zax-assistant[bot] Dec 1, 2024
e92ac36
Update package.json
zax-assistant[bot] Dec 1, 2024
a0f1154
Update DocumentService.ts
fwaisi Dec 1, 2024
7547d3e
Merge branch 'LEAN-3843-test' of https://github.com/Atypon-OpenSource…
fwaisi Dec 1, 2024
6696f47
Update package.json
zax-assistant[bot] Dec 1, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@manuscripts/api",
"version": "1.13.20",
"version": "1.13.21-LEAN-3843-test.10",
"description": "Manuscripts API server",
"license": "Apache-2.0",
"main": "dist/index",
Expand Down
18 changes: 18 additions & 0 deletions src/Controller/V2/Document/DocumentController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ export class DocumentController extends BaseController {
)
return await DIContainer.sharedContainer.authorityService.getEvents(manuscriptID, versionID)
}
async processSteps({
projectID,
manuscriptID,
payload,
user,
}: {
projectID: string
manuscriptID: string
payload: any
user: any
}): Promise<any> {
const result = await this.receiveSteps(projectID, manuscriptID, payload, user)

// Broadcast steps to other connected clients
this.broadcastSteps(manuscriptID, result)

return result
}

async receiveSteps(
projectID: string,
Expand Down
34 changes: 29 additions & 5 deletions src/Controller/V2/Document/DocumentRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,42 @@ export class DocumentRoute extends BaseRoute {
await this.documentController.deleteDocument(projectID, manuscriptID, user)
res.sendStatus(StatusCodes.OK).end()
}
private async receiveSteps(req: Request, res: Response) {
const { manuscriptID, projectID } = req.params
const user = req.user
const payload = req.body
private async processSteps({
projectID,
manuscriptID,
payload,
user,
}: {
projectID: string
manuscriptID: string
payload: any
user: any
}): Promise<any> {
const result = await this.documentController.receiveSteps(
projectID,
manuscriptID,
payload,
user
)

// Broadcast steps to other connected clients
this.documentController.broadcastSteps(manuscriptID, result)
res.status(StatusCodes.OK).send(result)

return result
}

private async receiveSteps(req: Request, res: Response) {
const { manuscriptID, projectID } = req.params
const user = req.user
const payload = req.body
console.log('received steps', user)
try {
const result = await this.processSteps({ projectID, manuscriptID, payload, user })
res.status(StatusCodes.OK).send(result)
} catch (error) {
console.error('Error processing steps:', error)
res.status(StatusCodes.INTERNAL_SERVER_ERROR).send({ error: error.message })
}
}

private async stepsSince(req: Request, res: Response) {
Expand Down
37 changes: 36 additions & 1 deletion src/DomainServices/DocumentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

import { IncomingMessage } from 'http'
import { Duplex } from 'stream'
import { ErrorEvent, WebSocket, WebSocketServer } from 'ws'
import { ErrorEvent, MessageEvent, WebSocket, WebSocketServer } from 'ws'

import { DocumentController } from '../Controller/V2/Document/DocumentController'
import { DIContainer } from '../DIContainer/DIContainer'
import { MissingManuscriptError, RoleDoesNotPermitOperationError } from '../Errors'
import { ProjectUserRole } from '../Models/ProjectModels'
Expand All @@ -39,6 +40,7 @@ export interface SnapshotLabelResult {
const EMPTY_PERMISSIONS = new Set<DocumentPermission>()
export class DocumentService {
constructor(private readonly socketsService: SocketsService) {}
private documentController = new DocumentController()

async getPermissions(
projectID: string,
Expand Down Expand Up @@ -126,6 +128,12 @@ export class DocumentService {
private attachListeners(_stream: Duplex, ws: WebSocket, manuscriptID: string) {
ws.onerror = (error) => this.onError(ws, error)
ws.onclose = () => this.onClose(ws, manuscriptID)
// Wrap the async method in a synchronous event handler
ws.onmessage = (event: MessageEvent) => {
this.onMessage(ws, event).catch((error) => {
console.error('Unhandled error in WebSocket message handler', error)
})
}
}

private onError(ws: WebSocket, error: ErrorEvent) {
Expand All @@ -136,6 +144,33 @@ export class DocumentService {
log.info(`connection closed for ${manuscriptID}`)
this.closeSocket(ws, manuscriptID)
}
private async onMessage(ws: WebSocket, event: MessageEvent): Promise<void> {
try {
// Parse message data (ensure it conforms to your expected structure)
const { projectID, manuscriptID, payload, token } = JSON.parse(event.data as string)
const valid = validateToken(token)
const user = await DIContainer.sharedContainer.userClient.findByID(valid.userID)
console.log('received steps', user)
const result = await this.documentController.processSteps({
projectID,
manuscriptID,
payload,
user,
})
// Acknowledge success
ws.send(JSON.stringify({ status: 'success', result }))
} catch (error) {
console.error('Error processing WebSocket message:', error)

// Notify the client about the error
ws.send(
JSON.stringify({
status: 'error',
message: error instanceof Error ? error.message : String(error),
})
)
}
}

private closeSocket(ws: WebSocket, manuscriptID?: string) {
try {
Expand Down