]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Basic video redundancy implementation
authorChocobozzz <me@florianbigard.com>
Tue, 11 Sep 2018 14:27:07 +0000 (16:27 +0200)
committerChocobozzz <me@florianbigard.com>
Thu, 13 Sep 2018 12:05:49 +0000 (14:05 +0200)
77 files changed:
client/src/app/+admin/admin.module.ts
client/src/app/+admin/follows/followers-list/followers-list.component.ts
client/src/app/+admin/follows/following-list/following-list.component.html
client/src/app/+admin/follows/following-list/following-list.component.scss [new file with mode: 0644]
client/src/app/+admin/follows/following-list/following-list.component.ts
client/src/app/+admin/follows/shared/follow.service.ts
client/src/app/+admin/follows/shared/redundancy-checkbox.component.html [new file with mode: 0644]
client/src/app/+admin/follows/shared/redundancy-checkbox.component.scss [new file with mode: 0644]
client/src/app/+admin/follows/shared/redundancy-checkbox.component.ts [new file with mode: 0644]
client/src/app/+admin/follows/shared/redundancy.service.ts [new file with mode: 0644]
config/default.yaml
config/production.yaml.example
config/test.yaml
package.json
server.ts
server/controllers/activitypub/client.ts
server/controllers/activitypub/outbox.ts
server/controllers/api/search.ts
server/controllers/api/server/follows.ts
server/controllers/api/server/index.ts
server/controllers/api/server/redundancy.ts [new file with mode: 0644]
server/controllers/api/videos/abuse.ts
server/helpers/activitypub.ts
server/helpers/custom-validators/activitypub/activity.ts
server/helpers/custom-validators/activitypub/cache-file.ts [new file with mode: 0644]
server/helpers/custom-validators/activitypub/misc.ts
server/helpers/custom-validators/activitypub/undo.ts
server/helpers/custom-validators/activitypub/videos.ts
server/helpers/webtorrent.ts
server/initializers/checker.ts
server/initializers/constants.ts
server/initializers/database.ts
server/initializers/migrations/0270-server-redundancy.ts [new file with mode: 0644]
server/lib/activitypub/actor.ts
server/lib/activitypub/cache-file.ts [new file with mode: 0644]
server/lib/activitypub/process/process-create.ts
server/lib/activitypub/process/process-undo.ts
server/lib/activitypub/process/process-update.ts
server/lib/activitypub/send/send-accept.ts
server/lib/activitypub/send/send-announce.ts
server/lib/activitypub/send/send-create.ts
server/lib/activitypub/send/send-delete.ts
server/lib/activitypub/send/send-follow.ts
server/lib/activitypub/send/send-like.ts
server/lib/activitypub/send/send-undo.ts
server/lib/activitypub/send/send-update.ts
server/lib/activitypub/send/utils.ts
server/lib/activitypub/url.ts
server/lib/activitypub/videos.ts
server/lib/redundancy.ts [new file with mode: 0644]
server/lib/schedulers/videos-redundancy-scheduler.ts [new file with mode: 0644]
server/middlewares/validators/redundancy.ts [new file with mode: 0644]
server/models/activitypub/actor-follow.ts
server/models/activitypub/actor.ts
server/models/redundancy/video-redundancy.ts [new file with mode: 0644]
server/models/server/server.ts
server/models/video/video-file.ts
server/models/video/video.ts
server/tests/api/check-params/follows.ts
server/tests/api/check-params/index.ts
server/tests/api/check-params/redundancy.ts [new file with mode: 0644]
server/tests/api/server/index.ts
server/tests/api/server/redundancy.ts [new file with mode: 0644]
server/tests/utils/server/follows.ts
server/tests/utils/server/redundancy.ts [new file with mode: 0644]
shared/models/activitypub/activity.ts
shared/models/activitypub/objects/cache-file-object.ts [new file with mode: 0644]
shared/models/activitypub/objects/common-objects.ts
shared/models/activitypub/objects/index.ts
shared/models/activitypub/objects/video-torrent-object.ts
shared/models/actors/follow.model.ts
shared/models/avatars/index.ts [new file with mode: 0644]
shared/models/index.ts
shared/models/redundancy/index.ts [new file with mode: 0644]
shared/models/redundancy/videos-redundancy.model.ts [new file with mode: 0644]
shared/models/users/user-right.enum.ts
yarn.lock

