2017-06-05 14:53:49 -05:00
|
|
|
import * as express from 'express'
|
2017-12-29 12:10:13 -06:00
|
|
|
import { extname, join } from 'path'
|
2018-01-03 04:36:03 -06:00
|
|
|
import * as sharp from 'sharp'
|
2017-12-29 12:10:13 -06:00
|
|
|
import * as uuidv4 from 'uuid/v4'
|
2017-11-10 10:27:49 -06:00
|
|
|
import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared'
|
2018-01-03 04:54:42 -06:00
|
|
|
import { unlinkPromise } from '../../helpers/core-utils'
|
2017-12-28 04:16:08 -06:00
|
|
|
import { retryTransactionWrapper } from '../../helpers/database-utils'
|
|
|
|
import { logger } from '../../helpers/logger'
|
2017-12-29 12:10:13 -06:00
|
|
|
import { createReqFiles, getFormattedObjects } from '../../helpers/utils'
|
2018-01-03 04:36:03 -06:00
|
|
|
import { AVATAR_MIMETYPE_EXT, AVATARS_SIZE, CONFIG, sequelizeTypescript } from '../../initializers'
|
2018-01-03 09:38:50 -06:00
|
|
|
import { sendUpdateUser } from '../../lib/activitypub/send'
|
2017-12-14 10:38:41 -06:00
|
|
|
import { createUserAccountAndChannel } from '../../lib/user'
|
2017-05-15 15:22:03 -05:00
|
|
|
import {
|
2017-12-28 04:16:08 -06:00
|
|
|
asyncMiddleware, authenticate, ensureUserHasRight, ensureUserRegistrationAllowed, paginationValidator, setPagination, setUsersSort,
|
|
|
|
setVideosSort, token, usersAddValidator, usersGetValidator, usersRegisterValidator, usersRemoveValidator, usersSortValidator,
|
|
|
|
usersUpdateMeValidator, usersUpdateValidator, usersVideoRatingValidator
|
2017-05-15 15:22:03 -05:00
|
|
|
} from '../../middlewares'
|
2017-12-29 12:10:13 -06:00
|
|
|
import { usersUpdateMyAvatarValidator, 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'
|
2017-12-29 12:10:13 -06:00
|
|
|
import { AvatarModel } from '../../models/avatar/avatar'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { VideoModel } from '../../models/video/video'
|
2017-05-15 15:22:03 -05:00
|
|
|
|
2017-12-29 12:10:13 -06:00
|
|
|
const reqAvatarFile = createReqFiles('avatarfile', CONFIG.STORAGE.AVATARS_DIR, AVATAR_MIMETYPE_EXT)
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2017-10-31 05:52:52 -05:00
|
|
|
usersRouter.get('/me/videos',
|
|
|
|
authenticate,
|
|
|
|
paginationValidator,
|
|
|
|
videosSortValidator,
|
|
|
|
setVideosSort,
|
|
|
|
setPagination,
|
|
|
|
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,
|
|
|
|
setUsersSort,
|
|
|
|
setPagination,
|
2017-10-25 04:55:06 -05:00
|
|
|
asyncMiddleware(listUsers)
|
2016-08-16 15:31:45 -05:00
|
|
|
)
|
|
|
|
|
2017-09-05 14:29:39 -05:00
|
|
|
usersRouter.get('/:id',
|
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),
|
|
|
|
asyncMiddleware(createUserRetryWrapper)
|
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),
|
|
|
|
asyncMiddleware(usersRegisterValidator),
|
2017-11-16 11:40:50 -06:00
|
|
|
asyncMiddleware(registerUserRetryWrapper)
|
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,
|
|
|
|
usersUpdateMyAvatarValidator,
|
|
|
|
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
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
usersRouter.post('/token', 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
|
|
|
|
const resultList = await VideoModel.listUserVideosForApi(user.id ,req.query.start, req.query.count, req.query.sort)
|
2017-10-31 05:52:52 -05:00
|
|
|
|
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function createUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-10-24 12:41:09 -05:00
|
|
|
const options = {
|
2017-11-16 11:40:50 -06:00
|
|
|
arguments: [ req ],
|
2017-10-24 12:41:09 -05:00
|
|
|
errorMessage: 'Cannot insert the user with many retries.'
|
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await retryTransactionWrapper(createUser, options)
|
|
|
|
|
|
|
|
// TODO : include Location of the new user -> 201
|
|
|
|
return res.type('json').status(204).end()
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
2017-11-16 11:40:50 -06:00
|
|
|
async function createUser (req: express.Request) {
|
2017-07-10 12:43:21 -05:00
|
|
|
const body: UserCreate = req.body
|
2017-12-12 10:53:50 -06:00
|
|
|
const user = new UserModel({
|
2017-07-10 12:43:21 -05:00
|
|
|
username: body.username,
|
|
|
|
password: body.password,
|
|
|
|
email: body.email,
|
2017-04-03 14:24:36 -05:00
|
|
|
displayNSFW: false,
|
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
|
|
|
})
|
|
|
|
|
2017-11-10 07:48:08 -06:00
|
|
|
await createUserAccountAndChannel(user)
|
2017-10-25 04:55:06 -05:00
|
|
|
|
2017-11-10 07:48:08 -06:00
|
|
|
logger.info('User %s with its channel and account created.', body.username)
|
2016-08-04 15:32:36 -05:00
|
|
|
}
|
|
|
|
|
2017-11-16 11:40:50 -06:00
|
|
|
async function registerUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const options = {
|
|
|
|
arguments: [ req ],
|
|
|
|
errorMessage: 'Cannot insert the user with many retries.'
|
|
|
|
}
|
|
|
|
|
|
|
|
await retryTransactionWrapper(registerUser, options)
|
|
|
|
|
|
|
|
return res.type('json').status(204).end()
|
|
|
|
}
|
|
|
|
|
|
|
|
async function registerUser (req: express.Request) {
|
2017-09-06 09:35:40 -05:00
|
|
|
const body: UserCreate = req.body
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
const user = new UserModel({
|
2017-09-06 09:35:40 -05:00
|
|
|
username: body.username,
|
|
|
|
password: body.password,
|
|
|
|
email: body.email,
|
|
|
|
displayNSFW: false,
|
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
|
|
|
|
})
|
|
|
|
|
2017-11-10 07:48:08 -06:00
|
|
|
await createUserAccountAndChannel(user)
|
2017-11-16 11:40:50 -06:00
|
|
|
|
|
|
|
logger.info('User %s with its channel and account registered.', body.username)
|
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
|
|
|
}
|
|
|
|
|
2017-09-05 14:29:39 -05:00
|
|
|
function getUser (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-10-31 09:20:35 -05:00
|
|
|
return res.json(res.locals.user.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
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-12-12 10:53:50 -06:00
|
|
|
const user = await UserModel.loadById(req.params.id)
|
2017-10-25 04:55:06 -05:00
|
|
|
|
|
|
|
await user.destroy()
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
const user = res.locals.oauth.token.user
|
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
|
|
|
|
if (body.displayNSFW !== undefined) user.displayNSFW = body.displayNSFW
|
2017-12-19 03:45:49 -06:00
|
|
|
if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo
|
2017-10-25 04:55:06 -05:00
|
|
|
|
|
|
|
await user.save()
|
2018-01-03 09:38:50 -06:00
|
|
|
await sendUpdateUser(user, undefined)
|
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) {
|
|
|
|
const avatarPhysicalFile = req.files['avatarfile'][0]
|
2018-01-03 09:38:50 -06:00
|
|
|
const user = res.locals.oauth.token.user
|
|
|
|
const actor = user.Account.Actor
|
2017-12-29 12:10:13 -06:00
|
|
|
|
|
|
|
const avatarDir = CONFIG.STORAGE.AVATARS_DIR
|
|
|
|
const source = join(avatarDir, avatarPhysicalFile.filename)
|
|
|
|
const extension = extname(avatarPhysicalFile.filename)
|
|
|
|
const avatarName = uuidv4() + extension
|
|
|
|
const destination = join(avatarDir, avatarName)
|
|
|
|
|
2018-01-03 04:36:03 -06:00
|
|
|
await sharp(source)
|
|
|
|
.resize(AVATARS_SIZE.width, AVATARS_SIZE.height)
|
|
|
|
.toFile(destination)
|
|
|
|
|
|
|
|
await unlinkPromise(source)
|
2017-12-29 12:10:13 -06:00
|
|
|
|
|
|
|
const { avatar } = await sequelizeTypescript.transaction(async t => {
|
|
|
|
const avatar = await AvatarModel.create({
|
|
|
|
filename: avatarName
|
|
|
|
}, { transaction: t })
|
|
|
|
|
|
|
|
if (actor.Avatar) {
|
2018-01-03 09:38:50 -06:00
|
|
|
try {
|
|
|
|
await actor.Avatar.destroy({ transaction: t })
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot remove old avatar of user %s.', user.username, err)
|
|
|
|
}
|
2017-12-29 12:10:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
actor.set('avatarId', avatar.id)
|
2018-01-03 09:38:50 -06:00
|
|
|
actor.Avatar = avatar
|
2017-12-29 12:10:13 -06:00
|
|
|
await actor.save({ transaction: t })
|
|
|
|
|
2018-01-03 09:38:50 -06:00
|
|
|
await sendUpdateUser(user, undefined)
|
|
|
|
|
2017-12-29 12:10:13 -06:00
|
|
|
return { actor, avatar }
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
2017-12-12 10:53:50 -06:00
|
|
|
const user = res.locals.user as UserModel
|
2017-09-05 14:29:39 -05:00
|
|
|
|
|
|
|
if (body.email !== undefined) user.email = body.email
|
|
|
|
if (body.videoQuota !== undefined) user.videoQuota = body.videoQuota
|
2017-10-27 09:55:03 -05:00
|
|
|
if (body.role !== undefined) user.role = body.role
|
2017-09-05 14:29:39 -05:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await user.save()
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|