Discord bot
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

presenter.ts 3.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import FolderViewMode from '../../models/FolderViewMode'
  2. import Folder from '../../models/Folder'
  3. import FolderService from './service'
  4. import Item from '../../models/Item'
  5. import {_} from '../../utilities/messages'
  6. export default class FolderPresenter {
  7. private readonly folderService = new FolderService()
  8. private async getCommonProps(folderId: string) {
  9. return Promise.all([
  10. this.folderService.getHierarchy(folderId),
  11. this.folderService.getChildren(folderId),
  12. this.folderService.getItems(folderId),
  13. ])
  14. }
  15. async getRootFolderViewState({ session, query, mode, }) {
  16. // TODO make this authentication checking more like a middleware.
  17. if (!session) {
  18. return {
  19. redirect: {
  20. url: new URL('/', process.env.BASE_URL)
  21. },
  22. }
  23. }
  24. const folderId = session.user.rootFolder.id
  25. const [hierarchy, children, items] = await this.getCommonProps(folderId)
  26. return {
  27. body: {
  28. data: {
  29. query,
  30. mode,
  31. items: items.map(Item.formatForView),
  32. children: children.map(c => Folder.formatForView(c)),
  33. hierarchy: hierarchy.map(h => Folder.formatForView(h)),
  34. },
  35. },
  36. }
  37. }
  38. async getDescendantFolderViewState({ session, query, mode, id, }) {
  39. // TODO make this authentication checking more like a middleware.
  40. if (!session) {
  41. return {
  42. redirect: {
  43. url: new URL('/', process.env.BASE_URL)
  44. },
  45. }
  46. }
  47. const folderId = id as string
  48. const [hierarchy, children, items] = await this.getCommonProps(folderId)
  49. return {
  50. body: {
  51. data: {
  52. query,
  53. mode,
  54. items: items.map(Item.formatForView),
  55. children: children.map(Folder.formatForView),
  56. hierarchy: hierarchy.map(Folder.formatForView),
  57. },
  58. },
  59. }
  60. }
  61. async getFolder({ id }) {
  62. try {
  63. const data = await this.folderService.get(id)
  64. // TODO compute owner of folder by getting the root folder
  65. if (data) {
  66. return {
  67. status: 200,
  68. title: _('FOLDER_FOUND_TITLE'),
  69. body: {
  70. description: _('FOLDER_FOUND_DESCRIPTION'),
  71. data,
  72. },
  73. }
  74. }
  75. return {
  76. status: 404,
  77. title: _('FOLDER_NOT_FOUND_TITLE'),
  78. body: {
  79. description: _('FOLDER_NOT_FOUND_DESCRIPTION'),
  80. },
  81. }
  82. } catch {
  83. return {
  84. status: 500,
  85. title: _('ERROR_RETRIEVING_FOLDER_TITLE'),
  86. body: {
  87. description: _('ERROR_RETRIEVING_FOLDER_DESCRIPTION'),
  88. },
  89. }
  90. }
  91. }
  92. async createFolder({ name, parentId, origin = null }) {
  93. try {
  94. const newFolder = await this.folderService.create(name, parentId)
  95. return {
  96. status: 201,
  97. title: _('FOLDER_CREATED_TITLE'),
  98. body: {
  99. description: _('FOLDER_CREATED_DESCRIPTION'),
  100. data: newFolder,
  101. },
  102. redirect: {
  103. url: new URL(`/my/folders/${newFolder.id}`, process.env.BASE_URL),
  104. },
  105. }
  106. } catch {
  107. return {
  108. status: 500,
  109. title: _('ERROR_CREATING_FOLDER_TITLE'),
  110. body: {
  111. description: _('ERROR_CREATING_FOLDER_DESCRIPTION'),
  112. },
  113. redirect: {
  114. url: new URL(origin),
  115. },
  116. }
  117. }
  118. }
  119. generateRedirectToCreateFolder({ sourceURL, }) {
  120. const url = sourceURL ? new URL(sourceURL) : new URL('/my/folders', process.env.BASE_URL)
  121. const search = Object.fromEntries(url.searchParams.entries())
  122. url.search = new URLSearchParams({
  123. ...search,
  124. 'mode': FolderViewMode.NEW_FOLDER,
  125. }).toString()
  126. return {
  127. redirect: {
  128. url
  129. }
  130. }
  131. }
  132. }