diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-02-21 21:35:59 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-02-26 20:01:26 +0100 |
commit | 9e167724f7e933f41d9ea2e1c31772bf4c560a28 (patch) | |
tree | 093cb7c1b088f35aaf847f859a313a121c8cd233 /server/controllers/api/remote/videos.js | |
parent | 0150b17e51df3e9fad8a59133d828c68f8ba672b (diff) | |
download | PeerTube-9e167724f7e933f41d9ea2e1c31772bf4c560a28.tar.gz PeerTube-9e167724f7e933f41d9ea2e1c31772bf4c560a28.tar.zst PeerTube-9e167724f7e933f41d9ea2e1c31772bf4c560a28.zip |
Server: make a basic "quick and dirty update" for videos
This system will be useful to to update some int video attributes
(likes, dislikes, views...)
The classic system is not used because we need some optimization for
scaling
Diffstat (limited to 'server/controllers/api/remote/videos.js')
-rw-r--r-- | server/controllers/api/remote/videos.js | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/server/controllers/api/remote/videos.js b/server/controllers/api/remote/videos.js index f8b4949cd..79b503d4d 100644 --- a/server/controllers/api/remote/videos.js +++ b/server/controllers/api/remote/videos.js | |||
@@ -31,6 +31,13 @@ router.post('/', | |||
31 | remoteVideos | 31 | remoteVideos |
32 | ) | 32 | ) |
33 | 33 | ||
34 | router.post('/qadu', | ||
35 | signatureValidators.signature, | ||
36 | secureMiddleware.checkSignature, | ||
37 | videosValidators.remoteQaduVideos, | ||
38 | remoteVideosQadu | ||
39 | ) | ||
40 | |||
34 | // --------------------------------------------------------------------------- | 41 | // --------------------------------------------------------------------------- |
35 | 42 | ||
36 | module.exports = router | 43 | module.exports = router |
@@ -62,6 +69,73 @@ function remoteVideos (req, res, next) { | |||
62 | return res.type('json').status(204).end() | 69 | return res.type('json').status(204).end() |
63 | } | 70 | } |
64 | 71 | ||
72 | function remoteVideosQadu (req, res, next) { | ||
73 | const requests = req.body.data | ||
74 | const fromPod = res.locals.secure.pod | ||
75 | |||
76 | eachSeries(requests, function (request, callbackEach) { | ||
77 | const videoData = request.data | ||
78 | |||
79 | quickAndDirtyUpdateVideoRetryWrapper(videoData, fromPod, callbackEach) | ||
80 | }, function (err) { | ||
81 | if (err) logger.error('Error managing remote videos.', { error: err }) | ||
82 | }) | ||
83 | |||
84 | return res.type('json').status(204).end() | ||
85 | } | ||
86 | |||
87 | function quickAndDirtyUpdateVideoRetryWrapper (videoData, fromPod, finalCallback) { | ||
88 | const options = { | ||
89 | arguments: [ videoData, fromPod ], | ||
90 | errorMessage: 'Cannot update quick and dirty the remote video with many retries.' | ||
91 | } | ||
92 | |||
93 | databaseUtils.retryTransactionWrapper(quickAndDirtyUpdateVideo, options, finalCallback) | ||
94 | } | ||
95 | |||
96 | function quickAndDirtyUpdateVideo (videoData, fromPod, finalCallback) { | ||
97 | waterfall([ | ||
98 | databaseUtils.startSerializableTransaction, | ||
99 | |||
100 | function findVideo (t, callback) { | ||
101 | fetchVideo(fromPod.host, videoData.remoteId, function (err, videoInstance) { | ||
102 | return callback(err, t, videoInstance) | ||
103 | }) | ||
104 | }, | ||
105 | |||
106 | function updateVideoIntoDB (t, videoInstance, callback) { | ||
107 | const options = { transaction: t } | ||
108 | |||
109 | if (videoData.views) { | ||
110 | videoInstance.set('views', videoData.views) | ||
111 | } | ||
112 | |||
113 | if (videoData.likes) { | ||
114 | videoInstance.set('likes', videoData.likes) | ||
115 | } | ||
116 | |||
117 | if (videoData.dislikes) { | ||
118 | videoInstance.set('dislikes', videoData.dislikes) | ||
119 | } | ||
120 | |||
121 | videoInstance.save(options).asCallback(function (err) { | ||
122 | return callback(err, t) | ||
123 | }) | ||
124 | }, | ||
125 | |||
126 | databaseUtils.commitTransaction | ||
127 | |||
128 | ], function (err, t) { | ||
129 | if (err) { | ||
130 | logger.debug('Cannot quick and dirty update the remote video.', { error: err }) | ||
131 | return databaseUtils.rollbackTransaction(err, t, finalCallback) | ||
132 | } | ||
133 | |||
134 | logger.info('Remote video %s quick and dirty updated', videoData.name) | ||
135 | return finalCallback(null) | ||
136 | }) | ||
137 | } | ||
138 | |||
65 | // Handle retries on fail | 139 | // Handle retries on fail |
66 | function addRemoteVideoRetryWrapper (videoToCreateData, fromPod, finalCallback) { | 140 | function addRemoteVideoRetryWrapper (videoToCreateData, fromPod, finalCallback) { |
67 | const options = { | 141 | const options = { |