BaseMessagesController.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { Request, Response } from 'express'
  2. import { BaseHttpController, results } from 'inversify-express-utils'
  3. import { HttpStatusCode } from '@standardnotes/responses'
  4. import { ControllerContainerInterface, MapperInterface } from '@standardnotes/domain-core'
  5. import { GetMessagesSentToUser } from '../../../Domain/UseCase/Messaging/GetMessagesSentToUser/GetMessagesSentToUser'
  6. import { MessageHttpRepresentation } from '../../../Mapping/Http/MessageHttpRepresentation'
  7. import { Message } from '../../../Domain/Message/Message'
  8. import { SendMessageToUser } from '../../../Domain/UseCase/Messaging/SendMessageToUser/SendMessageToUser'
  9. import { DeleteAllMessagesSentToUser } from '../../../Domain/UseCase/Messaging/DeleteAllMessagesSentToUser/DeleteAllMessagesSentToUser'
  10. import { DeleteMessage } from '../../../Domain/UseCase/Messaging/DeleteMessage/DeleteMessage'
  11. import { GetMessagesSentByUser } from '../../../Domain/UseCase/Messaging/GetMessagesSentByUser/GetMessagesSentByUser'
  12. export class BaseMessagesController extends BaseHttpController {
  13. constructor(
  14. protected getMessageSentToUserUseCase: GetMessagesSentToUser,
  15. protected getMessagesSentByUserUseCase: GetMessagesSentByUser,
  16. protected sendMessageToUserUseCase: SendMessageToUser,
  17. protected deleteMessagesSentToUserUseCase: DeleteAllMessagesSentToUser,
  18. protected deleteMessageUseCase: DeleteMessage,
  19. protected messageHttpMapper: MapperInterface<Message, MessageHttpRepresentation>,
  20. private controllerContainer?: ControllerContainerInterface,
  21. ) {
  22. super()
  23. if (this.controllerContainer !== undefined) {
  24. this.controllerContainer.register('sync.messages.get-received', this.getMessages.bind(this))
  25. this.controllerContainer.register('sync.messages.get-sent', this.getMessagesSent.bind(this))
  26. this.controllerContainer.register('sync.messages.send', this.sendMessage.bind(this))
  27. this.controllerContainer.register('sync.messages.delete-all', this.deleteMessagesSentToUser.bind(this))
  28. this.controllerContainer.register('sync.messages.delete', this.deleteMessage.bind(this))
  29. }
  30. }
  31. async getMessages(_request: Request, response: Response): Promise<results.JsonResult> {
  32. const result = await this.getMessageSentToUserUseCase.execute({
  33. recipientUuid: response.locals.user.uuid,
  34. })
  35. if (result.isFailed()) {
  36. return this.json(
  37. {
  38. error: {
  39. message: result.getError(),
  40. },
  41. },
  42. HttpStatusCode.BadRequest,
  43. )
  44. }
  45. return this.json({
  46. messages: result.getValue().map((message) => this.messageHttpMapper.toProjection(message)),
  47. })
  48. }
  49. async getMessagesSent(_request: Request, response: Response): Promise<results.JsonResult> {
  50. const result = await this.getMessagesSentByUserUseCase.execute({
  51. senderUuid: response.locals.user.uuid,
  52. })
  53. if (result.isFailed()) {
  54. return this.json(
  55. {
  56. error: {
  57. message: result.getError(),
  58. },
  59. },
  60. HttpStatusCode.BadRequest,
  61. )
  62. }
  63. return this.json({
  64. messages: result.getValue().map((message) => this.messageHttpMapper.toProjection(message)),
  65. })
  66. }
  67. async sendMessage(request: Request, response: Response): Promise<results.JsonResult> {
  68. const result = await this.sendMessageToUserUseCase.execute({
  69. senderUuid: response.locals.user.uuid,
  70. recipientUuid: request.body.recipient_uuid,
  71. encryptedMessage: request.body.encrypted_message,
  72. replaceabilityIdentifier: request.body.replaceability_identifier,
  73. })
  74. if (result.isFailed()) {
  75. return this.json(
  76. {
  77. error: {
  78. message: result.getError(),
  79. },
  80. },
  81. HttpStatusCode.BadRequest,
  82. )
  83. }
  84. return this.json({
  85. message: this.messageHttpMapper.toProjection(result.getValue()),
  86. })
  87. }
  88. async deleteMessagesSentToUser(_request: Request, response: Response): Promise<results.JsonResult> {
  89. const result = await this.deleteMessagesSentToUserUseCase.execute({
  90. recipientUuid: response.locals.user.uuid,
  91. })
  92. if (result.isFailed()) {
  93. return this.json(
  94. {
  95. error: {
  96. message: result.getError(),
  97. },
  98. },
  99. HttpStatusCode.BadRequest,
  100. )
  101. }
  102. return this.json({ success: true })
  103. }
  104. async deleteMessage(request: Request, response: Response): Promise<results.JsonResult> {
  105. const result = await this.deleteMessageUseCase.execute({
  106. messageUuid: request.params.messageUuid,
  107. originatorUuid: response.locals.user.uuid,
  108. })
  109. if (result.isFailed()) {
  110. return this.json(
  111. {
  112. error: {
  113. message: result.getError(),
  114. },
  115. },
  116. HttpStatusCode.BadRequest,
  117. )
  118. }
  119. return this.json({ success: true })
  120. }
  121. }