Add MANAGE_PEERTUBE_FOLLOW right
[oweals/peertube.git] / server / controllers / activitypub / videos.ts
1 // import * as express from 'express'
2 // import * as Bluebird from 'bluebird'
3 // import * as Sequelize from 'sequelize'
4 //
5 // import { database as db } from '../../../initializers/database'
6 // import {
7 //   REQUEST_ENDPOINT_ACTIONS,
8 //   REQUEST_ENDPOINTS,
9 //   REQUEST_VIDEO_EVENT_TYPES,
10 //   REQUEST_VIDEO_QADU_TYPES
11 // } from '../../../initializers'
12 // import {
13 //   checkSignature,
14 //   signatureValidator,
15 //   remoteVideosValidator,
16 //   remoteQaduVideosValidator,
17 //   remoteEventsVideosValidator
18 // } from '../../../middlewares'
19 // import { logger, retryTransactionWrapper, resetSequelizeInstance } from '../../../helpers'
20 // import { quickAndDirtyUpdatesVideoToFriends, fetchVideoChannelByHostAndUUID } from '../../../lib'
21 // import { PodInstance, VideoFileInstance } from '../../../models'
22 // import {
23 //   RemoteVideoRequest,
24 //   RemoteVideoCreateData,
25 //   RemoteVideoUpdateData,
26 //   RemoteVideoRemoveData,
27 //   RemoteVideoReportAbuseData,
28 //   RemoteQaduVideoRequest,
29 //   RemoteQaduVideoData,
30 //   RemoteVideoEventRequest,
31 //   RemoteVideoEventData,
32 //   RemoteVideoChannelCreateData,
33 //   RemoteVideoChannelUpdateData,
34 //   RemoteVideoChannelRemoveData,
35 //   RemoteVideoAccountRemoveData,
36 //   RemoteVideoAccountCreateData
37 // } from '../../../../shared'
38 // import { VideoInstance } from '../../../models/video/video-interface'
39 //
40 // const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS]
41 //
42 // // Functions to call when processing a remote request
43 // // FIXME: use RemoteVideoRequestType as id type
44 // const functionsHash: { [ id: string ]: (...args) => Promise<any> } = {}
45 // functionsHash[ENDPOINT_ACTIONS.ADD_VIDEO] = addRemoteVideoRetryWrapper
46 // functionsHash[ENDPOINT_ACTIONS.UPDATE_VIDEO] = updateRemoteVideoRetryWrapper
47 // functionsHash[ENDPOINT_ACTIONS.REMOVE_VIDEO] = removeRemoteVideoRetryWrapper
48 // functionsHash[ENDPOINT_ACTIONS.ADD_CHANNEL] = addRemoteVideoChannelRetryWrapper
49 // functionsHash[ENDPOINT_ACTIONS.UPDATE_CHANNEL] = updateRemoteVideoChannelRetryWrapper
50 // functionsHash[ENDPOINT_ACTIONS.REMOVE_CHANNEL] = removeRemoteVideoChannelRetryWrapper
51 // functionsHash[ENDPOINT_ACTIONS.REPORT_ABUSE] = reportAbuseRemoteVideoRetryWrapper
52 // functionsHash[ENDPOINT_ACTIONS.ADD_ACCOUNT] = addRemoteVideoAccountRetryWrapper
53 // functionsHash[ENDPOINT_ACTIONS.REMOVE_ACCOUNT] = removeRemoteVideoAccountRetryWrapper
54 //
55 // const remoteVideosRouter = express.Router()
56 //
57 // remoteVideosRouter.post('/',
58 //   signatureValidator,
59 //   checkSignature,
60 //   remoteVideosValidator,
61 //   remoteVideos
62 // )
63 //
64 // remoteVideosRouter.post('/qadu',
65 //   signatureValidator,
66 //   checkSignature,
67 //   remoteQaduVideosValidator,
68 //   remoteVideosQadu
69 // )
70 //
71 // remoteVideosRouter.post('/events',
72 //   signatureValidator,
73 //   checkSignature,
74 //   remoteEventsVideosValidator,
75 //   remoteVideosEvents
76 // )
77 //
78 // // ---------------------------------------------------------------------------
79 //
80 // export {
81 //   remoteVideosRouter
82 // }
83 //
84 // // ---------------------------------------------------------------------------
85 //
86 // function remoteVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
87 //   const requests: RemoteVideoRequest[] = req.body.data
88 //   const fromPod = res.locals.secure.pod
89 //
90 //   // We need to process in the same order to keep consistency
91 //   Bluebird.each(requests, request => {
92 //     const data = request.data
93 //
94 //     // Get the function we need to call in order to process the request
95 //     const fun = functionsHash[request.type]
96 //     if (fun === undefined) {
97 //       logger.error('Unknown remote request type %s.', request.type)
98 //       return
99 //     }
100 //
101 //     return fun.call(this, data, fromPod)
102 //   })
103 //   .catch(err => logger.error('Error managing remote videos.', err))
104 //
105 //   // Don't block the other pod
106 //   return res.type('json').status(204).end()
107 // }
108 //
109 // function remoteVideosQadu (req: express.Request, res: express.Response, next: express.NextFunction) {
110 //   const requests: RemoteQaduVideoRequest[] = req.body.data
111 //   const fromPod = res.locals.secure.pod
112 //
113 //   Bluebird.each(requests, request => {
114 //     const videoData = request.data
115 //
116 //     return quickAndDirtyUpdateVideoRetryWrapper(videoData, fromPod)
117 //   })
118 //   .catch(err => logger.error('Error managing remote videos.', err))
119 //
120 //   return res.type('json').status(204).end()
121 // }
122 //
123 // function remoteVideosEvents (req: express.Request, res: express.Response, next: express.NextFunction) {
124 //   const requests: RemoteVideoEventRequest[] = req.body.data
125 //   const fromPod = res.locals.secure.pod
126 //
127 //   Bluebird.each(requests, request => {
128 //     const eventData = request.data
129 //
130 //     return processVideosEventsRetryWrapper(eventData, fromPod)
131 //   })
132 //   .catch(err => logger.error('Error managing remote videos.', err))
133 //
134 //   return res.type('json').status(204).end()
135 // }
136 //
137 // async function processVideosEventsRetryWrapper (eventData: RemoteVideoEventData, fromPod: PodInstance) {
138 //   const options = {
139 //     arguments: [ eventData, fromPod ],
140 //     errorMessage: 'Cannot process videos events with many retries.'
141 //   }
142 //
143 //   await retryTransactionWrapper(processVideosEvents, options)
144 // }
145 //
146 // async function processVideosEvents (eventData: RemoteVideoEventData, fromPod: PodInstance) {
147 //   await db.sequelize.transaction(async t => {
148 //     const sequelizeOptions = { transaction: t }
149 //     const videoInstance = await fetchLocalVideoByUUID(eventData.uuid, t)
150 //
151 //     let columnToUpdate
152 //     let qaduType
153 //
154 //     switch (eventData.eventType) {
155 //     case REQUEST_VIDEO_EVENT_TYPES.VIEWS:
156 //       columnToUpdate = 'views'
157 //       qaduType = REQUEST_VIDEO_QADU_TYPES.VIEWS
158 //       break
159 //
160 //     case REQUEST_VIDEO_EVENT_TYPES.LIKES:
161 //       columnToUpdate = 'likes'
162 //       qaduType = REQUEST_VIDEO_QADU_TYPES.LIKES
163 //       break
164 //
165 //     case REQUEST_VIDEO_EVENT_TYPES.DISLIKES:
166 //       columnToUpdate = 'dislikes'
167 //       qaduType = REQUEST_VIDEO_QADU_TYPES.DISLIKES
168 //       break
169 //
170 //     default:
171 //       throw new Error('Unknown video event type.')
172 //     }
173 //
174 //     const query = {}
175 //     query[columnToUpdate] = eventData.count
176 //
177 //     await videoInstance.increment(query, sequelizeOptions)
178 //
179 //     const qadusParams = [
180 //       {
181 //         videoId: videoInstance.id,
182 //         type: qaduType
183 //       }
184 //     ]
185 //     await quickAndDirtyUpdatesVideoToFriends(qadusParams, t)
186 //   })
187 //
188 //   logger.info('Remote video event processed for video with uuid %s.', eventData.uuid)
189 // }
190 //
191 // async function quickAndDirtyUpdateVideoRetryWrapper (videoData: RemoteQaduVideoData, fromPod: PodInstance) {
192 //   const options = {
193 //     arguments: [ videoData, fromPod ],
194 //     errorMessage: 'Cannot update quick and dirty the remote video with many retries.'
195 //   }
196 //
197 //   await retryTransactionWrapper(quickAndDirtyUpdateVideo, options)
198 // }
199 //
200 // async function quickAndDirtyUpdateVideo (videoData: RemoteQaduVideoData, fromPod: PodInstance) {
201 //   let videoUUID = ''
202 //
203 //   await db.sequelize.transaction(async t => {
204 //     const videoInstance = await fetchVideoByHostAndUUID(fromPod.host, videoData.uuid, t)
205 //     const sequelizeOptions = { transaction: t }
206 //
207 //     videoUUID = videoInstance.uuid
208 //
209 //     if (videoData.views) {
210 //       videoInstance.set('views', videoData.views)
211 //     }
212 //
213 //     if (videoData.likes) {
214 //       videoInstance.set('likes', videoData.likes)
215 //     }
216 //
217 //     if (videoData.dislikes) {
218 //       videoInstance.set('dislikes', videoData.dislikes)
219 //     }
220 //
221 //     await videoInstance.save(sequelizeOptions)
222 //   })
223 //
224 //   logger.info('Remote video with uuid %s quick and dirty updated', videoUUID)
225 // }
226 //
227 // async function reportAbuseRemoteVideoRetryWrapper (reportData: RemoteVideoReportAbuseData, fromPod: PodInstance) {
228 //   const options = {
229 //     arguments: [ reportData, fromPod ],
230 //     errorMessage: 'Cannot create remote abuse video with many retries.'
231 //   }
232 //
233 //   await retryTransactionWrapper(reportAbuseRemoteVideo, options)
234 // }
235 //
236 // async function reportAbuseRemoteVideo (reportData: RemoteVideoReportAbuseData, fromPod: PodInstance) {
237 //   logger.debug('Reporting remote abuse for video %s.', reportData.videoUUID)
238 //
239 //   await db.sequelize.transaction(async t => {
240 //     const videoInstance = await fetchLocalVideoByUUID(reportData.videoUUID, t)
241 //     const videoAbuseData = {
242 //       reporterUsername: reportData.reporterUsername,
243 //       reason: reportData.reportReason,
244 //       reporterPodId: fromPod.id,
245 //       videoId: videoInstance.id
246 //     }
247 //
248 //     await db.VideoAbuse.create(videoAbuseData)
249 //
250 //   })
251 //
252 //   logger.info('Remote abuse for video uuid %s created', reportData.videoUUID)
253 // }
254 //
255 // async function fetchLocalVideoByUUID (id: string, t: Sequelize.Transaction) {
256 //   try {
257 //     const video = await db.Video.loadLocalVideoByUUID(id, t)
258 //
259 //     if (!video) throw new Error('Video ' + id + ' not found')
260 //
261 //     return video
262 //   } catch (err) {
263 //     logger.error('Cannot load owned video from id.', { error: err.stack, id })
264 //     throw err
265 //   }
266 // }
267 //
268 // async function fetchVideoByHostAndUUID (podHost: string, uuid: string, t: Sequelize.Transaction) {
269 //   try {
270 //     const video = await db.Video.loadByHostAndUUID(podHost, uuid, t)
271 //     if (!video) throw new Error('Video not found')
272 //
273 //     return video
274 //   } catch (err) {
275 //     logger.error('Cannot load video from host and uuid.', { error: err.stack, podHost, uuid })
276 //     throw err
277 //   }
278 // }