2017-06-05 14:53:49 -05:00
|
|
|
import * as express from 'express'
|
2018-02-13 11:17:05 -06:00
|
|
|
import 'multer'
|
2018-03-29 03:58:24 -05:00
|
|
|
import * as RateLimit from 'express-rate-limit'
|
2017-11-10 10:27:49 -06:00
|
|
|
import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared'
|
2017-12-28 04:16:08 -06:00
|
|
|
import { logger } from '../../helpers/logger'
|
2018-04-24 08:10:54 -05:00
|
|
|
import { getFormattedObjects } from '../../helpers/utils'
|
2018-06-29 04:29:23 -05:00
|
|
|
import { CONFIG, IMAGE_MIMETYPE_EXT, RATES_LIMIT, sequelizeTypescript } from '../../initializers'
|
2018-02-15 07:46:26 -06:00
|
|
|
import { sendUpdateActor } from '../../lib/activitypub/send'
|
2018-01-30 06:27:07 -06:00
|
|
|
import { Emailer } from '../../lib/emailer'
|
|
|
|
import { Redis } from '../../lib/redis'
|
2017-12-14 10:38:41 -06:00
|
|
|
import { createUserAccountAndChannel } from '../../lib/user'
|
2017-05-15 15:22:03 -05:00
|
|
|
import {
|
2018-01-30 08:16:24 -06:00
|
|
|
asyncMiddleware,
|
2018-06-13 07:27:40 -05:00
|
|
|
asyncRetryTransactionMiddleware,
|
2018-01-30 08:16:24 -06:00
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight,
|
|
|
|
ensureUserRegistrationAllowed,
|
2018-05-22 12:43:13 -05:00
|
|
|
ensureUserRegistrationAllowedForIP,
|
2018-01-30 08:16:24 -06:00
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
|
|
|
setDefaultSort,
|
|
|
|
token,
|
|
|
|
usersAddValidator,
|
|
|
|
usersGetValidator,
|
|
|
|
usersRegisterValidator,
|
|
|
|
usersRemoveValidator,
|
|
|
|
usersSortValidator,
|
|
|
|
usersUpdateMeValidator,
|
|
|
|
usersUpdateValidator,
|
|
|
|
usersVideoRatingValidator
|
2017-05-15 15:22:03 -05:00
|
|
|
} from '../../middlewares'
|
2018-08-02 10:48:50 -05:00
|
|
|
import {
|
2018-08-08 03:55:27 -05:00
|
|
|
deleteMeValidator,
|
2018-08-02 10:48:50 -05:00
|
|
|
usersAskResetPasswordValidator,
|
2018-08-08 07:58:21 -05:00
|
|
|
usersBlockingValidator,
|
2018-08-02 10:48:50 -05:00
|
|
|
usersResetPasswordValidator,
|
|
|
|
videoImportsSortValidator,
|
|
|
|
videosSortValidator
|
|
|
|
} from '../../middlewares/validators'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { AccountVideoRateModel } from '../../models/account/account-video-rate'
|
|
|
|
import { UserModel } from '../../models/account/user'
|
2018-01-23 02:15:36 -06:00
|
|
|
import { OAuthTokenModel } from '../../models/oauth/oauth-token'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { VideoModel } from '../../models/video/video'
|
2018-04-19 04:01:34 -05:00
|
|
|
import { VideoSortField } from '../../../client/src/app/shared/video/sort-field.type'
|
2018-04-24 08:10:54 -05:00
|
|
|
import { createReqFiles } from '../../helpers/express-utils'
|
2018-05-16 04:00:57 -05:00
|
|
|
import { UserVideoQuota } from '../../../shared/models/users/user-video-quota.model'
|
2018-06-29 04:29:23 -05:00
|
|
|
import { updateAvatarValidator } from '../../middlewares/validators/avatar'
|
|
|
|
import { updateActorAvatarFile } from '../../lib/avatar'
|
2018-07-31 07:04:26 -05:00
|
|
|
import { auditLoggerFactory, UserAuditView } from '../../helpers/audit-logger'
|
2018-08-02 10:48:50 -05:00
|
|
|
import { VideoImportModel } from '../../models/video/video-import'
|
2018-07-31 07:04:26 -05:00
|
|
|
|
|
|
|
const auditLogger = auditLoggerFactory('users')
|
2017-05-15 15:22:03 -05:00
|
|
|
|
2018-02-13 11:17:05 -06:00
|
|
|
const reqAvatarFile = createReqFiles([ 'avatarfile' ], IMAGE_MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.AVATARS_DIR })
|
2018-03-29 03:58:24 -05:00
|
|
|
const loginRateLimiter = new RateLimit({
|
|
|
|
windowMs: RATES_LIMIT.LOGIN.WINDOW_MS,
|
|
|
|
max: RATES_LIMIT.LOGIN.MAX,
|
|
|
|
delayMs: 0
|
|
|
|
})
|
2017-12-29 12:10:13 -06:00
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
const usersRouter = express.Router()
|
|
|
|
|
|
|
|
usersRouter.get('/me',
|
|
|
|
authenticate,
|
2017-10-25 04:55:06 -05:00
|
|
|
asyncMiddleware(getUserInformation)
|
2017-03-08 14:35:43 -06:00
|
|
|
)
|
2018-08-08 03:55:27 -05:00
|
|
|
usersRouter.delete('/me',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(deleteMeValidator),
|
|
|
|
asyncMiddleware(deleteMe)
|
|
|
|
)
|
2017-03-08 14:35:43 -06:00
|
|
|
|
2018-01-08 05:53:09 -06:00
|
|
|
usersRouter.get('/me/video-quota-used',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(getUserVideoQuotaUsed)
|
|
|
|
)
|
|
|
|
|
2018-08-02 10:48:50 -05:00
|
|
|
usersRouter.get('/me/videos/imports',
|
|
|
|
authenticate,
|
|
|
|
paginationValidator,
|
|
|
|
videoImportsSortValidator,
|
|
|
|
setDefaultSort,
|
|
|
|
setDefaultPagination,
|
|
|
|
asyncMiddleware(getUserVideoImports)
|
|
|
|
)
|
|
|
|
|
2017-10-31 05:52:52 -05:00
|
|
|
usersRouter.get('/me/videos',
|
|
|
|
authenticate,
|
|
|
|
paginationValidator,
|
|
|
|
videosSortValidator,
|
2018-01-17 03:50:33 -06:00
|
|
|
setDefaultSort,
|
2018-01-18 03:53:54 -06:00
|
|
|
setDefaultPagination,
|
2017-10-31 05:52:52 -05:00
|
|
|
asyncMiddleware(getUserVideos)
|
|
|
|
)
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
usersRouter.get('/me/videos/:videoId/rating',
|
|
|
|
authenticate,
|
2017-11-27 10:30:46 -06:00
|
|
|
asyncMiddleware(usersVideoRatingValidator),
|
2017-10-25 04:55:06 -05:00
|
|
|
asyncMiddleware(getUserVideoRating)
|
2017-03-08 14:35:43 -06:00
|
|
|
)
|
2016-08-04 15:32:36 -05:00
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
usersRouter.get('/',
|
2017-11-29 06:18:05 -06:00
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_USERS),
|
2017-05-15 15:22:03 -05:00
|
|
|
paginationValidator,
|
|
|
|
usersSortValidator,
|
2018-01-17 03:50:33 -06:00
|
|
|
setDefaultSort,
|
2018-01-18 03:53:54 -06:00
|
|
|
setDefaultPagination,
|
2017-10-25 04:55:06 -05:00
|
|
|
asyncMiddleware(listUsers)
|
2016-08-16 15:31:45 -05:00
|
|
|
)
|
|
|
|
|
2018-08-08 07:58:21 -05:00
|
|
|
usersRouter.post('/:id/block',
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_USERS),
|
|
|
|
asyncMiddleware(usersBlockingValidator),
|
|
|
|
asyncMiddleware(blockUser)
|
|
|
|
)
|
|
|
|
usersRouter.post('/:id/unblock',
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_USERS),
|
|
|
|
asyncMiddleware(usersBlockingValidator),
|
|
|
|
asyncMiddleware(unblockUser)
|
|
|
|
)
|
|
|
|
|
2017-09-05 14:29:39 -05:00
|
|
|
usersRouter.get('/:id',
|
2018-04-16 03:48:17 -05:00
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_USERS),
|
2017-11-27 10:30:46 -06:00
|
|
|
asyncMiddleware(usersGetValidator),
|
2017-09-05 14:29:39 -05:00
|
|
|
getUser
|
|
|
|
)
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
usersRouter.post('/',
|
|
|
|
authenticate,
|
2017-10-27 09:55:03 -05:00
|
|
|
ensureUserHasRight(UserRight.MANAGE_USERS),
|
2017-11-27 10:30:46 -06:00
|
|
|
asyncMiddleware(usersAddValidator),
|
2018-06-13 07:27:40 -05:00
|
|
|
asyncRetryTransactionMiddleware(createUser)
|
2016-08-04 15:32:36 -05:00
|
|
|
)
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
usersRouter.post('/register',
|
2017-11-27 10:30:46 -06:00
|
|
|
asyncMiddleware(ensureUserRegistrationAllowed),
|
2018-05-22 12:43:13 -05:00
|
|
|
ensureUserRegistrationAllowedForIP,
|
2017-11-27 10:30:46 -06:00
|
|
|
asyncMiddleware(usersRegisterValidator),
|
2018-06-13 07:27:40 -05:00
|
|
|
asyncRetryTransactionMiddleware(registerUser)
|
2017-04-09 05:08:36 -05:00
|
|
|
)
|
|
|
|
|
2017-09-05 14:29:39 -05:00
|
|
|
usersRouter.put('/me',
|
|
|
|
authenticate,
|
|
|
|
usersUpdateMeValidator,
|
2017-10-25 04:55:06 -05:00
|
|
|
asyncMiddleware(updateMe)
|
2017-09-05 14:29:39 -05:00
|
|
|
)
|
|
|
|
|
2017-12-29 12:10:13 -06:00
|
|
|
usersRouter.post('/me/avatar/pick',
|
|
|
|
authenticate,
|
|
|
|
reqAvatarFile,
|
2018-06-29 04:29:23 -05:00
|
|
|
updateAvatarValidator,
|
2017-12-29 12:10:13 -06:00
|
|
|
asyncMiddleware(updateMyAvatar)
|
|
|
|
)
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
usersRouter.put('/:id',
|
|
|
|
authenticate,
|
2017-10-27 09:55:03 -05:00
|
|
|
ensureUserHasRight(UserRight.MANAGE_USERS),
|
2017-11-27 10:30:46 -06:00
|
|
|
asyncMiddleware(usersUpdateValidator),
|
2017-10-25 04:55:06 -05:00
|
|
|
asyncMiddleware(updateUser)
|
2016-08-04 15:32:36 -05:00
|
|
|
)
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
usersRouter.delete('/:id',
|
|
|
|
authenticate,
|
2017-10-27 09:55:03 -05:00
|
|
|
ensureUserHasRight(UserRight.MANAGE_USERS),
|
2017-11-27 10:30:46 -06:00
|
|
|
asyncMiddleware(usersRemoveValidator),
|
2017-10-25 04:55:06 -05:00
|
|
|
asyncMiddleware(removeUser)
|
2016-08-04 15:32:36 -05:00
|
|
|
)
|
2016-08-05 09:09:39 -05:00
|
|
|
|
2018-01-30 06:27:07 -06:00
|
|
|
usersRouter.post('/ask-reset-password',
|
|
|
|
asyncMiddleware(usersAskResetPasswordValidator),
|
|
|
|
asyncMiddleware(askResetUserPassword)
|
|
|
|
)
|
|
|
|
|
|
|
|
usersRouter.post('/:id/reset-password',
|
|
|
|
asyncMiddleware(usersResetPasswordValidator),
|
|
|
|
asyncMiddleware(resetUserPassword)
|
|
|
|
)
|
|
|
|
|
2018-03-29 03:58:24 -05:00
|
|
|
usersRouter.post('/token',
|
|
|
|
loginRateLimiter,
|
|
|
|
token,
|
|
|
|
success
|
|
|
|
)
|
2016-08-04 15:32:36 -05:00
|
|
|
// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
|
2016-03-21 05:56:33 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
export {
|
|
|
|
usersRouter
|
|
|
|
}
|
2016-03-21 05:56:33 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-10-31 05:52:52 -05:00
|
|
|
async function getUserVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-12-12 10:53:50 -06:00
|
|
|
const user = res.locals.oauth.token.User as UserModel
|
2018-06-12 13:04:58 -05:00
|
|
|
const resultList = await VideoModel.listUserVideosForApi(
|
2018-04-19 04:01:34 -05:00
|
|
|
user.Account.id,
|
|
|
|
req.query.start as number,
|
|
|
|
req.query.count as number,
|
2018-08-13 09:57:13 -05:00
|
|
|
req.query.sort as VideoSortField
|
2018-04-19 04:01:34 -05:00
|
|
|
)
|
2017-10-31 05:52:52 -05:00
|
|
|
|
2018-06-14 11:06:56 -05:00
|
|
|
const additionalAttributes = {
|
|
|
|
waitTranscoding: true,
|
|
|
|
state: true,
|
2018-08-13 09:57:13 -05:00
|
|
|
scheduledUpdate: true,
|
|
|
|
blacklistInfo: true
|
2018-06-14 11:06:56 -05:00
|
|
|
}
|
2018-06-12 13:04:58 -05:00
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total, { additionalAttributes }))
|
2017-10-31 05:52:52 -05:00
|
|
|
}
|
|
|
|
|
2018-08-02 10:48:50 -05:00
|
|
|
async function getUserVideoImports (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const user = res.locals.oauth.token.User as UserModel
|
|
|
|
const resultList = await VideoImportModel.listUserVideoImportsForApi(
|
2018-08-07 03:07:53 -05:00
|
|
|
user.id,
|
2018-08-02 10:48:50 -05:00
|
|
|
req.query.start as number,
|
|
|
|
req.query.count as number,
|
|
|
|
req.query.sort
|
|
|
|
)
|
|
|
|
|
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
|
|
|
}
|
|
|
|
|
2018-06-13 07:27:40 -05:00
|
|
|
async function createUser (req: express.Request, res: express.Response) {
|
2017-07-10 12:43:21 -05:00
|
|
|
const body: UserCreate = req.body
|
2018-01-18 03:53:54 -06:00
|
|
|
const userToCreate = new UserModel({
|
2017-07-10 12:43:21 -05:00
|
|
|
username: body.username,
|
|
|
|
password: body.password,
|
|
|
|
email: body.email,
|
2018-04-19 04:01:34 -05:00
|
|
|
nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
|
2017-12-19 03:45:49 -06:00
|
|
|
autoPlayVideo: true,
|
2017-10-27 09:55:03 -05:00
|
|
|
role: body.role,
|
2017-09-04 13:07:54 -05:00
|
|
|
videoQuota: body.videoQuota
|
2016-08-04 15:32:36 -05:00
|
|
|
})
|
|
|
|
|
2018-01-18 03:53:54 -06:00
|
|
|
const { user, account } = await createUserAccountAndChannel(userToCreate)
|
2017-10-25 04:55:06 -05:00
|
|
|
|
2018-07-31 07:04:26 -05:00
|
|
|
auditLogger.create(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new UserAuditView(user.toFormattedJSON()))
|
2017-11-10 07:48:08 -06:00
|
|
|
logger.info('User %s with its channel and account created.', body.username)
|
2018-01-18 03:53:54 -06:00
|
|
|
|
2018-06-13 07:27:40 -05:00
|
|
|
return res.json({
|
|
|
|
user: {
|
|
|
|
id: user.id,
|
|
|
|
account: {
|
|
|
|
id: account.id,
|
|
|
|
uuid: account.Actor.uuid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).end()
|
2017-11-16 11:40:50 -06:00
|
|
|
}
|
|
|
|
|
2018-06-13 07:27:40 -05:00
|
|
|
async function registerUser (req: express.Request, res: express.Response) {
|
2017-09-06 09:35:40 -05:00
|
|
|
const body: UserCreate = req.body
|
|
|
|
|
2018-07-31 07:04:26 -05:00
|
|
|
const userToCreate = new UserModel({
|
2017-09-06 09:35:40 -05:00
|
|
|
username: body.username,
|
|
|
|
password: body.password,
|
|
|
|
email: body.email,
|
2018-04-19 04:01:34 -05:00
|
|
|
nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
|
2017-12-19 03:45:49 -06:00
|
|
|
autoPlayVideo: true,
|
2017-10-27 09:55:03 -05:00
|
|
|
role: UserRole.USER,
|
2017-09-06 09:35:40 -05:00
|
|
|
videoQuota: CONFIG.USER.VIDEO_QUOTA
|
|
|
|
})
|
|
|
|
|
2018-07-31 07:04:26 -05:00
|
|
|
const { user } = await createUserAccountAndChannel(userToCreate)
|
2017-11-16 11:40:50 -06:00
|
|
|
|
2018-07-31 07:04:26 -05:00
|
|
|
auditLogger.create(body.username, new UserAuditView(user.toFormattedJSON()))
|
2017-11-16 11:40:50 -06:00
|
|
|
logger.info('User %s with its channel and account registered.', body.username)
|
2018-06-13 07:27:40 -05:00
|
|
|
|
|
|
|
return res.type('json').status(204).end()
|
2017-09-06 09:35:40 -05:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-10-31 05:52:52 -05:00
|
|
|
// We did not load channels in res.locals.user
|
2017-12-12 10:53:50 -06:00
|
|
|
const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username)
|
2017-10-25 04:55:06 -05:00
|
|
|
|
|
|
|
return res.json(user.toFormattedJSON())
|
2016-08-05 10:19:08 -05:00
|
|
|
}
|
|
|
|
|
2018-01-08 05:53:09 -06:00
|
|
|
async function getUserVideoQuotaUsed (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
// We did not load channels in res.locals.user
|
|
|
|
const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username)
|
|
|
|
const videoQuotaUsed = await UserModel.getOriginalVideoFileTotalFromUser(user)
|
|
|
|
|
2018-05-16 04:00:57 -05:00
|
|
|
const data: UserVideoQuota = {
|
2018-01-08 05:53:09 -06:00
|
|
|
videoQuotaUsed
|
2018-05-16 04:00:57 -05:00
|
|
|
}
|
|
|
|
return res.json(data)
|
2018-01-08 05:53:09 -06:00
|
|
|
}
|
|
|
|
|
2018-08-08 07:58:21 -05:00
|
|
|
async function unblockUser (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const user: UserModel = res.locals.user
|
|
|
|
|
|
|
|
await changeUserBlock(res, user, false)
|
|
|
|
|
|
|
|
return res.status(204).end()
|
|
|
|
}
|
|
|
|
|
|
|
|
async function blockUser (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const user: UserModel = res.locals.user
|
2018-08-08 10:36:10 -05:00
|
|
|
const reason = req.body.reason
|
2018-08-08 07:58:21 -05:00
|
|
|
|
2018-08-08 10:36:10 -05:00
|
|
|
await changeUserBlock(res, user, true, reason)
|
2018-08-08 07:58:21 -05:00
|
|
|
|
|
|
|
return res.status(204).end()
|
|
|
|
}
|
|
|
|
|
2017-09-05 14:29:39 -05:00
|
|
|
function getUser (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2018-01-08 05:53:09 -06:00
|
|
|
return res.json((res.locals.user as UserModel).toFormattedJSON())
|
2017-09-05 14:29:39 -05:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-07-11 09:01:56 -05:00
|
|
|
const videoId = +req.params.videoId
|
2017-11-10 10:27:49 -06:00
|
|
|
const accountId = +res.locals.oauth.token.User.Account.id
|
2017-03-08 14:35:43 -06:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
const ratingObj = await AccountVideoRateModel.load(accountId, videoId, null)
|
2017-10-26 01:51:11 -05:00
|
|
|
const rating = ratingObj ? ratingObj.type : 'none'
|
|
|
|
|
|
|
|
const json: FormattedUserVideoRate = {
|
|
|
|
videoId,
|
|
|
|
rating
|
|
|
|
}
|
|
|
|
res.json(json)
|
2017-03-08 14:35:43 -06:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-12-12 10:53:50 -06:00
|
|
|
const resultList = await UserModel.listForApi(req.query.start, req.query.count, req.query.sort)
|
2017-10-25 04:55:06 -05:00
|
|
|
|
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
2016-08-04 15:32:36 -05:00
|
|
|
}
|
|
|
|
|
2018-08-08 03:55:27 -05:00
|
|
|
async function deleteMe (req: express.Request, res: express.Response) {
|
|
|
|
const user: UserModel = res.locals.oauth.token.User
|
|
|
|
|
|
|
|
await user.destroy()
|
|
|
|
|
|
|
|
auditLogger.delete(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new UserAuditView(user.toFormattedJSON()))
|
|
|
|
|
|
|
|
return res.sendStatus(204)
|
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2018-08-08 03:55:27 -05:00
|
|
|
const user: UserModel = res.locals.user
|
2017-10-25 04:55:06 -05:00
|
|
|
|
|
|
|
await user.destroy()
|
|
|
|
|
2018-07-31 07:04:26 -05:00
|
|
|
auditLogger.delete(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new UserAuditView(user.toFormattedJSON()))
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
return res.sendStatus(204)
|
2016-08-04 15:32:36 -05:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function updateMe (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-09-05 14:29:39 -05:00
|
|
|
const body: UserUpdateMe = req.body
|
2017-07-10 12:43:21 -05:00
|
|
|
|
2018-02-15 07:46:26 -06:00
|
|
|
const user: UserModel = res.locals.oauth.token.user
|
2018-07-31 07:04:26 -05:00
|
|
|
const oldUserAuditView = new UserAuditView(user.toFormattedJSON())
|
2017-04-03 14:24:36 -05:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
if (body.password !== undefined) user.password = body.password
|
|
|
|
if (body.email !== undefined) user.email = body.email
|
2018-04-19 04:01:34 -05:00
|
|
|
if (body.nsfwPolicy !== undefined) user.nsfwPolicy = body.nsfwPolicy
|
2017-12-19 03:45:49 -06:00
|
|
|
if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo
|
2017-10-25 04:55:06 -05:00
|
|
|
|
2018-02-15 07:46:26 -06:00
|
|
|
await sequelizeTypescript.transaction(async t => {
|
|
|
|
await user.save({ transaction: t })
|
|
|
|
|
2018-04-26 03:03:40 -05:00
|
|
|
if (body.displayName !== undefined) user.Account.name = body.displayName
|
2018-02-15 07:46:26 -06:00
|
|
|
if (body.description !== undefined) user.Account.description = body.description
|
|
|
|
await user.Account.save({ transaction: t })
|
|
|
|
|
|
|
|
await sendUpdateActor(user.Account, t)
|
2018-07-31 07:04:26 -05:00
|
|
|
|
|
|
|
auditLogger.update(
|
|
|
|
res.locals.oauth.token.User.Account.Actor.getIdentifier(),
|
|
|
|
new UserAuditView(user.toFormattedJSON()),
|
|
|
|
oldUserAuditView
|
|
|
|
)
|
2018-02-15 07:46:26 -06:00
|
|
|
})
|
2017-10-25 04:55:06 -05:00
|
|
|
|
2017-10-25 09:52:01 -05:00
|
|
|
return res.sendStatus(204)
|
2016-08-04 15:32:36 -05:00
|
|
|
}
|
|
|
|
|
2017-12-29 12:10:13 -06:00
|
|
|
async function updateMyAvatar (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2018-06-12 13:04:58 -05:00
|
|
|
const avatarPhysicalFile = req.files[ 'avatarfile' ][ 0 ]
|
2018-07-31 07:04:26 -05:00
|
|
|
const user: UserModel = res.locals.oauth.token.user
|
|
|
|
const oldUserAuditView = new UserAuditView(user.toFormattedJSON())
|
|
|
|
const account = user.Account
|
2017-12-29 12:10:13 -06:00
|
|
|
|
2018-06-29 04:29:23 -05:00
|
|
|
const avatar = await updateActorAvatarFile(avatarPhysicalFile, account.Actor, account)
|
2017-12-29 12:10:13 -06:00
|
|
|
|
2018-07-31 07:04:26 -05:00
|
|
|
auditLogger.update(
|
|
|
|
res.locals.oauth.token.User.Account.Actor.getIdentifier(),
|
|
|
|
new UserAuditView(user.toFormattedJSON()),
|
|
|
|
oldUserAuditView
|
|
|
|
)
|
|
|
|
|
2017-12-29 12:10:13 -06:00
|
|
|
return res
|
|
|
|
.json({
|
|
|
|
avatar: avatar.toFormattedJSON()
|
|
|
|
})
|
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-09-05 14:29:39 -05:00
|
|
|
const body: UserUpdate = req.body
|
2018-07-31 07:04:26 -05:00
|
|
|
const userToUpdate = res.locals.user as UserModel
|
|
|
|
const oldUserAuditView = new UserAuditView(userToUpdate.toFormattedJSON())
|
|
|
|
const roleChanged = body.role !== undefined && body.role !== userToUpdate.role
|
2017-09-05 14:29:39 -05:00
|
|
|
|
2018-07-31 07:04:26 -05:00
|
|
|
if (body.email !== undefined) userToUpdate.email = body.email
|
|
|
|
if (body.videoQuota !== undefined) userToUpdate.videoQuota = body.videoQuota
|
|
|
|
if (body.role !== undefined) userToUpdate.role = body.role
|
2017-09-05 14:29:39 -05:00
|
|
|
|
2018-07-31 07:04:26 -05:00
|
|
|
const user = await userToUpdate.save()
|
2017-10-25 04:55:06 -05:00
|
|
|
|
2018-01-23 02:15:36 -06:00
|
|
|
// Destroy user token to refresh rights
|
|
|
|
if (roleChanged) {
|
2018-07-31 07:04:26 -05:00
|
|
|
await OAuthTokenModel.deleteUserToken(userToUpdate.id)
|
2018-01-23 02:15:36 -06:00
|
|
|
}
|
|
|
|
|
2018-07-31 07:04:26 -05:00
|
|
|
auditLogger.update(
|
|
|
|
res.locals.oauth.token.User.Account.Actor.getIdentifier(),
|
|
|
|
new UserAuditView(user.toFormattedJSON()),
|
|
|
|
oldUserAuditView
|
|
|
|
)
|
|
|
|
|
2018-01-03 09:38:50 -06:00
|
|
|
// Don't need to send this update to followers, these attributes are not propagated
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
return res.sendStatus(204)
|
2017-09-05 14:29:39 -05:00
|
|
|
}
|
|
|
|
|
2018-01-30 06:27:07 -06:00
|
|
|
async function askResetUserPassword (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const user = res.locals.user as UserModel
|
|
|
|
|
|
|
|
const verificationString = await Redis.Instance.setResetPasswordVerificationString(user.id)
|
|
|
|
const url = CONFIG.WEBSERVER.URL + '/reset-password?userId=' + user.id + '&verificationString=' + verificationString
|
|
|
|
await Emailer.Instance.addForgetPasswordEmailJob(user.email, url)
|
|
|
|
|
|
|
|
return res.status(204).end()
|
|
|
|
}
|
|
|
|
|
|
|
|
async function resetUserPassword (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const user = res.locals.user as UserModel
|
|
|
|
user.password = req.body.password
|
|
|
|
|
|
|
|
await user.save()
|
|
|
|
|
|
|
|
return res.status(204).end()
|
|
|
|
}
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
function success (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2016-03-21 05:56:33 -05:00
|
|
|
res.end()
|
|
|
|
}
|
2018-08-08 07:58:21 -05:00
|
|
|
|
2018-08-08 10:36:10 -05:00
|
|
|
async function changeUserBlock (res: express.Response, user: UserModel, block: boolean, reason?: string) {
|
2018-08-08 07:58:21 -05:00
|
|
|
const oldUserAuditView = new UserAuditView(user.toFormattedJSON())
|
|
|
|
|
|
|
|
user.blocked = block
|
2018-08-08 10:36:10 -05:00
|
|
|
user.blockedReason = reason || null
|
2018-08-08 07:58:21 -05:00
|
|
|
|
|
|
|
await sequelizeTypescript.transaction(async t => {
|
|
|
|
await OAuthTokenModel.deleteUserToken(user.id, t)
|
|
|
|
|
|
|
|
await user.save({ transaction: t })
|
|
|
|
})
|
|
|
|
|
2018-08-08 10:36:10 -05:00
|
|
|
await Emailer.Instance.addUserBlockJob(user, block, reason)
|
|
|
|
|
2018-08-08 07:58:21 -05:00
|
|
|
auditLogger.update(
|
|
|
|
res.locals.oauth.token.User.Account.Actor.getIdentifier(),
|
|
|
|
new UserAuditView(user.toFormattedJSON()),
|
|
|
|
oldUserAuditView
|
|
|
|
)
|
|
|
|
}
|