]>
Commit | Line | Data |
---|---|---|
22a16e36 | 1 | import { Component, Input, OnInit } from '@angular/core' |
660d11e9 RK |
2 | import { Router } from '@angular/router' |
3 | import { AuthService } from '@app/core' | |
22a16e36 C |
4 | import { UserSubscriptionService } from '@app/shared/user-subscription/user-subscription.service' |
5 | import { VideoChannel } from '@app/shared/video-channel/video-channel.model' | |
6 | import { NotificationsService } from 'angular2-notifications' | |
7 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
39ba2e8e C |
8 | import { VideoService } from '@app/shared/video/video.service' |
9 | import { FeedFormat } from '../../../../../shared/models/feeds' | |
22a16e36 C |
10 | |
11 | @Component({ | |
12 | selector: 'my-subscribe-button', | |
13 | templateUrl: './subscribe-button.component.html', | |
14 | styleUrls: [ './subscribe-button.component.scss' ] | |
15 | }) | |
16 | export class SubscribeButtonComponent implements OnInit { | |
17 | @Input() videoChannel: VideoChannel | |
18 | @Input() displayFollowers = false | |
f37dc0dd | 19 | @Input() size: 'small' | 'normal' = 'normal' |
22a16e36 C |
20 | |
21 | subscribed: boolean | |
22 | ||
23 | constructor ( | |
660d11e9 RK |
24 | private authService: AuthService, |
25 | private router: Router, | |
22a16e36 C |
26 | private notificationsService: NotificationsService, |
27 | private userSubscriptionService: UserSubscriptionService, | |
39ba2e8e C |
28 | private i18n: I18n, |
29 | private videoService: VideoService | |
22a16e36 C |
30 | ) { } |
31 | ||
32 | get uri () { | |
33 | return this.videoChannel.name + '@' + this.videoChannel.host | |
34 | } | |
35 | ||
660d11e9 RK |
36 | get uriAccount () { |
37 | return this.videoChannel.ownerAccount.name + '@' + this.videoChannel.host | |
38 | } | |
39 | ||
22a16e36 | 40 | ngOnInit () { |
660d11e9 RK |
41 | if (this.isUserLoggedIn()) { |
42 | this.userSubscriptionService.isSubscriptionExists(this.uri) | |
43 | .subscribe( | |
44 | res => this.subscribed = res[this.uri], | |
22a16e36 | 45 | |
660d11e9 RK |
46 | err => this.notificationsService.error(this.i18n('Error'), err.message) |
47 | ) | |
48 | } | |
22a16e36 C |
49 | } |
50 | ||
51 | subscribe () { | |
660d11e9 RK |
52 | if (this.isUserLoggedIn()) { |
53 | this.localSubscribe() | |
54 | } else { | |
8be9f43a | 55 | this.authService.redirectUrl = this.router.url |
660d11e9 RK |
56 | this.gotoLogin() |
57 | } | |
58 | } | |
59 | ||
60 | localSubscribe () { | |
22a16e36 C |
61 | this.userSubscriptionService.addSubscription(this.uri) |
62 | .subscribe( | |
63 | () => { | |
64 | this.subscribed = true | |
65 | ||
66 | this.notificationsService.success( | |
67 | this.i18n('Subscribed'), | |
68 | this.i18n('Subscribed to {{nameWithHost}}', { nameWithHost: this.videoChannel.displayName }) | |
69 | ) | |
70 | }, | |
71 | ||
72 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
73 | ) | |
74 | } | |
75 | ||
76 | unsubscribe () { | |
660d11e9 RK |
77 | if (this.isUserLoggedIn()) { |
78 | this.localUnsubscribe() | |
79 | } | |
80 | } | |
81 | ||
82 | localUnsubscribe () { | |
22a16e36 C |
83 | this.userSubscriptionService.deleteSubscription(this.uri) |
84 | .subscribe( | |
85 | () => { | |
86 | this.subscribed = false | |
87 | ||
88 | this.notificationsService.success( | |
89 | this.i18n('Unsubscribed'), | |
90 | this.i18n('Unsubscribed from {{nameWithHost}}', { nameWithHost: this.videoChannel.displayName }) | |
91 | ) | |
92 | }, | |
93 | ||
94 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
95 | ) | |
96 | } | |
660d11e9 RK |
97 | |
98 | isUserLoggedIn () { | |
99 | return this.authService.isLoggedIn() | |
100 | } | |
101 | ||
102 | gotoLogin () { | |
103 | this.router.navigate([ '/login' ]) | |
104 | } | |
105 | ||
106 | rssOpen () { | |
39ba2e8e C |
107 | const rssFeed = this.videoService |
108 | .getVideoChannelFeedUrls(this.videoChannel.id) | |
109 | .find(i => i.format === FeedFormat.RSS) | |
110 | ||
111 | window.open(rssFeed.url) | |
660d11e9 | 112 | } |
22a16e36 | 113 | } |