]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Remove duplicated videos on unfollow/delete redundancy
authorChocobozzz <me@florianbigard.com>
Fri, 28 Sep 2018 08:07:05 +0000 (10:07 +0200)
committerChocobozzz <me@florianbigard.com>
Fri, 28 Sep 2018 08:07:05 +0000 (10:07 +0200)
client/src/sass/application.scss
client/src/sass/include/_fonts.scss
server/controllers/api/server/follows.ts
server/controllers/api/server/redundancy.ts
server/lib/redundancy.ts
server/models/redundancy/video-redundancy.ts
server/tests/api/server/redundancy.ts

index 0029c22ef6bcc04dff1afc4c2d5bc9c4872fc167..40a9ed231478f29b26fabf0902b4ada04763a475 100644 (file)
@@ -1,4 +1,4 @@
-$icon-font-path: '../../node_modules/@neos21/bootstrap3-glyphicons/assets/fonts/';
+$icon-font-path: '~@neos21/bootstrap3-glyphicons/assets/fonts/';
 @import '_bootstrap';
 
 @import '_variables';
index 61717e6f567405229498e0b7c5060b83fd26cb3a..d4694a747965cb63aadfe8b41b51264c8c0060b9 100644 (file)
@@ -1,4 +1,4 @@
-$FontPathSourceSansPro: '../../node_modules/npm-font-source-sans-pro/fonts';
+$FontPathSourceSansPro: '~npm-font-source-sans-pro/fonts';
 
 @font-face {
   font-family: 'Source Sans Pro';
index a4eae6b45b69df0288d733b1cd993050119c81c7..d62400e423a9a18c80c4e09b37a873f5a92c2d98 100644 (file)
@@ -17,6 +17,7 @@ import {
 import { followersSortValidator, followingSortValidator, followValidator } from '../../../middlewares/validators'
 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
 import { JobQueue } from '../../../lib/job-queue'
+import { removeRedundancyOf } from '../../../lib/redundancy'
 
 const serverFollowsRouter = express.Router()
 serverFollowsRouter.get('/following',
@@ -101,6 +102,10 @@ async function removeFollow (req: express.Request, res: express.Response, next:
     server.redundancyAllowed = false
     await server.save({ transaction: t })
 
+    // Async, could be long
+    removeRedundancyOf(server.id)
+      .catch(err => logger.error('Cannot remove redundancy of %s.', server.host, err))
+
     await follow.destroy({ transaction: t })
   })
 
index 4216b9e353835c57c46641c2ec757bd01b87f488..4140c49912595c242c17c3494644137ee9cfd2c4 100644 (file)
@@ -3,6 +3,8 @@ import { UserRight } from '../../../../shared/models/users'
 import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../../middlewares'
 import { updateServerRedundancyValidator } from '../../../middlewares/validators/redundancy'
 import { ServerModel } from '../../../models/server/server'
+import { removeRedundancyOf } from '../../../lib/redundancy'
+import { logger } from '../../../helpers/logger'
 
 const serverRedundancyRouter = express.Router()
 
@@ -28,5 +30,9 @@ async function updateRedundancy (req: express.Request, res: express.Response, ne
 
   await server.save()
 
+  // Async, could be long
+  removeRedundancyOf(server.id)
+    .catch(err => logger.error('Cannot remove redundancy of %s.', server.host, err))
+
   return res.sendStatus(204)
 }
index 16b1226585d76f277a65be40001c3f29ef497fed..04d3ded8fabec8b0bf0f0b171c01257875510744 100644 (file)
@@ -12,8 +12,17 @@ async function removeVideoRedundancy (videoRedundancy: VideoRedundancyModel, t?:
   await videoRedundancy.destroy({ transaction: t })
 }
 
+async function removeRedundancyOf (serverId: number) {
+  const videosRedundancy = await VideoRedundancyModel.listLocalOfServer(serverId)
+
+  for (const redundancy of videosRedundancy) {
+    await removeVideoRedundancy(redundancy)
+  }
+}
+
 // ---------------------------------------------------------------------------
 
 export {
+  removeRedundancyOf,
   removeVideoRedundancy
 }
index 3c87ec2c13470b70999a98ddf39d723d7c5a37bc..e6716480280c32f1ce89faf44f360f8fee62ee80 100644 (file)
@@ -286,6 +286,47 @@ export class VideoRedundancyModel extends Model<VideoRedundancyModel> {
     return VideoRedundancyModel.scope([ ScopeNames.WITH_VIDEO ]).findAll(query)
   }
 
+  static async listLocalOfServer (serverId: number) {
+    const actor = await getServerActor()
+
+    const query = {
+      where: {
+        actorId: actor.id
+      },
+      include: [
+        {
+          model: VideoFileModel,
+          required: true,
+          include: [
+            {
+              model: VideoModel,
+              required: true,
+              include: [
+                {
+                  attributes: [],
+                  model: VideoChannelModel.unscoped(),
+                  required: true,
+                  include: [
+                    {
+                      attributes: [],
+                      model: ActorModel.unscoped(),
+                      required: true,
+                      where: {
+                        serverId
+                      }
+                    }
+                  ]
+                }
+              ]
+            }
+          ]
+        }
+      ]
+    }
+
+    return VideoRedundancyModel.findAll(query)
+  }
+
   static async getStats (strategy: VideoRedundancyStrategy) {
     const actor = await getServerActor()
 
index 9b3b1b4ad5fdf78465c7aac06ca074cda93de806..e1709891d7de49adaae37f534d2f1a29b969005c 100644 (file)
@@ -12,7 +12,7 @@ import {
   killallServers, makeGetRequest,
   root,
   ServerInfo,
-  setAccessTokensToServers,
+  setAccessTokensToServers, unfollow,
   uploadVideo,
   viewVideo,
   wait
@@ -39,6 +39,8 @@ function checkMagnetWebseeds (file: { magnetUri: string, resolution: { id: numbe
     const found = parsed.urlList.find(url => url === `${ws}-${file.resolution.id}.mp4`)
     expect(found, `Webseed ${ws} not found in ${file.magnetUri} on server ${server.url}`).to.not.be.undefined
   }
+
+  expect(parsed.urlList).to.have.lengthOf(baseWebseeds.length)
 }
 
 async function runServers (strategy: VideoRedundancyStrategy, additionalParams: any = {}) {
@@ -136,23 +138,21 @@ async function check2Webseeds (strategy: VideoRedundancyStrategy, videoUUID?: st
   ]
 
   for (const server of servers) {
-    {
-      const res = await getVideo(server.url, videoUUID)
+    const res = await getVideo(server.url, videoUUID)
 
-      const video: VideoDetails = res.body
+    const video: VideoDetails = res.body
 
-      for (const file of video.files) {
-        checkMagnetWebseeds(file, webseeds, server)
-
-        // Only servers 1 and 2 have the video
-        if (server.serverNumber !== 3) {
-          await makeGetRequest({
-            url: server.url,
-            statusCodeExpected: 200,
-            path: '/static/webseed/' + `${videoUUID}-${file.resolution.id}.mp4`,
-            contentType: null
-          })
-        }
+    for (const file of video.files) {
+      checkMagnetWebseeds(file, webseeds, server)
+
+      // Only servers 1 and 2 have the video
+      if (server.serverNumber !== 3) {
+        await makeGetRequest({
+          url: server.url,
+          statusCodeExpected: 200,
+          path: '/static/webseed/' + `${videoUUID}-${file.resolution.id}.mp4`,
+          contentType: null
+        })
       }
     }
   }
@@ -182,6 +182,21 @@ async function enableRedundancyOnServer1 () {
   expect(server2.following.hostRedundancyAllowed).to.be.true
 }
 
+async function disableRedundancyOnServer1 () {
+  await updateRedundancy(servers[ 0 ].url, servers[ 0 ].accessToken, servers[ 1 ].host, false)
+
+  const res = await getFollowingListPaginationAndSort(servers[ 0 ].url, 0, 5, '-createdAt')
+  const follows: ActorFollow[] = res.body.data
+  const server2 = follows.find(f => f.following.host === 'localhost:9002')
+  const server3 = follows.find(f => f.following.host === 'localhost:9003')
+
+  expect(server3).to.not.be.undefined
+  expect(server3.following.hostRedundancyAllowed).to.be.false
+
+  expect(server2).to.not.be.undefined
+  expect(server2.following.hostRedundancyAllowed).to.be.false
+}
+
 async function cleanServers () {
   killallServers(servers)
 }
@@ -217,6 +232,17 @@ describe('Test videos redundancy', function () {
       await checkStatsWith2Webseed(strategy)
     })
 
+    it('Should undo redundancy on server 1 and remove duplicated videos', async function () {
+      this.timeout(40000)
+
+      await disableRedundancyOnServer1()
+
+      await waitJobs(servers)
+      await wait(5000)
+
+      await check1WebSeed(strategy)
+    })
+
     after(function () {
       return cleanServers()
     })
@@ -251,6 +277,17 @@ describe('Test videos redundancy', function () {
       await checkStatsWith2Webseed(strategy)
     })
 
+    it('Should unfollow on server 1 and remove duplicated videos', async function () {
+      this.timeout(40000)
+
+      await unfollow(servers[0].url, servers[0].accessToken, servers[1])
+
+      await waitJobs(servers)
+      await wait(5000)
+
+      await check1WebSeed(strategy)
+    })
+
     after(function () {
       return cleanServers()
     })