aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-12-29 18:02:03 +0100
committerChocobozzz <florian.bigard@gmail.com>2016-12-29 18:02:03 +0100
commit4ff0d86208dafbdd07beb6286fd93c795db8a95f (patch)
treef24b12065afd2e9ec0c89f806961803dfcb678a8
parentd396a937b642d616beb72dde54c0c2d37c7e8c30 (diff)
downloadPeerTube-4ff0d86208dafbdd07beb6286fd93c795db8a95f.tar.gz
PeerTube-4ff0d86208dafbdd07beb6286fd93c795db8a95f.tar.zst
PeerTube-4ff0d86208dafbdd07beb6286fd93c795db8a95f.zip
Server: little refractoring
-rw-r--r--server/controllers/api/remote.js79
-rw-r--r--server/controllers/api/videos.js46
-rw-r--r--server/middlewares/secure.js4
-rw-r--r--server/models/author.js29
-rw-r--r--server/models/tag.js40
5 files changed, 93 insertions, 105 deletions
diff --git a/server/controllers/api/remote.js b/server/controllers/api/remote.js
index ac850c2d2..929ade555 100644
--- a/server/controllers/api/remote.js
+++ b/server/controllers/api/remote.js
@@ -28,7 +28,7 @@ module.exports = router
28 28
29function remoteVideos (req, res, next) { 29function remoteVideos (req, res, next) {
30 const requests = req.body.data 30 const requests = req.body.data
31 const fromHost = req.body.signature.host 31 const fromPod = res.locals.secure.pod
32 32
33 // We need to process in the same order to keep consistency 33 // We need to process in the same order to keep consistency
34 // TODO: optimization 34 // TODO: optimization
@@ -36,9 +36,9 @@ function remoteVideos (req, res, next) {
36 const videoData = request.data 36 const videoData = request.data
37 37
38 if (request.type === 'add') { 38 if (request.type === 'add') {
39 addRemoteVideo(videoData, fromHost, callbackEach) 39 addRemoteVideo(videoData, fromPod, callbackEach)
40 } else if (request.type === 'remove') { 40 } else if (request.type === 'remove') {
41 removeRemoteVideo(videoData, fromHost, callbackEach) 41 removeRemoteVideo(videoData, fromPod, callbackEach)
42 } else { 42 } else {
43 logger.error('Unkown remote request type %s.', request.type) 43 logger.error('Unkown remote request type %s.', request.type)
44 } 44 }
@@ -50,7 +50,7 @@ function remoteVideos (req, res, next) {
50 return res.type('json').status(204).end() 50 return res.type('json').status(204).end()
51} 51}
52 52
53function addRemoteVideo (videoToCreateData, fromHost, finalCallback) { 53function addRemoteVideo (videoToCreateData, fromPod, finalCallback) {
54 logger.debug('Adding remote video "%s".', videoToCreateData.name) 54 logger.debug('Adding remote video "%s".', videoToCreateData.name)
55 55
56 waterfall([ 56 waterfall([
@@ -61,70 +61,21 @@ function addRemoteVideo (videoToCreateData, fromHost, finalCallback) {
61 }) 61 })
62 }, 62 },
63 63
64 function findOrCreatePod (t, callback) { 64 function findOrCreateAuthor (t, callback) {
65 const query = { 65 const name = videoToCreateData.author
66 where: { 66 const podId = fromPod.id
67 host: fromHost 67 // This author is from another pod so we do not associate a user
68 }, 68 const userId = null
69 defaults: {
70 host: fromHost
71 },
72 transaction: t
73 }
74
75 db.Pod.findOrCreate(query).asCallback(function (err, result) {
76 // [ instance, wasCreated ]
77 return callback(err, t, result[0])
78 })
79 },
80 69
81 function findOrCreateAuthor (t, pod, callback) { 70 db.Author.findOrCreateAuthor(name, podId, userId, t, function (err, authorInstance) {
82 const username = videoToCreateData.author 71 return callback(err, t, authorInstance)
83
84 const query = {
85 where: {
86 name: username,
87 podId: pod.id,
88 userId: null
89 },
90 defaults: {
91 name: username,
92 podId: pod.id,
93 userId: null
94 },
95 transaction: t
96 }
97
98 db.Author.findOrCreate(query).asCallback(function (err, result) {
99 // [ instance, wasCreated ]
100 return callback(err, t, result[0])
101 }) 72 })
102 }, 73 },
103 74
104 function findOrCreateTags (t, author, callback) { 75 function findOrCreateTags (t, author, callback) {
105 const tags = videoToCreateData.tags 76 const tags = videoToCreateData.tags
106 const tagInstances = []
107
108 each(tags, function (tag, callbackEach) {
109 const query = {
110 where: {
111 name: tag
112 },
113 defaults: {
114 name: tag
115 },
116 transaction: t
117 }
118
119 db.Tag.findOrCreate(query).asCallback(function (err, res) {
120 if (err) return callbackEach(err)
121 77
122 // res = [ tag, isCreated ] 78 db.Tag.findOrCreateTags(tags, t, function (err, tagInstances) {
123 const tag = res[0]
124 tagInstances.push(tag)
125 return callbackEach()
126 })
127 }, function (err) {
128 return callback(err, t, author, tagInstances) 79 return callback(err, t, author, tagInstances)
129 }) 80 })
130 }, 81 },
@@ -192,18 +143,18 @@ function addRemoteVideo (videoToCreateData, fromHost, finalCallback) {
192 }) 143 })
193} 144}
194 145
195function removeRemoteVideo (videoToRemoveData, fromHost, callback) { 146function removeRemoteVideo (videoToRemoveData, fromPod, callback) {
196 // TODO: use bulkDestroy? 147 // TODO: use bulkDestroy?
197 148
198 // We need the list because we have to remove some other stuffs (thumbnail etc) 149 // We need the list because we have to remove some other stuffs (thumbnail etc)
199 db.Video.listByHostAndRemoteId(fromHost, videoToRemoveData.remoteId, function (err, videosList) { 150 db.Video.listByHostAndRemoteId(fromPod.host, videoToRemoveData.remoteId, function (err, videosList) {
200 if (err) { 151 if (err) {
201 logger.error('Cannot list videos from host and remote id.', { error: err.message }) 152 logger.error('Cannot list videos from host and remote id.', { error: err.message })
202 return callback(err) 153 return callback(err)
203 } 154 }
204 155
205 if (videosList.length === 0) { 156 if (videosList.length === 0) {
206 logger.error('No remote video was found for this pod.', { remoteId: videoToRemoveData.remoteId, podHost: fromHost }) 157 logger.error('No remote video was found for this pod.', { remoteId: videoToRemoveData.remoteId, podHost: fromPod.host })
207 } 158 }
208 159
209 each(videosList, function (video, callbackEach) { 160 each(videosList, function (video, callbackEach) {
diff --git a/server/controllers/api/videos.js b/server/controllers/api/videos.js
index ddf85d77d..f29edac74 100644
--- a/server/controllers/api/videos.js
+++ b/server/controllers/api/videos.js
@@ -1,6 +1,5 @@
1'use strict' 1'use strict'
2 2
3const each = require('async/each')
4const express = require('express') 3const express = require('express')
5const fs = require('fs') 4const fs = require('fs')
6const multer = require('multer') 5const multer = require('multer')
@@ -97,51 +96,20 @@ function addVideo (req, res, next) {
97 function findOrCreateAuthor (t, callback) { 96 function findOrCreateAuthor (t, callback) {
98 const user = res.locals.oauth.token.User 97 const user = res.locals.oauth.token.User
99 98
100 const query = { 99 const name = user.username
101 where: { 100 // null because it is OUR pod
102 name: user.username, 101 const podId = null
103 podId: null, 102 const userId = user.id
104 userId: user.id
105 },
106 defaults: {
107 name: user.username,
108 podId: null, // null because it is OUR pod
109 userId: user.id
110 },
111 transaction: t
112 }
113
114 db.Author.findOrCreate(query).asCallback(function (err, result) {
115 const authorInstance = result[0]
116 103
104 db.Author.findOrCreateAuthor(name, podId, userId, t, function (err, authorInstance) {
117 return callback(err, t, authorInstance) 105 return callback(err, t, authorInstance)
118 }) 106 })
119 }, 107 },
120 108
121 function findOrCreateTags (t, author, callback) { 109 function findOrCreateTags (t, author, callback) {
122 const tags = videoInfos.tags 110 const tags = videoInfos.tags
123 const tagInstances = [] 111
124 112 db.Tag.findOrCreateTags(tags, t, function (err, tagInstances) {
125 each(tags, function (tag, callbackEach) {
126 const query = {
127 where: {
128 name: tag
129 },
130 defaults: {
131 name: tag
132 },
133 transaction: t
134 }
135
136 db.Tag.findOrCreate(query).asCallback(function (err, res) {
137 if (err) return callbackEach(err)
138
139 // res = [ tag, isCreated ]
140 const tag = res[0]
141 tagInstances.push(tag)
142 return callbackEach()
143 })
144 }, function (err) {
145 return callback(err, t, author, tagInstances) 113 return callback(err, t, author, tagInstances)
146 }) 114 })
147 }, 115 },
diff --git a/server/middlewares/secure.js b/server/middlewares/secure.js
index b7b4cdfb4..2aae715c4 100644
--- a/server/middlewares/secure.js
+++ b/server/middlewares/secure.js
@@ -26,6 +26,10 @@ function checkSignature (req, res, next) {
26 const signatureOk = peertubeCrypto.checkSignature(pod.publicKey, host, req.body.signature.signature) 26 const signatureOk = peertubeCrypto.checkSignature(pod.publicKey, host, req.body.signature.signature)
27 27
28 if (signatureOk === true) { 28 if (signatureOk === true) {
29 res.locals.secure = {
30 pod
31 }
32
29 return next() 33 return next()
30 } 34 }
31 35
diff --git a/server/models/author.js b/server/models/author.js
index 5835ada99..7d15fb6ec 100644
--- a/server/models/author.js
+++ b/server/models/author.js
@@ -29,7 +29,9 @@ module.exports = function (sequelize, DataTypes) {
29 } 29 }
30 ], 30 ],
31 classMethods: { 31 classMethods: {
32 associate 32 associate,
33
34 findOrCreateAuthor
33 } 35 }
34 } 36 }
35 ) 37 )
@@ -56,3 +58,28 @@ function associate (models) {
56 onDelete: 'cascade' 58 onDelete: 'cascade'
57 }) 59 })
58} 60}
61
62function findOrCreateAuthor (name, podId, userId, transaction, callback) {
63 if (!callback) {
64 callback = transaction
65 transaction = null
66 }
67
68 const author = {
69 name,
70 podId,
71 userId
72 }
73
74 const query = {
75 where: author,
76 defaults: author
77 }
78
79 if (transaction) query.transaction = transaction
80
81 this.findOrCreate(query).asCallback(function (err, result) {
82 // [ instance, wasCreated ]
83 return callback(err, result[0])
84 })
85}
diff --git a/server/models/tag.js b/server/models/tag.js
index 27eecdc84..145e090c1 100644
--- a/server/models/tag.js
+++ b/server/models/tag.js
@@ -1,5 +1,7 @@
1'use strict' 1'use strict'
2 2
3const each = require('async/each')
4
3// --------------------------------------------------------------------------- 5// ---------------------------------------------------------------------------
4 6
5module.exports = function (sequelize, DataTypes) { 7module.exports = function (sequelize, DataTypes) {
@@ -19,7 +21,9 @@ module.exports = function (sequelize, DataTypes) {
19 } 21 }
20 ], 22 ],
21 classMethods: { 23 classMethods: {
22 associate 24 associate,
25
26 findOrCreateTags
23 } 27 }
24 } 28 }
25 ) 29 )
@@ -36,3 +40,37 @@ function associate (models) {
36 onDelete: 'cascade' 40 onDelete: 'cascade'
37 }) 41 })
38} 42}
43
44function findOrCreateTags (tags, transaction, callback) {
45 if (!callback) {
46 callback = transaction
47 transaction = null
48 }
49
50 const self = this
51 const tagInstances = []
52
53 each(tags, function (tag, callbackEach) {
54 const query = {
55 where: {
56 name: tag
57 },
58 defaults: {
59 name: tag
60 }
61 }
62
63 if (transaction) query.transaction = transaction
64
65 self.findOrCreate(query).asCallback(function (err, res) {
66 if (err) return callbackEach(err)
67
68 // res = [ tag, isCreated ]
69 const tag = res[0]
70 tagInstances.push(tag)
71 return callbackEach()
72 })
73 }, function (err) {
74 return callback(err, tagInstances)
75 })
76}