]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/user-subscription/subscribe-button.component.ts
Add ability to list all local videos on client
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / user-subscription / subscribe-button.component.ts
index 46d6dbaf7f861253e72b611fce629007ccc8dc27..315ea5037d8d13cb3ab1329a90eecea27d0aaf6c 100644 (file)
@@ -1,11 +1,12 @@
 import { Component, Input, OnInit } from '@angular/core'
+import { Router } from '@angular/router'
 import { AuthService } from '@app/core'
-import { RestExtractor } from '@app/shared/rest'
-import { RedirectService } from '@app/core/routing/redirect.service'
 import { UserSubscriptionService } from '@app/shared/user-subscription/user-subscription.service'
 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
 import { NotificationsService } from 'angular2-notifications'
 import { I18n } from '@ngx-translate/i18n-polyfill'
+import { VideoService } from '@app/shared/video/video.service'
+import { FeedFormat } from '../../../../../shared/models/feeds'
 
 @Component({
   selector: 'my-subscribe-button',
@@ -15,32 +16,48 @@ import { I18n } from '@ngx-translate/i18n-polyfill'
 export class SubscribeButtonComponent implements OnInit {
   @Input() videoChannel: VideoChannel
   @Input() displayFollowers = false
+  @Input() size: 'small' | 'normal' = 'normal'
 
   subscribed: boolean
 
   constructor (
     private authService: AuthService,
-    private restExtractor: RestExtractor,
-    private redirectService: RedirectService,
+    private router: Router,
     private notificationsService: NotificationsService,
     private userSubscriptionService: UserSubscriptionService,
-    private i18n: I18n
+    private i18n: I18n,
+    private videoService: VideoService
   ) { }
 
   get uri () {
     return this.videoChannel.name + '@' + this.videoChannel.host
   }
 
+  get uriAccount () {
+    return this.videoChannel.ownerAccount.name + '@' + this.videoChannel.host
+  }
+
   ngOnInit () {
-    this.userSubscriptionService.isSubscriptionExists(this.uri)
-      .subscribe(
-        exists => this.subscribed = exists,
+    if (this.isUserLoggedIn()) {
+      this.userSubscriptionService.isSubscriptionExists(this.uri)
+        .subscribe(
+          res => this.subscribed = res[this.uri],
 
-        err => this.notificationsService.error(this.i18n('Error'), err.message)
-      )
+          err => this.notificationsService.error(this.i18n('Error'), err.message)
+        )
+    }
   }
 
   subscribe () {
+    if (this.isUserLoggedIn()) {
+      this.localSubscribe()
+    } else {
+      this.authService.redirectUrl = this.router.url
+      this.gotoLogin()
+    }
+  }
+
+  localSubscribe () {
     this.userSubscriptionService.addSubscription(this.uri)
       .subscribe(
         () => {
@@ -57,6 +74,12 @@ export class SubscribeButtonComponent implements OnInit {
   }
 
   unsubscribe () {
+    if (this.isUserLoggedIn()) {
+      this.localUnsubscribe()
+    }
+  }
+
+  localUnsubscribe () {
     this.userSubscriptionService.deleteSubscription(this.uri)
         .subscribe(
           () => {
@@ -71,4 +94,20 @@ export class SubscribeButtonComponent implements OnInit {
           err => this.notificationsService.error(this.i18n('Error'), err.message)
         )
   }
+
+  isUserLoggedIn () {
+    return this.authService.isLoggedIn()
+  }
+
+  gotoLogin () {
+    this.router.navigate([ '/login' ])
+  }
+
+  rssOpen () {
+    const rssFeed = this.videoService
+                      .getVideoChannelFeedUrls(this.videoChannel.id)
+                      .find(i => i.format === FeedFormat.RSS)
+
+    window.open(rssFeed.url)
+  }
 }