aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/videos')
-rw-r--r--server/controllers/api/videos/abuse.ts23
-rw-r--r--server/controllers/api/videos/comment.ts24
-rw-r--r--server/controllers/api/videos/import.ts6
-rw-r--r--server/controllers/api/videos/index.ts12
-rw-r--r--server/controllers/api/videos/ownership.ts13
-rw-r--r--server/controllers/api/videos/rate.ts11
6 files changed, 48 insertions, 41 deletions
diff --git a/server/controllers/api/videos/abuse.ts b/server/controllers/api/videos/abuse.ts
index 08e11b00b..d0c81804b 100644
--- a/server/controllers/api/videos/abuse.ts
+++ b/server/controllers/api/videos/abuse.ts
@@ -21,6 +21,7 @@ import { AccountModel } from '../../../models/account/account'
21import { VideoModel } from '../../../models/video/video' 21import { VideoModel } from '../../../models/video/video'
22import { VideoAbuseModel } from '../../../models/video/video-abuse' 22import { VideoAbuseModel } from '../../../models/video/video-abuse'
23import { auditLoggerFactory, VideoAbuseAuditView } from '../../../helpers/audit-logger' 23import { auditLoggerFactory, VideoAbuseAuditView } from '../../../helpers/audit-logger'
24import { UserModel } from '../../../models/account/user'
24 25
25const auditLogger = auditLoggerFactory('abuse') 26const auditLogger = auditLoggerFactory('abuse')
26const abuseVideoRouter = express.Router() 27const abuseVideoRouter = express.Router()
@@ -95,17 +96,18 @@ async function deleteVideoAbuse (req: express.Request, res: express.Response) {
95 96
96async function reportVideoAbuse (req: express.Request, res: express.Response) { 97async function reportVideoAbuse (req: express.Request, res: express.Response) {
97 const videoInstance = res.locals.video as VideoModel 98 const videoInstance = res.locals.video as VideoModel
98 const reporterAccount = res.locals.oauth.token.User.Account as AccountModel
99 const body: VideoAbuseCreate = req.body 99 const body: VideoAbuseCreate = req.body
100 100
101 const abuseToCreate = {
102 reporterAccountId: reporterAccount.id,
103 reason: body.reason,
104 videoId: videoInstance.id,
105 state: VideoAbuseState.PENDING
106 }
107
108 const videoAbuse: VideoAbuseModel = await sequelizeTypescript.transaction(async t => { 101 const videoAbuse: VideoAbuseModel = await sequelizeTypescript.transaction(async t => {
102 const reporterAccount = await AccountModel.load((res.locals.oauth.token.User as UserModel).Account.id, t)
103
104 const abuseToCreate = {
105 reporterAccountId: reporterAccount.id,
106 reason: body.reason,
107 videoId: videoInstance.id,
108 state: VideoAbuseState.PENDING
109 }
110
109 const videoAbuseInstance = await VideoAbuseModel.create(abuseToCreate, { transaction: t }) 111 const videoAbuseInstance = await VideoAbuseModel.create(abuseToCreate, { transaction: t })
110 videoAbuseInstance.Video = videoInstance 112 videoAbuseInstance.Video = videoInstance
111 videoAbuseInstance.Account = reporterAccount 113 videoAbuseInstance.Account = reporterAccount
@@ -121,7 +123,6 @@ async function reportVideoAbuse (req: express.Request, res: express.Response) {
121 }) 123 })
122 124
123 logger.info('Abuse report for video %s created.', videoInstance.name) 125 logger.info('Abuse report for video %s created.', videoInstance.name)
124 return res.json({ 126
125 videoAbuse: videoAbuse.toFormattedJSON() 127 return res.json({ videoAbuse: videoAbuse.toFormattedJSON() }).end()
126 }).end()
127} 128}
diff --git a/server/controllers/api/videos/comment.ts b/server/controllers/api/videos/comment.ts
index e35247829..dc25e1e85 100644
--- a/server/controllers/api/videos/comment.ts
+++ b/server/controllers/api/videos/comment.ts
@@ -23,7 +23,9 @@ import {
23} from '../../../middlewares/validators/video-comments' 23} from '../../../middlewares/validators/video-comments'
24import { VideoModel } from '../../../models/video/video' 24import { VideoModel } from '../../../models/video/video'
25import { VideoCommentModel } from '../../../models/video/video-comment' 25import { VideoCommentModel } from '../../../models/video/video-comment'
26import { auditLoggerFactory, CommentAuditView } from '../../../helpers/audit-logger' 26import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger'
27import { AccountModel } from '../../../models/account/account'
28import { UserModel } from '../../../models/account/user'
27 29
28const auditLogger = auditLoggerFactory('comments') 30const auditLogger = auditLoggerFactory('comments')
29const videoCommentRouter = express.Router() 31const videoCommentRouter = express.Router()
@@ -86,7 +88,7 @@ async function listVideoThreadComments (req: express.Request, res: express.Respo
86 let resultList: ResultList<VideoCommentModel> 88 let resultList: ResultList<VideoCommentModel>
87 89
88 if (video.commentsEnabled === true) { 90 if (video.commentsEnabled === true) {
89 resultList = await VideoCommentModel.listThreadCommentsForApi(res.locals.video.id, res.locals.videoCommentThread.id) 91 resultList = await VideoCommentModel.listThreadCommentsForApi(video.id, res.locals.videoCommentThread.id)
90 } else { 92 } else {
91 resultList = { 93 resultList = {
92 total: 0, 94 total: 0,
@@ -101,15 +103,17 @@ async function addVideoCommentThread (req: express.Request, res: express.Respons
101 const videoCommentInfo: VideoCommentCreate = req.body 103 const videoCommentInfo: VideoCommentCreate = req.body
102 104
103 const comment = await sequelizeTypescript.transaction(async t => { 105 const comment = await sequelizeTypescript.transaction(async t => {
106 const account = await AccountModel.load((res.locals.oauth.token.User as UserModel).Account.id, t)
107
104 return createVideoComment({ 108 return createVideoComment({
105 text: videoCommentInfo.text, 109 text: videoCommentInfo.text,
106 inReplyToComment: null, 110 inReplyToComment: null,
107 video: res.locals.video, 111 video: res.locals.video,
108 account: res.locals.oauth.token.User.Account 112 account
109 }, t) 113 }, t)
110 }) 114 })
111 115
112 auditLogger.create(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new CommentAuditView(comment.toFormattedJSON())) 116 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
113 117
114 return res.json({ 118 return res.json({
115 comment: comment.toFormattedJSON() 119 comment: comment.toFormattedJSON()
@@ -120,19 +124,19 @@ async function addVideoCommentReply (req: express.Request, res: express.Response
120 const videoCommentInfo: VideoCommentCreate = req.body 124 const videoCommentInfo: VideoCommentCreate = req.body
121 125
122 const comment = await sequelizeTypescript.transaction(async t => { 126 const comment = await sequelizeTypescript.transaction(async t => {
127 const account = await AccountModel.load((res.locals.oauth.token.User as UserModel).Account.id, t)
128
123 return createVideoComment({ 129 return createVideoComment({
124 text: videoCommentInfo.text, 130 text: videoCommentInfo.text,
125 inReplyToComment: res.locals.videoComment, 131 inReplyToComment: res.locals.videoComment,
126 video: res.locals.video, 132 video: res.locals.video,
127 account: res.locals.oauth.token.User.Account 133 account
128 }, t) 134 }, t)
129 }) 135 })
130 136
131 auditLogger.create(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new CommentAuditView(comment.toFormattedJSON())) 137 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
132 138
133 return res.json({ 139 return res.json({ comment: comment.toFormattedJSON() }).end()
134 comment: comment.toFormattedJSON()
135 }).end()
136} 140}
137 141
138async function removeVideoComment (req: express.Request, res: express.Response) { 142async function removeVideoComment (req: express.Request, res: express.Response) {
@@ -143,7 +147,7 @@ async function removeVideoComment (req: express.Request, res: express.Response)
143 }) 147 })
144 148
145 auditLogger.delete( 149 auditLogger.delete(
146 res.locals.oauth.token.User.Account.Actor.getIdentifier(), 150 getAuditIdFromRes(res),
147 new CommentAuditView(videoCommentInstance.toFormattedJSON()) 151 new CommentAuditView(videoCommentInstance.toFormattedJSON())
148 ) 152 )
149 logger.info('Video comment %d deleted.', videoCommentInstance.id) 153 logger.info('Video comment %d deleted.', videoCommentInstance.id)
diff --git a/server/controllers/api/videos/import.ts b/server/controllers/api/videos/import.ts
index 44f15ef74..398fd5a7f 100644
--- a/server/controllers/api/videos/import.ts
+++ b/server/controllers/api/videos/import.ts
@@ -1,7 +1,7 @@
1import * as express from 'express' 1import * as express from 'express'
2import * as magnetUtil from 'magnet-uri' 2import * as magnetUtil from 'magnet-uri'
3import 'multer' 3import 'multer'
4import { auditLoggerFactory, VideoImportAuditView } from '../../../helpers/audit-logger' 4import { auditLoggerFactory, getAuditIdFromRes, VideoImportAuditView } from '../../../helpers/audit-logger'
5import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoImportAddValidator } from '../../../middlewares' 5import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoImportAddValidator } from '../../../middlewares'
6import { 6import {
7 CONFIG, 7 CONFIG,
@@ -114,7 +114,7 @@ async function addTorrentImport (req: express.Request, res: express.Response, to
114 } 114 }
115 await JobQueue.Instance.createJob({ type: 'video-import', payload }) 115 await JobQueue.Instance.createJob({ type: 'video-import', payload })
116 116
117 auditLogger.create(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new VideoImportAuditView(videoImport.toFormattedJSON())) 117 auditLogger.create(getAuditIdFromRes(res), new VideoImportAuditView(videoImport.toFormattedJSON()))
118 118
119 return res.json(videoImport.toFormattedJSON()).end() 119 return res.json(videoImport.toFormattedJSON()).end()
120} 120}
@@ -158,7 +158,7 @@ async function addYoutubeDLImport (req: express.Request, res: express.Response)
158 } 158 }
159 await JobQueue.Instance.createJob({ type: 'video-import', payload }) 159 await JobQueue.Instance.createJob({ type: 'video-import', payload })
160 160
161 auditLogger.create(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new VideoImportAuditView(videoImport.toFormattedJSON())) 161 auditLogger.create(getAuditIdFromRes(res), new VideoImportAuditView(videoImport.toFormattedJSON()))
162 162
163 return res.json(videoImport.toFormattedJSON()).end() 163 return res.json(videoImport.toFormattedJSON()).end()
164} 164}
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts
index 0c9e6c2d1..581046782 100644
--- a/server/controllers/api/videos/index.ts
+++ b/server/controllers/api/videos/index.ts
@@ -4,7 +4,7 @@ import { VideoCreate, VideoPrivacy, VideoState, VideoUpdate } from '../../../../
4import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffmpeg-utils' 4import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffmpeg-utils'
5import { processImage } from '../../../helpers/image-utils' 5import { processImage } from '../../../helpers/image-utils'
6import { logger } from '../../../helpers/logger' 6import { logger } from '../../../helpers/logger'
7import { auditLoggerFactory, VideoAuditView } from '../../../helpers/audit-logger' 7import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger'
8import { getFormattedObjects, getServerActor } from '../../../helpers/utils' 8import { getFormattedObjects, getServerActor } from '../../../helpers/utils'
9import { 9import {
10 CONFIG, 10 CONFIG,
@@ -253,7 +253,7 @@ async function addVideo (req: express.Request, res: express.Response) {
253 253
254 await federateVideoIfNeeded(video, true, t) 254 await federateVideoIfNeeded(video, true, t)
255 255
256 auditLogger.create(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new VideoAuditView(videoCreated.toFormattedDetailsJSON())) 256 auditLogger.create(getAuditIdFromRes(res), new VideoAuditView(videoCreated.toFormattedDetailsJSON()))
257 logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoCreated.uuid) 257 logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoCreated.uuid)
258 258
259 return videoCreated 259 return videoCreated
@@ -354,7 +354,7 @@ async function updateVideo (req: express.Request, res: express.Response) {
354 await federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, t) 354 await federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, t)
355 355
356 auditLogger.update( 356 auditLogger.update(
357 res.locals.oauth.token.User.Account.Actor.getIdentifier(), 357 getAuditIdFromRes(res),
358 new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()), 358 new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()),
359 oldVideoAuditView 359 oldVideoAuditView
360 ) 360 )
@@ -393,9 +393,9 @@ async function viewVideo (req: express.Request, res: express.Response) {
393 Redis.Instance.setIPVideoView(ip, videoInstance.uuid) 393 Redis.Instance.setIPVideoView(ip, videoInstance.uuid)
394 ]) 394 ])
395 395
396 const serverAccount = await getServerActor() 396 const serverActor = await getServerActor()
397 397
398 await sendCreateView(serverAccount, videoInstance, undefined) 398 await sendCreateView(serverActor, videoInstance, undefined)
399 399
400 return res.status(204).end() 400 return res.status(204).end()
401} 401}
@@ -439,7 +439,7 @@ async function removeVideo (req: express.Request, res: express.Response) {
439 await videoInstance.destroy({ transaction: t }) 439 await videoInstance.destroy({ transaction: t })
440 }) 440 })
441 441
442 auditLogger.delete(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new VideoAuditView(videoInstance.toFormattedDetailsJSON())) 442 auditLogger.delete(getAuditIdFromRes(res), new VideoAuditView(videoInstance.toFormattedDetailsJSON()))
443 logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid) 443 logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid)
444 444
445 return res.type('json').status(204).end() 445 return res.type('json').status(204).end()
diff --git a/server/controllers/api/videos/ownership.ts b/server/controllers/api/videos/ownership.ts
index d26ed6cfc..5ea7d7c6a 100644
--- a/server/controllers/api/videos/ownership.ts
+++ b/server/controllers/api/videos/ownership.ts
@@ -19,6 +19,7 @@ import { VideoChannelModel } from '../../../models/video/video-channel'
19import { getFormattedObjects } from '../../../helpers/utils' 19import { getFormattedObjects } from '../../../helpers/utils'
20import { changeVideoChannelShare } from '../../../lib/activitypub' 20import { changeVideoChannelShare } from '../../../lib/activitypub'
21import { sendUpdateVideo } from '../../../lib/activitypub/send' 21import { sendUpdateVideo } from '../../../lib/activitypub/send'
22import { UserModel } from '../../../models/account/user'
22 23
23const ownershipVideoRouter = express.Router() 24const ownershipVideoRouter = express.Router()
24 25
@@ -58,26 +59,25 @@ export {
58 59
59async function giveVideoOwnership (req: express.Request, res: express.Response) { 60async function giveVideoOwnership (req: express.Request, res: express.Response) {
60 const videoInstance = res.locals.video as VideoModel 61 const videoInstance = res.locals.video as VideoModel
61 const initiatorAccount = res.locals.oauth.token.User.Account as AccountModel 62 const initiatorAccountId = (res.locals.oauth.token.User as UserModel).Account.id
62 const nextOwner = res.locals.nextOwner as AccountModel 63 const nextOwner = res.locals.nextOwner as AccountModel
63 64
64 await sequelizeTypescript.transaction(t => { 65 await sequelizeTypescript.transaction(t => {
65 return VideoChangeOwnershipModel.findOrCreate({ 66 return VideoChangeOwnershipModel.findOrCreate({
66 where: { 67 where: {
67 initiatorAccountId: initiatorAccount.id, 68 initiatorAccountId,
68 nextOwnerAccountId: nextOwner.id, 69 nextOwnerAccountId: nextOwner.id,
69 videoId: videoInstance.id, 70 videoId: videoInstance.id,
70 status: VideoChangeOwnershipStatus.WAITING 71 status: VideoChangeOwnershipStatus.WAITING
71 }, 72 },
72 defaults: { 73 defaults: {
73 initiatorAccountId: initiatorAccount.id, 74 initiatorAccountId,
74 nextOwnerAccountId: nextOwner.id, 75 nextOwnerAccountId: nextOwner.id,
75 videoId: videoInstance.id, 76 videoId: videoInstance.id,
76 status: VideoChangeOwnershipStatus.WAITING 77 status: VideoChangeOwnershipStatus.WAITING
77 }, 78 },
78 transaction: t 79 transaction: t
79 }) 80 })
80
81 }) 81 })
82 82
83 logger.info('Ownership change for video %s created.', videoInstance.name) 83 logger.info('Ownership change for video %s created.', videoInstance.name)
@@ -85,9 +85,10 @@ async function giveVideoOwnership (req: express.Request, res: express.Response)
85} 85}
86 86
87async function listVideoOwnership (req: express.Request, res: express.Response) { 87async function listVideoOwnership (req: express.Request, res: express.Response) {
88 const currentAccount = res.locals.oauth.token.User.Account as AccountModel 88 const currentAccountId = (res.locals.oauth.token.User as UserModel).Account.id
89
89 const resultList = await VideoChangeOwnershipModel.listForApi( 90 const resultList = await VideoChangeOwnershipModel.listForApi(
90 currentAccount.id, 91 currentAccountId,
91 req.query.start || 0, 92 req.query.start || 0,
92 req.query.count || 10, 93 req.query.count || 10,
93 req.query.sort || 'createdAt' 94 req.query.sort || 'createdAt'
diff --git a/server/controllers/api/videos/rate.ts b/server/controllers/api/videos/rate.ts
index b1732837d..dc322bb0c 100644
--- a/server/controllers/api/videos/rate.ts
+++ b/server/controllers/api/videos/rate.ts
@@ -28,10 +28,11 @@ async function rateVideo (req: express.Request, res: express.Response) {
28 const body: UserVideoRateUpdate = req.body 28 const body: UserVideoRateUpdate = req.body
29 const rateType = body.rating 29 const rateType = body.rating
30 const videoInstance: VideoModel = res.locals.video 30 const videoInstance: VideoModel = res.locals.video
31 const accountInstance: AccountModel = res.locals.oauth.token.User.Account
32 31
33 await sequelizeTypescript.transaction(async t => { 32 await sequelizeTypescript.transaction(async t => {
34 const sequelizeOptions = { transaction: t } 33 const sequelizeOptions = { transaction: t }
34
35 const accountInstance = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
35 const previousRate = await AccountVideoRateModel.load(accountInstance.id, videoInstance.id, t) 36 const previousRate = await AccountVideoRateModel.load(accountInstance.id, videoInstance.id, t)
36 37
37 let likesToIncrement = 0 38 let likesToIncrement = 0
@@ -47,10 +48,10 @@ async function rateVideo (req: express.Request, res: express.Response) {
47 else if (previousRate.type === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement-- 48 else if (previousRate.type === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement--
48 49
49 if (rateType === 'none') { // Destroy previous rate 50 if (rateType === 'none') { // Destroy previous rate
50 await previousRate.destroy({ transaction: t }) 51 await previousRate.destroy(sequelizeOptions)
51 } else { // Update previous rate 52 } else { // Update previous rate
52 previousRate.type = rateType 53 previousRate.type = rateType
53 await previousRate.save({ transaction: t }) 54 await previousRate.save(sequelizeOptions)
54 } 55 }
55 } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate 56 } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate
56 const query = { 57 const query = {
@@ -70,9 +71,9 @@ async function rateVideo (req: express.Request, res: express.Response) {
70 await videoInstance.increment(incrementQuery, sequelizeOptions) 71 await videoInstance.increment(incrementQuery, sequelizeOptions)
71 72
72 await sendVideoRateChange(accountInstance, videoInstance, likesToIncrement, dislikesToIncrement, t) 73 await sendVideoRateChange(accountInstance, videoInstance, likesToIncrement, dislikesToIncrement, t)
73 })
74 74
75 logger.info('Account video rate for video %s of account %s updated.', videoInstance.name, accountInstance.name) 75 logger.info('Account video rate for video %s of account %s updated.', videoInstance.name, accountInstance.name)
76 })
76 77
77 return res.type('json').status(204).end() 78 return res.type('json').status(204).end()
78} 79}