index e94aac1ebc2fa355f203e6381fe7f6cf26843f71..5784609efd25df332ee60f3bbaa46d7191f73031 100644 (file)
@@ -14,6 +14,8 @@ import { UserCreateComponent, UserListComponent, UsersComponent, UserService, Us
 import { ModerationCommentModalComponent, VideoAbuseListComponent, VideoBlacklistListComponent } from './moderation'
 import { UserBanModalComponent } from '@app/+admin/users/user-list/user-ban-modal.component'
 import { ModerationComponent } from '@app/+admin/moderation/moderation.component'
+import { RedundancyCheckboxComponent } from '@app/+admin/follows/shared/redundancy-checkbox.component'
+import { RedundancyService } from '@app/+admin/follows/shared/redundancy.service'
 
 @NgModule({
   imports: [
@@ -29,6 +31,7 @@ import { ModerationComponent } from '@app/+admin/moderation/moderation.component
     FollowingAddComponent,
     FollowersListComponent,
     FollowingListComponent,
+    RedundancyCheckboxComponent,
 
     UsersComponent,
     UserCreateComponent,
@@ -54,6 +57,7 @@ import { ModerationComponent } from '@app/+admin/moderation/moderation.component
 
   providers: [
     FollowService,
+    RedundancyService,
     UserService,
     JobService,
     ConfigService
index 96fb67588ddda45617e239bfd19c7839ab0ac54e..ca993dcd37e1b24a15f43adde1a821a7dcf66431 100644 (file)
@@ -2,7 +2,7 @@ import { Component, OnInit } from '@angular/core'
 
 import { NotificationsService } from 'angular2-notifications'
 import { SortMeta } from 'primeng/primeng'
-import { AccountFollow } from '../../../../../../shared/models/actors/follow.model'
+import { ActorFollow } from '../../../../../../shared/models/actors/follow.model'
 import { RestPagination, RestTable } from '../../../shared'
 import { FollowService } from '../shared'
 import { I18n } from '@ngx-translate/i18n-polyfill'
@@ -13,7 +13,7 @@ import { I18n } from '@ngx-translate/i18n-polyfill'
   styleUrls: [ './followers-list.component.scss' ]
 })
 export class FollowersListComponent extends RestTable implements OnInit {
-  followers: AccountFollow[] = []
+  followers: ActorFollow[] = []
   totalRecords = 0
   rowsPerPage = 10
   sort: SortMeta = { field: 'createdAt', order: 1 }
index e4a45e88c44e7a38d9f1bf7232e0da56aeb08702..66ab64c5055bb0d00eb3640913f664aec470a571 100644 (file)
@@ -8,6 +8,7 @@
       <th i18n>Host</th>
       <th i18n>State</th>
       <th i18n pSortableColumn="createdAt">Created <p-sortIcon field="createdAt"></p-sortIcon></th>
+      <th i18n>Redundancy allowed</th>
       <th></th>
     </tr>
   </ng-template>
       <td>{{ follow.following.host }}</td>
       <td>{{ follow.state }}</td>
       <td>{{ follow.createdAt }}</td>
+      <td>
+        <my-redundancy-checkbox
+          [host]="follow.following.host" [redundancyAllowed]="follow.following.hostRedundancyAllowed"
+        ></my-redundancy-checkbox>
+      </td>
       <td class="action-cell">
         <my-delete-button (click)="removeFollowing(follow)"></my-delete-button>
       </td>
diff --git a/client/src/app/+admin/follows/following-list/following-list.component.scss b/client/src/app/+admin/follows/following-list/following-list.component.scss
new file mode 100644 (file)
index 0000000..bfcdcaa
--- /dev/null
@@ -0,0 +1,13 @@
+@import '_variables';
+@import '_mixins';
+
+my-redundancy-checkbox /deep/ my-peertube-checkbox {
+  .form-group {
+    margin-bottom: 0;
+    align-items: center;
+  }
+
+  label {
+    margin: 0;
+  }
+}
\ No newline at end of file
index 06e341e6834419d15e286d4bf202dfcaf831cb92..dd57884c6445102b8e796be00f73e64ed55cf320 100644 (file)
@@ -1,7 +1,7 @@
 import { Component, OnInit } from '@angular/core'
 import { NotificationsService } from 'angular2-notifications'
 import { SortMeta } from 'primeng/primeng'
-import { AccountFollow } from '../../../../../../shared/models/actors/follow.model'
+import { ActorFollow } from '../../../../../../shared/models/actors/follow.model'
 import { ConfirmService } from '../../../core/confirm/confirm.service'
 import { RestPagination, RestTable } from '../../../shared'
 import { FollowService } from '../shared'
@@ -9,10 +9,11 @@ import { I18n } from '@ngx-translate/i18n-polyfill'
 
 @Component({
   selector: 'my-followers-list',
-  templateUrl: './following-list.component.html'
+  templateUrl: './following-list.component.html',
+  styleUrls: [ './following-list.component.scss' ]
 })
 export class FollowingListComponent extends RestTable implements OnInit {
-  following: AccountFollow[] = []
+  following: ActorFollow[] = []
   totalRecords = 0
   rowsPerPage = 10
   sort: SortMeta = { field: 'createdAt', order: 1 }
@@ -31,7 +32,7 @@ export class FollowingListComponent extends RestTable implements OnInit {
     this.loadSort()
   }
 
-  async removeFollowing (follow: AccountFollow) {
+  async removeFollowing (follow: ActorFollow) {
     const res = await this.confirmService.confirm(
       this.i18n('Do you really want to unfollow {{host}}?', { host: follow.following.host }),
       this.i18n('Unfollow')
index 87ea5fb0c69362e41c09bd679d4d996ce2e4eac3..27169a9cdb7d1f2b73a32f600ce6cb07f4eed620 100644 (file)
@@ -3,7 +3,7 @@ import { HttpClient, HttpParams } from '@angular/common/http'
 import { Injectable } from '@angular/core'
 import { SortMeta } from 'primeng/primeng'
 import { Observable } from 'rxjs'
-import { AccountFollow, ResultList } from '../../../../../../shared'
+import { ActorFollow, ResultList } from '../../../../../../shared'
 import { environment } from '../../../../environments/environment'
 import { RestExtractor, RestPagination, RestService } from '../../../shared'
 
@@ -18,22 +18,22 @@ export class FollowService {
   ) {
   }
 
-  getFollowing (pagination: RestPagination, sort: SortMeta): Observable<ResultList<AccountFollow>> {
+  getFollowing (pagination: RestPagination, sort: SortMeta): Observable<ResultList<ActorFollow>> {
     let params = new HttpParams()
     params = this.restService.addRestGetParams(params, pagination, sort)
 
-    return this.authHttp.get<ResultList<AccountFollow>>(FollowService.BASE_APPLICATION_URL + '/following', { params })
+    return this.authHttp.get<ResultList<ActorFollow>>(FollowService.BASE_APPLICATION_URL + '/following', { params })
                .pipe(
                  map(res => this.restExtractor.convertResultListDateToHuman(res)),
                  catchError(res => this.restExtractor.handleError(res))
                )
   }
 
-  getFollowers (pagination: RestPagination, sort: SortMeta): Observable<ResultList<AccountFollow>> {
+  getFollowers (pagination: RestPagination, sort: SortMeta): Observable<ResultList<ActorFollow>> {
     let params = new HttpParams()
     params = this.restService.addRestGetParams(params, pagination, sort)
 
-    return this.authHttp.get<ResultList<AccountFollow>>(FollowService.BASE_APPLICATION_URL + '/followers', { params })
+    return this.authHttp.get<ResultList<ActorFollow>>(FollowService.BASE_APPLICATION_URL + '/followers', { params })
                .pipe(
                  map(res => this.restExtractor.convertResultListDateToHuman(res)),
                  catchError(res => this.restExtractor.handleError(res))
@@ -52,7 +52,7 @@ export class FollowService {
                )
   }
 
-  unfollow (follow: AccountFollow) {
+  unfollow (follow: ActorFollow) {
     return this.authHttp.delete(FollowService.BASE_APPLICATION_URL + '/following/' + follow.following.host)
                .pipe(
                  map(this.restExtractor.extractDataBool),
diff --git a/client/src/app/+admin/follows/shared/redundancy-checkbox.component.html b/client/src/app/+admin/follows/shared/redundancy-checkbox.component.html
new file mode 100644 (file)
index 0000000..8a57d65
--- /dev/null
@@ -0,0 +1,3 @@
+<my-peertube-checkbox
+  [inputName]="host + '-redundancy-allowed'" [(ngModel)]="redundancyAllowed" (ngModelChange)="updateRedundancyState()"
+></my-peertube-checkbox>
\ No newline at end of file
diff --git a/client/src/app/+admin/follows/shared/redundancy-checkbox.component.scss b/client/src/app/+admin/follows/shared/redundancy-checkbox.component.scss
new file mode 100644 (file)
index 0000000..5e67747
--- /dev/null
@@ -0,0 +1,2 @@
+@import '_variables';
+@import '_mixins';
diff --git a/client/src/app/+admin/follows/shared/redundancy-checkbox.component.ts b/client/src/app/+admin/follows/shared/redundancy-checkbox.component.ts
new file mode 100644 (file)
index 0000000..ff4725e
--- /dev/null
@@ -0,0 +1,42 @@
+import { Component, Input } from '@angular/core'
+import { AuthService } from '@app/core'
+import { RestExtractor } from '@app/shared/rest'
+import { RedirectService } from '@app/core/routing/redirect.service'
+import { NotificationsService } from 'angular2-notifications'
+import { I18n } from '@ngx-translate/i18n-polyfill'
+import { RedundancyService } from '@app/+admin/follows/shared/redundancy.service'
+
+@Component({
+  selector: 'my-redundancy-checkbox',
+  templateUrl: './redundancy-checkbox.component.html',
+  styleUrls: [ './redundancy-checkbox.component.scss' ]
+})
+export class RedundancyCheckboxComponent {
+  @Input() redundancyAllowed: boolean
+  @Input() host: string
+
+  constructor (
+    private authService: AuthService,
+    private restExtractor: RestExtractor,
+    private redirectService: RedirectService,
+    private notificationsService: NotificationsService,
+    private redundancyService: RedundancyService,
+    private i18n: I18n
+  ) { }
+
+  updateRedundancyState () {
+    this.redundancyService.updateRedundancy(this.host, this.redundancyAllowed)
+      .subscribe(
+        () => {
+          const stateLabel = this.redundancyAllowed ? this.i18n('enabled') : this.i18n('disabled')
+
+          this.notificationsService.success(
+            this.i18n('Success'),
+            this.i18n('Redundancy for {{host}} is {{stateLabel}}', { host: this.host, stateLabel })
+          )
+        },
+
+          err => this.notificationsService.error(this.i18n('Error'), err.message)
+      )
+  }
+}
diff --git a/client/src/app/+admin/follows/shared/redundancy.service.ts b/client/src/app/+admin/follows/shared/redundancy.service.ts
new file mode 100644 (file)
index 0000000..96b29fa
--- /dev/null
@@ -0,0 +1,29 @@
+import { catchError, map } from 'rxjs/operators'
+import { HttpClient } from '@angular/common/http'
+import { Injectable } from '@angular/core'
+import { RestExtractor, RestService } from '@app/shared'
+import { environment } from '../../../../environments/environment'
+
+@Injectable()
+export class RedundancyService {
+  static BASE_USER_SUBSCRIPTIONS_URL = environment.apiUrl + '/api/v1/server/redundancy'
+
+  constructor (
+    private authHttp: HttpClient,
+    private restExtractor: RestExtractor,
+    private restService: RestService
+  ) { }
+
+  updateRedundancy (host: string, redundancyAllowed: boolean) {
+    const url = RedundancyService.BASE_USER_SUBSCRIPTIONS_URL + '/' + host
+
+    const body = { redundancyAllowed }
+
+    return this.authHttp.put(url, body)
+               .pipe(
+                 map(this.restExtractor.extractDataBool),
+                 catchError(err => this.restExtractor.handleError(err))
+               )
+  }
+
+}
index 40458bb3895fa32d41895b50d58d4def1442201c..af29a4379eda35712c51a1402ee7e07805452930 100644 (file)
@@ -66,6 +66,15 @@ trending:
   videos:
     interval_days: 7 # Compute trending videos for the last x days
 
+# Cache remote videos on your server, to help other instances to broadcast the video
+# You can define multiple caches using different sizes/strategies
+# Once you have defined your strategies, choose which instances you want to cache in admin -> manage follows -> following
+redundancy:
+  videos:
+#    -
+#      size: '10GB'
+#      strategy: 'most-views' # Cache videos that have the most views
+
 cache:
   previews:
     size: 500 # Max number of previews you want to cache
index d032e4a0c406c5ccb84ff5c23c1443633c50ac74..ddd43093f45000657421c39ffebb5cd2e07a97c9 100644 (file)
@@ -67,6 +67,15 @@ trending:
   videos:
     interval_days: 7 # Compute trending videos for the last x days
 
+# Cache remote videos on your server, to help other instances to broadcast the video
+# You can define multiple caches using different sizes/strategies
+# Once you have defined your strategies, choose which instances you want to cache in admin -> manage follows -> following
+redundancy:
+  videos:
+#    -
+#      size: '10GB'
+#      strategy: 'most-views' # Cache videos that have the most views
+
 ###############################################################################
 #
 # From this point, all the following keys can be overridden by the web interface
index 6a8e47aace48d2e25a495ee4f2a069c9c7a45cb4..0f280eabd1b0f156b5f93d8b7bc2ddee6d7fe985 100644 (file)
@@ -21,6 +21,12 @@ smtp:
 log:
   level: 'debug'
 
+redundancy:
+  videos:
+    -
+      size: '100KB'
+      strategy: 'most-views'
+
 cache:
   previews:
     size: 1
index fafe4b2966e3eff384e7fa89f0ccc2c5096c476c..5a8843b0c5f984f148a313f204b997e0f8f9b6f9 100644 (file)
@@ -86,6 +86,7 @@
     "bluebird": "^3.5.0",
     "body-parser": "^1.12.4",
     "bull": "^3.4.2",
+    "bytes": "^3.0.0",
     "commander": "^2.13.0",
     "concurrently": "^4.0.1",
     "config": "^2.0.1",
     "@types/bluebird": "3.5.21",
     "@types/body-parser": "^1.16.3",
     "@types/bull": "^3.3.12",
+    "@types/bytes": "^3.0.0",
     "@types/chai": "^4.0.4",
     "@types/chai-json-schema": "^1.4.3",
     "@types/chai-xml": "^0.3.1",
index 76d00edd3ae77ee0b2b6add58b1c461dbcb2a188..8bc5e5f320d53781584d0e1e18edd29bf3ce61a9 100644 (file)
--- a/server.ts
+++ b/server.ts
@@ -94,6 +94,7 @@ import { BadActorFollowScheduler } from './server/lib/schedulers/bad-actor-follo
 import { RemoveOldJobsScheduler } from './server/lib/schedulers/remove-old-jobs-scheduler'
 import { UpdateVideosScheduler } from './server/lib/schedulers/update-videos-scheduler'
 import { YoutubeDlUpdateScheduler } from './server/lib/schedulers/youtube-dl-update-scheduler'
+import { VideosRedundancyScheduler } from './server/lib/schedulers/videos-redundancy-scheduler'
 
 // ----------- Command line -----------
 
@@ -206,6 +207,7 @@ async function startApplication () {
   RemoveOldJobsScheduler.Instance.enable()
   UpdateVideosScheduler.Instance.enable()
   YoutubeDlUpdateScheduler.Instance.enable()
+  VideosRedundancyScheduler.Instance.enable()
 
   // Redis initialization
   Redis.Instance.init()
index 54cf44419f6f0a99b90e26b2387c9593e9bf929b..2e168ea78812c6876fa158e685ca60f09f72daa7 100644 (file)
@@ -3,9 +3,9 @@ import * as express from 'express'
 import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos'
 import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
 import { CONFIG, ROUTE_CACHE_LIFETIME } from '../../initializers'
-import { buildVideoAnnounce } from '../../lib/activitypub/send'
+import { buildAnnounceWithVideoAudience } from '../../lib/activitypub/send'
 import { audiencify, getAudience } from '../../lib/activitypub/audience'
-import { createActivityData } from '../../lib/activitypub/send/send-create'
+import { buildCreateActivity } from '../../lib/activitypub/send/send-create'
 import { asyncMiddleware, executeIfActivityPub, localAccountValidator, localVideoChannelValidator } from '../../middlewares'
 import { videosGetValidator, videosShareValidator } from '../../middlewares/validators'
 import { videoCommentGetValidator } from '../../middlewares/validators/video-comments'
@@ -26,6 +26,8 @@ import {
   getVideoSharesActivityPubUrl
 } from '../../lib/activitypub'
 import { VideoCaptionModel } from '../../models/video/video-caption'
+import { videoRedundancyGetValidator } from '../../middlewares/validators/redundancy'
+import { getServerActor } from '../../helpers/utils'
 
 const activityPubClientRouter = express.Router()
 
@@ -93,6 +95,11 @@ activityPubClientRouter.get('/video-channels/:name/following',
   executeIfActivityPub(asyncMiddleware(videoChannelFollowingController))
 )
 
+activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?',
+  executeIfActivityPub(asyncMiddleware(videoRedundancyGetValidator)),
+  executeIfActivityPub(asyncMiddleware(videoRedundancyController))
+)
+
 // ---------------------------------------------------------------------------
 
 export {
@@ -131,7 +138,7 @@ async function videoController (req: express.Request, res: express.Response, nex
   const videoObject = audiencify(video.toActivityPubObject(), audience)
 
   if (req.path.endsWith('/activity')) {
-    const data = createActivityData(video.url, video.VideoChannel.Account.Actor, videoObject, audience)
+    const data = buildCreateActivity(video.url, video.VideoChannel.Account.Actor, videoObject, audience)
     return activityPubResponse(activityPubContextify(data), res)
   }
 
@@ -140,9 +147,9 @@ async function videoController (req: express.Request, res: express.Response, nex
 
 async function videoAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) {
   const share = res.locals.videoShare as VideoShareModel
-  const object = await buildVideoAnnounce(share.Actor, share, res.locals.video, undefined)
+  const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.video, undefined)
 
-  return activityPubResponse(activityPubContextify(object), res)
+  return activityPubResponse(activityPubContextify(activity), res)
 }
 
 async function videoAnnouncesController (req: express.Request, res: express.Response, next: express.NextFunction) {
@@ -219,13 +226,28 @@ async function videoCommentController (req: express.Request, res: express.Respon
   const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience)
 
   if (req.path.endsWith('/activity')) {
-    const data = createActivityData(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience)
+    const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience)
     return activityPubResponse(activityPubContextify(data), res)
   }
 
   return activityPubResponse(activityPubContextify(videoCommentObject), res)
 }
 
+async function videoRedundancyController (req: express.Request, res: express.Response) {
+  const videoRedundancy = res.locals.videoRedundancy
+  const serverActor = await getServerActor()
+
+  const audience = getAudience(serverActor)
+  const object = audiencify(videoRedundancy.toActivityPubObject(), audience)
+
+  if (req.path.endsWith('/activity')) {
+    const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience)
+    return activityPubResponse(activityPubContextify(data), res)
+  }
+
+  return activityPubResponse(activityPubContextify(object), res)
+}
+
 // ---------------------------------------------------------------------------
 
 async function actorFollowing (req: express.Request, actor: ActorModel) {
index db69ae54b4c4d1fdc6a01dbcf9850a6c41095942..bd0e4fe9d471ba6966aaf465e66f28841336f30f 100644 (file)
@@ -3,7 +3,7 @@ import { Activity } from '../../../shared/models/activitypub/activity'
 import { VideoPrivacy } from '../../../shared/models/videos'
 import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
 import { logger } from '../../helpers/logger'
-import { announceActivityData, createActivityData } from '../../lib/activitypub/send'
+import { buildAnnounceActivity, buildCreateActivity } from '../../lib/activitypub/send'
 import { buildAudience } from '../../lib/activitypub/audience'
 import { asyncMiddleware, localAccountValidator, localVideoChannelValidator } from '../../middlewares'
 import { AccountModel } from '../../models/account/account'
@@ -60,12 +60,12 @@ async function buildActivities (actor: ActorModel, start: number, count: number)
     // This is a shared video
     if (video.VideoShares !== undefined && video.VideoShares.length !== 0) {
       const videoShare = video.VideoShares[0]
-      const announceActivity = announceActivityData(videoShare.url, actor, video.url, createActivityAudience)
+      const announceActivity = buildAnnounceActivity(videoShare.url, actor, video.url, createActivityAudience)
 
       activities.push(announceActivity)
     } else {
       const videoObject = video.toActivityPubObject()
-      const createActivity = createActivityData(video.url, byActor, videoObject, createActivityAudience)
+      const createActivity = buildCreateActivity(video.url, byActor, videoObject, createActivityAudience)
 
       activities.push(createActivity)
     }
index bb7174891e2b02c39246c5f2f01a9a73ef9f9777..28a7a04cadc461142e1a5e13841ad7f02c827ef0 100644 (file)
@@ -17,8 +17,6 @@ import {
 import { VideoChannelsSearchQuery, VideosSearchQuery } from '../../../shared/models/search'
 import { getOrCreateActorAndServerAndModel, getOrCreateVideoAndAccountAndChannel } from '../../lib/activitypub'
 import { logger } from '../../helpers/logger'
-import { User } from '../../../shared/models/users'
-import { CONFIG } from '../../initializers/constants'
 import { VideoChannelModel } from '../../models/video/video-channel'
 import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
 
index 23308445f746dd419182d8af7ef216e7f5437185..a4eae6b45b69df0288d733b1cd993050119c81c7 100644 (file)
@@ -96,6 +96,11 @@ async function removeFollow (req: express.Request, res: express.Response, next:
   await sequelizeTypescript.transaction(async t => {
     if (follow.state === 'accepted') await sendUndoFollow(follow, t)
 
+    // Disable redundancy on unfollowed instances
+    const server = follow.ActorFollowing.Server
+    server.redundancyAllowed = false
+    await server.save({ transaction: t })
+
     await follow.destroy({ transaction: t })
   })
 
index 850a52cdb045b1ad1c68a9f75dc98c023cdd6095..43bca2c10945b4be8fb83a18a8f9c5224212784c 100644 (file)
@@ -1,10 +1,12 @@
 import * as express from 'express'
 import { serverFollowsRouter } from './follows'
 import { statsRouter } from './stats'
+import { serverRedundancyRouter } from './redundancy'
 
 const serverRouter = express.Router()
 
 serverRouter.use('/', serverFollowsRouter)
+serverRouter.use('/', serverRedundancyRouter)
 serverRouter.use('/', statsRouter)
 
 // ---------------------------------------------------------------------------
diff --git a/server/controllers/api/server/redundancy.ts b/server/controllers/api/server/redundancy.ts
new file mode 100644 (file)
index 0000000..4216b9e
--- /dev/null
@@ -0,0 +1,32 @@
+import * as express from 'express'
+import { UserRight } from '../../../../shared/models/users'
+import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../../middlewares'
+import { updateServerRedundancyValidator } from '../../../middlewares/validators/redundancy'
+import { ServerModel } from '../../../models/server/server'
+
+const serverRedundancyRouter = express.Router()
+
+serverRedundancyRouter.put('/redundancy/:host',
+  authenticate,
+  ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW),
+  asyncMiddleware(updateServerRedundancyValidator),
+  asyncMiddleware(updateRedundancy)
+)
+
+// ---------------------------------------------------------------------------
+
+export {
+  serverRedundancyRouter
+}
+
+// ---------------------------------------------------------------------------
+
+async function updateRedundancy (req: express.Request, res: express.Response, next: express.NextFunction) {
+  const server = res.locals.server as ServerModel
+
+  server.redundancyAllowed = req.body.redundancyAllowed
+
+  await server.save()
+
+  return res.sendStatus(204)
+}
index 59bdf6257c3282302d594be51e376a2e5a0b48b1..08e11b00bd930e5b0e604be68f7d3e73a454f179 100644 (file)
@@ -112,7 +112,7 @@ async function reportVideoAbuse (req: express.Request, res: express.Response) {
 
     // We send the video abuse to the origin server
     if (videoInstance.isOwned() === false) {
-      await sendVideoAbuse(reporterAccount.Actor, videoAbuseInstance, videoInstance, t)
+      await sendVideoAbuse(reporterAccount.Actor, videoAbuseInstance, videoInstance)
     }
 
     auditLogger.create(reporterAccount.Actor.getIdentifier(), new VideoAbuseAuditView(videoAbuseInstance.toFormattedJSON()))
index a9de11fb0984ff8b76f95bb84259b9c6b5a03b71..1304c7559d0c4b302bebebfc9731696826d18161 100644 (file)
@@ -14,20 +14,24 @@ function activityPubContextify <T> (data: T) {
       'https://w3id.org/security/v1',
       {
         RsaSignature2017: 'https://w3id.org/security#RsaSignature2017',
+        pt: 'https://joinpeertube.org/ns',
+        schema: 'http://schema.org#',
         Hashtag: 'as:Hashtag',
-        uuid: 'http://schema.org/identifier',
-        category: 'http://schema.org/category',
-        licence: 'http://schema.org/license',
-        subtitleLanguage: 'http://schema.org/subtitleLanguage',
+        uuid: 'schema:identifier',
+        category: 'schema:category',
+        licence: 'schema:license',
+        subtitleLanguage: 'schema:subtitleLanguage',
         sensitive: 'as:sensitive',
-        language: 'http://schema.org/inLanguage',
-        views: 'http://schema.org/Number',
-        stats: 'http://schema.org/Number',
-        size: 'http://schema.org/Number',
-        fps: 'http://schema.org/Number',
-        commentsEnabled: 'http://schema.org/Boolean',
-        waitTranscoding: 'http://schema.org/Boolean',
-        support: 'http://schema.org/Text'
+        language: 'schema:inLanguage',
+        views: 'schema:Number',
+        stats: 'schema:Number',
+        size: 'schema:Number',
+        fps: 'schema:Number',
+        commentsEnabled: 'schema:Boolean',
+        waitTranscoding: 'schema:Boolean',
+        expires: 'schema:expires',
+        support: 'schema:Text',
+        CacheFile: 'pt:CacheFile'
       },
       {
         likes: {
index 381a29e66b236e9407dfc6abd6daf2e827a18bc2..2562ead9b874549317b3ebc0b758a84ebc0f918e 100644 (file)
@@ -1,7 +1,10 @@
 import * as validator from 'validator'
 import { Activity, ActivityType } from '../../../../shared/models/activitypub'
 import {
-  isActorAcceptActivityValid, isActorDeleteActivityValid, isActorFollowActivityValid, isActorRejectActivityValid,
+  isActorAcceptActivityValid,
+  isActorDeleteActivityValid,
+  isActorFollowActivityValid,
+  isActorRejectActivityValid,
   isActorUpdateActivityValid
 } from './actor'
 import { isAnnounceActivityValid } from './announce'
@@ -11,12 +14,13 @@ import { isUndoActivityValid } from './undo'
 import { isVideoCommentCreateActivityValid, isVideoCommentDeleteActivityValid } from './video-comments'
 import {
   isVideoFlagValid,
-  sanitizeAndCheckVideoTorrentCreateActivity,
   isVideoTorrentDeleteActivityValid,
+  sanitizeAndCheckVideoTorrentCreateActivity,
   sanitizeAndCheckVideoTorrentUpdateActivity
 } from './videos'
 import { isViewActivityValid } from './view'
 import { exists } from '../misc'
+import { isCacheFileCreateActivityValid, isCacheFileUpdateActivityValid } from './cache-file'
 
 function isRootActivityValid (activity: any) {
   return Array.isArray(activity['@context']) && (
@@ -67,11 +71,13 @@ function checkCreateActivity (activity: any) {
     isDislikeActivityValid(activity) ||
     sanitizeAndCheckVideoTorrentCreateActivity(activity) ||
     isVideoFlagValid(activity) ||
-    isVideoCommentCreateActivityValid(activity)
+    isVideoCommentCreateActivityValid(activity) ||
+    isCacheFileCreateActivityValid(activity)
 }
 
 function checkUpdateActivity (activity: any) {
-  return sanitizeAndCheckVideoTorrentUpdateActivity(activity) ||
+  return isCacheFileUpdateActivityValid(activity) ||
+    sanitizeAndCheckVideoTorrentUpdateActivity(activity) ||
     isActorUpdateActivityValid(activity)
 }
 
diff --git a/server/helpers/custom-validators/activitypub/cache-file.ts b/server/helpers/custom-validators/activitypub/cache-file.ts
new file mode 100644 (file)
index 0000000..bd70934
--- /dev/null
@@ -0,0 +1,28 @@
+import { isActivityPubUrlValid, isBaseActivityValid } from './misc'
+import { isRemoteVideoUrlValid } from './videos'
+import { isDateValid, exists } from '../misc'
+import { CacheFileObject } from '../../../../shared/models/activitypub/objects'
+
+function isCacheFileCreateActivityValid (activity: any) {
+  return isBaseActivityValid(activity, 'Create') &&
+    isCacheFileObjectValid(activity.object)
+}
+
+function isCacheFileUpdateActivityValid (activity: any) {
+  return isBaseActivityValid(activity, 'Update') &&
+    isCacheFileObjectValid(activity.object)
+}
+
+function isCacheFileObjectValid (object: CacheFileObject) {
+  return exists(object) &&
+    object.type === 'CacheFile' &&
+    isDateValid(object.expires) &&
+    isActivityPubUrlValid(object.object) &&
+    isRemoteVideoUrlValid(object.url)
+}
+
+export {
+  isCacheFileUpdateActivityValid,
+  isCacheFileCreateActivityValid,
+  isCacheFileObjectValid
+}
index 6c5c7abca671efc61035cd99d91dcb9f196e488a..4e2c57f04814b02c3776d93b2a3fccaf1107258e 100644 (file)
@@ -3,7 +3,7 @@ import { CONSTRAINTS_FIELDS } from '../../../initializers'
 import { isTestInstance } from '../../core-utils'
 import { exists } from '../misc'
 
-function isActivityPubUrlValid (url: string) {
+function isUrlValid (url: string) {
   const isURLOptions = {
     require_host: true,
     require_tld: true,
@@ -17,13 +17,18 @@ function isActivityPubUrlValid (url: string) {
     isURLOptions.require_tld = false
   }
 
-  return exists(url) && validator.isURL('' + url, isURLOptions) && validator.isLength('' + url, CONSTRAINTS_FIELDS.ACTORS.URL)
+  return exists(url) && validator.isURL('' + url, isURLOptions)
+}
+
+function isActivityPubUrlValid (url: string) {
+  return isUrlValid(url) && validator.isLength('' + url, CONSTRAINTS_FIELDS.ACTORS.URL)
 }
 
 function isBaseActivityValid (activity: any, type: string) {
   return (activity['@context'] === undefined || Array.isArray(activity['@context'])) &&
     activity.type === type &&
     isActivityPubUrlValid(activity.id) &&
+    exists(activity.actor) &&
     (isActivityPubUrlValid(activity.actor) || isActivityPubUrlValid(activity.actor.id)) &&
     (
       activity.to === undefined ||
@@ -49,6 +54,7 @@ function setValidAttributedTo (obj: any) {
 }
 
 export {
+  isUrlValid,
   isActivityPubUrlValid,
   isBaseActivityValid,
   setValidAttributedTo
index f50f224fa433539017561d2f664dd40b52802d68..578035893b61a55065784d79ea961f84141b442e 100644 (file)
@@ -2,6 +2,7 @@ import { isActorFollowActivityValid } from './actor'
 import { isBaseActivityValid } from './misc'
 import { isDislikeActivityValid, isLikeActivityValid } from './rate'
 import { isAnnounceActivityValid } from './announce'
+import { isCacheFileCreateActivityValid } from './cache-file'
 
 function isUndoActivityValid (activity: any) {
   return isBaseActivityValid(activity, 'Undo') &&
@@ -9,7 +10,8 @@ function isUndoActivityValid (activity: any) {
       isActorFollowActivityValid(activity.object) ||
       isLikeActivityValid(activity.object) ||
       isDislikeActivityValid(activity.object) ||
-      isAnnounceActivityValid(activity.object)
+      isAnnounceActivityValid(activity.object) ||
+      isCacheFileCreateActivityValid(activity.object)
     )
 }
 
index 0362f43ab2b7a3c521d0497e85ad0d9677671d26..f76eba47482ba717c16a9fe935e4e27524f8ac4a 100644 (file)
@@ -75,6 +75,30 @@ function sanitizeAndCheckVideoTorrentObject (video: any) {
     video.attributedTo.length !== 0
 }
 
+function isRemoteVideoUrlValid (url: any) {
+  // FIXME: Old bug, we used the width to represent the resolution. Remove it in a few release (currently beta.11)
+  if (url.width && !url.height) url.height = url.width
+
+  return url.type === 'Link' &&
+    (
+      ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mimeType) !== -1 &&
+      isActivityPubUrlValid(url.href) &&
+      validator.isInt(url.height + '', { min: 0 }) &&
+      validator.isInt(url.size + '', { min: 0 }) &&
+      (!url.fps || validator.isInt(url.fps + '', { min: 0 }))
+    ) ||
+    (
+      ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mimeType) !== -1 &&
+      isActivityPubUrlValid(url.href) &&
+      validator.isInt(url.height + '', { min: 0 })
+    ) ||
+    (
+      ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.indexOf(url.mimeType) !== -1 &&
+      validator.isLength(url.href, { min: 5 }) &&
+      validator.isInt(url.height + '', { min: 0 })
+    )
+}
+
 // ---------------------------------------------------------------------------
 
 export {
@@ -83,7 +107,8 @@ export {
   isVideoTorrentDeleteActivityValid,
   isRemoteStringIdentifierValid,
   isVideoFlagValid,
-  sanitizeAndCheckVideoTorrentObject
+  sanitizeAndCheckVideoTorrentObject,
+  isRemoteVideoUrlValid
 }
 
 // ---------------------------------------------------------------------------
@@ -147,26 +172,4 @@ function setRemoteVideoTruncatedContent (video: any) {
   return true
 }
 
-function isRemoteVideoUrlValid (url: any) {
-  // FIXME: Old bug, we used the width to represent the resolution. Remove it in a few realease (currently beta.11)
-  if (url.width && !url.height) url.height = url.width
 
-  return url.type === 'Link' &&
-    (
-      ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.indexOf(url.mimeType) !== -1 &&
-      isActivityPubUrlValid(url.href) &&
-      validator.isInt(url.height + '', { min: 0 }) &&
-      validator.isInt(url.size + '', { min: 0 }) &&
-      (!url.fps || validator.isInt(url.fps + '', { min: 0 }))
-    ) ||
-    (
-      ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.indexOf(url.mimeType) !== -1 &&
-      isActivityPubUrlValid(url.href) &&
-      validator.isInt(url.height + '', { min: 0 })
-    ) ||
-    (
-      ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.indexOf(url.mimeType) !== -1 &&
-      validator.isLength(url.href, { min: 5 }) &&
-      validator.isInt(url.height + '', { min: 0 })
-    )
-}
index 1c0cc7058a4389ed31c3749f24310b5a422f963a..2fdfd18760d2d54616b841926d621b4db7b0d28e 100644 (file)
@@ -5,44 +5,49 @@ import { createWriteStream, remove } from 'fs-extra'
 import { CONFIG } from '../initializers'
 import { join } from 'path'
 
-function downloadWebTorrentVideo (target: { magnetUri: string, torrentName: string }) {
+function downloadWebTorrentVideo (target: { magnetUri: string, torrentName?: string }, timeout?: number) {
   const id = target.magnetUri || target.torrentName
+  let timer
 
   const path = generateVideoTmpPath(id)
   logger.info('Importing torrent video %s', id)
 
   return new Promise<string>((res, rej) => {
     const webtorrent = new WebTorrent()
+    let file: WebTorrent.TorrentFile
 
     const torrentId = target.magnetUri || join(CONFIG.STORAGE.TORRENTS_DIR, target.torrentName)
 
     const options = { path: CONFIG.STORAGE.VIDEOS_DIR }
     const torrent = webtorrent.add(torrentId, options, torrent => {
-      if (torrent.files.length !== 1) return rej(new Error('The number of files is not equal to 1 for ' + torrentId))
+      if (torrent.files.length !== 1) {
+        if (timer) clearTimeout(timer)
 
-      const file = torrent.files[ 0 ]
+        return safeWebtorrentDestroy(webtorrent, torrentId, file.name, target.torrentName)
+          .then(() => rej(new Error('The number of files is not equal to 1 for ' + torrentId)))
+      }
+
+      file = torrent.files[ 0 ]
 
       const writeStream = createWriteStream(path)
       writeStream.on('finish', () => {
-        webtorrent.destroy(async err => {
-          if (err) return rej(err)
-
-          if (target.torrentName) {
-            remove(torrentId)
-              .catch(err => logger.error('Cannot remove torrent %s in webtorrent download.', torrentId, { err }))
-          }
+        if (timer) clearTimeout(timer)
 
-          remove(join(CONFIG.STORAGE.VIDEOS_DIR, file.name))
-            .catch(err => logger.error('Cannot remove torrent file %s in webtorrent download.', file.name, { err }))
-
-          res(path)
-        })
+        return safeWebtorrentDestroy(webtorrent, torrentId, file.name, target.torrentName)
+          .then(() => res(path))
       })
 
       file.createReadStream().pipe(writeStream)
     })
 
     torrent.on('error', err => rej(err))
+
+    if (timeout) {
+      timer = setTimeout(async () => {
+        return safeWebtorrentDestroy(webtorrent, torrentId, file ? file.name : undefined, target.torrentName)
+          .then(() => rej(new Error('Webtorrent download timeout.')))
+      }, timeout)
+    }
   })
 }
 
@@ -51,3 +56,29 @@ function downloadWebTorrentVideo (target: { magnetUri: string, torrentName: stri
 export {
   downloadWebTorrentVideo
 }
+
+// ---------------------------------------------------------------------------
+
+function safeWebtorrentDestroy (webtorrent: WebTorrent.Instance, torrentId: string, filename?: string, torrentName?: string) {
+  return new Promise(res => {
+    webtorrent.destroy(err => {
+      // Delete torrent file
+      if (torrentName) {
+        remove(torrentId)
+          .catch(err => logger.error('Cannot remove torrent %s in webtorrent download.', torrentId, { err }))
+      }
+
+      // Delete downloaded file
+      if (filename) {
+        remove(join(CONFIG.STORAGE.VIDEOS_DIR, filename))
+          .catch(err => logger.error('Cannot remove torrent file %s in webtorrent download.', filename, { err }))
+      }
+
+      if (err) {
+        logger.warn('Cannot destroy webtorrent in timeout.', { err })
+      }
+
+      return res()
+    })
+  })
+}
index 9dd104035f8f4991519793035dd5752692a80530..6a2badd3580e4c7b4c4b36d260966bde5df9d5c4 100644 (file)
@@ -7,6 +7,9 @@ import { parse } from 'url'
 import { CONFIG } from './constants'
 import { logger } from '../helpers/logger'
 import { getServerActor } from '../helpers/utils'
+import { VideosRedundancy } from '../../shared/models/redundancy'
+import { isArray } from '../helpers/custom-validators/misc'
+import { uniq } from 'lodash'
 
 async function checkActivityPubUrls () {
   const actor = await getServerActor()
@@ -35,6 +38,20 @@ function checkConfig () {
     return 'NSFW policy setting should be "do_not_list" or "blur" or "display" instead of ' + defaultNSFWPolicy
   }
 
+  const redundancyVideos = config.get<VideosRedundancy[]>('redundancy.videos')
+  if (isArray(redundancyVideos)) {
+    for (const r of redundancyVideos) {
+      if ([ 'most-views' ].indexOf(r.strategy) === -1) {
+        return 'Redundancy video entries should have "most-views" strategy instead of ' + r.strategy
+      }
+    }
+
+    const filtered = uniq(redundancyVideos.map(r => r.strategy))
+    if (filtered.length !== redundancyVideos.length) {
+      return 'Redundancy video entries should have uniq strategies'
+    }
+  }
+
   return null
 }
 
index 5b7ea5d6c0a78d1e9a0a350913fe65b0504e8c28..6b4afbfd8a1e6f42ab4a477b306cc00076afc922 100644 (file)
@@ -1,6 +1,6 @@
 import { IConfig } from 'config'
 import { dirname, join } from 'path'
-import { JobType, VideoRateType, VideoState } from '../../shared/models'
+import { JobType, VideoRateType, VideoRedundancyStrategy, VideoState, VideosRedundancy } from '../../shared/models'
 import { ActivityPubActorType } from '../../shared/models/activitypub'
 import { FollowState } from '../../shared/models/actors'
 import { VideoAbuseState, VideoImportState, VideoPrivacy } from '../../shared/models/videos'
@@ -9,13 +9,14 @@ import { buildPath, isTestInstance, root, sanitizeHost, sanitizeUrl } from '../h
 import { NSFWPolicyType } from '../../shared/models/videos/nsfw-policy.type'
 import { invert } from 'lodash'
 import { CronRepeatOptions, EveryRepeatOptions } from 'bull'
+import * as bytes from 'bytes'
 
 // Use a variable to reload the configuration if we need
 let config: IConfig = require('config')
 
 // ---------------------------------------------------------------------------
 
-const LAST_MIGRATION_VERSION = 265
+const LAST_MIGRATION_VERSION = 270
 
 // ---------------------------------------------------------------------------
 
@@ -137,7 +138,8 @@ let SCHEDULER_INTERVALS_MS = {
   badActorFollow: 60000 * 60, // 1 hour
   removeOldJobs: 60000 * 60, // 1 hour
   updateVideos: 60000, // 1 minute
-  youtubeDLUpdate: 60000 * 60 * 24 // 1 day
+  youtubeDLUpdate: 60000 * 60 * 24, // 1 day
+  videosRedundancy: 60000 * 2 // 2 hours
 }
 
 // ---------------------------------------------------------------------------
@@ -208,6 +210,9 @@ const CONFIG = {
       INTERVAL_DAYS: config.get<number>('trending.videos.interval_days')
     }
   },
+  REDUNDANCY: {
+    VIDEOS: buildVideosRedundancy(config.get<any[]>('redundancy.videos'))
+  },
   ADMIN: {
     get EMAIL () { return config.get<string>('admin.email') }
   },
@@ -321,6 +326,9 @@ const CONSTRAINTS_FIELDS = {
       }
     }
   },
+  VIDEOS_REDUNDANCY: {
+    URL: { min: 3, max: 2000 } // Length
+  },
   VIDEOS: {
     NAME: { min: 3, max: 120 }, // Length
     LANGUAGE: { min: 1, max: 10 }, // Length
@@ -584,6 +592,13 @@ const CACHE = {
   }
 }
 
+const REDUNDANCY = {
+  VIDEOS: {
+    EXPIRES_AFTER_MS: 48 * 3600 * 1000, // 2 days
+    RANDOMIZED_FACTOR: 5
+  }
+}
+
 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
 
 // ---------------------------------------------------------------------------
@@ -629,8 +644,11 @@ if (isTestInstance() === true) {
   SCHEDULER_INTERVALS_MS.badActorFollow = 10000
   SCHEDULER_INTERVALS_MS.removeOldJobs = 10000
   SCHEDULER_INTERVALS_MS.updateVideos = 5000
+  SCHEDULER_INTERVALS_MS.videosRedundancy = 5000
   REPEAT_JOBS['videos-views'] = { every: 5000 }
 
+  REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR = 1
+
   VIDEO_VIEW_LIFETIME = 1000 // 1 second
 
   JOB_ATTEMPTS['email'] = 1
@@ -653,6 +671,7 @@ export {
   CONFIG,
   CONSTRAINTS_FIELDS,
   EMBED_SIZE,
+  REDUNDANCY,
   JOB_CONCURRENCY,
   JOB_ATTEMPTS,
   LAST_MIGRATION_VERSION,
@@ -722,6 +741,17 @@ function updateWebserverConfig () {
   CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
 }
 
+function buildVideosRedundancy (objs: { strategy: VideoRedundancyStrategy, size: string }[]): VideosRedundancy[] {
+  if (!objs) return []
+
+  return objs.map(obj => {
+    return {
+      strategy: obj.strategy,
+      size: bytes.parse(obj.size)
+    }
+  })
+}
+
 function buildLanguages () {
   const iso639 = require('iso-639-3')
 
index b68e1a882a26ed4d8f04071646fd6e89dac94350..4d57bf8aa10bc3c068364d1df2b65e9fbc0add07 100644 (file)
@@ -27,6 +27,7 @@ import { VideoCaptionModel } from '../models/video/video-caption'
 import { VideoImportModel } from '../models/video/video-import'
 import { VideoViewModel } from '../models/video/video-views'
 import { VideoChangeOwnershipModel } from '../models/video/video-change-ownership'
+import { VideoRedundancyModel } from '../models/redundancy/video-redundancy'
 
 require('pg').defaults.parseInt8 = true // Avoid BIGINT to be converted to string
 
@@ -87,7 +88,8 @@ async function initDatabaseModels (silent: boolean) {
     VideoCommentModel,
     ScheduleVideoUpdateModel,
     VideoImportModel,
-    VideoViewModel
+    VideoViewModel,
+    VideoRedundancyModel
   ])
 
   // Check extensions exist in the database
diff --git a/server/initializers/migrations/0270-server-redundancy.ts b/server/initializers/migrations/0270-server-redundancy.ts
new file mode 100644 (file)
index 0000000..903ba8a
--- /dev/null
@@ -0,0 +1,24 @@
+import * as Sequelize from 'sequelize'
+
+async function up (utils: {
+  transaction: Sequelize.Transaction
+  queryInterface: Sequelize.QueryInterface
+  sequelize: Sequelize.Sequelize
+}): Promise<any> {
+  {
+    const data = {
+      type: Sequelize.BOOLEAN,
+      allowNull: false,
+      defaultValue: false
+    }
+
+    await utils.queryInterface.addColumn('server', 'redundancyAllowed', data)
+  }
+
+}
+
+function down (options) {
+  throw new Error('Not implemented.')
+}
+
+export { up, down }
index 1657262d71904845c391481240db3c7d8a3cef1a..3464add0311e2c6410d22f45a9ac1e66a50ac150 100644 (file)
@@ -400,17 +400,15 @@ async function refreshActorIfNeeded (actor: ActorModel): Promise<{ actor: ActorM
       await actor.save({ transaction: t })
 
       if (actor.Account) {
-        await actor.save({ transaction: t })
-
         actor.Account.set('name', result.name)
         actor.Account.set('description', result.summary)
+
         await actor.Account.save({ transaction: t })
       } else if (actor.VideoChannel) {
-        await actor.save({ transaction: t })
-
         actor.VideoChannel.set('name', result.name)
         actor.VideoChannel.set('description', result.summary)
         actor.VideoChannel.set('support', result.support)
+
         await actor.VideoChannel.save({ transaction: t })
       }
 
diff --git a/server/lib/activitypub/cache-file.ts b/server/lib/activitypub/cache-file.ts
new file mode 100644 (file)
index 0000000..7325ddc
--- /dev/null
@@ -0,0 +1,47 @@
+import { CacheFileObject } from '../../../shared/index'
+import { VideoModel } from '../../models/video/video'
+import { ActorModel } from '../../models/activitypub/actor'
+import { sequelizeTypescript } from '../../initializers'
+import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
+
+function cacheFileActivityObjectToDBAttributes (cacheFileObject: CacheFileObject, video: VideoModel, byActor: ActorModel) {
+  const url = cacheFileObject.url
+
+  const videoFile = video.VideoFiles.find(f => {
+    return f.resolution === url.height && f.fps === url.fps
+  })
+
+  if (!videoFile) throw new Error(`Cannot find video file ${url.height} ${url.fps} of video ${video.url}`)
+
+  return {
+    expiresOn: new Date(cacheFileObject.expires),
+    url: cacheFileObject.id,
+    fileUrl: cacheFileObject.url.href,
+    strategy: null,
+    videoFileId: videoFile.id,
+    actorId: byActor.id
+  }
+}
+
+function createCacheFile (cacheFileObject: CacheFileObject, video: VideoModel, byActor: ActorModel) {
+  return sequelizeTypescript.transaction(async t => {
+    const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor)
+
+    return VideoRedundancyModel.create(attributes, { transaction: t })
+  })
+}
+
+function updateCacheFile (cacheFileObject: CacheFileObject, redundancyModel: VideoRedundancyModel, byActor: ActorModel) {
+  const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, redundancyModel.VideoFile.Video, byActor)
+
+  redundancyModel.set('expires', attributes.expiresOn)
+  redundancyModel.set('fileUrl', attributes.fileUrl)
+
+  return redundancyModel.save()
+}
+
+export {
+  createCacheFile,
+  updateCacheFile,
+  cacheFileActivityObjectToDBAttributes
+}
index 16f426e23e3379d1dee3207e4b3815d24486e0c0..32e555acf6cb209f47529bf4754cf09610c33480 100644 (file)
@@ -1,4 +1,4 @@
-import { ActivityCreate, VideoAbuseState, VideoTorrentObject } from '../../../../shared'
+import { ActivityCreate, CacheFileObject, VideoAbuseState, VideoTorrentObject } from '../../../../shared'
 import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects'
 import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
 import { retryTransactionWrapper } from '../../../helpers/database-utils'
@@ -12,6 +12,7 @@ import { addVideoComment, resolveThread } from '../video-comments'
 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
 import { forwardActivity, forwardVideoRelatedActivity } from '../send/utils'
 import { Redis } from '../../redis'
+import { createCacheFile } from '../cache-file'
 
 async function processCreateActivity (activity: ActivityCreate) {
   const activityObject = activity.object
@@ -28,6 +29,8 @@ async function processCreateActivity (activity: ActivityCreate) {
     return retryTransactionWrapper(processCreateVideoAbuse, actor, activityObject as VideoAbuseObject)
   } else if (activityType === 'Note') {
     return retryTransactionWrapper(processCreateVideoComment, actor, activity)
+  } else if (activityType === 'CacheFile') {
+    return retryTransactionWrapper(processCacheFile, actor, activity)
   }
 
   logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
@@ -97,6 +100,20 @@ async function processCreateView (byActor: ActorModel, activity: ActivityCreate)
   }
 }
 
+async function processCacheFile (byActor: ActorModel, activity: ActivityCreate) {
+  const cacheFile = activity.object as CacheFileObject
+
+  const { video } = await getOrCreateVideoAndAccountAndChannel(cacheFile.object)
+
+  await createCacheFile(cacheFile, video, byActor)
+
+  if (video.isOwned()) {
+    // Don't resend the activity to the sender
+    const exceptions = [ byActor ]
+    await forwardActivity(activity, undefined, exceptions)
+  }
+}
+
 async function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
   logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
 
@@ -113,7 +130,7 @@ async function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateDat
       state: VideoAbuseState.PENDING
     }
 
-    await VideoAbuseModel.create(videoAbuseData)
+    await VideoAbuseModel.create(videoAbuseData, { transaction: t })
 
     logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)
   })
index 1c1de8827c9da037708111eb8af73981ab68c11c..0eb5fa392161f30ad31283218bcfe6568f4d454f 100644 (file)
@@ -1,4 +1,4 @@
-import { ActivityAnnounce, ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub'
+import { ActivityAnnounce, ActivityFollow, ActivityLike, ActivityUndo, CacheFileObject } from '../../../../shared/models/activitypub'
 import { DislikeObject } from '../../../../shared/models/activitypub/objects'
 import { getActorUrl } from '../../../helpers/activitypub'
 import { retryTransactionWrapper } from '../../../helpers/database-utils'
@@ -11,6 +11,7 @@ import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
 import { forwardVideoRelatedActivity } from '../send/utils'
 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
 import { VideoShareModel } from '../../../models/video/video-share'
+import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
 
 async function processUndoActivity (activity: ActivityUndo) {
   const activityToUndo = activity.object
@@ -19,11 +20,21 @@ async function processUndoActivity (activity: ActivityUndo) {
 
   if (activityToUndo.type === 'Like') {
     return retryTransactionWrapper(processUndoLike, actorUrl, activity)
-  } else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') {
-    return retryTransactionWrapper(processUndoDislike, actorUrl, activity)
-  } else if (activityToUndo.type === 'Follow') {
+  }
+
+  if (activityToUndo.type === 'Create') {
+    if (activityToUndo.object.type === 'Dislike') {
+      return retryTransactionWrapper(processUndoDislike, actorUrl, activity)
+    } else if (activityToUndo.object.type === 'CacheFile') {
+      return retryTransactionWrapper(processUndoCacheFile, actorUrl, activity)
+    }
+  }
+
+  if (activityToUndo.type === 'Follow') {
     return retryTransactionWrapper(processUndoFollow, actorUrl, activityToUndo)
-  } else if (activityToUndo.type === 'Announce') {
+  }
+
+  if (activityToUndo.type === 'Announce') {
     return retryTransactionWrapper(processUndoAnnounce, actorUrl, activityToUndo)
   }
 
@@ -88,6 +99,29 @@ async function processUndoDislike (actorUrl: string, activity: ActivityUndo) {
   })
 }
 
+async function processUndoCacheFile (actorUrl: string, activity: ActivityUndo) {
+  const cacheFileObject = activity.object.object as CacheFileObject
+
+  const { video } = await getOrCreateVideoAndAccountAndChannel(cacheFileObject.object)
+
+  return sequelizeTypescript.transaction(async t => {
+    const byActor = await ActorModel.loadByUrl(actorUrl)
+    if (!byActor) throw new Error('Unknown actor ' + actorUrl)
+
+    const cacheFile = await VideoRedundancyModel.loadByUrl(cacheFileObject.id)
+    if (!cacheFile) throw new Error('Unknown video cache ' + cacheFile.url)
+
+    await cacheFile.destroy()
+
+    if (video.isOwned()) {
+      // Don't resend the activity to the sender
+      const exceptions = [ byActor ]
+
+      await forwardVideoRelatedActivity(activity, t, exceptions, video)
+    }
+  })
+}
+
 function processUndoFollow (actorUrl: string, followActivity: ActivityFollow) {
   return sequelizeTypescript.transaction(async t => {
     const follower = await ActorModel.loadByUrl(actorUrl, t)
index d2ad738a26f0414544d61e0b1d7c7396a6c4bce7..d3af1a181b0885b1c192f5471ee4dc654a3228d7 100644 (file)
@@ -1,4 +1,4 @@
-import { ActivityUpdate, VideoTorrentObject } from '../../../../shared/models/activitypub'
+import { ActivityUpdate, CacheFileObject, VideoTorrentObject } from '../../../../shared/models/activitypub'
 import { ActivityPubActor } from '../../../../shared/models/activitypub/activitypub-actor'
 import { resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers/database-utils'
 import { logger } from '../../../helpers/logger'
@@ -7,8 +7,11 @@ import { AccountModel } from '../../../models/account/account'
 import { ActorModel } from '../../../models/activitypub/actor'
 import { VideoChannelModel } from '../../../models/video/video-channel'
 import { fetchAvatarIfExists, getOrCreateActorAndServerAndModel, updateActorAvatarInstance, updateActorInstance } from '../actor'
-import { getOrCreateVideoAndAccountAndChannel, getOrCreateVideoChannelFromVideoObject, updateVideoFromAP } from '../videos'
+import { getOrCreateVideoAndAccountAndChannel, updateVideoFromAP, getOrCreateVideoChannelFromVideoObject } from '../videos'
 import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos'
+import { isCacheFileObjectValid } from '../../../helpers/custom-validators/activitypub/cache-file'
+import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
+import { createCacheFile, updateCacheFile } from '../cache-file'
 
 async function processUpdateActivity (activity: ActivityUpdate) {
   const actor = await getOrCreateActorAndServerAndModel(activity.actor)
@@ -16,10 +19,16 @@ async function processUpdateActivity (activity: ActivityUpdate) {
 
   if (objectType === 'Video') {
     return retryTransactionWrapper(processUpdateVideo, actor, activity)
-  } else if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
+  }
+
+  if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
     return retryTransactionWrapper(processUpdateActor, actor, activity)
   }
 
+  if (objectType === 'CacheFile') {
+    return retryTransactionWrapper(processUpdateCacheFile, actor, activity)
+  }
+
   return undefined
 }
 
@@ -42,7 +51,24 @@ async function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate)
   const { video } = await getOrCreateVideoAndAccountAndChannel(videoObject.id)
   const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
 
-  return updateVideoFromAP(video, videoObject, actor, channelActor, activity.to)
+  return updateVideoFromAP(video, videoObject, actor.Account, channelActor.VideoChannel, activity.to)
+}
+
+async function processUpdateCacheFile (byActor: ActorModel, activity: ActivityUpdate) {
+  const cacheFileObject = activity.object as CacheFileObject
+
+  if (!isCacheFileObjectValid(cacheFileObject) === false) {
+    logger.debug('Cahe file object sent by update is not valid.', { cacheFileObject })
+    return undefined
+  }
+
+  const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id)
+  if (!redundancyModel) {
+    const { video } = await getOrCreateVideoAndAccountAndChannel(cacheFileObject.id)
+    return createCacheFile(cacheFileObject, video, byActor)
+  }
+
+  return updateCacheFile(cacheFileObject, redundancyModel, byActor)
 }
 
 async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
index ef679707bfe72df3d9332f531e8ee6691da53d2a..b6abde13dc8665677997a333b6145920c881a9c7 100644 (file)
@@ -3,7 +3,7 @@ import { ActorModel } from '../../../models/activitypub/actor'
 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
 import { getActorFollowAcceptActivityPubUrl, getActorFollowActivityPubUrl } from '../url'
 import { unicastTo } from './utils'
-import { followActivityData } from './send-follow'
+import { buildFollowActivity } from './send-follow'
 import { logger } from '../../../helpers/logger'
 
 async function sendAccept (actorFollow: ActorFollowModel) {
@@ -18,10 +18,10 @@ async function sendAccept (actorFollow: ActorFollowModel) {
   logger.info('Creating job to accept follower %s.', follower.url)
 
   const followUrl = getActorFollowActivityPubUrl(actorFollow)
-  const followData = followActivityData(followUrl, follower, me)
+  const followData = buildFollowActivity(followUrl, follower, me)
 
   const url = getActorFollowAcceptActivityPubUrl(actorFollow)
-  const data = acceptActivityData(url, me, followData)
+  const data = buildAcceptActivity(url, me, followData)
 
   return unicastTo(data, me, follower.inboxUrl)
 }
@@ -34,7 +34,7 @@ export {
 
 // ---------------------------------------------------------------------------
 
-function acceptActivityData (url: string, byActor: ActorModel, followActivityData: ActivityFollow): ActivityAccept {
+function buildAcceptActivity (url: string, byActor: ActorModel, followActivityData: ActivityFollow): ActivityAccept {
   return {
     type: 'Accept',
     id: url,
index 352813d73d218dd0b37deb2fcd40345fd774fb6d..f137217f819201bdf3d4c6337d806fb863085299 100644 (file)
@@ -4,45 +4,44 @@ import { ActorModel } from '../../../models/activitypub/actor'
 import { VideoModel } from '../../../models/video/video'
 import { VideoShareModel } from '../../../models/video/video-share'
 import { broadcastToFollowers } from './utils'
-import { getActorsInvolvedInVideo, getAudience, getObjectFollowersAudience } from '../audience'
+import { audiencify, getActorsInvolvedInVideo, getAudience, getObjectFollowersAudience } from '../audience'
 import { logger } from '../../../helpers/logger'
 
-async function buildVideoAnnounce (byActor: ActorModel, videoShare: VideoShareModel, video: VideoModel, t: Transaction) {
+async function buildAnnounceWithVideoAudience (byActor: ActorModel, videoShare: VideoShareModel, video: VideoModel, t: Transaction) {
   const announcedObject = video.url
 
-  const accountsToForwardView = await getActorsInvolvedInVideo(video, t)
-  const audience = getObjectFollowersAudience(accountsToForwardView)
-  return announceActivityData(videoShare.url, byActor, announcedObject, audience)
+  const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
+  const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
+
+  const activity = buildAnnounceActivity(videoShare.url, byActor, announcedObject, audience)
+
+  return { activity, actorsInvolvedInVideo }
 }
 
 async function sendVideoAnnounce (byActor: ActorModel, videoShare: VideoShareModel, video: VideoModel, t: Transaction) {
-  const data = await buildVideoAnnounce(byActor, videoShare, video, t)
+  const { activity, actorsInvolvedInVideo } = await buildAnnounceWithVideoAudience(byActor, videoShare, video, t)
 
   logger.info('Creating job to send announce %s.', videoShare.url)
 
-  const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
   const followersException = [ byActor ]
-
-  return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, followersException)
+  return broadcastToFollowers(activity, byActor, actorsInvolvedInVideo, t, followersException)
 }
 
-function announceActivityData (url: string, byActor: ActorModel, object: string, audience?: ActivityAudience): ActivityAnnounce {
+function buildAnnounceActivity (url: string, byActor: ActorModel, object: string, audience?: ActivityAudience): ActivityAnnounce {
   if (!audience) audience = getAudience(byActor)
 
-  return {
-    type: 'Announce',
-    to: audience.to,
-    cc: audience.cc,
+  return audiencify({
+    type: 'Announce' as 'Announce',
     id: url,
     actor: byActor.url,
     object
-  }
+  }, audience)
 }
 
 // ---------------------------------------------------------------------------
 
 export {
   sendVideoAnnounce,
-  announceActivityData,
-  buildVideoAnnounce
+  buildAnnounceActivity,
+  buildAnnounceWithVideoAudience
 }
index fc76cdd8abdb058feee52ded0841ea04deb81034..6f89b1a2291a14226a9e2dbcc24561834977df2c 100644 (file)
@@ -17,6 +17,7 @@ import {
   getVideoCommentAudience
 } from '../audience'
 import { logger } from '../../../helpers/logger'
+import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
 
 async function sendCreateVideo (video: VideoModel, t: Transaction) {
   if (video.privacy === VideoPrivacy.PRIVATE) return undefined
@@ -27,12 +28,12 @@ async function sendCreateVideo (video: VideoModel, t: Transaction) {
   const videoObject = video.toActivityPubObject()
 
   const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
-  const data = createActivityData(video.url, byActor, videoObject, audience)
+  const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience)
 
-  return broadcastToFollowers(data, byActor, [ byActor ], t)
+  return broadcastToFollowers(createActivity, byActor, [ byActor ], t)
 }
 
-async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel, t: Transaction) {
+async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel) {
   if (!video.VideoChannel.Account.Actor.serverId) return // Local
 
   const url = getVideoAbuseActivityPubUrl(videoAbuse)
@@ -40,9 +41,23 @@ async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel,
   logger.info('Creating job to send video abuse %s.', url)
 
   const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] }
-  const data = createActivityData(url, byActor, videoAbuse.toActivityPubObject(), audience)
+  const createActivity = buildCreateActivity(url, byActor, videoAbuse.toActivityPubObject(), audience)
 
-  return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
+  return unicastTo(createActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
+}
+
+async function sendCreateCacheFile (byActor: ActorModel, fileRedundancy: VideoRedundancyModel) {
+  logger.info('Creating job to send file cache of %s.', fileRedundancy.url)
+
+  const redundancyObject = fileRedundancy.toActivityPubObject()
+
+  const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(fileRedundancy.VideoFile.Video.id)
+  const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, undefined)
+
+  const audience = getVideoAudience(video, actorsInvolvedInVideo)
+  const createActivity = buildCreateActivity(fileRedundancy.url, byActor, redundancyObject, audience)
+
+  return unicastTo(createActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
 }
 
 async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
@@ -66,73 +81,73 @@ async function sendCreateVideoComment (comment: VideoCommentModel, t: Transactio
     audience = getObjectFollowersAudience(actorsInvolvedInComment.concat(parentsCommentActors))
   }
 
-  const data = createActivityData(comment.url, byActor, commentObject, audience)
+  const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
 
   // This was a reply, send it to the parent actors
   const actorsException = [ byActor ]
-  await broadcastToActors(data, byActor, parentsCommentActors, actorsException)
+  await broadcastToActors(createActivity, byActor, parentsCommentActors, actorsException)
 
   // Broadcast to our followers
-  await broadcastToFollowers(data, byActor, [ byActor ], t)
+  await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
 
   // Send to actors involved in the comment
-  if (isOrigin) return broadcastToFollowers(data, byActor, actorsInvolvedInComment, t, actorsException)
+  if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
 
   // Send to origin
-  return unicastTo(data, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
+  return unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
 }
 
 async function sendCreateView (byActor: ActorModel, video: VideoModel, t: Transaction) {
   logger.info('Creating job to send view of %s.', video.url)
 
   const url = getVideoViewActivityPubUrl(byActor, video)
-  const viewActivityData = createViewActivityData(byActor, video)
+  const viewActivity = buildViewActivity(byActor, video)
 
   const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
 
   // Send to origin
   if (video.isOwned() === false) {
     const audience = getVideoAudience(video, actorsInvolvedInVideo)
-    const data = createActivityData(url, byActor, viewActivityData, audience)
+    const createActivity = buildCreateActivity(url, byActor, viewActivity, audience)
 
-    return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
+    return unicastTo(createActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
   }
 
   // Send to followers
   const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
-  const data = createActivityData(url, byActor, viewActivityData, audience)
+  const createActivity = buildCreateActivity(url, byActor, viewActivity, audience)
 
   // Use the server actor to send the view
   const serverActor = await getServerActor()
   const actorsException = [ byActor ]
-  return broadcastToFollowers(data, serverActor, actorsInvolvedInVideo, t, actorsException)
+  return broadcastToFollowers(createActivity, serverActor, actorsInvolvedInVideo, t, actorsException)
 }
 
 async function sendCreateDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
   logger.info('Creating job to dislike %s.', video.url)
 
   const url = getVideoDislikeActivityPubUrl(byActor, video)
-  const dislikeActivityData = createDislikeActivityData(byActor, video)
+  const dislikeActivity = buildDislikeActivity(byActor, video)
 
   const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
 
   // Send to origin
   if (video.isOwned() === false) {
     const audience = getVideoAudience(video, actorsInvolvedInVideo)
-    const data = createActivityData(url, byActor, dislikeActivityData, audience)
+    const createActivity = buildCreateActivity(url, byActor, dislikeActivity, audience)
 
-    return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
+    return unicastTo(createActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
   }
 
   // Send to followers
   const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
-  const data = createActivityData(url, byActor, dislikeActivityData, audience)
+  const createActivity = buildCreateActivity(url, byActor, dislikeActivity, audience)
 
   const actorsException = [ byActor ]
-  return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, actorsException)
+  return broadcastToFollowers(createActivity, byActor, actorsInvolvedInVideo, t, actorsException)
 }
 
-function createActivityData (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityCreate {
+function buildCreateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityCreate {
   if (!audience) audience = getAudience(byActor)
 
   return audiencify(
@@ -146,7 +161,7 @@ function createActivityData (url: string, byActor: ActorModel, object: any, audi
   )
 }
 
-function createDislikeActivityData (byActor: ActorModel, video: VideoModel) {
+function buildDislikeActivity (byActor: ActorModel, video: VideoModel) {
   return {
     type: 'Dislike',
     actor: byActor.url,
@@ -154,7 +169,7 @@ function createDislikeActivityData (byActor: ActorModel, video: VideoModel) {
   }
 }
 
-function createViewActivityData (byActor: ActorModel, video: VideoModel) {
+function buildViewActivity (byActor: ActorModel, video: VideoModel) {
   return {
     type: 'View',
     actor: byActor.url,
@@ -167,9 +182,10 @@ function createViewActivityData (byActor: ActorModel, video: VideoModel) {
 export {
   sendCreateVideo,
   sendVideoAbuse,
-  createActivityData,
+  buildCreateActivity,
   sendCreateView,
   sendCreateDislike,
-  createDislikeActivityData,
-  sendCreateVideoComment
+  buildDislikeActivity,
+  sendCreateVideoComment,
+  sendCreateCacheFile
 }
index 3d1dfb699e287965bbbf974741be4ef2a34f5a41..47918254362d50f5dd1ecbf159118f46387014cc 100644 (file)
@@ -15,24 +15,23 @@ async function sendDeleteVideo (video: VideoModel, t: Transaction) {
   const url = getDeleteActivityPubUrl(video.url)
   const byActor = video.VideoChannel.Account.Actor
 
-  const data = deleteActivityData(url, video.url, byActor)
+  const activity = buildDeleteActivity(url, video.url, byActor)
 
-  const actorsInvolved = await VideoShareModel.loadActorsByShare(video.id, t)
-  actorsInvolved.push(byActor)
+  const actorsInvolved = await getActorsInvolvedInVideo(video, t)
 
-  return broadcastToFollowers(data, byActor, actorsInvolved, t)
+  return broadcastToFollowers(activity, byActor, actorsInvolved, t)
 }
 
 async function sendDeleteActor (byActor: ActorModel, t: Transaction) {
   logger.info('Creating job to broadcast delete of actor %s.', byActor.url)
 
   const url = getDeleteActivityPubUrl(byActor.url)
-  const data = deleteActivityData(url, byActor.url, byActor)
+  const activity = buildDeleteActivity(url, byActor.url, byActor)
 
   const actorsInvolved = await VideoShareModel.loadActorsByVideoOwner(byActor.id, t)
   actorsInvolved.push(byActor)
 
-  return broadcastToFollowers(data, byActor, actorsInvolved, t)
+  return broadcastToFollowers(activity, byActor, actorsInvolved, t)
 }
 
 async function sendDeleteVideoComment (videoComment: VideoCommentModel, t: Transaction) {
@@ -45,23 +44,23 @@ async function sendDeleteVideoComment (videoComment: VideoCommentModel, t: Trans
   const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, t)
 
   const actorsInvolvedInComment = await getActorsInvolvedInVideo(videoComment.Video, t)
-  actorsInvolvedInComment.push(byActor)
+  actorsInvolvedInComment.push(byActor) // Add the actor that commented the video
 
   const audience = getVideoCommentAudience(videoComment, threadParentComments, actorsInvolvedInComment, isVideoOrigin)
-  const data = deleteActivityData(url, videoComment.url, byActor, audience)
+  const activity = buildDeleteActivity(url, videoComment.url, byActor, audience)
 
   // This was a reply, send it to the parent actors
   const actorsException = [ byActor ]
-  await broadcastToActors(data, byActor, threadParentComments.map(c => c.Account.Actor), actorsException)
+  await broadcastToActors(activity, byActor, threadParentComments.map(c => c.Account.Actor), actorsException)
 
   // Broadcast to our followers
-  await broadcastToFollowers(data, byActor, [ byActor ], t)
+  await broadcastToFollowers(activity, byActor, [ byActor ], t)
 
   // Send to actors involved in the comment
-  if (isVideoOrigin) return broadcastToFollowers(data, byActor, actorsInvolvedInComment, t, actorsException)
+  if (isVideoOrigin) return broadcastToFollowers(activity, byActor, actorsInvolvedInComment, t, actorsException)
 
   // Send to origin
-  return unicastTo(data, byActor, videoComment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
+  return unicastTo(activity, byActor, videoComment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
 }
 
 // ---------------------------------------------------------------------------
@@ -74,7 +73,7 @@ export {
 
 // ---------------------------------------------------------------------------
 
-function deleteActivityData (url: string, object: string, byActor: ActorModel, audience?: ActivityAudience): ActivityDelete {
+function buildDeleteActivity (url: string, object: string, byActor: ActorModel, audience?: ActivityAudience): ActivityDelete {
   const activity = {
     type: 'Delete' as 'Delete',
     id: url,
index 46d08c17be1e3875e7a7370bfba41de0793fbe50..170b46b482e035ce3b97ea1acfaf9c136e323d56 100644 (file)
@@ -15,12 +15,12 @@ function sendFollow (actorFollow: ActorFollowModel) {
   logger.info('Creating job to send follow request to %s.', following.url)
 
   const url = getActorFollowActivityPubUrl(actorFollow)
-  const data = followActivityData(url, me, following)
+  const data = buildFollowActivity(url, me, following)
 
   return unicastTo(data, me, following.inboxUrl)
 }
 
-function followActivityData (url: string, byActor: ActorModel, targetActor: ActorModel): ActivityFollow {
+function buildFollowActivity (url: string, byActor: ActorModel, targetActor: ActorModel): ActivityFollow {
   return {
     type: 'Follow',
     id: url,
@@ -33,5 +33,5 @@ function followActivityData (url: string, byActor: ActorModel, targetActor: Acto
 
 export {
   sendFollow,
-  followActivityData
+  buildFollowActivity
 }
index 83225f5df1d62186561e40a43953815d35773c13..a5408ac6a2acbcd4975fb505b1da78eb49b0112f 100644 (file)
@@ -17,20 +17,20 @@ async function sendLike (byActor: ActorModel, video: VideoModel, t: Transaction)
   // Send to origin
   if (video.isOwned() === false) {
     const audience = getVideoAudience(video, accountsInvolvedInVideo)
-    const data = likeActivityData(url, byActor, video, audience)
+    const data = buildLikeActivity(url, byActor, video, audience)
 
     return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
   }
 
   // Send to followers
   const audience = getObjectFollowersAudience(accountsInvolvedInVideo)
-  const data = likeActivityData(url, byActor, video, audience)
+  const activity = buildLikeActivity(url, byActor, video, audience)
 
   const followersException = [ byActor ]
-  return broadcastToFollowers(data, byActor, accountsInvolvedInVideo, t, followersException)
+  return broadcastToFollowers(activity, byActor, accountsInvolvedInVideo, t, followersException)
 }
 
-function likeActivityData (url: string, byActor: ActorModel, video: VideoModel, audience?: ActivityAudience): ActivityLike {
+function buildLikeActivity (url: string, byActor: ActorModel, video: VideoModel, audience?: ActivityAudience): ActivityLike {
   if (!audience) audience = getAudience(byActor)
 
   return audiencify(
@@ -48,5 +48,5 @@ function likeActivityData (url: string, byActor: ActorModel, video: VideoModel,
 
 export {
   sendLike,
-  likeActivityData
+  buildLikeActivity
 }
index 30d0fd98be63b1d2fc57787ddd7c14b759ddf518..a50673c7975cb042f1a8d2d5fa4dce467ce4d2fa 100644 (file)
@@ -13,12 +13,13 @@ import { VideoModel } from '../../../models/video/video'
 import { getActorFollowActivityPubUrl, getUndoActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from '../url'
 import { broadcastToFollowers, unicastTo } from './utils'
 import { audiencify, getActorsInvolvedInVideo, getAudience, getObjectFollowersAudience, getVideoAudience } from '../audience'
-import { createActivityData, createDislikeActivityData } from './send-create'
-import { followActivityData } from './send-follow'
-import { likeActivityData } from './send-like'
+import { buildCreateActivity, buildDislikeActivity } from './send-create'
+import { buildFollowActivity } from './send-follow'
+import { buildLikeActivity } from './send-like'
 import { VideoShareModel } from '../../../models/video/video-share'
-import { buildVideoAnnounce } from './send-announce'
+import { buildAnnounceWithVideoAudience } from './send-announce'
 import { logger } from '../../../helpers/logger'
+import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
 
 async function sendUndoFollow (actorFollow: ActorFollowModel, t: Transaction) {
   const me = actorFollow.ActorFollower
@@ -32,10 +33,10 @@ async function sendUndoFollow (actorFollow: ActorFollowModel, t: Transaction) {
   const followUrl = getActorFollowActivityPubUrl(actorFollow)
   const undoUrl = getUndoActivityPubUrl(followUrl)
 
-  const object = followActivityData(followUrl, me, following)
-  const data = undoActivityData(undoUrl, me, object)
+  const followActivity = buildFollowActivity(followUrl, me, following)
+  const undoActivity = undoActivityData(undoUrl, me, followActivity)
 
-  return unicastTo(data, me, following.inboxUrl)
+  return unicastTo(undoActivity, me, following.inboxUrl)
 }
 
 async function sendUndoLike (byActor: ActorModel, video: VideoModel, t: Transaction) {
@@ -45,21 +46,21 @@ async function sendUndoLike (byActor: ActorModel, video: VideoModel, t: Transact
   const undoUrl = getUndoActivityPubUrl(likeUrl)
 
   const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
-  const object = likeActivityData(likeUrl, byActor, video)
+  const likeActivity = buildLikeActivity(likeUrl, byActor, video)
 
   // Send to origin
   if (video.isOwned() === false) {
     const audience = getVideoAudience(video, actorsInvolvedInVideo)
-    const data = undoActivityData(undoUrl, byActor, object, audience)
+    const undoActivity = undoActivityData(undoUrl, byActor, likeActivity, audience)
 
-    return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
+    return unicastTo(undoActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
   }
 
   const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
-  const data = undoActivityData(undoUrl, byActor, object, audience)
+  const undoActivity = undoActivityData(undoUrl, byActor, likeActivity, audience)
 
   const followersException = [ byActor ]
-  return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, followersException)
+  return broadcastToFollowers(undoActivity, byActor, actorsInvolvedInVideo, t, followersException)
 }
 
 async function sendUndoDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
@@ -69,20 +70,20 @@ async function sendUndoDislike (byActor: ActorModel, video: VideoModel, t: Trans
   const undoUrl = getUndoActivityPubUrl(dislikeUrl)
 
   const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
-  const dislikeActivity = createDislikeActivityData(byActor, video)
-  const object = createActivityData(dislikeUrl, byActor, dislikeActivity)
+  const dislikeActivity = buildDislikeActivity(byActor, video)
+  const createDislikeActivity = buildCreateActivity(dislikeUrl, byActor, dislikeActivity)
 
   if (video.isOwned() === false) {
     const audience = getVideoAudience(video, actorsInvolvedInVideo)
-    const data = undoActivityData(undoUrl, byActor, object, audience)
+    const undoActivity = undoActivityData(undoUrl, byActor, createDislikeActivity, audience)
 
-    return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
+    return unicastTo(undoActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
   }
 
-  const data = undoActivityData(undoUrl, byActor, object)
+  const undoActivity = undoActivityData(undoUrl, byActor, createDislikeActivity)
 
   const followersException = [ byActor ]
-  return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, followersException)
+  return broadcastToFollowers(undoActivity, byActor, actorsInvolvedInVideo, t, followersException)
 }
 
 async function sendUndoAnnounce (byActor: ActorModel, videoShare: VideoShareModel, video: VideoModel, t: Transaction) {
@@ -90,12 +91,27 @@ async function sendUndoAnnounce (byActor: ActorModel, videoShare: VideoShareMode
 
   const undoUrl = getUndoActivityPubUrl(videoShare.url)
 
-  const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
-  const object = await buildVideoAnnounce(byActor, videoShare, video, t)
-  const data = undoActivityData(undoUrl, byActor, object)
+  const { activity: announceActivity, actorsInvolvedInVideo } = await buildAnnounceWithVideoAudience(byActor, videoShare, video, t)
+  const undoActivity = undoActivityData(undoUrl, byActor, announceActivity)
 
   const followersException = [ byActor ]
-  return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, followersException)
+  return broadcastToFollowers(undoActivity, byActor, actorsInvolvedInVideo, t, followersException)
+}
+
+async function sendUndoCacheFile (byActor: ActorModel, redundancyModel: VideoRedundancyModel, t: Transaction) {
+  logger.info('Creating job to undo cache file %s.', redundancyModel.url)
+
+  const undoUrl = getUndoActivityPubUrl(redundancyModel.url)
+
+  const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(redundancyModel.VideoFile.Video.id)
+  const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
+
+  const audience = getVideoAudience(video, actorsInvolvedInVideo)
+  const createActivity = buildCreateActivity(redundancyModel.url, byActor, redundancyModel.toActivityPubObject())
+
+  const undoActivity = undoActivityData(undoUrl, byActor, createActivity, audience)
+
+  return unicastTo(undoActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
 }
 
 // ---------------------------------------------------------------------------
@@ -104,7 +120,8 @@ export {
   sendUndoFollow,
   sendUndoLike,
   sendUndoDislike,
-  sendUndoAnnounce
+  sendUndoAnnounce,
+  sendUndoCacheFile
 }
 
 // ---------------------------------------------------------------------------
index 6f1d8089886511c1df7c3b645eb050d56cfb25d4..605473338f340413730bec2f512d30a421ede3ba 100644 (file)
@@ -7,11 +7,11 @@ import { VideoModel } from '../../../models/video/video'
 import { VideoChannelModel } from '../../../models/video/video-channel'
 import { VideoShareModel } from '../../../models/video/video-share'
 import { getUpdateActivityPubUrl } from '../url'
-import { broadcastToFollowers } from './utils'
-import { audiencify, getAudience } from '../audience'
+import { broadcastToFollowers, unicastTo } from './utils'
+import { audiencify, getActorsInvolvedInVideo, getAudience, getObjectFollowersAudience } from '../audience'
 import { logger } from '../../../helpers/logger'
-import { videoFeedsValidator } from '../../../middlewares/validators'
 import { VideoCaptionModel } from '../../../models/video/video-caption'
+import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
 
 async function sendUpdateVideo (video: VideoModel, t: Transaction, overrodeByActor?: ActorModel) {
   logger.info('Creating job to update video %s.', video.url)
@@ -26,12 +26,12 @@ async function sendUpdateVideo (video: VideoModel, t: Transaction, overrodeByAct
   const videoObject = video.toActivityPubObject()
   const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
 
-  const data = updateActivityData(url, byActor, videoObject, audience)
+  const updateActivity = buildUpdateActivity(url, byActor, videoObject, audience)
 
-  const actorsInvolved = await VideoShareModel.loadActorsByShare(video.id, t)
-  actorsInvolved.push(byActor)
+  const actorsInvolved = await getActorsInvolvedInVideo(video, t)
+  if (overrodeByActor) actorsInvolved.push(overrodeByActor)
 
-  return broadcastToFollowers(data, byActor, actorsInvolved, t)
+  return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t)
 }
 
 async function sendUpdateActor (accountOrChannel: AccountModel | VideoChannelModel, t: Transaction) {
@@ -42,7 +42,7 @@ async function sendUpdateActor (accountOrChannel: AccountModel | VideoChannelMod
   const url = getUpdateActivityPubUrl(byActor.url, byActor.updatedAt.toISOString())
   const accountOrChannelObject = accountOrChannel.toActivityPubObject()
   const audience = getAudience(byActor)
-  const data = updateActivityData(url, byActor, accountOrChannelObject, audience)
+  const updateActivity = buildUpdateActivity(url, byActor, accountOrChannelObject, audience)
 
   let actorsInvolved: ActorModel[]
   if (accountOrChannel instanceof AccountModel) {
@@ -55,19 +55,35 @@ async function sendUpdateActor (accountOrChannel: AccountModel | VideoChannelMod
 
   actorsInvolved.push(byActor)
 
-  return broadcastToFollowers(data, byActor, actorsInvolved, t)
+  return broadcastToFollowers(updateActivity, byActor, actorsInvolved, t)
+}
+
+async function sendUpdateCacheFile (byActor: ActorModel, redundancyModel: VideoRedundancyModel) {
+  logger.info('Creating job to update cache file %s.', redundancyModel.url)
+
+  const url = getUpdateActivityPubUrl(redundancyModel.url, redundancyModel.updatedAt.toISOString())
+  const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(redundancyModel.VideoFile.Video.id)
+
+  const redundancyObject = redundancyModel.toActivityPubObject()
+
+  const accountsInvolvedInVideo = await getActorsInvolvedInVideo(video, undefined)
+  const audience = getObjectFollowersAudience(accountsInvolvedInVideo)
+
+  const updateActivity = buildUpdateActivity(url, byActor, redundancyObject, audience)
+  return unicastTo(updateActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
 }
 
 // ---------------------------------------------------------------------------
 
 export {
   sendUpdateActor,
-  sendUpdateVideo
+  sendUpdateVideo,
+  sendUpdateCacheFile
 }
 
 // ---------------------------------------------------------------------------
 
-function updateActivityData (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityUpdate {
+function buildUpdateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityUpdate {
   if (!audience) audience = getAudience(byActor)
 
   return audiencify(
index da437292eef24543457f6d2b77793e49b9ceed70..c20c156331a4be9ac0f27c3eac77d3d35323effb 100644 (file)
@@ -59,11 +59,11 @@ async function forwardActivity (
 async function broadcastToFollowers (
   data: any,
   byActor: ActorModel,
-  toActorFollowers: ActorModel[],
+  toFollowersOf: ActorModel[],
   t: Transaction,
   actorsException: ActorModel[] = []
 ) {
-  const uris = await computeFollowerUris(toActorFollowers, actorsException, t)
+  const uris = await computeFollowerUris(toFollowersOf, actorsException, t)
   return broadcastTo(uris, data, byActor)
 }
 
@@ -115,8 +115,8 @@ export {
 
 // ---------------------------------------------------------------------------
 
-async function computeFollowerUris (toActorFollower: ActorModel[], actorsException: ActorModel[], t: Transaction) {
-  const toActorFollowerIds = toActorFollower.map(a => a.id)
+async function computeFollowerUris (toFollowersOf: ActorModel[], actorsException: ActorModel[], t: Transaction) {
+  const toActorFollowerIds = toFollowersOf.map(a => a.id)
 
   const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
   const sharedInboxesException = await buildSharedInboxesException(actorsException)
index 262463310bd0ad3fa21ff9961c895140e60e7c1a..2e7c56955523241ed6856100e16e504225efceff 100644 (file)
@@ -4,11 +4,18 @@ import { ActorFollowModel } from '../../models/activitypub/actor-follow'
 import { VideoModel } from '../../models/video/video'
 import { VideoAbuseModel } from '../../models/video/video-abuse'
 import { VideoCommentModel } from '../../models/video/video-comment'
+import { VideoFileModel } from '../../models/video/video-file'
 
 function getVideoActivityPubUrl (video: VideoModel) {
   return CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
 }
 
+function getVideoCacheFileActivityPubUrl (videoFile: VideoFileModel) {
+  const suffixFPS = videoFile.fps ? '-' + videoFile.fps : ''
+
+  return `${CONFIG.WEBSERVER.URL}/redundancy/videos/${videoFile.Video.uuid}/${videoFile.resolution}${suffixFPS}`
+}
+
 function getVideoCommentActivityPubUrl (video: VideoModel, videoComment: VideoCommentModel) {
   return CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid + '/comments/' + videoComment.id
 }
@@ -101,5 +108,6 @@ export {
   getVideoSharesActivityPubUrl,
   getVideoCommentsActivityPubUrl,
   getVideoLikesActivityPubUrl,
-  getVideoDislikesActivityPubUrl
+  getVideoDislikesActivityPubUrl,
+  getVideoCacheFileActivityPubUrl
 }
index 6c2095897f07e155ff598def8e9f88e4db0107c7..783f78d3ec8417ec0bcee5b96835482e3e57b8fb 100644 (file)
@@ -3,12 +3,12 @@ import * as sequelize from 'sequelize'
 import * as magnetUtil from 'magnet-uri'
 import { join } from 'path'
 import * as request from 'request'
-import { ActivityIconObject, VideoState } from '../../../shared/index'
+import { ActivityIconObject, ActivityVideoUrlObject, VideoState, ActivityUrlObject } from '../../../shared/index'
 import { VideoTorrentObject } from '../../../shared/models/activitypub/objects'
 import { VideoPrivacy } from '../../../shared/models/videos'
 import { sanitizeAndCheckVideoTorrentObject } from '../../helpers/custom-validators/activitypub/videos'
 import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos'
-import { resetSequelizeInstance, retryTransactionWrapper, updateInstanceWithAnother } from '../../helpers/database-utils'
+import { resetSequelizeInstance, retryTransactionWrapper } from '../../helpers/database-utils'
 import { logger } from '../../helpers/logger'
 import { doRequest, doRequestAndSaveToFile } from '../../helpers/requests'
 import { ACTIVITY_PUB, CONFIG, REMOTE_SCHEME, sequelizeTypescript, VIDEO_MIMETYPE_EXT } from '../../initializers'
@@ -17,7 +17,7 @@ import { TagModel } from '../../models/video/tag'
 import { VideoModel } from '../../models/video/video'
 import { VideoChannelModel } from '../../models/video/video-channel'
 import { VideoFileModel } from '../../models/video/video-file'
-import { getOrCreateActorAndServerAndModel, updateActorAvatarInstance } from './actor'
+import { getOrCreateActorAndServerAndModel } from './actor'
 import { addVideoComments } from './video-comments'
 import { crawlCollectionPage } from './crawl'
 import { sendCreateVideo, sendUpdateVideo } from './send'
@@ -25,7 +25,6 @@ import { isArray } from '../../helpers/custom-validators/misc'
 import { VideoCaptionModel } from '../../models/video/video-caption'
 import { JobQueue } from '../job-queue'
 import { ActivitypubHttpFetcherPayload } from '../job-queue/handlers/activitypub-http-fetcher'
-import { getUrlFromWebfinger } from '../../helpers/webfinger'
 import { createRates } from './video-rates'
 import { addVideoShares, shareVideoByServerAndChannel } from './share'
 import { AccountModel } from '../../models/account/account'
@@ -137,10 +136,7 @@ async function videoActivityObjectToDBAttributes (
 }
 
 function videoFileActivityUrlToDBAttributes (videoCreated: VideoModel, videoObject: VideoTorrentObject) {
-  const mimeTypes = Object.keys(VIDEO_MIMETYPE_EXT)
-  const fileUrls = videoObject.url.filter(u => {
-    return mimeTypes.indexOf(u.mimeType) !== -1 && u.mimeType.startsWith('video/')
-  })
+  const fileUrls = videoObject.url.filter(u => isActivityVideoUrlObject(u)) as ActivityVideoUrlObject[]
 
   if (fileUrls.length === 0) {
     throw new Error('Cannot find video files for ' + videoCreated.url)
@@ -331,8 +327,8 @@ async function refreshVideoIfNeeded (video: VideoModel): Promise<VideoModel> {
 
     const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
     const account = await AccountModel.load(channelActor.VideoChannel.accountId)
-    return updateVideoFromAP(video, videoObject, account.Actor, channelActor)
 
+    return updateVideoFromAP(video, videoObject, account, channelActor.VideoChannel)
   } catch (err) {
     logger.warn('Cannot refresh video.', { err })
     return video
@@ -342,8 +338,8 @@ async function refreshVideoIfNeeded (video: VideoModel): Promise<VideoModel> {
 async function updateVideoFromAP (
   video: VideoModel,
   videoObject: VideoTorrentObject,
-  accountActor: ActorModel,
-  channelActor: ActorModel,
+  account: AccountModel,
+  channel: VideoChannelModel,
   overrideTo?: string[]
 ) {
   logger.debug('Updating remote video "%s".', videoObject.uuid)
@@ -359,12 +355,12 @@ async function updateVideoFromAP (
 
       // Check actor has the right to update the video
       const videoChannel = video.VideoChannel
-      if (videoChannel.Account.Actor.id !== accountActor.id) {
-        throw new Error('Account ' + accountActor.url + ' does not own video channel ' + videoChannel.Actor.url)
+      if (videoChannel.Account.id !== account.id) {
+        throw new Error('Account ' + account.Actor.url + ' does not own video channel ' + videoChannel.Actor.url)
       }
 
       const to = overrideTo ? overrideTo : videoObject.to
-      const videoData = await videoActivityObjectToDBAttributes(channelActor.VideoChannel, videoObject, to)
+      const videoData = await videoActivityObjectToDBAttributes(channel, videoObject, to)
       video.set('name', videoData.name)
       video.set('uuid', videoData.uuid)
       video.set('url', videoData.url)
@@ -444,3 +440,11 @@ export {
   addVideoShares,
   createRates
 }
+
+// ---------------------------------------------------------------------------
+
+function isActivityVideoUrlObject (url: ActivityUrlObject): url is ActivityVideoUrlObject {
+  const mimeTypes = Object.keys(VIDEO_MIMETYPE_EXT)
+
+  return mimeTypes.indexOf(url.mimeType) !== -1 && url.mimeType.startsWith('video/')
+}
diff --git a/server/lib/redundancy.ts b/server/lib/redundancy.ts
new file mode 100644 (file)
index 0000000..78221cc
--- /dev/null
@@ -0,0 +1,18 @@
+import { VideoRedundancyModel } from '../models/redundancy/video-redundancy'
+import { sendUndoCacheFile } from './activitypub/send'
+import { Transaction } from 'sequelize'
+import { getServerActor } from '../helpers/utils'
+
+async function removeVideoRedundancy (videoRedundancy: VideoRedundancyModel, t?: Transaction) {
+  const serverActor = await getServerActor()
+
+  await sendUndoCacheFile(serverActor, videoRedundancy, t)
+
+  await videoRedundancy.destroy({ transaction: t })
+}
+
+// ---------------------------------------------------------------------------
+
+export {
+  removeVideoRedundancy
+}
diff --git a/server/lib/schedulers/videos-redundancy-scheduler.ts b/server/lib/schedulers/videos-redundancy-scheduler.ts
new file mode 100644 (file)
index 0000000..ee9ba17
--- /dev/null
@@ -0,0 +1,161 @@
+import { AbstractScheduler } from './abstract-scheduler'
+import { CONFIG, JOB_TTL, REDUNDANCY, SCHEDULER_INTERVALS_MS } from '../../initializers'
+import { logger } from '../../helpers/logger'
+import { VideoRedundancyStrategy } from '../../../shared/models/redundancy'
+import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
+import { VideoFileModel } from '../../models/video/video-file'
+import { sortBy } from 'lodash'
+import { downloadWebTorrentVideo } from '../../helpers/webtorrent'
+import { join } from 'path'
+import { rename } from 'fs-extra'
+import { getServerActor } from '../../helpers/utils'
+import { sendCreateCacheFile, sendUpdateCacheFile } from '../activitypub/send'
+import { VideoModel } from '../../models/video/video'
+import { getVideoCacheFileActivityPubUrl } from '../activitypub/url'
+import { removeVideoRedundancy } from '../redundancy'
+import { isTestInstance } from '../../helpers/core-utils'
+
+export class VideosRedundancyScheduler extends AbstractScheduler {
+
+  private static instance: AbstractScheduler
+  private executing = false
+
+  protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.videosRedundancy
+
+  private constructor () {
+    super()
+  }
+
+  async execute () {
+    if (this.executing) return
+
+    this.executing = true
+
+    for (const obj of CONFIG.REDUNDANCY.VIDEOS) {
+
+      try {
+        const videoToDuplicate = await this.findVideoToDuplicate(obj.strategy)
+        if (!videoToDuplicate) continue
+
+        const videoFiles = videoToDuplicate.VideoFiles
+        videoFiles.forEach(f => f.Video = videoToDuplicate)
+
+        const videosRedundancy = await VideoRedundancyModel.getVideoFiles(obj.strategy)
+        if (this.isTooHeavy(videosRedundancy, videoFiles, obj.size)) {
+          if (!isTestInstance()) logger.info('Video %s is too big for our cache, skipping.', videoToDuplicate.url)
+          continue
+        }
+
+        logger.info('Will duplicate video %s in redundancy scheduler "%s".', videoToDuplicate.url, obj.strategy)
+
+        await this.createVideoRedundancy(obj.strategy, videoFiles)
+      } catch (err) {
+        logger.error('Cannot run videos redundancy %s.', obj.strategy, { err })
+      }
+    }
+
+    const expired = await VideoRedundancyModel.listAllExpired()
+
+    for (const m of expired) {
+      logger.info('Removing expired video %s from our redundancy system.', this.buildEntryLogId(m))
+
+      try {
+        await m.destroy()
+      } catch (err) {
+        logger.error('Cannot remove %s video from our redundancy system.', this.buildEntryLogId(m))
+      }
+    }
+
+    this.executing = false
+  }
+
+  static get Instance () {
+    return this.instance || (this.instance = new this())
+  }
+
+  private findVideoToDuplicate (strategy: VideoRedundancyStrategy) {
+    if (strategy === 'most-views') return VideoRedundancyModel.findMostViewToDuplicate(REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR)
+  }
+
+  private async createVideoRedundancy (strategy: VideoRedundancyStrategy, filesToDuplicate: VideoFileModel[]) {
+    const serverActor = await getServerActor()
+
+    for (const file of filesToDuplicate) {
+      const existing = await VideoRedundancyModel.loadByFileId(file.id)
+      if (existing) {
+        logger.info('Duplicating %s - %d in videos redundancy with "%s" strategy.', file.Video.url, file.resolution, strategy)
+
+        existing.expiresOn = this.buildNewExpiration()
+        await existing.save()
+
+        await sendUpdateCacheFile(serverActor, existing)
+        continue
+      }
+
+      // We need more attributes and check if the video still exists
+      const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(file.Video.id)
+      if (!video) continue
+
+      logger.info('Duplicating %s - %d in videos redundancy with "%s" strategy.', video.url, file.resolution, strategy)
+
+      const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
+      const magnetUri = video.generateMagnetUri(file, baseUrlHttp, baseUrlWs)
+
+      const tmpPath = await downloadWebTorrentVideo({ magnetUri }, JOB_TTL['video-import'])
+
+      const destPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(file))
+      await rename(tmpPath, destPath)
+
+      const createdModel = await VideoRedundancyModel.create({
+        expiresOn: new Date(Date.now() + REDUNDANCY.VIDEOS.EXPIRES_AFTER_MS),
+        url: getVideoCacheFileActivityPubUrl(file),
+        fileUrl: video.getVideoFileUrl(file, CONFIG.WEBSERVER.URL),
+        strategy,
+        videoFileId: file.id,
+        actorId: serverActor.id
+      })
+      createdModel.VideoFile = file
+
+      await sendCreateCacheFile(serverActor, createdModel)
+    }
+  }
+
+  // Unused, but could be useful in the future, with a custom strategy
+  private async purgeVideosIfNeeded (videosRedundancy: VideoRedundancyModel[], filesToDuplicate: VideoFileModel[], maxSize: number) {
+    const sortedVideosRedundancy = sortBy(videosRedundancy, 'createdAt')
+
+    while (this.isTooHeavy(sortedVideosRedundancy, filesToDuplicate, maxSize)) {
+      const toDelete = sortedVideosRedundancy.shift()
+
+      const videoFile = toDelete.VideoFile
+      logger.info('Purging video %s (resolution %d) from our redundancy system.', videoFile.Video.url, videoFile.resolution)
+
+      await removeVideoRedundancy(toDelete, undefined)
+    }
+
+    return sortedVideosRedundancy
+  }
+
+  private isTooHeavy (videosRedundancy: VideoRedundancyModel[], filesToDuplicate: VideoFileModel[], maxSizeArg: number) {
+    const maxSize = maxSizeArg - this.getTotalFileSizes(filesToDuplicate)
+
+    const redundancyReducer = (previous: number, current: VideoRedundancyModel) => previous + current.VideoFile.size
+    const totalDuplicated = videosRedundancy.reduce(redundancyReducer, 0)
+
+    return totalDuplicated > maxSize
+  }
+
+  private buildNewExpiration () {
+    return new Date(Date.now() + REDUNDANCY.VIDEOS.EXPIRES_AFTER_MS)
+  }
+
+  private buildEntryLogId (object: VideoRedundancyModel) {
+    return `${object.VideoFile.Video.url}-${object.VideoFile.resolution}`
+  }
+
+  private getTotalFileSizes (files: VideoFileModel[]) {
+    const fileReducer = (previous: number, current: VideoFileModel) => previous + current.size
+
+    return files.reduce(fileReducer, 0)
+  }
+}
diff --git a/server/middlewares/validators/redundancy.ts b/server/middlewares/validators/redundancy.ts
new file mode 100644 (file)
index 0000000..d91b475
--- /dev/null
@@ -0,0 +1,80 @@
+import * as express from 'express'
+import 'express-validator'
+import { param, body } from 'express-validator/check'
+import { exists, isBooleanValid, isIdOrUUIDValid, toIntOrNull } from '../../helpers/custom-validators/misc'
+import { isVideoExist } from '../../helpers/custom-validators/videos'
+import { logger } from '../../helpers/logger'
+import { areValidationErrors } from './utils'
+import { VideoModel } from '../../models/video/video'
+import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
+import { isHostValid } from '../../helpers/custom-validators/servers'
+import { getServerActor } from '../../helpers/utils'
+import { ActorFollowModel } from '../../models/activitypub/actor-follow'
+import { SERVER_ACTOR_NAME } from '../../initializers'
+import { ServerModel } from '../../models/server/server'
+
+const videoRedundancyGetValidator = [
+  param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
+  param('resolution')
+    .customSanitizer(toIntOrNull)
+    .custom(exists).withMessage('Should have a valid resolution'),
+  param('fps')
+    .optional()
+    .customSanitizer(toIntOrNull)
+    .custom(exists).withMessage('Should have a valid fps'),
+
+  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
+    logger.debug('Checking videoRedundancyGetValidator parameters', { parameters: req.params })
+
+    if (areValidationErrors(req, res)) return
+    if (!await isVideoExist(req.params.videoId, res)) return
+
+    const video: VideoModel = res.locals.video
+    const videoFile = video.VideoFiles.find(f => {
+      return f.resolution === req.params.resolution && (!req.params.fps || f.fps === req.params.fps)
+    })
+
+    if (!videoFile) return res.status(404).json({ error: 'Video file not found.' })
+    res.locals.videoFile = videoFile
+
+    const videoRedundancy = await VideoRedundancyModel.loadByFileId(videoFile.id)
+    if (!videoRedundancy)return res.status(404).json({ error: 'Video redundancy not found.' })
+    res.locals.videoRedundancy = videoRedundancy
+
+    return next()
+  }
+]
+
+const updateServerRedundancyValidator = [
+  param('host').custom(isHostValid).withMessage('Should have a valid host'),
+  body('redundancyAllowed')
+    .toBoolean()
+    .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed attribute'),
+
+  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
+    logger.debug('Checking updateServerRedundancy parameters', { parameters: req.params })
+
+    if (areValidationErrors(req, res)) return
+
+    const server = await ServerModel.loadByHost(req.params.host)
+
+    if (!server) {
+      return res
+        .status(404)
+        .json({
+          error: `Server ${req.params.host} not found.`
+        })
+        .end()
+    }
+
+    res.locals.server = server
+    return next()
+  }
+]
+
+// ---------------------------------------------------------------------------
+
+export {
+  videoRedundancyGetValidator,
+  updateServerRedundancyValidator
+}
index 8bc09599747608c6520b07c01ce55cc716f736aa..27bb43daedf6d729418a70d75d11339a00bc5afa 100644 (file)
@@ -19,7 +19,7 @@ import {
   UpdatedAt
 } from 'sequelize-typescript'
 import { FollowState } from '../../../shared/models/actors'
-import { AccountFollow } from '../../../shared/models/actors/follow.model'
+import { ActorFollow } from '../../../shared/models/actors/follow.model'
 import { logger } from '../../helpers/logger'
 import { getServerActor } from '../../helpers/utils'
 import { ACTOR_FOLLOW_SCORE } from '../../initializers'
@@ -529,7 +529,7 @@ export class ActorFollowModel extends Model<ActorFollowModel> {
     return ActorFollowModel.findAll(query)
   }
 
-  toFormattedJSON (): AccountFollow {
+  toFormattedJSON (): ActorFollow {
     const follower = this.ActorFollower.toFormattedJSON()
     const following = this.ActorFollowing.toFormattedJSON()
 
index 119d0c1da1bba13c2a09c5afc2643975f78b5ab9..ef8dd9f7cbfa8026b00affe99d8c61b9574a2d47 100644 (file)
@@ -76,7 +76,13 @@ export const unusedActorAttributesForAPI = [
       },
       {
         model: () => VideoChannelModel.unscoped(),
-        required: false
+        required: false,
+        include: [
+          {
+            model: () => AccountModel,
+            required: true
+          }
+        ]
       },
       {
         model: () => ServerModel,
@@ -337,6 +343,7 @@ export class ActorModel extends Model<ActorModel> {
       uuid: this.uuid,
       name: this.preferredUsername,
       host: this.getHost(),
+      hostRedundancyAllowed: this.getRedundancyAllowed(),
       followingCount: this.followingCount,
       followersCount: this.followersCount,
       avatar,
@@ -440,6 +447,10 @@ export class ActorModel extends Model<ActorModel> {
     return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST
   }
 
+  getRedundancyAllowed () {
+    return this.Server ? this.Server.redundancyAllowed : false
+  }
+
   getAvatarUrl () {
     if (!this.avatarId) return undefined
 
diff --git a/server/models/redundancy/video-redundancy.ts b/server/models/redundancy/video-redundancy.ts
new file mode 100644 (file)
index 0000000..48ec772
--- /dev/null
@@ -0,0 +1,249 @@
+import {
+  AfterDestroy,
+  AllowNull,
+  BelongsTo,
+  Column,
+  CreatedAt,
+  DataType,
+  ForeignKey,
+  Is,
+  Model,
+  Scopes,
+  Sequelize,
+  Table,
+  UpdatedAt
+} from 'sequelize-typescript'
+import { ActorModel } from '../activitypub/actor'
+import { throwIfNotValid } from '../utils'
+import { isActivityPubUrlValid, isUrlValid } from '../../helpers/custom-validators/activitypub/misc'
+import { CONSTRAINTS_FIELDS, VIDEO_EXT_MIMETYPE } from '../../initializers'
+import { VideoFileModel } from '../video/video-file'
+import { isDateValid } from '../../helpers/custom-validators/misc'
+import { getServerActor } from '../../helpers/utils'
+import { VideoModel } from '../video/video'
+import { VideoRedundancyStrategy } from '../../../shared/models/redundancy'
+import { logger } from '../../helpers/logger'
+import { CacheFileObject } from '../../../shared'
+import { VideoChannelModel } from '../video/video-channel'
+import { ServerModel } from '../server/server'
+import { sample } from 'lodash'
+import { isTestInstance } from '../../helpers/core-utils'
+
+export enum ScopeNames {
+  WITH_VIDEO = 'WITH_VIDEO'
+}
+
+@Scopes({
+  [ ScopeNames.WITH_VIDEO ]: {
+    include: [
+      {
+        model: () => VideoFileModel,
+        required: true,
+        include: [
+          {
+            model: () => VideoModel,
+            required: true
+          }
+        ]
+      }
+    ]
+  }
+})
+
+@Table({
+  tableName: 'videoRedundancy',
+  indexes: [
+    {
+      fields: [ 'videoFileId' ]
+    },
+    {
+      fields: [ 'actorId' ]
+    },
+    {
+      fields: [ 'url' ],
+      unique: true
+    }
+  ]
+})
+export class VideoRedundancyModel extends Model<VideoRedundancyModel> {
+
+  @CreatedAt
+  createdAt: Date
+
+  @UpdatedAt
+  updatedAt: Date
+
+  @AllowNull(false)
+  @Column
+  expiresOn: Date
+
+  @AllowNull(false)
+  @Is('VideoRedundancyFileUrl', value => throwIfNotValid(value, isUrlValid, 'fileUrl'))
+  @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS_REDUNDANCY.URL.max))
+  fileUrl: string
+
+  @AllowNull(false)
+  @Is('VideoRedundancyUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
+  @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS_REDUNDANCY.URL.max))
+  url: string
+
+  @AllowNull(true)
+  @Column
+  strategy: string // Only used by us
+
+  @ForeignKey(() => VideoFileModel)
+  @Column
+  videoFileId: number
+
+  @BelongsTo(() => VideoFileModel, {
+    foreignKey: {
+      allowNull: false
+    },
+    onDelete: 'cascade'
+  })
+  VideoFile: VideoFileModel
+
+  @ForeignKey(() => ActorModel)
+  @Column
+  actorId: number
+
+  @BelongsTo(() => ActorModel, {
+    foreignKey: {
+      allowNull: false
+    },
+    onDelete: 'cascade'
+  })
+  Actor: ActorModel
+
+  @AfterDestroy
+  static removeFilesAndSendDelete (instance: VideoRedundancyModel) {
+    // Not us
+    if (!instance.strategy) return
+
+    logger.info('Removing video file %s-.', instance.VideoFile.Video.uuid, instance.VideoFile.resolution)
+
+    return instance.VideoFile.Video.removeFile(instance.VideoFile)
+  }
+
+  static loadByFileId (videoFileId: number) {
+    const query = {
+      where: {
+        videoFileId
+      }
+    }
+
+    return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query)
+  }
+
+  static loadByUrl (url: string) {
+    const query = {
+      where: {
+        url
+      }
+    }
+
+    return VideoRedundancyModel.findOne(query)
+  }
+
+  static async findMostViewToDuplicate (randomizedFactor: number) {
+    // On VideoModel!
+    const query = {
+      logging: !isTestInstance(),
+      limit: randomizedFactor,
+      order: [ [ 'views', 'DESC' ] ],
+      include: [
+        {
+          model: VideoFileModel.unscoped(),
+          required: true,
+          where: {
+            id: {
+              [ Sequelize.Op.notIn ]: await VideoRedundancyModel.buildExcludeIn()
+            }
+          }
+        },
+        {
+          attributes: [],
+          model: VideoChannelModel.unscoped(),
+          required: true,
+          include: [
+            {
+              attributes: [],
+              model: ActorModel.unscoped(),
+              required: true,
+              include: [
+                {
+                  attributes: [],
+                  model: ServerModel.unscoped(),
+                  required: true,
+                  where: {
+                    redundancyAllowed: true
+                  }
+                }
+              ]
+            }
+          ]
+        }
+      ]
+    }
+
+    const rows = await VideoModel.unscoped().findAll(query)
+
+    return sample(rows)
+  }
+
+  static async getVideoFiles (strategy: VideoRedundancyStrategy) {
+    const actor = await getServerActor()
+
+    const queryVideoFiles = {
+      logging: !isTestInstance(),
+      where: {
+        actorId: actor.id,
+        strategy
+      }
+    }
+
+    return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO)
+                               .findAll(queryVideoFiles)
+  }
+
+  static listAllExpired () {
+    const query = {
+      logging: !isTestInstance(),
+      where: {
+        expiresOn: {
+          [Sequelize.Op.lt]: new Date()
+        }
+      }
+    }
+
+    return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO)
+                               .findAll(query)
+  }
+
+  toActivityPubObject (): CacheFileObject {
+    return {
+      id: this.url,
+      type: 'CacheFile' as 'CacheFile',
+      object: this.VideoFile.Video.url,
+      expires: this.expiresOn.toISOString(),
+      url: {
+        type: 'Link',
+        mimeType: VIDEO_EXT_MIMETYPE[ this.VideoFile.extname ] as any,
+        href: this.fileUrl,
+        height: this.VideoFile.resolution,
+        size: this.VideoFile.size,
+        fps: this.VideoFile.fps
+      }
+    }
+  }
+
+  private static async buildExcludeIn () {
+    const actor = await getServerActor()
+
+    return Sequelize.literal(
+      '(' +
+        `SELECT "videoFileId" FROM "videoRedundancy" WHERE "actorId" = ${actor.id} AND "expiresOn" >= NOW()` +
+      ')'
+    )
+  }
+}
index 9749f503e30e7782f0ea3bf12a0dd62ed2829c12..ca3b24d51dac3c6c6161398a047fb0d64a4f9fa8 100644 (file)
@@ -1,4 +1,4 @@
-import { AllowNull, Column, CreatedAt, HasMany, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
+import { AllowNull, Column, CreatedAt, Default, HasMany, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
 import { isHostValid } from '../../helpers/custom-validators/servers'
 import { ActorModel } from '../activitypub/actor'
 import { throwIfNotValid } from '../utils'
@@ -19,6 +19,11 @@ export class ServerModel extends Model<ServerModel> {
   @Column
   host: string
 
+  @AllowNull(false)
+  @Default(false)
+  @Column
+  redundancyAllowed: boolean
+
   @CreatedAt
   createdAt: Date
 
@@ -34,4 +39,14 @@ export class ServerModel extends Model<ServerModel> {
     hooks: true
   })
   Actors: ActorModel[]
+
+  static loadByHost (host: string) {
+    const query = {
+      where: {
+        host
+      }
+    }
+
+    return ServerModel.findOne(query)
+  }
 }
index 3bc4855f38afb2bb5c86cb1adf24a8ed96caf0a6..0907ea569fa3f0f3a21c34056842d0ef924d9174 100644 (file)
@@ -1,5 +1,18 @@
 import { values } from 'lodash'
-import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
+import {
+  AllowNull,
+  BelongsTo,
+  Column,
+  CreatedAt,
+  DataType,
+  Default,
+  ForeignKey,
+  HasMany,
+  Is,
+  Model,
+  Table,
+  UpdatedAt
+} from 'sequelize-typescript'
 import {
   isVideoFileInfoHashValid,
   isVideoFileResolutionValid,
@@ -10,6 +23,7 @@ import { CONSTRAINTS_FIELDS } from '../../initializers'
 import { throwIfNotValid } from '../utils'
 import { VideoModel } from './video'
 import * as Sequelize from 'sequelize'
+import { VideoRedundancyModel } from '../redundancy/video-redundancy'
 
 @Table({
   tableName: 'videoFile',
@@ -70,6 +84,15 @@ export class VideoFileModel extends Model<VideoFileModel> {
   })
   Video: VideoModel
 
+  @HasMany(() => VideoRedundancyModel, {
+    foreignKey: {
+      allowNull: false
+    },
+    onDelete: 'CASCADE',
+    hooks: true
+  })
+  RedundancyVideos: VideoRedundancyModel[]
+
   static isInfohashExists (infoHash: string) {
     const query = 'SELECT 1 FROM "videoFile" WHERE "infoHash" = $infoHash LIMIT 1'
     const options = {
index 86316653f026a861f2028c7a383128f7e72ebdb2..27c631dcd7e071a8f9c9a163feafb4ca0518d803 100644 (file)
@@ -27,13 +27,13 @@ import {
   Table,
   UpdatedAt
 } from 'sequelize-typescript'
-import { VideoPrivacy, VideoResolution, VideoState } from '../../../shared'
+import { ActivityUrlObject, VideoPrivacy, VideoResolution, VideoState } from '../../../shared'
 import { VideoTorrentObject } from '../../../shared/models/activitypub/objects'
 import { Video, VideoDetails, VideoFile } from '../../../shared/models/videos'
 import { VideoFilter } from '../../../shared/models/videos/video-query.type'
 import { createTorrentPromise, peertubeTruncate } from '../../helpers/core-utils'
 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
-import { isBooleanValid } from '../../helpers/custom-validators/misc'
+import { isArray, isBooleanValid } from '../../helpers/custom-validators/misc'
 import {
   isVideoCategoryValid,
   isVideoDescriptionValid,
@@ -90,6 +90,7 @@ import { VideoCaptionModel } from './video-caption'
 import { VideoBlacklistModel } from './video-blacklist'
 import { copy, remove, rename, stat, writeFile } from 'fs-extra'
 import { VideoViewModel } from './video-views'
+import { VideoRedundancyModel } from '../redundancy/video-redundancy'
 
 // FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation
 const indexes: Sequelize.DefineIndexesOptions[] = [
@@ -470,7 +471,13 @@ type AvailableForListIDsOptions = {
     include: [
       {
         model: () => VideoFileModel.unscoped(),
-        required: false
+        required: false,
+        include: [
+          {
+            model: () => VideoRedundancyModel.unscoped(),
+            required: false
+          }
+        ]
       }
     ]
   },
@@ -633,6 +640,7 @@ export class VideoModel extends Model<VideoModel> {
       name: 'videoId',
       allowNull: false
     },
+    hooks: true,
     onDelete: 'cascade'
   })
   VideoFiles: VideoFileModel[]
@@ -1325,9 +1333,7 @@ export class VideoModel extends Model<VideoModel> {
         [ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ],
         [ CONFIG.WEBSERVER.URL + '/tracker/announce' ]
       ],
-      urlList: [
-        CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
-      ]
+      urlList: [ CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile) ]
     }
 
     const torrent = await createTorrentPromise(this.getVideoFilePath(videoFile), options)
@@ -1535,11 +1541,11 @@ export class VideoModel extends Model<VideoModel> {
       }
     }
 
-    const url = []
+    const url: ActivityUrlObject[] = []
     for (const file of this.VideoFiles) {
       url.push({
         type: 'Link',
-        mimeType: VIDEO_EXT_MIMETYPE[ file.extname ],
+        mimeType: VIDEO_EXT_MIMETYPE[ file.extname ] as any,
         href: this.getVideoFileUrl(file, baseUrlHttp),
         height: file.resolution,
         size: file.size,
@@ -1548,14 +1554,14 @@ export class VideoModel extends Model<VideoModel> {
 
       url.push({
         type: 'Link',
-        mimeType: 'application/x-bittorrent',
+        mimeType: 'application/x-bittorrent' as 'application/x-bittorrent',
         href: this.getTorrentUrl(file, baseUrlHttp),
         height: file.resolution
       })
 
       url.push({
         type: 'Link',
-        mimeType: 'application/x-bittorrent;x-scheme-handler/magnet',
+        mimeType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
         href: this.generateMagnetUri(file, baseUrlHttp, baseUrlWs),
         height: file.resolution
       })
@@ -1796,7 +1802,7 @@ export class VideoModel extends Model<VideoModel> {
       (now - updatedAtTime) > ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL
   }
 
-  private getBaseUrls () {
+  getBaseUrls () {
     let baseUrlHttp
     let baseUrlWs
 
@@ -1811,39 +1817,42 @@ export class VideoModel extends Model<VideoModel> {
     return { baseUrlHttp, baseUrlWs }
   }
 
-  private getThumbnailUrl (baseUrlHttp: string) {
+  generateMagnetUri (videoFile: VideoFileModel, baseUrlHttp: string, baseUrlWs: string) {
+    const xs = this.getTorrentUrl(videoFile, baseUrlHttp)
+    const announce = [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
+    let urlList = [ this.getVideoFileUrl(videoFile, baseUrlHttp) ]
+
+    const redundancies = videoFile.RedundancyVideos
+    if (isArray(redundancies)) urlList = urlList.concat(redundancies.map(r => r.fileUrl))
+
+    const magnetHash = {
+      xs,
+      announce,
+      urlList,
+      infoHash: videoFile.infoHash,
+      name: this.name
+    }
+
+    return magnetUtil.encode(magnetHash)
+  }
+
+  getThumbnailUrl (baseUrlHttp: string) {
     return baseUrlHttp + STATIC_PATHS.THUMBNAILS + this.getThumbnailName()
   }
 
-  private getTorrentUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
+  getTorrentUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
     return baseUrlHttp + STATIC_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
   }
 
-  private getTorrentDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
+  getTorrentDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
     return baseUrlHttp + STATIC_DOWNLOAD_PATHS.TORRENTS + this.getTorrentFileName(videoFile)
   }
 
-  private getVideoFileUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
+  getVideoFileUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
     return baseUrlHttp + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
   }
 
-  private getVideoFileDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
+  getVideoFileDownloadUrl (videoFile: VideoFileModel, baseUrlHttp: string) {
     return baseUrlHttp + STATIC_DOWNLOAD_PATHS.VIDEOS + this.getVideoFilename(videoFile)
   }
-
-  private generateMagnetUri (videoFile: VideoFileModel, baseUrlHttp: string, baseUrlWs: string) {
-    const xs = this.getTorrentUrl(videoFile, baseUrlHttp)
-    const announce = [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
-    const urlList = [ this.getVideoFileUrl(videoFile, baseUrlHttp) ]
-
-    const magnetHash = {
-      xs,
-      announce,
-      urlList,
-      infoHash: videoFile.infoHash,
-      name: this.name
-    }
-
-    return magnetUtil.encode(magnetHash)
-  }
 }
index 2bc3b27d9e8b039ff41684e6449c99a52ebce607..cdc95c81a6fc848de1b2b2b049efded4ad572ade 100644 (file)
@@ -169,15 +169,6 @@ describe('Test server follows API validators', function () {
           statusCodeExpected: 404
         })
       })
-
-      it('Should succeed with the correct parameters', async function () {
-        await makeDeleteRequest({
-          url: server.url,
-          path: path + '/localhost:9002',
-          token: server.accessToken,
-          statusCodeExpected: 404
-        })
-      })
     })
   })
 
index 777acbb0f876b59b7eea9fe18189c2c99ccadbae..44460a1673fd6e53044ca50ecf4bae55181823b0 100644 (file)
@@ -1,15 +1,17 @@
 // Order of the tests we want to execute
 import './accounts'
+import './config'
 import './follows'
 import './jobs'
+import './redundancy'
+import './search'
 import './services'
+import './user-subscriptions'
 import './users'
 import './video-abuses'
 import './video-blacklist'
 import './video-captions'
 import './video-channels'
 import './video-comments'
-import './videos'
 import './video-imports'
-import './search'
-import './user-subscriptions'
+import './videos'
diff --git a/server/tests/api/check-params/redundancy.ts b/server/tests/api/check-params/redundancy.ts
new file mode 100644 (file)
index 0000000..aa588e3
--- /dev/null
@@ -0,0 +1,103 @@
+/* tslint:disable:no-unused-expression */
+
+import 'mocha'
+
+import {
+  createUser,
+  doubleFollow,
+  flushAndRunMultipleServers,
+  flushTests,
+  killallServers,
+  makePutBodyRequest,
+  ServerInfo,
+  setAccessTokensToServers,
+  userLogin
+} from '../../utils'
+
+describe('Test server redundancy API validators', function () {
+  let servers: ServerInfo[]
+  let userAccessToken = null
+
+  // ---------------------------------------------------------------
+
+  before(async function () {
+    this.timeout(30000)
+
+    await flushTests()
+    servers = await flushAndRunMultipleServers(2)
+
+    await setAccessTokensToServers(servers)
+    await doubleFollow(servers[0], servers[1])
+
+    const user = {
+      username: 'user1',
+      password: 'password'
+    }
+
+    await createUser(servers[0].url, servers[0].accessToken, user.username, user.password)
+    userAccessToken = await userLogin(servers[0], user)
+  })
+
+  describe('When updating redundancy', function () {
+    const path = '/api/v1/server/redundancy'
+
+    it('Should fail with an invalid token', async function () {
+      await makePutBodyRequest({
+        url: servers[0].url,
+        path: path + '/localhost:9002',
+        fields: { redundancyAllowed: true },
+        token: 'fake_token',
+        statusCodeExpected: 401
+      })
+    })
+
+    it('Should fail if the user is not an administrator', async function () {
+      await makePutBodyRequest({
+        url: servers[0].url,
+        path: path + '/localhost:9002',
+        fields: { redundancyAllowed: true },
+        token: userAccessToken,
+        statusCodeExpected: 403
+      })
+    })
+
+    it('Should fail if we do not follow this server', async function () {
+      await makePutBodyRequest({
+        url: servers[0].url,
+        path: path + '/example.com',
+        fields: { redundancyAllowed: true },
+        token: servers[0].accessToken,
+        statusCodeExpected: 404
+      })
+    })
+
+    it('Should fail without de redundancyAllowed param', async function () {
+      await makePutBodyRequest({
+        url: servers[0].url,
+        path: path + '/localhost:9002',
+        fields: { blabla: true },
+        token: servers[0].accessToken,
+        statusCodeExpected: 400
+      })
+    })
+
+    it('Should succeed with the correct parameters', async function () {
+      await makePutBodyRequest({
+        url: servers[0].url,
+        path: path + '/localhost:9002',
+        fields: { redundancyAllowed: true },
+        token: servers[0].accessToken,
+        statusCodeExpected: 204
+      })
+    })
+  })
+
+  after(async function () {
+    killallServers(servers)
+
+    // Keep the logs if the test failed
+    if (this['ok']) {
+      await flushTests()
+    }
+  })
+})
index eeb8b7a28ef5fd0990624b7da860cbcb86e0557e..c74c68a33b0de613d1d3cfcc9652abd6b705b9ef 100644 (file)
@@ -3,6 +3,7 @@ import './email'
 import './follows'
 import './handle-down'
 import './jobs'
+import './redundancy'
 import './reverse-proxy'
 import './stats'
 import './tracker'
diff --git a/server/tests/api/server/redundancy.ts b/server/tests/api/server/redundancy.ts
new file mode 100644 (file)
index 0000000..c0ec75a
--- /dev/null
@@ -0,0 +1,140 @@
+/* tslint:disable:no-unused-expression */
+
+import * as chai from 'chai'
+import 'mocha'
+import { VideoDetails } from '../../../../shared/models/videos'
+import {
+  doubleFollow,
+  flushAndRunMultipleServers,
+  flushTests,
+  getFollowingListPaginationAndSort,
+  getVideo,
+  killallServers,
+  ServerInfo,
+  setAccessTokensToServers,
+  uploadVideo,
+  wait,
+  root, viewVideo
+} from '../../utils'
+import { waitJobs } from '../../utils/server/jobs'
+import * as magnetUtil from 'magnet-uri'
+import { updateRedundancy } from '../../utils/server/redundancy'
+import { ActorFollow } from '../../../../shared/models/actors'
+import { readdir } from 'fs-extra'
+import { join } from 'path'
+
+const expect = chai.expect
+
+function checkMagnetWebseeds (file: { magnetUri: string, resolution: { id: number } }, baseWebseeds: string[]) {
+  const parsed = magnetUtil.decode(file.magnetUri)
+
+  for (const ws of baseWebseeds) {
+    const found = parsed.urlList.find(url => url === `${ws}-${file.resolution.id}.mp4`)
+    expect(found, `Webseed ${ws} not found in ${file.magnetUri}`).to.not.be.undefined
+  }
+}
+
+describe('Test videos redundancy', function () {
+  let servers: ServerInfo[] = []
+  let video1Server2UUID: string
+  let video2Server2UUID: string
+
+  before(async function () {
+    this.timeout(120000)
+
+    servers = await flushAndRunMultipleServers(3)
+
+    // Get the access tokens
+    await setAccessTokensToServers(servers)
+
+    {
+      const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 1 server 2' })
+      video1Server2UUID = res.body.video.uuid
+
+      await viewVideo(servers[1].url, video1Server2UUID)
+    }
+
+    {
+      const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video 2 server 2' })
+      video2Server2UUID = res.body.video.uuid
+    }
+
+    await waitJobs(servers)
+
+    // Server 1 and server 2 follow each other
+    await doubleFollow(servers[0], servers[1])
+    // Server 1 and server 3 follow each other
+    await doubleFollow(servers[0], servers[2])
+    // Server 2 and server 3 follow each other
+    await doubleFollow(servers[1], servers[2])
+
+    await waitJobs(servers)
+  })
+
+  it('Should have 1 webseed on the first video', async function () {
+    const webseeds = [
+      'http://localhost:9002/static/webseed/' + video1Server2UUID
+    ]
+
+    for (const server of servers) {
+      const res = await getVideo(server.url, video1Server2UUID)
+
+      const video: VideoDetails = res.body
+      video.files.forEach(f => checkMagnetWebseeds(f, webseeds))
+    }
+  })
+
+  it('Should enable redundancy on server 1', async function () {
+    await updateRedundancy(servers[0].url, servers[0].accessToken, servers[1].host, true)
+
+    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.true
+  })
+
+  it('Should have 2 webseed on the first video', async function () {
+    this.timeout(40000)
+
+    await waitJobs(servers)
+    await wait(15000)
+    await waitJobs(servers)
+
+    const webseeds = [
+      'http://localhost:9001/static/webseed/' + video1Server2UUID,
+      'http://localhost:9002/static/webseed/' + video1Server2UUID
+    ]
+
+    for (const server of servers) {
+      const res = await getVideo(server.url, video1Server2UUID)
+
+      const video: VideoDetails = res.body
+
+      for (const file of video.files) {
+        checkMagnetWebseeds(file, webseeds)
+      }
+    }
+
+    const files = await readdir(join(root(), 'test1', 'videos'))
+    expect(files).to.have.lengthOf(4)
+
+    for (const resolution of [ 240, 360, 480, 720 ]) {
+      expect(files.find(f => f === `${video1Server2UUID}-${resolution}.mp4`)).to.not.be.undefined
+    }
+  })
+
+  after(async function () {
+    killallServers(servers)
+
+    // Keep the logs if the test failed
+    if (this['ok']) {
+      await flushTests()
+    }
+  })
+})
index d21fb5e5895bbabfdc18cc5dd9445f4773caf519..8a65a958b1145e0b688f5e5a6597ec5761deb274 100644 (file)
@@ -1,5 +1,4 @@
 import * as request from 'supertest'
-import { wait } from '../miscs/miscs'
 import { ServerInfo } from './servers'
 import { waitJobs } from './jobs'
 
diff --git a/server/tests/utils/server/redundancy.ts b/server/tests/utils/server/redundancy.ts
new file mode 100644 (file)
index 0000000..c39ff2c
--- /dev/null
@@ -0,0 +1,17 @@
+import { makePutBodyRequest } from '../requests/requests'
+
+async function updateRedundancy (url: string, accessToken: string, host: string, redundancyAllowed: boolean, expectedStatus = 204) {
+  const path = '/api/v1/server/redundancy/' + host
+
+  return makePutBodyRequest({
+    url,
+    path,
+    token: accessToken,
+    fields: { redundancyAllowed },
+    statusCodeExpected: expectedStatus
+  })
+}
+
+export {
+  updateRedundancy
+}
index 46e883e5f081d7a5a5a7dae888ab1b01e3919113..44cb99efb9da885254ba4300b084df782233829d 100644 (file)
@@ -1,6 +1,6 @@
 import { ActivityPubActor } from './activitypub-actor'
 import { ActivityPubSignature } from './activitypub-signature'
-import { VideoTorrentObject } from './objects'
+import { CacheFileObject, VideoTorrentObject } from './objects'
 import { DislikeObject } from './objects/dislike-object'
 import { VideoAbuseObject } from './objects/video-abuse-object'
 import { VideoCommentObject } from './objects/video-comment-object'
@@ -29,12 +29,12 @@ export interface BaseActivity {
 
 export interface ActivityCreate extends BaseActivity {
   type: 'Create'
-  object: VideoTorrentObject | VideoAbuseObject | ViewObject | DislikeObject | VideoCommentObject
+  object: VideoTorrentObject | VideoAbuseObject | ViewObject | DislikeObject | VideoCommentObject | CacheFileObject
 }
 
 export interface ActivityUpdate extends BaseActivity {
   type: 'Update'
-  object: VideoTorrentObject | ActivityPubActor
+  object: VideoTorrentObject | ActivityPubActor | CacheFileObject
 }
 
 export interface ActivityDelete extends BaseActivity {
diff --git a/shared/models/activitypub/objects/cache-file-object.ts b/shared/models/activitypub/objects/cache-file-object.ts
new file mode 100644 (file)
index 0000000..0a5125f
--- /dev/null
@@ -0,0 +1,9 @@
+import { ActivityVideoUrlObject } from './common-objects'
+
+export interface CacheFileObject {
+  id: string
+  type: 'CacheFile',
+  object: string
+  expires: string
+  url: ActivityVideoUrlObject
+}
index ff2cfdbb4894850d0cf9120f1ebcd0a4bf9c550e..1de60da94f5cf4445fafbf8e79d6c6566f3c8721 100644 (file)
@@ -17,16 +17,31 @@ export interface ActivityIconObject {
   height: number
 }
 
-export interface ActivityUrlObject {
+export type ActivityVideoUrlObject = {
   type: 'Link'
-  mimeType: 'video/mp4' | 'video/webm' | 'application/x-bittorrent' | 'application/x-bittorrent;x-scheme-handler/magnet'
+  mimeType: 'video/mp4' | 'video/webm' | 'video/ogg'
   href: string
   height: number
-
-  size?: number
-  fps?: number
+  size: number
+  fps: number
 }
 
+export type ActivityUrlObject =
+  ActivityVideoUrlObject
+  |
+  {
+    type: 'Link'
+    mimeType: 'application/x-bittorrent' | 'application/x-bittorrent;x-scheme-handler/magnet'
+    href: string
+    height: number
+  }
+  |
+  {
+    type: 'Link'
+    mimeType: 'text/html'
+    href: string
+  }
+
 export interface ActivityPubAttributedTo {
   type: 'Group' | 'Person'
   id: string
index 3efd3ef131328816c42c96af66132cd9b386afed..fba61e12fcfb103ba247eff3a2d89da6f175de5e 100644 (file)
@@ -1,3 +1,4 @@
+export * from './cache-file-object'
 export * from './common-objects'
 export * from './video-abuse-object'
 export * from './video-torrent-object'
index 90de8967b251c08bca479ce5d79ff572a53abc6f..8504c178fa0d50eed5e52345d3db86c66d34c6f3 100644 (file)
@@ -1,10 +1,10 @@
 import {
   ActivityIconObject,
-  ActivityIdentifierObject, ActivityPubAttributedTo,
+  ActivityIdentifierObject,
+  ActivityPubAttributedTo,
   ActivityTagObject,
   ActivityUrlObject
 } from './common-objects'
-import { ActivityPubOrderedCollection } from '../activitypub-ordered-collection'
 import { VideoState } from '../../videos'
 
 export interface VideoTorrentObject {
index 70562bfc7d3278ae9e06f88e245f23bae27afd64..7de638cbac462213286f672dc8ad6adc81badc44 100644 (file)
@@ -2,10 +2,10 @@ import { Actor } from './actor.model'
 
 export type FollowState = 'pending' | 'accepted'
 
-export interface AccountFollow {
+export interface ActorFollow {
   id: number
-  follower: Actor
-  following: Actor
+  follower: Actor & { hostRedundancyAllowed: boolean }
+  following: Actor & { hostRedundancyAllowed: boolean }
   score: number
   state: FollowState
   createdAt: Date
diff --git a/shared/models/avatars/index.ts b/shared/models/avatars/index.ts
new file mode 100644 (file)
index 0000000..65e8e08
--- /dev/null
@@ -0,0 +1 @@
+export * from './avatar.model'
index 170f620e716837f6bae968653537516347ba84e0..e61d6cbdc791fa5d37c59f94134ad14c4c2e3e0c 100644 (file)
@@ -1,5 +1,7 @@
-export * from './actors'
 export * from './activitypub'
+export * from './actors'
+export * from './avatars'
+export * from './redundancy'
 export * from './users'
 export * from './videos'
 export * from './feeds'
diff --git a/shared/models/redundancy/index.ts b/shared/models/redundancy/index.ts
new file mode 100644 (file)
index 0000000..61bf0fc
--- /dev/null
@@ -0,0 +1 @@
+export * from './videos-redundancy.model'
diff --git a/shared/models/redundancy/videos-redundancy.model.ts b/shared/models/redundancy/videos-redundancy.model.ts
new file mode 100644 (file)
index 0000000..eb84964
--- /dev/null
@@ -0,0 +1,6 @@
+export type VideoRedundancyStrategy = 'most-views'
+
+export interface VideosRedundancy {
+  strategy: VideoRedundancyStrategy
+  size: number
+}
index 64ad3e9b9aecadc4ae609980e762fa97b2d9205a..c4ccd632f376cc2c55b83c03232ee19dfb545b14 100644 (file)
@@ -3,6 +3,7 @@ export enum UserRight {
 
   MANAGE_USERS,
   MANAGE_SERVER_FOLLOW,
+  MANAGE_SERVER_REDUNDANCY,
   MANAGE_VIDEO_ABUSES,
   MANAGE_JOBS,
   MANAGE_CONFIGURATION,
index 735d5671522613822f008d1b9fc9a16190fe5100..c8fb211170bfffe9c39b960c659da791413d1f16 100644 (file)
--- a/yarn.lock
+++ b/yarn.lock
     "@types/bluebird" "*"
     "@types/ioredis" "*"
 
+"@types/bytes@^3.0.0":
+  version "3.0.0"
+  resolved "https://registry.yarnpkg.com/@types/bytes/-/bytes-3.0.0.tgz#549eeacd0a8fecfaa459334583a4edcee738e6db"
+
 "@types/caseless@*":
   version "0.12.1"
   resolved "https://registry.yarnpkg.com/@types/caseless/-/caseless-0.12.1.tgz#9794c69c8385d0192acc471a540d1f8e0d16218a"
@@ -993,7 +997,7 @@ bytes@1:
   version "1.0.0"
   resolved "https://registry.yarnpkg.com/bytes/-/bytes-1.0.0.tgz#3569ede8ba34315fab99c3e92cb04c7220de1fa8"
 
-bytes@3.0.0:
+bytes@3.0.0, bytes@^3.0.0:
   version "3.0.0"
   resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.0.0.tgz#d32815404d689699f85a4ea4fa8755dd13a96048"