X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fvideos%2Fchannel.ts;h=2012575c8676fe0f3d76ba7d55c6082de06c3047;hb=1174a8479ab9ee47b3305d668fe757435057a298;hp=d99f47c32d8a27cdbe16688614053648dae5c1df;hpb=a2431b7dcbc72c05101dcdbe631ff84a823aeb51;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/videos/channel.ts b/server/controllers/api/videos/channel.ts index d99f47c32..2012575c8 100644 --- a/server/controllers/api/videos/channel.ts +++ b/server/controllers/api/videos/channel.ts @@ -1,30 +1,25 @@ import * as express from 'express' import { VideoChannelCreate, VideoChannelUpdate } from '../../../../shared' -import { getFormattedObjects, logger, resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers' -import { database as db } from '../../../initializers' -import { createVideoChannel } from '../../../lib' -import { sendUpdateVideoChannel } from '../../../lib/activitypub/send/send-update' +import { retryTransactionWrapper } from '../../../helpers/database-utils' +import { logger } from '../../../helpers/logger' +import { getFormattedObjects, resetSequelizeInstance } from '../../../helpers/utils' +import { sequelizeTypescript } from '../../../initializers' +import { setAsyncActorKeys } from '../../../lib/activitypub' +import { createVideoChannel } from '../../../lib/video-channel' import { - asyncMiddleware, - authenticate, - listVideoAccountChannelsValidator, - paginationValidator, - setPagination, - setVideoChannelsSort, - videoChannelsAddValidator, - videoChannelsGetValidator, - videoChannelsRemoveValidator, - videoChannelsSortValidator, + asyncMiddleware, authenticate, listVideoAccountChannelsValidator, paginationValidator, setDefaultSort, setPagination, + videoChannelsAddValidator, videoChannelsGetValidator, videoChannelsRemoveValidator, videoChannelsSortValidator, videoChannelsUpdateValidator } from '../../../middlewares' -import { AccountInstance, VideoChannelInstance } from '../../../models' +import { AccountModel } from '../../../models/account/account' +import { VideoChannelModel } from '../../../models/video/video-channel' const videoChannelRouter = express.Router() videoChannelRouter.get('/channels', paginationValidator, videoChannelsSortValidator, - setVideoChannelsSort, + setDefaultSort, setPagination, asyncMiddleware(listVideoChannels) ) @@ -66,13 +61,13 @@ export { // --------------------------------------------------------------------------- async function listVideoChannels (req: express.Request, res: express.Response, next: express.NextFunction) { - const resultList = await db.VideoChannel.listForApi(req.query.start, req.query.count, req.query.sort) + const resultList = await VideoChannelModel.listForApi(req.query.start, req.query.count, req.query.sort) return res.json(getFormattedObjects(resultList.data, resultList.total)) } async function listVideoAccountChannels (req: express.Request, res: express.Response, next: express.NextFunction) { - const resultList = await db.VideoChannel.listByAccount(res.locals.account.id) + const resultList = await VideoChannelModel.listByAccount(res.locals.account.id) return res.json(getFormattedObjects(resultList.data, resultList.total)) } @@ -93,14 +88,15 @@ async function addVideoChannelRetryWrapper (req: express.Request, res: express.R async function addVideoChannel (req: express.Request, res: express.Response) { const videoChannelInfo: VideoChannelCreate = req.body - const account: AccountInstance = res.locals.oauth.token.User.Account - let videoChannelCreated: VideoChannelInstance + const account: AccountModel = res.locals.oauth.token.User.Account - await db.sequelize.transaction(async t => { - videoChannelCreated = await createVideoChannel(videoChannelInfo, account, t) + const videoChannelCreated = await sequelizeTypescript.transaction(async t => { + return createVideoChannel(videoChannelInfo, account, t) }) - logger.info('Video channel with uuid %s created.', videoChannelCreated.uuid) + setAsyncActorKeys(videoChannelCreated.Actor) + + logger.info('Video channel with uuid %s created.', videoChannelCreated.Actor.uuid) } async function updateVideoChannelRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { @@ -115,12 +111,12 @@ async function updateVideoChannelRetryWrapper (req: express.Request, res: expres } async function updateVideoChannel (req: express.Request, res: express.Response) { - const videoChannelInstance: VideoChannelInstance = res.locals.videoChannel + const videoChannelInstance = res.locals.videoChannel as VideoChannelModel const videoChannelFieldsSave = videoChannelInstance.toJSON() - const videoChannelInfoToUpdate: VideoChannelUpdate = req.body + const videoChannelInfoToUpdate = req.body as VideoChannelUpdate try { - await db.sequelize.transaction(async t => { + await sequelizeTypescript.transaction(async t => { const sequelizeOptions = { transaction: t } @@ -128,12 +124,13 @@ async function updateVideoChannel (req: express.Request, res: express.Response) if (videoChannelInfoToUpdate.name !== undefined) videoChannelInstance.set('name', videoChannelInfoToUpdate.name) if (videoChannelInfoToUpdate.description !== undefined) videoChannelInstance.set('description', videoChannelInfoToUpdate.description) - const videoChannelInstanceUpdated = await videoChannelInstance.save(sequelizeOptions) + await videoChannelInstance.save(sequelizeOptions) - await sendUpdateVideoChannel(videoChannelInstanceUpdated, t) + // TODO + // await sendUpdateVideoChannel(videoChannelInstanceUpdated, t) }) - logger.info('Video channel with name %s and uuid %s updated.', videoChannelInstance.name, videoChannelInstance.uuid) + logger.info('Video channel with name %s and uuid %s updated.', videoChannelInstance.name, videoChannelInstance.Actor.uuid) } catch (err) { logger.debug('Cannot update the video channel.', err) @@ -158,17 +155,18 @@ async function removeVideoChannelRetryWrapper (req: express.Request, res: expres } async function removeVideoChannel (req: express.Request, res: express.Response) { - const videoChannelInstance: VideoChannelInstance = res.locals.videoChannel + const videoChannelInstance: VideoChannelModel = res.locals.videoChannel - await db.sequelize.transaction(async t => { + return sequelizeTypescript.transaction(async t => { await videoChannelInstance.destroy({ transaction: t }) + + logger.info('Video channel with name %s and uuid %s deleted.', videoChannelInstance.name, videoChannelInstance.Actor.uuid) }) - logger.info('Video channel with name %s and uuid %s deleted.', videoChannelInstance.name, videoChannelInstance.uuid) } async function getVideoChannel (req: express.Request, res: express.Response, next: express.NextFunction) { - const videoChannelWithVideos = await db.VideoChannel.loadAndPopulateAccountAndVideos(res.locals.videoChannel.id) + const videoChannelWithVideos = await VideoChannelModel.loadAndPopulateAccountAndVideos(res.locals.videoChannel.id) return res.json(videoChannelWithVideos.toFormattedJSON()) }