From 453e83ea5d81d203ba34bc43cd5c2c750ba40568 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 15 Aug 2019 11:53:26 +0200 Subject: Stronger model typings --- server/controllers/activitypub/client.ts | 37 +++++++++++++++--------------- server/controllers/activitypub/inbox.ts | 5 ++-- server/controllers/activitypub/outbox.ts | 10 ++------ server/controllers/api/search.ts | 5 ++-- server/controllers/api/users/index.ts | 3 ++- server/controllers/api/users/me.ts | 2 +- server/controllers/api/users/my-history.ts | 1 - server/controllers/api/video-channel.ts | 2 +- server/controllers/api/video-playlist.ts | 36 ++++++++++++++--------------- server/controllers/api/videos/abuse.ts | 7 +++--- server/controllers/api/videos/blacklist.ts | 17 +++++++------- server/controllers/api/videos/captions.ts | 9 ++++---- server/controllers/api/videos/comment.ts | 17 ++++++-------- server/controllers/api/videos/import.ts | 34 ++++++++++++++------------- server/controllers/api/videos/index.ts | 15 ++++++------ server/controllers/api/videos/ownership.ts | 5 ++-- server/controllers/api/videos/rate.ts | 2 +- server/controllers/api/videos/watching.ts | 2 +- server/controllers/feeds.ts | 2 +- server/controllers/services.ts | 2 +- server/controllers/static.ts | 6 ++--- server/controllers/webfinger.ts | 2 +- 22 files changed, 111 insertions(+), 110 deletions(-) (limited to 'server/controllers') diff --git a/server/controllers/activitypub/client.ts b/server/controllers/activitypub/client.ts index 11504b354..98f5c8865 100644 --- a/server/controllers/activitypub/client.ts +++ b/server/controllers/activitypub/client.ts @@ -16,7 +16,6 @@ import { } from '../../middlewares' import { getAccountVideoRateValidator, videoCommentGetValidator } from '../../middlewares/validators' import { AccountModel } from '../../models/account/account' -import { ActorModel } from '../../models/activitypub/actor' import { ActorFollowModel } from '../../models/activitypub/actor-follow' import { VideoModel } from '../../models/video/video' import { VideoCommentModel } from '../../models/video/video-comment' @@ -38,6 +37,7 @@ import { buildDislikeActivity } from '../../lib/activitypub/send/send-dislike' import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '../../middlewares/validators/videos/video-playlists' import { VideoPlaylistModel } from '../../models/video/video-playlist' import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' +import { MAccountId, MActorId, MVideo, MVideoAPWithoutCaption } from '@server/typings/models' const activityPubClientRouter = express.Router() @@ -148,7 +148,7 @@ activityPubClientRouter.get('/redundancy/streaming-playlists/:streamingPlaylistT activityPubClientRouter.get('/video-playlists/:playlistId', executeIfActivityPub, - asyncMiddleware(videoPlaylistsGetValidator), + asyncMiddleware(videoPlaylistsGetValidator('all')), asyncMiddleware(videoPlaylistController) ) activityPubClientRouter.get('/video-playlists/:playlistId/:videoId', @@ -208,18 +208,19 @@ function getAccountVideoRate (rateType: VideoRateType) { async function videoController (req: express.Request, res: express.Response) { // We need more attributes - const video = await VideoModel.loadForGetAPI({ id: res.locals.video.id }) + const video = await VideoModel.loadForGetAPI({ id: res.locals.onlyVideoWithRights.id }) as MVideoAPWithoutCaption if (video.url.startsWith(WEBSERVER.URL) === false) return res.redirect(video.url) // We need captions to render AP object - video.VideoCaptions = await VideoCaptionModel.listVideoCaptions(video.id) + const captions = await VideoCaptionModel.listVideoCaptions(video.id) + const videoWithCaptions: MVideoAPWithoutCaption = Object.assign(video, { VideoCaptions: captions }) - const audience = getAudience(video.VideoChannel.Account.Actor, video.privacy === VideoPrivacy.PUBLIC) - const videoObject = audiencify(video.toActivityPubObject(), audience) + const audience = getAudience(videoWithCaptions.VideoChannel.Account.Actor, videoWithCaptions.privacy === VideoPrivacy.PUBLIC) + const videoObject = audiencify(videoWithCaptions.toActivityPubObject(), audience) if (req.path.endsWith('/activity')) { - const data = buildCreateActivity(video.url, video.VideoChannel.Account.Actor, videoObject, audience) + const data = buildCreateActivity(videoWithCaptions.url, video.VideoChannel.Account.Actor, videoObject, audience) return activityPubResponse(activityPubContextify(data), res) } @@ -231,13 +232,13 @@ async function videoAnnounceController (req: express.Request, res: express.Respo if (share.url.startsWith(WEBSERVER.URL) === false) return res.redirect(share.url) - const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.video, undefined) + const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.videoAll, undefined) return activityPubResponse(activityPubContextify(activity), res) } async function videoAnnouncesController (req: express.Request, res: express.Response) { - const video = res.locals.video + const video = res.locals.onlyVideo const handler = async (start: number, count: number) => { const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count) @@ -252,21 +253,21 @@ async function videoAnnouncesController (req: express.Request, res: express.Resp } async function videoLikesController (req: express.Request, res: express.Response) { - const video = res.locals.video + const video = res.locals.onlyVideo const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video)) return activityPubResponse(activityPubContextify(json), res) } async function videoDislikesController (req: express.Request, res: express.Response) { - const video = res.locals.video + const video = res.locals.onlyVideo const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video)) return activityPubResponse(activityPubContextify(json), res) } async function videoCommentsController (req: express.Request, res: express.Response) { - const video = res.locals.video + const video = res.locals.onlyVideo const handler = async (start: number, count: number) => { const result = await VideoCommentModel.listAndCountByVideoId(video.id, start, count) @@ -301,7 +302,7 @@ async function videoChannelFollowingController (req: express.Request, res: expre } async function videoCommentController (req: express.Request, res: express.Response) { - const videoComment = res.locals.videoComment + const videoComment = res.locals.videoCommentFull if (videoComment.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoComment.url) @@ -337,7 +338,7 @@ async function videoRedundancyController (req: express.Request, res: express.Res } async function videoPlaylistController (req: express.Request, res: express.Response) { - const playlist = res.locals.videoPlaylist + const playlist = res.locals.videoPlaylistFull // We need more attributes playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId) @@ -358,7 +359,7 @@ async function videoPlaylistElementController (req: express.Request, res: expres // --------------------------------------------------------------------------- -async function actorFollowing (req: express.Request, actor: ActorModel) { +async function actorFollowing (req: express.Request, actor: MActorId) { const handler = (start: number, count: number) => { return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count) } @@ -366,7 +367,7 @@ async function actorFollowing (req: express.Request, actor: ActorModel) { return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page) } -async function actorFollowers (req: express.Request, actor: ActorModel) { +async function actorFollowers (req: express.Request, actor: MActorId) { const handler = (start: number, count: number) => { return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count) } @@ -374,7 +375,7 @@ async function actorFollowers (req: express.Request, actor: ActorModel) { return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page) } -async function actorPlaylists (req: express.Request, account: AccountModel) { +async function actorPlaylists (req: express.Request, account: MAccountId) { const handler = (start: number, count: number) => { return VideoPlaylistModel.listPublicUrlsOfForAP(account.id, start, count) } @@ -382,7 +383,7 @@ async function actorPlaylists (req: express.Request, account: AccountModel) { return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page) } -function videoRates (req: express.Request, rateType: VideoRateType, video: VideoModel, url: string) { +function videoRates (req: express.Request, rateType: VideoRateType, video: MVideo, url: string) { const handler = async (start: number, count: number) => { const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count) return { diff --git a/server/controllers/activitypub/inbox.ts b/server/controllers/activitypub/inbox.ts index 2d3eef222..d9df253aa 100644 --- a/server/controllers/activitypub/inbox.ts +++ b/server/controllers/activitypub/inbox.ts @@ -7,7 +7,7 @@ import { asyncMiddleware, checkSignature, localAccountValidator, localVideoChann import { activityPubValidator } from '../../middlewares/validators/activitypub/activity' import { queue } from 'async' import { ActorModel } from '../../models/activitypub/actor' -import { SignatureActorModel } from '../../typings/models' +import { MActorDefault, MActorSignature } from '../../typings/models' const inboxRouter = express.Router() @@ -41,7 +41,8 @@ export { // --------------------------------------------------------------------------- -const inboxQueue = queue<{ activities: Activity[], signatureActor?: SignatureActorModel, inboxActor?: ActorModel }, Error>((task, cb) => { +type QueueParam = { activities: Activity[], signatureActor?: MActorSignature, inboxActor?: MActorDefault } +const inboxQueue = queue((task, cb) => { const options = { signatureActor: task.signatureActor, inboxActor: task.inboxActor } processActivities(task.activities, options) diff --git a/server/controllers/activitypub/outbox.ts b/server/controllers/activitypub/outbox.ts index 38b6ec976..f3dd2ad7d 100644 --- a/server/controllers/activitypub/outbox.ts +++ b/server/controllers/activitypub/outbox.ts @@ -6,11 +6,9 @@ import { logger } from '../../helpers/logger' import { buildAnnounceActivity, buildCreateActivity } from '../../lib/activitypub/send' import { buildAudience } from '../../lib/activitypub/audience' import { asyncMiddleware, localAccountValidator, localVideoChannelValidator } from '../../middlewares' -import { AccountModel } from '../../models/account/account' -import { ActorModel } from '../../models/activitypub/actor' import { VideoModel } from '../../models/video/video' import { activityPubResponse } from './utils' -import { VideoChannelModel } from '../../models/video/video-channel' +import { MActorLight } from '@server/typings/models' const outboxRouter = express.Router() @@ -45,14 +43,10 @@ async function outboxController (req: express.Request, res: express.Response) { return activityPubResponse(activityPubContextify(json), res) } -async function buildActivities (actor: ActorModel, start: number, count: number) { +async function buildActivities (actor: MActorLight, start: number, count: number) { const data = await VideoModel.listAllAndSharedByActorForOutbox(actor.id, start, count) const activities: Activity[] = [] - // Avoid too many SQL requests - const actors = data.data.map(v => v.VideoChannel.Account.Actor) - actors.push(actor) - for (const video of data.data) { const byActor = video.VideoChannel.Account.Actor const createActivityAudience = buildAudience([ byActor.followersUrl ], video.privacy === VideoPrivacy.PUBLIC) diff --git a/server/controllers/api/search.ts b/server/controllers/api/search.ts index 9a1e30b83..7fef7c173 100644 --- a/server/controllers/api/search.ts +++ b/server/controllers/api/search.ts @@ -19,6 +19,7 @@ import { getOrCreateActorAndServerAndModel, getOrCreateVideoAndAccountAndChannel import { logger } from '../../helpers/logger' import { VideoChannelModel } from '../../models/video/video-channel' import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger' +import { MChannelAccountDefault, MVideoAccountAllFiles } from '../../typings/models' const searchRouter = express.Router() @@ -84,7 +85,7 @@ async function searchVideoChannelsDB (query: VideoChannelsSearchQuery, res: expr } async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean, res: express.Response) { - let videoChannel: VideoChannelModel + let videoChannel: MChannelAccountDefault let uri = search if (isWebfingerSearch) { @@ -137,7 +138,7 @@ async function searchVideosDB (query: VideosSearchQuery, res: express.Response) } async function searchVideoURI (url: string, res: express.Response) { - let video: VideoModel + let video: MVideoAccountAllFiles // Check if we can fetch a remote video with the URL if (isUserAbleToSearchRemoteURI(res)) { diff --git a/server/controllers/api/users/index.ts b/server/controllers/api/users/index.ts index ae40e86f8..e6b678f3a 100644 --- a/server/controllers/api/users/index.ts +++ b/server/controllers/api/users/index.ts @@ -48,6 +48,7 @@ import { CONFIG } from '../../../initializers/config' import { sequelizeTypescript } from '../../../initializers/database' import { UserAdminFlag } from '../../../../shared/models/users/user-flag.model' import { UserRegister } from '../../../../shared/models/users/user-register.model' +import { MUser, MUserAccountDefault } from '@server/typings/models' const auditLogger = auditLoggerFactory('users') @@ -359,7 +360,7 @@ function success (req: express.Request, res: express.Response) { res.end() } -async function changeUserBlock (res: express.Response, user: UserModel, block: boolean, reason?: string) { +async function changeUserBlock (res: express.Response, user: MUserAccountDefault, block: boolean, reason?: string) { const oldUserAuditView = new UserAuditView(user.toFormattedJSON()) user.blocked = block diff --git a/server/controllers/api/users/me.ts b/server/controllers/api/users/me.ts index e7ed3de64..af054f620 100644 --- a/server/controllers/api/users/me.ts +++ b/server/controllers/api/users/me.ts @@ -147,7 +147,7 @@ async function getUserVideoQuotaUsed (req: express.Request, res: express.Respons } async function getUserVideoRating (req: express.Request, res: express.Response) { - const videoId = res.locals.video.id + const videoId = res.locals.videoId.id const accountId = +res.locals.oauth.token.User.Account.id const ratingObj = await AccountVideoRateModel.load(accountId, videoId, null) diff --git a/server/controllers/api/users/my-history.ts b/server/controllers/api/users/my-history.ts index 7025c0ff1..4da1f3496 100644 --- a/server/controllers/api/users/my-history.ts +++ b/server/controllers/api/users/my-history.ts @@ -7,7 +7,6 @@ import { setDefaultPagination, userHistoryRemoveValidator } from '../../../middlewares' -import { UserModel } from '../../../models/account/user' import { getFormattedObjects } from '../../../helpers/utils' import { UserVideoHistoryModel } from '../../../models/account/user-video-history' import { sequelizeTypescript } from '../../../initializers' diff --git a/server/controllers/api/video-channel.ts b/server/controllers/api/video-channel.ts index 81a03a62b..2b6184a83 100644 --- a/server/controllers/api/video-channel.ts +++ b/server/controllers/api/video-channel.ts @@ -136,7 +136,7 @@ async function updateVideoChannelAvatar (req: express.Request, res: express.Resp async function addVideoChannel (req: express.Request, res: express.Response) { const videoChannelInfo: VideoChannelCreate = req.body - const videoChannelCreated: VideoChannelModel = await sequelizeTypescript.transaction(async t => { + const videoChannelCreated = await sequelizeTypescript.transaction(async t => { const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t) return createVideoChannel(videoChannelInfo, account, t) diff --git a/server/controllers/api/video-playlist.ts b/server/controllers/api/video-playlist.ts index bd454f553..d9f0ff925 100644 --- a/server/controllers/api/video-playlist.ts +++ b/server/controllers/api/video-playlist.ts @@ -40,7 +40,7 @@ import { JobQueue } from '../../lib/job-queue' import { CONFIG } from '../../initializers/config' import { sequelizeTypescript } from '../../initializers/database' import { createPlaylistMiniatureFromExisting } from '../../lib/thumbnail' -import { VideoModel } from '../../models/video/video' +import { MVideoPlaylistFull, MVideoPlaylistThumbnail, MVideoThumbnail } from '@server/typings/models' const reqThumbnailFile = createReqFiles([ 'thumbnailfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { thumbnailfile: CONFIG.STORAGE.TMP_DIR }) @@ -58,7 +58,7 @@ videoPlaylistRouter.get('/', ) videoPlaylistRouter.get('/:playlistId', - asyncMiddleware(videoPlaylistsGetValidator), + asyncMiddleware(videoPlaylistsGetValidator('summary')), getVideoPlaylist ) @@ -83,7 +83,7 @@ videoPlaylistRouter.delete('/:playlistId', ) videoPlaylistRouter.get('/:playlistId/videos', - asyncMiddleware(videoPlaylistsGetValidator), + asyncMiddleware(videoPlaylistsGetValidator('summary')), paginationValidator, setDefaultPagination, optionalAuthenticate, @@ -140,7 +140,7 @@ async function listVideoPlaylists (req: express.Request, res: express.Response) } function getVideoPlaylist (req: express.Request, res: express.Response) { - const videoPlaylist = res.locals.videoPlaylist + const videoPlaylist = res.locals.videoPlaylistSummary if (videoPlaylist.isOutdated()) { JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'video-playlist', url: videoPlaylist.url } }) @@ -159,7 +159,7 @@ async function addVideoPlaylist (req: express.Request, res: express.Response) { description: videoPlaylistInfo.description, privacy: videoPlaylistInfo.privacy || VideoPlaylistPrivacy.PRIVATE, ownerAccountId: user.Account.id - }) + }) as MVideoPlaylistFull videoPlaylist.url = getVideoPlaylistActivityPubUrl(videoPlaylist) // We use the UUID, so set the URL after building the object @@ -175,8 +175,8 @@ async function addVideoPlaylist (req: express.Request, res: express.Response) { ? await createPlaylistMiniatureFromExisting(thumbnailField[0].path, videoPlaylist, false) : undefined - const videoPlaylistCreated: VideoPlaylistModel = await sequelizeTypescript.transaction(async t => { - const videoPlaylistCreated = await videoPlaylist.save({ transaction: t }) + const videoPlaylistCreated = await sequelizeTypescript.transaction(async t => { + const videoPlaylistCreated = await videoPlaylist.save({ transaction: t }) as MVideoPlaylistFull if (thumbnailModel) { thumbnailModel.automaticallyGenerated = false @@ -201,7 +201,7 @@ async function addVideoPlaylist (req: express.Request, res: express.Response) { } async function updateVideoPlaylist (req: express.Request, res: express.Response) { - const videoPlaylistInstance = res.locals.videoPlaylist + const videoPlaylistInstance = res.locals.videoPlaylistFull const videoPlaylistFieldsSave = videoPlaylistInstance.toJSON() const videoPlaylistInfoToUpdate = req.body as VideoPlaylistUpdate @@ -275,7 +275,7 @@ async function updateVideoPlaylist (req: express.Request, res: express.Response) } async function removeVideoPlaylist (req: express.Request, res: express.Response) { - const videoPlaylistInstance = res.locals.videoPlaylist + const videoPlaylistInstance = res.locals.videoPlaylistSummary await sequelizeTypescript.transaction(async t => { await videoPlaylistInstance.destroy({ transaction: t }) @@ -290,10 +290,10 @@ async function removeVideoPlaylist (req: express.Request, res: express.Response) async function addVideoInPlaylist (req: express.Request, res: express.Response) { const body: VideoPlaylistElementCreate = req.body - const videoPlaylist = res.locals.videoPlaylist - const video = res.locals.video + const videoPlaylist = res.locals.videoPlaylistFull + const video = res.locals.onlyVideo - const playlistElement: VideoPlaylistElementModel = await sequelizeTypescript.transaction(async t => { + const playlistElement = await sequelizeTypescript.transaction(async t => { const position = await VideoPlaylistElementModel.getNextPositionOf(videoPlaylist.id, t) const playlistElement = await VideoPlaylistElementModel.create({ @@ -330,7 +330,7 @@ async function addVideoInPlaylist (req: express.Request, res: express.Response) async function updateVideoPlaylistElement (req: express.Request, res: express.Response) { const body: VideoPlaylistElementUpdate = req.body - const videoPlaylist = res.locals.videoPlaylist + const videoPlaylist = res.locals.videoPlaylistFull const videoPlaylistElement = res.locals.videoPlaylistElement const playlistElement: VideoPlaylistElementModel = await sequelizeTypescript.transaction(async t => { @@ -354,7 +354,7 @@ async function updateVideoPlaylistElement (req: express.Request, res: express.Re async function removeVideoFromPlaylist (req: express.Request, res: express.Response) { const videoPlaylistElement = res.locals.videoPlaylistElement - const videoPlaylist = res.locals.videoPlaylist + const videoPlaylist = res.locals.videoPlaylistFull const positionToDelete = videoPlaylistElement.position await sequelizeTypescript.transaction(async t => { @@ -381,7 +381,7 @@ async function removeVideoFromPlaylist (req: express.Request, res: express.Respo } async function reorderVideosPlaylist (req: express.Request, res: express.Response) { - const videoPlaylist = res.locals.videoPlaylist + const videoPlaylist = res.locals.videoPlaylistFull const body: VideoPlaylistReorder = req.body const start: number = body.startPosition @@ -434,7 +434,7 @@ async function reorderVideosPlaylist (req: express.Request, res: express.Respons } async function getVideoPlaylistVideos (req: express.Request, res: express.Response) { - const videoPlaylistInstance = res.locals.videoPlaylist + const videoPlaylistInstance = res.locals.videoPlaylistSummary const user = res.locals.oauth ? res.locals.oauth.token.User : undefined const server = await getServerActor() @@ -453,7 +453,7 @@ async function getVideoPlaylistVideos (req: express.Request, res: express.Respon return res.json(getFormattedObjects(resultList.data, resultList.total, options)) } -async function regeneratePlaylistThumbnail (videoPlaylist: VideoPlaylistModel) { +async function regeneratePlaylistThumbnail (videoPlaylist: MVideoPlaylistThumbnail) { await videoPlaylist.Thumbnail.destroy() videoPlaylist.Thumbnail = null @@ -461,7 +461,7 @@ async function regeneratePlaylistThumbnail (videoPlaylist: VideoPlaylistModel) { if (firstElement) await generateThumbnailForPlaylist(videoPlaylist, firstElement.Video) } -async function generateThumbnailForPlaylist (videoPlaylist: VideoPlaylistModel, video: VideoModel) { +async function generateThumbnailForPlaylist (videoPlaylist: MVideoPlaylistThumbnail, video: MVideoThumbnail) { logger.info('Generating default thumbnail to playlist %s.', videoPlaylist.url) const inputPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, video.getMiniature().filename) diff --git a/server/controllers/api/videos/abuse.ts b/server/controllers/api/videos/abuse.ts index 77808466c..39c841ffe 100644 --- a/server/controllers/api/videos/abuse.ts +++ b/server/controllers/api/videos/abuse.ts @@ -21,6 +21,7 @@ import { VideoAbuseModel } from '../../../models/video/video-abuse' import { auditLoggerFactory, VideoAbuseAuditView } from '../../../helpers/audit-logger' import { Notifier } from '../../../lib/notifier' import { sendVideoAbuse } from '../../../lib/activitypub/send/send-flag' +import { MVideoAbuseAccountVideo } from '../../../typings/models/video' const auditLogger = auditLoggerFactory('abuse') const abuseVideoRouter = express.Router() @@ -94,10 +95,10 @@ async function deleteVideoAbuse (req: express.Request, res: express.Response) { } async function reportVideoAbuse (req: express.Request, res: express.Response) { - const videoInstance = res.locals.video + const videoInstance = res.locals.videoAll const body: VideoAbuseCreate = req.body - const videoAbuse: VideoAbuseModel = await sequelizeTypescript.transaction(async t => { + const videoAbuse = await sequelizeTypescript.transaction(async t => { const reporterAccount = await AccountModel.load(res.locals.oauth.token.User.Account.id, t) const abuseToCreate = { @@ -107,7 +108,7 @@ async function reportVideoAbuse (req: express.Request, res: express.Response) { state: VideoAbuseState.PENDING } - const videoAbuseInstance = await VideoAbuseModel.create(abuseToCreate, { transaction: t }) + const videoAbuseInstance: MVideoAbuseAccountVideo = await VideoAbuseModel.create(abuseToCreate, { transaction: t }) videoAbuseInstance.Video = videoInstance videoAbuseInstance.Account = reporterAccount diff --git a/server/controllers/api/videos/blacklist.ts b/server/controllers/api/videos/blacklist.ts index 9ff494def..2a667480d 100644 --- a/server/controllers/api/videos/blacklist.ts +++ b/server/controllers/api/videos/blacklist.ts @@ -1,5 +1,5 @@ import * as express from 'express' -import { VideoBlacklist, UserRight, VideoBlacklistCreate, VideoBlacklistType } from '../../../../shared' +import { UserRight, VideoBlacklistCreate, VideoBlacklistType } from '../../../../shared' import { logger } from '../../../helpers/logger' import { getFormattedObjects } from '../../../helpers/utils' import { @@ -11,15 +11,16 @@ import { setBlacklistSort, setDefaultPagination, videosBlacklistAddValidator, + videosBlacklistFiltersValidator, videosBlacklistRemoveValidator, - videosBlacklistUpdateValidator, - videosBlacklistFiltersValidator + videosBlacklistUpdateValidator } from '../../../middlewares' import { VideoBlacklistModel } from '../../../models/video/video-blacklist' import { sequelizeTypescript } from '../../../initializers' import { Notifier } from '../../../lib/notifier' import { sendDeleteVideo } from '../../../lib/activitypub/send' import { federateVideoIfNeeded } from '../../../lib/activitypub' +import { MVideoBlacklistVideo } from '@server/typings/models' const blacklistRouter = express.Router() @@ -64,7 +65,7 @@ export { // --------------------------------------------------------------------------- async function addVideoToBlacklist (req: express.Request, res: express.Response) { - const videoInstance = res.locals.video + const videoInstance = res.locals.videoAll const body: VideoBlacklistCreate = req.body const toCreate = { @@ -74,7 +75,7 @@ async function addVideoToBlacklist (req: express.Request, res: express.Response) type: VideoBlacklistType.MANUAL } - const blacklist = await VideoBlacklistModel.create(toCreate) + const blacklist: MVideoBlacklistVideo = await VideoBlacklistModel.create(toCreate) blacklist.Video = videoInstance if (body.unfederate === true) { @@ -83,7 +84,7 @@ async function addVideoToBlacklist (req: express.Request, res: express.Response) Notifier.Instance.notifyOnVideoBlacklist(blacklist) - logger.info('Video %s blacklisted.', res.locals.video.uuid) + logger.info('Video %s blacklisted.', videoInstance.uuid) return res.type('json').status(204).end() } @@ -108,7 +109,7 @@ async function listBlacklist (req: express.Request, res: express.Response) { async function removeVideoFromBlacklistController (req: express.Request, res: express.Response) { const videoBlacklist = res.locals.videoBlacklist - const video = res.locals.video + const video = res.locals.videoAll const videoBlacklistType = await sequelizeTypescript.transaction(async t => { const unfederated = videoBlacklist.unfederated @@ -135,7 +136,7 @@ async function removeVideoFromBlacklistController (req: express.Request, res: ex Notifier.Instance.notifyOnNewVideoIfNeeded(video) } - logger.info('Video %s removed from blacklist.', res.locals.video.uuid) + logger.info('Video %s removed from blacklist.', video.uuid) return res.type('json').status(204).end() } diff --git a/server/controllers/api/videos/captions.ts b/server/controllers/api/videos/captions.ts index 44c255232..37481d12f 100644 --- a/server/controllers/api/videos/captions.ts +++ b/server/controllers/api/videos/captions.ts @@ -10,6 +10,7 @@ import { federateVideoIfNeeded } from '../../../lib/activitypub' import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils' import { CONFIG } from '../../../initializers/config' import { sequelizeTypescript } from '../../../initializers/database' +import { MVideoCaptionVideo } from '@server/typings/models' const reqVideoCaptionAdd = createReqFiles( [ 'captionfile' ], @@ -46,19 +47,19 @@ export { // --------------------------------------------------------------------------- async function listVideoCaptions (req: express.Request, res: express.Response) { - const data = await VideoCaptionModel.listVideoCaptions(res.locals.video.id) + const data = await VideoCaptionModel.listVideoCaptions(res.locals.videoId.id) return res.json(getFormattedObjects(data, data.length)) } async function addVideoCaption (req: express.Request, res: express.Response) { const videoCaptionPhysicalFile = req.files['captionfile'][0] - const video = res.locals.video + const video = res.locals.videoAll const videoCaption = new VideoCaptionModel({ videoId: video.id, language: req.params.captionLanguage - }) + }) as MVideoCaptionVideo videoCaption.Video = video // Move physical file @@ -75,7 +76,7 @@ async function addVideoCaption (req: express.Request, res: express.Response) { } async function deleteVideoCaption (req: express.Request, res: express.Response) { - const video = res.locals.video + const video = res.locals.videoAll const videoCaption = res.locals.videoCaption await sequelizeTypescript.transaction(async t => { diff --git a/server/controllers/api/videos/comment.ts b/server/controllers/api/videos/comment.ts index bc6d81a7c..b2b06b170 100644 --- a/server/controllers/api/videos/comment.ts +++ b/server/controllers/api/videos/comment.ts @@ -27,9 +27,6 @@ import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../. import { AccountModel } from '../../../models/account/account' import { Notifier } from '../../../lib/notifier' import { Hooks } from '../../../lib/plugins/hooks' -import { ActorModel } from '../../../models/activitypub/actor' -import { VideoChannelModel } from '../../../models/video/video-channel' -import { VideoModel } from '../../../models/video/video' import { sendDeleteVideoComment } from '../../../lib/activitypub/send' const auditLogger = auditLoggerFactory('comments') @@ -75,7 +72,7 @@ export { // --------------------------------------------------------------------------- async function listVideoThreads (req: express.Request, res: express.Response) { - const video = res.locals.video + const video = res.locals.onlyVideo const user = res.locals.oauth ? res.locals.oauth.token.User : undefined let resultList: ResultList @@ -86,7 +83,7 @@ async function listVideoThreads (req: express.Request, res: express.Response) { start: req.query.start, count: req.query.count, sort: req.query.sort, - user: user + user }, 'filter:api.video-threads.list.params') resultList = await Hooks.wrapPromiseFun( @@ -105,7 +102,7 @@ async function listVideoThreads (req: express.Request, res: express.Response) { } async function listVideoThreadComments (req: express.Request, res: express.Response) { - const video = res.locals.video + const video = res.locals.onlyVideo const user = res.locals.oauth ? res.locals.oauth.token.User : undefined let resultList: ResultList @@ -141,7 +138,7 @@ async function addVideoCommentThread (req: express.Request, res: express.Respons return createVideoComment({ text: videoCommentInfo.text, inReplyToComment: null, - video: res.locals.video, + video: res.locals.videoAll, account }, t) }) @@ -164,8 +161,8 @@ async function addVideoCommentReply (req: express.Request, res: express.Response return createVideoComment({ text: videoCommentInfo.text, - inReplyToComment: res.locals.videoComment, - video: res.locals.video, + inReplyToComment: res.locals.videoCommentFull, + video: res.locals.videoAll, account }, t) }) @@ -179,7 +176,7 @@ async function addVideoCommentReply (req: express.Request, res: express.Response } async function removeVideoComment (req: express.Request, res: express.Response) { - const videoCommentInstance = res.locals.videoComment + const videoCommentInstance = res.locals.videoCommentFull await sequelizeTypescript.transaction(async t => { await videoCommentInstance.destroy({ transaction: t }) diff --git a/server/controllers/api/videos/import.ts b/server/controllers/api/videos/import.ts index 04c9b547b..adc2f9aa2 100644 --- a/server/controllers/api/videos/import.ts +++ b/server/controllers/api/videos/import.ts @@ -15,7 +15,6 @@ import { VideoImportModel } from '../../../models/video/video-import' import { JobQueue } from '../../../lib/job-queue/job-queue' import { join } from 'path' import { isArray } from '../../../helpers/custom-validators/misc' -import { VideoChannelModel } from '../../../models/video/video-channel' import * as Bluebird from 'bluebird' import * as parseTorrent from 'parse-torrent' import { getSecureTorrentName } from '../../../helpers/utils' @@ -25,8 +24,14 @@ import { CONFIG } from '../../../initializers/config' import { sequelizeTypescript } from '../../../initializers/database' import { createVideoMiniatureFromExisting } from '../../../lib/thumbnail' import { ThumbnailType } from '../../../../shared/models/videos/thumbnail.type' -import { ThumbnailModel } from '../../../models/video/thumbnail' -import { UserModel } from '../../../models/account/user' +import { + MChannelActorAccountDefault, + MThumbnail, + MUser, + MVideoThumbnailAccountDefault, + MVideoWithBlacklistLight +} from '@server/typings/models' +import { MVideoImport, MVideoImportVideo } from '@server/typings/models/video/video-import' const auditLogger = auditLoggerFactory('video-imports') const videoImportsRouter = express.Router() @@ -225,28 +230,28 @@ async function processPreview (req: express.Request, video: VideoModel) { } function insertIntoDB (parameters: { - video: VideoModel, - thumbnailModel: ThumbnailModel, - previewModel: ThumbnailModel, - videoChannel: VideoChannelModel, + video: MVideoThumbnailAccountDefault, + thumbnailModel: MThumbnail, + previewModel: MThumbnail, + videoChannel: MChannelActorAccountDefault, tags: string[], - videoImportAttributes: Partial, - user: UserModel -}): Bluebird { + videoImportAttributes: Partial, + user: MUser +}): Bluebird { const { video, thumbnailModel, previewModel, videoChannel, tags, videoImportAttributes, user } = parameters return sequelizeTypescript.transaction(async t => { const sequelizeOptions = { transaction: t } // Save video object in database - const videoCreated = await video.save(sequelizeOptions) + const videoCreated = await video.save(sequelizeOptions) as (MVideoThumbnailAccountDefault & MVideoWithBlacklistLight) videoCreated.VideoChannel = videoChannel if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t) if (previewModel) await videoCreated.addAndSaveThumbnail(previewModel, t) await autoBlacklistVideoIfNeeded({ - video, + video: videoCreated, user, notify: false, isRemote: false, @@ -259,16 +264,13 @@ function insertIntoDB (parameters: { const tagInstances = await TagModel.findOrCreateTags(tags, t) await videoCreated.$set('Tags', tagInstances, sequelizeOptions) - videoCreated.Tags = tagInstances - } else { - videoCreated.Tags = [] } // Create video import object in database const videoImport = await VideoImportModel.create( Object.assign({ videoId: videoCreated.id }, videoImportAttributes), sequelizeOptions - ) + ) as MVideoImportVideo videoImport.Video = videoCreated return videoImport diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index 155ca4678..9af71d276 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts @@ -63,6 +63,7 @@ import { createVideoMiniatureFromExisting, generateVideoMiniature } from '../../ import { ThumbnailType } from '../../../../shared/models/videos/thumbnail.type' import { VideoTranscodingPayload } from '../../../lib/job-queue/handlers/video-transcoding' import { Hooks } from '../../../lib/plugins/hooks' +import { MVideoFullLight } from '@server/typings/models' const auditLogger = auditLoggerFactory('videos') const videosRouter = express.Router() @@ -238,7 +239,7 @@ async function addVideo (req: express.Request, res: express.Response) { const { videoCreated } = await sequelizeTypescript.transaction(async t => { const sequelizeOptions = { transaction: t } - const videoCreated = await video.save(sequelizeOptions) + const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight await videoCreated.addAndSaveThumbnail(thumbnailModel, t) await videoCreated.addAndSaveThumbnail(previewModel, t) @@ -318,7 +319,7 @@ async function addVideo (req: express.Request, res: express.Response) { } async function updateVideo (req: express.Request, res: express.Response) { - const videoInstance = res.locals.video + const videoInstance = res.locals.videoAll const videoFieldsSave = videoInstance.toJSON() const oldVideoAuditView = new VideoAuditView(videoInstance.toFormattedDetailsJSON()) const videoInfoToUpdate: VideoUpdate = req.body @@ -371,7 +372,7 @@ async function updateVideo (req: express.Request, res: express.Response) { } } - const videoInstanceUpdated = await videoInstance.save(sequelizeOptions) + const videoInstanceUpdated = await videoInstance.save(sequelizeOptions) as MVideoFullLight if (thumbnailModel) await videoInstanceUpdated.addAndSaveThumbnail(thumbnailModel, t) if (previewModel) await videoInstanceUpdated.addAndSaveThumbnail(previewModel, t) @@ -447,7 +448,7 @@ async function getVideo (req: express.Request, res: express.Response) { const video = await Hooks.wrapPromiseFun( VideoModel.loadForGetAPI, - { id: res.locals.video.id, userId }, + { id: res.locals.onlyVideoWithRights.id, userId }, 'filter:api.video.get.result' ) @@ -460,7 +461,7 @@ async function getVideo (req: express.Request, res: express.Response) { } async function viewVideo (req: express.Request, res: express.Response) { - const videoInstance = res.locals.video + const videoInstance = res.locals.videoAll const ip = req.ip const exists = await Redis.Instance.doesVideoIPViewExist(ip, videoInstance.uuid) @@ -483,7 +484,7 @@ async function viewVideo (req: express.Request, res: express.Response) { } async function getVideoDescription (req: express.Request, res: express.Response) { - const videoInstance = res.locals.video + const videoInstance = res.locals.videoAll let description = '' if (videoInstance.isOwned()) { @@ -522,7 +523,7 @@ async function listVideos (req: express.Request, res: express.Response) { } async function removeVideo (req: express.Request, res: express.Response) { - const videoInstance = res.locals.video + const videoInstance = res.locals.videoAll await sequelizeTypescript.transaction(async t => { await videoInstance.destroy({ transaction: t }) diff --git a/server/controllers/api/videos/ownership.ts b/server/controllers/api/videos/ownership.ts index 5272c1385..abb34082e 100644 --- a/server/controllers/api/videos/ownership.ts +++ b/server/controllers/api/videos/ownership.ts @@ -18,6 +18,7 @@ import { getFormattedObjects } from '../../../helpers/utils' import { changeVideoChannelShare } from '../../../lib/activitypub' import { sendUpdateVideo } from '../../../lib/activitypub/send' import { VideoModel } from '../../../models/video/video' +import { MVideoFullLight } from '@server/typings/models' const ownershipVideoRouter = express.Router() @@ -56,7 +57,7 @@ export { // --------------------------------------------------------------------------- async function giveVideoOwnership (req: express.Request, res: express.Response) { - const videoInstance = res.locals.video + const videoInstance = res.locals.videoAll const initiatorAccountId = res.locals.oauth.token.User.Account.id const nextOwner = res.locals.nextOwner @@ -107,7 +108,7 @@ async function acceptOwnership (req: express.Request, res: express.Response) { targetVideo.channelId = channel.id - const targetVideoUpdated = await targetVideo.save({ transaction: t }) + const targetVideoUpdated = await targetVideo.save({ transaction: t }) as MVideoFullLight targetVideoUpdated.VideoChannel = channel if (targetVideoUpdated.privacy !== VideoPrivacy.PRIVATE && targetVideoUpdated.state === VideoState.PUBLISHED) { diff --git a/server/controllers/api/videos/rate.ts b/server/controllers/api/videos/rate.ts index b65babedf..3d2f3d728 100644 --- a/server/controllers/api/videos/rate.ts +++ b/server/controllers/api/videos/rate.ts @@ -27,7 +27,7 @@ export { async function rateVideo (req: express.Request, res: express.Response) { const body: UserVideoRateUpdate = req.body const rateType = body.rating - const videoInstance = res.locals.video + const videoInstance = res.locals.videoAll const userAccount = res.locals.oauth.token.User.Account await sequelizeTypescript.transaction(async t => { diff --git a/server/controllers/api/videos/watching.ts b/server/controllers/api/videos/watching.ts index dcd1f070d..036e16f3a 100644 --- a/server/controllers/api/videos/watching.ts +++ b/server/controllers/api/videos/watching.ts @@ -23,7 +23,7 @@ async function userWatchVideo (req: express.Request, res: express.Response) { const user = res.locals.oauth.token.User const body: UserWatchingVideo = req.body - const { id: videoId } = res.locals.video as { id: number } + const { id: videoId } = res.locals.videoId await UserVideoHistoryModel.upsert({ videoId, diff --git a/server/controllers/feeds.ts b/server/controllers/feeds.ts index d3f581615..468f7a668 100644 --- a/server/controllers/feeds.ts +++ b/server/controllers/feeds.ts @@ -43,7 +43,7 @@ export { async function generateVideoCommentsFeed (req: express.Request, res: express.Response) { const start = 0 - const video = res.locals.video + const video = res.locals.videoAll const videoId: number = video ? video.id : undefined const comments = await VideoCommentModel.listForFeed(start, FEEDS.COUNT, videoId) diff --git a/server/controllers/services.ts b/server/controllers/services.ts index c1c53c3fc..ec057235f 100644 --- a/server/controllers/services.ts +++ b/server/controllers/services.ts @@ -23,7 +23,7 @@ export { // --------------------------------------------------------------------------- function generateOEmbed (req: express.Request, res: express.Response) { - const video = res.locals.video + const video = res.locals.videoAll const webserverUrl = WEBSERVER.URL const maxHeight = parseInt(req.query.maxheight, 10) const maxWidth = parseInt(req.query.maxwidth, 10) diff --git a/server/controllers/static.ts b/server/controllers/static.ts index 8979ef5f3..0f4772310 100644 --- a/server/controllers/static.ts +++ b/server/controllers/static.ts @@ -226,14 +226,14 @@ async function generateNodeinfo (req: express.Request, res: express.Response) { return res.send(json).end() } -async function downloadTorrent (req: express.Request, res: express.Response, next: express.NextFunction) { +async function downloadTorrent (req: express.Request, res: express.Response) { const { video, videoFile } = getVideoAndFile(req, res) if (!videoFile) return res.status(404).end() return res.download(video.getTorrentFilePath(videoFile), `${video.name}-${videoFile.resolution}p.torrent`) } -async function downloadVideoFile (req: express.Request, res: express.Response, next: express.NextFunction) { +async function downloadVideoFile (req: express.Request, res: express.Response) { const { video, videoFile } = getVideoAndFile(req, res) if (!videoFile) return res.status(404).end() @@ -242,7 +242,7 @@ async function downloadVideoFile (req: express.Request, res: express.Response, n function getVideoAndFile (req: express.Request, res: express.Response) { const resolution = parseInt(req.params.resolution, 10) - const video = res.locals.video + const video = res.locals.videoAll const videoFile = video.VideoFiles.find(f => f.resolution === resolution) diff --git a/server/controllers/webfinger.ts b/server/controllers/webfinger.ts index f2ba3c826..fc9575160 100644 --- a/server/controllers/webfinger.ts +++ b/server/controllers/webfinger.ts @@ -18,7 +18,7 @@ export { // --------------------------------------------------------------------------- function webfingerController (req: express.Request, res: express.Response) { - const actor = res.locals.actor + const actor = res.locals.actorFull const json = { subject: req.query.resource, -- cgit v1.2.3