diff options
author | Rigel Kent <sendmemail@rigelk.eu> | 2020-01-08 22:13:47 +0100 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2020-01-10 10:12:09 +0100 |
commit | 9270ccf6dca5b2955ad126947d4296deb385fdcb (patch) | |
tree | 120807348c51437aaa35e52bed9122cdfc08437f /client/src/app/shared/user-subscription/user-subscription.service.ts | |
parent | b061c8edb053d4a7a02f09d93d406f6a8c58006e (diff) | |
download | PeerTube-9270ccf6dca5b2955ad126947d4296deb385fdcb.tar.gz PeerTube-9270ccf6dca5b2955ad126947d4296deb385fdcb.tar.zst PeerTube-9270ccf6dca5b2955ad126947d4296deb385fdcb.zip |
Make subscribe buttons observe subscription statuses to synchronise
Diffstat (limited to 'client/src/app/shared/user-subscription/user-subscription.service.ts')
-rw-r--r-- | client/src/app/shared/user-subscription/user-subscription.service.ts | 92 |
1 files changed, 82 insertions, 10 deletions
diff --git a/client/src/app/shared/user-subscription/user-subscription.service.ts b/client/src/app/shared/user-subscription/user-subscription.service.ts index 83df40a43..bfb5848bc 100644 --- a/client/src/app/shared/user-subscription/user-subscription.service.ts +++ b/client/src/app/shared/user-subscription/user-subscription.service.ts | |||
@@ -1,44 +1,67 @@ | |||
1 | import { bufferTime, catchError, filter, first, map, share, switchMap } from 'rxjs/operators' | 1 | import { bufferTime, catchError, filter, map, tap, share, switchMap } from 'rxjs/operators' |
2 | import { Observable, ReplaySubject, Subject, of, merge } from 'rxjs' | ||
2 | import { HttpClient, HttpParams } from '@angular/common/http' | 3 | import { HttpClient, HttpParams } from '@angular/common/http' |
3 | import { Injectable } from '@angular/core' | 4 | import { Injectable } from '@angular/core' |
4 | import { ResultList } from '../../../../../shared' | 5 | import { ResultList } from '../../../../../shared' |
5 | import { environment } from '../../../environments/environment' | 6 | import { environment } from '../../../environments/environment' |
6 | import { RestExtractor, RestService } from '../rest' | 7 | import { RestExtractor, RestService } from '../rest' |
7 | import { Observable, ReplaySubject, Subject } from 'rxjs' | ||
8 | import { VideoChannel } from '@app/shared/video-channel/video-channel.model' | 8 | import { VideoChannel } from '@app/shared/video-channel/video-channel.model' |
9 | import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' | 9 | import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' |
10 | import { VideoChannel as VideoChannelServer } from '../../../../../shared/models/videos' | 10 | import { VideoChannel as VideoChannelServer } from '../../../../../shared/models/videos' |
11 | import { ComponentPaginationLight } from '@app/shared/rest/component-pagination.model' | 11 | import { ComponentPaginationLight } from '@app/shared/rest/component-pagination.model' |
12 | import { uniq } from 'lodash-es' | ||
13 | import * as debug from 'debug' | ||
14 | |||
15 | const logger = debug('peertube:subscriptions:UserSubscriptionService') | ||
12 | 16 | ||
13 | type SubscriptionExistResult = { [ uri: string ]: boolean } | 17 | type SubscriptionExistResult = { [ uri: string ]: boolean } |
18 | type SubscriptionExistResultObservable = { [ uri: string ]: Observable<boolean> } | ||
14 | 19 | ||
15 | @Injectable() | 20 | @Injectable() |
16 | export class UserSubscriptionService { | 21 | export class UserSubscriptionService { |
17 | static BASE_USER_SUBSCRIPTIONS_URL = environment.apiUrl + '/api/v1/users/me/subscriptions' | 22 | static BASE_USER_SUBSCRIPTIONS_URL = environment.apiUrl + '/api/v1/users/me/subscriptions' |
18 | 23 | ||
19 | // Use a replay subject because we "next" a value before subscribing | 24 | // Use a replay subject because we "next" a value before subscribing |
20 | private existsSubject: Subject<string> = new ReplaySubject(1) | 25 | private existsSubject = new ReplaySubject<string>(1) |
21 | private readonly existsObservable: Observable<SubscriptionExistResult> | 26 | private readonly existsObservable: Observable<SubscriptionExistResult> |
22 | 27 | ||
28 | private myAccountSubscriptionCache: SubscriptionExistResult = {} | ||
29 | private myAccountSubscriptionCacheObservable: SubscriptionExistResultObservable = {} | ||
30 | private myAccountSubscriptionCacheSubject = new Subject<SubscriptionExistResult>() | ||
31 | |||
23 | constructor ( | 32 | constructor ( |
24 | private authHttp: HttpClient, | 33 | private authHttp: HttpClient, |
25 | private restExtractor: RestExtractor, | 34 | private restExtractor: RestExtractor, |
26 | private restService: RestService | 35 | private restService: RestService |
27 | ) { | 36 | ) { |
28 | this.existsObservable = this.existsSubject.pipe( | 37 | this.existsObservable = merge( |
29 | bufferTime(500), | 38 | this.existsSubject.pipe( |
30 | filter(uris => uris.length !== 0), | 39 | bufferTime(500), |
31 | switchMap(uris => this.doSubscriptionsExist(uris)), | 40 | filter(uris => uris.length !== 0), |
32 | share() | 41 | map(uris => uniq(uris)), |
42 | switchMap(uris => this.doSubscriptionsExist(uris)), | ||
43 | share() | ||
44 | ), | ||
45 | |||
46 | this.myAccountSubscriptionCacheSubject | ||
33 | ) | 47 | ) |
34 | } | 48 | } |
35 | 49 | ||
50 | /** | ||
51 | * Subscription part | ||
52 | */ | ||
53 | |||
36 | deleteSubscription (nameWithHost: string) { | 54 | deleteSubscription (nameWithHost: string) { |
37 | const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/' + nameWithHost | 55 | const url = UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/' + nameWithHost |
38 | 56 | ||
39 | return this.authHttp.delete(url) | 57 | return this.authHttp.delete(url) |
40 | .pipe( | 58 | .pipe( |
41 | map(this.restExtractor.extractDataBool), | 59 | map(this.restExtractor.extractDataBool), |
60 | tap(() => { | ||
61 | this.myAccountSubscriptionCache[nameWithHost] = false | ||
62 | |||
63 | this.myAccountSubscriptionCacheSubject.next(this.myAccountSubscriptionCache) | ||
64 | }), | ||
42 | catchError(err => this.restExtractor.handleError(err)) | 65 | catchError(err => this.restExtractor.handleError(err)) |
43 | ) | 66 | ) |
44 | } | 67 | } |
@@ -50,6 +73,11 @@ export class UserSubscriptionService { | |||
50 | return this.authHttp.post(url, body) | 73 | return this.authHttp.post(url, body) |
51 | .pipe( | 74 | .pipe( |
52 | map(this.restExtractor.extractDataBool), | 75 | map(this.restExtractor.extractDataBool), |
76 | tap(() => { | ||
77 | this.myAccountSubscriptionCache[nameWithHost] = true | ||
78 | |||
79 | this.myAccountSubscriptionCacheSubject.next(this.myAccountSubscriptionCache) | ||
80 | }), | ||
53 | catchError(err => this.restExtractor.handleError(err)) | 81 | catchError(err => this.restExtractor.handleError(err)) |
54 | ) | 82 | ) |
55 | } | 83 | } |
@@ -69,10 +97,46 @@ export class UserSubscriptionService { | |||
69 | ) | 97 | ) |
70 | } | 98 | } |
71 | 99 | ||
100 | /** | ||
101 | * SubscriptionExist part | ||
102 | */ | ||
103 | |||
104 | listenToMyAccountSubscriptionCacheSubject () { | ||
105 | return this.myAccountSubscriptionCacheSubject.asObservable() | ||
106 | } | ||
107 | |||
108 | listenToSubscriptionCacheChange (nameWithHost: string) { | ||
109 | if (nameWithHost in this.myAccountSubscriptionCacheObservable) { | ||
110 | return this.myAccountSubscriptionCacheObservable[ nameWithHost ] | ||
111 | } | ||
112 | |||
113 | const obs = this.existsObservable | ||
114 | .pipe( | ||
115 | filter(existsResult => existsResult[ nameWithHost ] !== undefined), | ||
116 | map(existsResult => existsResult[ nameWithHost ]) | ||
117 | ) | ||
118 | |||
119 | this.myAccountSubscriptionCacheObservable[ nameWithHost ] = obs | ||
120 | return obs | ||
121 | } | ||
122 | |||
72 | doesSubscriptionExist (nameWithHost: string) { | 123 | doesSubscriptionExist (nameWithHost: string) { |
124 | logger('Running subscription check for %d.', nameWithHost) | ||
125 | |||
126 | if (nameWithHost in this.myAccountSubscriptionCache) { | ||
127 | logger('Found cache for %d.', nameWithHost) | ||
128 | |||
129 | return of(this.myAccountSubscriptionCache[ nameWithHost ]) | ||
130 | } | ||
131 | |||
73 | this.existsSubject.next(nameWithHost) | 132 | this.existsSubject.next(nameWithHost) |
74 | 133 | ||
75 | return this.existsObservable.pipe(first()) | 134 | logger('Fetching from network for %d.', nameWithHost) |
135 | return this.existsObservable.pipe( | ||
136 | filter(existsResult => existsResult[ nameWithHost ] !== undefined), | ||
137 | map(existsResult => existsResult[ nameWithHost ]), | ||
138 | tap(result => this.myAccountSubscriptionCache[ nameWithHost ] = result) | ||
139 | ) | ||
76 | } | 140 | } |
77 | 141 | ||
78 | private doSubscriptionsExist (uris: string[]): Observable<SubscriptionExistResult> { | 142 | private doSubscriptionsExist (uris: string[]): Observable<SubscriptionExistResult> { |
@@ -82,6 +146,14 @@ export class UserSubscriptionService { | |||
82 | params = this.restService.addObjectParams(params, { uris }) | 146 | params = this.restService.addObjectParams(params, { uris }) |
83 | 147 | ||
84 | return this.authHttp.get<SubscriptionExistResult>(url, { params }) | 148 | return this.authHttp.get<SubscriptionExistResult>(url, { params }) |
85 | .pipe(catchError(err => this.restExtractor.handleError(err))) | 149 | .pipe( |
150 | tap(res => { | ||
151 | this.myAccountSubscriptionCache = { | ||
152 | ...this.myAccountSubscriptionCache, | ||
153 | ...res | ||
154 | } | ||
155 | }), | ||
156 | catchError(err => this.restExtractor.handleError(err)) | ||
157 | ) | ||
86 | } | 158 | } |
87 | } | 159 | } |