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