2
1

me.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import * as express from 'express'
  2. import 'multer'
  3. import { UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../../shared'
  4. import { getFormattedObjects } from '../../../helpers/utils'
  5. import { MIMETYPES } from '../../../initializers/constants'
  6. import { sendUpdateActor } from '../../../lib/activitypub/send'
  7. import {
  8. asyncMiddleware,
  9. asyncRetryTransactionMiddleware,
  10. authenticate,
  11. paginationValidator,
  12. setDefaultPagination,
  13. setDefaultSort,
  14. usersUpdateMeValidator,
  15. usersVideoRatingValidator
  16. } from '../../../middlewares'
  17. import { deleteMeValidator, videoImportsSortValidator, videosSortValidator } from '../../../middlewares/validators'
  18. import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
  19. import { UserModel } from '../../../models/account/user'
  20. import { VideoModel } from '../../../models/video/video'
  21. import { VideoSortField } from '../../../../client/src/app/shared/video/sort-field.type'
  22. import { createReqFiles } from '../../../helpers/express-utils'
  23. import { UserVideoQuota } from '../../../../shared/models/users/user-video-quota.model'
  24. import { updateAvatarValidator } from '../../../middlewares/validators/avatar'
  25. import { updateActorAvatarFile } from '../../../lib/avatar'
  26. import { VideoImportModel } from '../../../models/video/video-import'
  27. import { AccountModel } from '../../../models/account/account'
  28. import { CONFIG } from '../../../initializers/config'
  29. import { sequelizeTypescript } from '../../../initializers/database'
  30. import { sendVerifyUserEmail } from '../../../lib/user'
  31. const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.TMP_DIR })
  32. const meRouter = express.Router()
  33. meRouter.get('/me',
  34. authenticate,
  35. asyncMiddleware(getUserInformation)
  36. )
  37. meRouter.delete('/me',
  38. authenticate,
  39. asyncMiddleware(deleteMeValidator),
  40. asyncMiddleware(deleteMe)
  41. )
  42. meRouter.get('/me/video-quota-used',
  43. authenticate,
  44. asyncMiddleware(getUserVideoQuotaUsed)
  45. )
  46. meRouter.get('/me/videos/imports',
  47. authenticate,
  48. paginationValidator,
  49. videoImportsSortValidator,
  50. setDefaultSort,
  51. setDefaultPagination,
  52. asyncMiddleware(getUserVideoImports)
  53. )
  54. meRouter.get('/me/videos',
  55. authenticate,
  56. paginationValidator,
  57. videosSortValidator,
  58. setDefaultSort,
  59. setDefaultPagination,
  60. asyncMiddleware(getUserVideos)
  61. )
  62. meRouter.get('/me/videos/:videoId/rating',
  63. authenticate,
  64. asyncMiddleware(usersVideoRatingValidator),
  65. asyncMiddleware(getUserVideoRating)
  66. )
  67. meRouter.put('/me',
  68. authenticate,
  69. asyncMiddleware(usersUpdateMeValidator),
  70. asyncRetryTransactionMiddleware(updateMe)
  71. )
  72. meRouter.post('/me/avatar/pick',
  73. authenticate,
  74. reqAvatarFile,
  75. updateAvatarValidator,
  76. asyncRetryTransactionMiddleware(updateMyAvatar)
  77. )
  78. // ---------------------------------------------------------------------------
  79. export {
  80. meRouter
  81. }
  82. // ---------------------------------------------------------------------------
  83. async function getUserVideos (req: express.Request, res: express.Response) {
  84. const user = res.locals.oauth.token.User
  85. const resultList = await VideoModel.listUserVideosForApi(
  86. user.Account.id,
  87. req.query.start as number,
  88. req.query.count as number,
  89. req.query.sort as VideoSortField,
  90. req.query.search as string
  91. )
  92. const additionalAttributes = {
  93. waitTranscoding: true,
  94. state: true,
  95. scheduledUpdate: true,
  96. blacklistInfo: true
  97. }
  98. return res.json(getFormattedObjects(resultList.data, resultList.total, { additionalAttributes }))
  99. }
  100. async function getUserVideoImports (req: express.Request, res: express.Response) {
  101. const user = res.locals.oauth.token.User
  102. const resultList = await VideoImportModel.listUserVideoImportsForApi(
  103. user.id,
  104. req.query.start as number,
  105. req.query.count as number,
  106. req.query.sort
  107. )
  108. return res.json(getFormattedObjects(resultList.data, resultList.total))
  109. }
  110. async function getUserInformation (req: express.Request, res: express.Response) {
  111. // We did not load channels in res.locals.user
  112. const user = await UserModel.loadForMeAPI(res.locals.oauth.token.user.username)
  113. return res.json(user.toMeFormattedJSON())
  114. }
  115. async function getUserVideoQuotaUsed (req: express.Request, res: express.Response) {
  116. const user = res.locals.oauth.token.user
  117. const videoQuotaUsed = await UserModel.getOriginalVideoFileTotalFromUser(user)
  118. const videoQuotaUsedDaily = await UserModel.getOriginalVideoFileTotalDailyFromUser(user)
  119. const data: UserVideoQuota = {
  120. videoQuotaUsed,
  121. videoQuotaUsedDaily
  122. }
  123. return res.json(data)
  124. }
  125. async function getUserVideoRating (req: express.Request, res: express.Response) {
  126. const videoId = res.locals.videoId.id
  127. const accountId = +res.locals.oauth.token.User.Account.id
  128. const ratingObj = await AccountVideoRateModel.load(accountId, videoId, null)
  129. const rating = ratingObj ? ratingObj.type : 'none'
  130. const json: FormattedUserVideoRate = {
  131. videoId,
  132. rating
  133. }
  134. return res.json(json)
  135. }
  136. async function deleteMe (req: express.Request, res: express.Response) {
  137. const user = res.locals.oauth.token.User
  138. await user.destroy()
  139. return res.sendStatus(204)
  140. }
  141. async function updateMe (req: express.Request, res: express.Response) {
  142. const body: UserUpdateMe = req.body
  143. let sendVerificationEmail = false
  144. const user = res.locals.oauth.token.user
  145. if (body.password !== undefined) user.password = body.password
  146. if (body.nsfwPolicy !== undefined) user.nsfwPolicy = body.nsfwPolicy
  147. if (body.webTorrentEnabled !== undefined) user.webTorrentEnabled = body.webTorrentEnabled
  148. if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo
  149. if (body.autoPlayNextVideo !== undefined) user.autoPlayNextVideo = body.autoPlayNextVideo
  150. if (body.autoPlayNextVideoPlaylist !== undefined) user.autoPlayNextVideoPlaylist = body.autoPlayNextVideoPlaylist
  151. if (body.videosHistoryEnabled !== undefined) user.videosHistoryEnabled = body.videosHistoryEnabled
  152. if (body.videoLanguages !== undefined) user.videoLanguages = body.videoLanguages
  153. if (body.theme !== undefined) user.theme = body.theme
  154. if (body.noInstanceConfigWarningModal !== undefined) user.noInstanceConfigWarningModal = body.noInstanceConfigWarningModal
  155. if (body.noWelcomeModal !== undefined) user.noWelcomeModal = body.noWelcomeModal
  156. if (body.email !== undefined) {
  157. if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) {
  158. user.pendingEmail = body.email
  159. sendVerificationEmail = true
  160. } else {
  161. user.email = body.email
  162. }
  163. }
  164. await sequelizeTypescript.transaction(async t => {
  165. await user.save({ transaction: t })
  166. if (body.displayName !== undefined || body.description !== undefined) {
  167. const userAccount = await AccountModel.load(user.Account.id, t)
  168. if (body.displayName !== undefined) userAccount.name = body.displayName
  169. if (body.description !== undefined) userAccount.description = body.description
  170. await userAccount.save({ transaction: t })
  171. await sendUpdateActor(userAccount, t)
  172. }
  173. })
  174. if (sendVerificationEmail === true) {
  175. await sendVerifyUserEmail(user, true)
  176. }
  177. return res.sendStatus(204)
  178. }
  179. async function updateMyAvatar (req: express.Request, res: express.Response) {
  180. const avatarPhysicalFile = req.files[ 'avatarfile' ][ 0 ]
  181. const user = res.locals.oauth.token.user
  182. const userAccount = await AccountModel.load(user.Account.id)
  183. const avatar = await updateActorAvatarFile(avatarPhysicalFile, userAccount)
  184. return res.json({ avatar: avatar.toFormattedJSON() })
  185. }