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