aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-12-15 15:58:10 +0100
committerChocobozzz <me@florianbigard.com>2021-12-16 10:08:55 +0100
commita9bfa85d2cdf13670aaced740da5b493fbeddfce (patch)
tree3781c9218d4cc7786b6589365c0efbed2151703d /client/src/app
parentc77fdc605b3ccc1ab6890f889d8200fbe9372949 (diff)
downloadPeerTube-a9bfa85d2cdf13670aaced740da5b493fbeddfce.tar.gz
PeerTube-a9bfa85d2cdf13670aaced740da5b493fbeddfce.tar.zst
PeerTube-a9bfa85d2cdf13670aaced740da5b493fbeddfce.zip
Add ability for admins to set default p2p policy
Diffstat (limited to 'client/src/app')
-rw-r--r--client/src/app/+videos/+video-watch/shared/information/privacy-concerns.component.ts26
-rw-r--r--client/src/app/+videos/+video-watch/shared/playlist/video-watch-playlist.component.ts39
-rw-r--r--client/src/app/+videos/+video-watch/shared/recommendations/recommended-videos.component.ts27
-rw-r--r--client/src/app/+videos/+video-watch/video-watch.component.ts64
-rw-r--r--client/src/app/app.component.ts17
-rw-r--r--client/src/app/core/auth/auth-user.model.ts44
-rw-r--r--client/src/app/core/auth/auth.service.ts16
-rw-r--r--client/src/app/core/core.module.ts3
-rw-r--r--client/src/app/core/users/index.ts1
-rw-r--r--client/src/app/core/users/user-local-storage.service.ts186
-rw-r--r--client/src/app/core/users/user.model.ts8
-rw-r--r--client/src/app/core/users/user.service.ts104
-rw-r--r--client/src/app/menu/menu.component.html2
-rw-r--r--client/src/app/menu/menu.component.ts4
-rw-r--r--client/src/app/shared/shared-search/search.service.ts7
-rw-r--r--client/src/app/shared/shared-user-settings/user-video-settings.component.html2
-rw-r--r--client/src/app/shared/shared-user-settings/user-video-settings.component.ts8
17 files changed, 319 insertions, 239 deletions
diff --git a/client/src/app/+videos/+video-watch/shared/information/privacy-concerns.component.ts b/client/src/app/+videos/+video-watch/shared/information/privacy-concerns.component.ts
index bbc81d46d..24030df3e 100644
--- a/client/src/app/+videos/+video-watch/shared/information/privacy-concerns.component.ts
+++ b/client/src/app/+videos/+video-watch/shared/information/privacy-concerns.component.ts
@@ -1,9 +1,8 @@
1import { Component, Input, OnInit } from '@angular/core' 1import { Component, Input, OnInit } from '@angular/core'
2import { ServerService } from '@app/core' 2import { ServerService, User, UserService } from '@app/core'
3import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage' 3import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage'
4import { HTMLServerConfig, Video } from '@shared/models' 4import { HTMLServerConfig, Video } from '@shared/models'
5import { getStoredP2PEnabled } from '../../../../../assets/player/peertube-player-local-storage' 5import { isP2PEnabled } from '../../../../../assets/player/utils'
6import { isWebRTCDisabled } from '../../../../../assets/player/utils'
7 6
8@Component({ 7@Component({
9 selector: 'my-privacy-concerns', 8 selector: 'my-privacy-concerns',
@@ -15,33 +14,32 @@ export class PrivacyConcernsComponent implements OnInit {
15 14
16 @Input() video: Video 15 @Input() video: Video
17 16
18 display = true 17 display = false
19 18
20 private serverConfig: HTMLServerConfig 19 private serverConfig: HTMLServerConfig
21 20
22 constructor ( 21 constructor (
23 private serverService: ServerService 22 private serverService: ServerService,
23 private userService: UserService
24 ) { } 24 ) { }
25 25
26 ngOnInit () { 26 ngOnInit () {
27 this.serverConfig = this.serverService.getHTMLConfig() 27 this.serverConfig = this.serverService.getHTMLConfig()
28 28
29 if (isWebRTCDisabled() || this.isTrackerDisabled() || this.isP2PDisabled() || this.alreadyAccepted()) { 29 this.userService.getAnonymousOrLoggedUser()
30 this.display = false 30 .subscribe(user => this.updateDisplay(user))
31 }
32 } 31 }
33 32
34 acceptedPrivacyConcern () { 33 acceptedPrivacyConcern () {
35 peertubeLocalStorage.setItem(PrivacyConcernsComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'true') 34 peertubeLocalStorage.setItem(PrivacyConcernsComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'true')
36 this.display = false
37 }
38 35
39 private isTrackerDisabled () { 36 this.display = false
40 return this.video.isLocal && this.serverConfig.tracker.enabled === false
41 } 37 }
42 38
43 private isP2PDisabled () { 39 private updateDisplay (user: User) {
44 return getStoredP2PEnabled() === false 40 if (isP2PEnabled(this.video, this.serverConfig, user.p2pEnabled) && !this.alreadyAccepted()) {
41 this.display = true
42 }
45 } 43 }
46 44
47 private alreadyAccepted () { 45 private alreadyAccepted () {
diff --git a/client/src/app/+videos/+video-watch/shared/playlist/video-watch-playlist.component.ts b/client/src/app/+videos/+video-watch/shared/playlist/video-watch-playlist.component.ts
index b2863fed6..fbf9a3687 100644
--- a/client/src/app/+videos/+video-watch/shared/playlist/video-watch-playlist.component.ts
+++ b/client/src/app/+videos/+video-watch/shared/playlist/video-watch-playlist.component.ts
@@ -1,16 +1,9 @@
1import { Component, EventEmitter, Input, Output } from '@angular/core' 1import { Component, EventEmitter, Input, Output } from '@angular/core'
2import { Router } from '@angular/router' 2import { Router } from '@angular/router'
3import { 3import { AuthService, ComponentPagination, HooksService, Notifier, SessionStorageService, UserService } from '@app/core'
4 AuthService,
5 ComponentPagination,
6 HooksService,
7 LocalStorageService,
8 Notifier,
9 SessionStorageService,
10 UserService
11} from '@app/core'
12import { VideoPlaylist, VideoPlaylistElement, VideoPlaylistService } from '@app/shared/shared-video-playlist' 4import { VideoPlaylist, VideoPlaylistElement, VideoPlaylistService } from '@app/shared/shared-video-playlist'
13import { peertubeLocalStorage, peertubeSessionStorage } from '@root-helpers/peertube-web-storage' 5import { peertubeSessionStorage } from '@root-helpers/peertube-web-storage'
6import { getBoolOrDefault } from '@root-helpers/local-storage-utils'
14import { VideoPlaylistPrivacy } from '@shared/models' 7import { VideoPlaylistPrivacy } from '@shared/models'
15 8
16@Component({ 9@Component({
@@ -19,8 +12,7 @@ import { VideoPlaylistPrivacy } from '@shared/models'
19 styleUrls: [ './video-watch-playlist.component.scss' ] 12 styleUrls: [ './video-watch-playlist.component.scss' ]
20}) 13})
21export class VideoWatchPlaylistComponent { 14export class VideoWatchPlaylistComponent {
22 static LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST = 'auto_play_video_playlist' 15 static SESSION_STORAGE_LOOP_PLAYLIST = 'loop_playlist'
23 static SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST = 'loop_playlist'
24 16
25 @Input() playlist: VideoPlaylist 17 @Input() playlist: VideoPlaylist
26 18
@@ -47,19 +39,15 @@ export class VideoWatchPlaylistComponent {
47 private auth: AuthService, 39 private auth: AuthService,
48 private notifier: Notifier, 40 private notifier: Notifier,
49 private videoPlaylist: VideoPlaylistService, 41 private videoPlaylist: VideoPlaylistService,
50 private localStorageService: LocalStorageService,
51 private sessionStorage: SessionStorageService, 42 private sessionStorage: SessionStorageService,
52 private router: Router 43 private router: Router
53 ) { 44 ) {
54 // defaults to true 45 this.userService.getAnonymousOrLoggedUser()
55 this.autoPlayNextVideoPlaylist = this.auth.isLoggedIn() 46 .subscribe(user => this.autoPlayNextVideoPlaylist = user.autoPlayNextVideoPlaylist)
56 ? this.auth.getUser().autoPlayNextVideoPlaylist
57 : this.localStorageService.getItem(VideoWatchPlaylistComponent.LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST) !== 'false'
58 47
59 this.setAutoPlayNextVideoPlaylistSwitchText() 48 this.setAutoPlayNextVideoPlaylistSwitchText()
60 49
61 // defaults to false 50 this.loopPlaylist = getBoolOrDefault(this.sessionStorage.getItem(VideoWatchPlaylistComponent.SESSION_STORAGE_LOOP_PLAYLIST), false)
62 this.loopPlaylist = this.sessionStorage.getItem(VideoWatchPlaylistComponent.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST) === 'true'
63 this.setLoopPlaylistSwitchText() 51 this.setLoopPlaylistSwitchText()
64 } 52 }
65 53
@@ -201,16 +189,9 @@ export class VideoWatchPlaylistComponent {
201 this.autoPlayNextVideoPlaylist = !this.autoPlayNextVideoPlaylist 189 this.autoPlayNextVideoPlaylist = !this.autoPlayNextVideoPlaylist
202 this.setAutoPlayNextVideoPlaylistSwitchText() 190 this.setAutoPlayNextVideoPlaylistSwitchText()
203 191
204 peertubeLocalStorage.setItem( 192 const details = { autoPlayNextVideoPlaylist: this.autoPlayNextVideoPlaylist }
205 VideoWatchPlaylistComponent.LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST,
206 this.autoPlayNextVideoPlaylist.toString()
207 )
208 193
209 if (this.auth.isLoggedIn()) { 194 if (this.auth.isLoggedIn()) {
210 const details = {
211 autoPlayNextVideoPlaylist: this.autoPlayNextVideoPlaylist
212 }
213
214 this.userService.updateMyProfile(details) 195 this.userService.updateMyProfile(details)
215 .subscribe({ 196 .subscribe({
216 next: () => { 197 next: () => {
@@ -219,6 +200,8 @@ export class VideoWatchPlaylistComponent {
219 200
220 error: err => this.notifier.error(err.message) 201 error: err => this.notifier.error(err.message)
221 }) 202 })
203 } else {
204 this.userService.updateMyAnonymousProfile(details)
222 } 205 }
223 } 206 }
224 207
@@ -227,7 +210,7 @@ export class VideoWatchPlaylistComponent {
227 this.setLoopPlaylistSwitchText() 210 this.setLoopPlaylistSwitchText()
228 211
229 peertubeSessionStorage.setItem( 212 peertubeSessionStorage.setItem(
230 VideoWatchPlaylistComponent.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST, 213 VideoWatchPlaylistComponent.SESSION_STORAGE_LOOP_PLAYLIST,
231 this.loopPlaylist.toString() 214 this.loopPlaylist.toString()
232 ) 215 )
233 } 216 }
diff --git a/client/src/app/+videos/+video-watch/shared/recommendations/recommended-videos.component.ts b/client/src/app/+videos/+video-watch/shared/recommendations/recommended-videos.component.ts
index dfc296d15..97f742499 100644
--- a/client/src/app/+videos/+video-watch/shared/recommendations/recommended-videos.component.ts
+++ b/client/src/app/+videos/+video-watch/shared/recommendations/recommended-videos.component.ts
@@ -1,10 +1,9 @@
1import { Observable } from 'rxjs' 1import { Observable } from 'rxjs'
2import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core' 2import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core'
3import { AuthService, Notifier, SessionStorageService, User, UserService } from '@app/core' 3import { AuthService, Notifier, User, UserService } from '@app/core'
4import { Video } from '@app/shared/shared-main' 4import { Video } from '@app/shared/shared-main'
5import { MiniatureDisplayOptions } from '@app/shared/shared-video-miniature' 5import { MiniatureDisplayOptions } from '@app/shared/shared-video-miniature'
6import { VideoPlaylist } from '@app/shared/shared-video-playlist' 6import { VideoPlaylist } from '@app/shared/shared-video-playlist'
7import { UserLocalStorageKeys } from '@root-helpers/users'
8import { RecommendationInfo } from './recommendation-info.model' 7import { RecommendationInfo } from './recommendation-info.model'
9import { RecommendedVideosStore } from './recommended-videos.store' 8import { RecommendedVideosStore } from './recommended-videos.store'
10 9
@@ -39,24 +38,14 @@ export class RecommendedVideosComponent implements OnInit, OnChanges {
39 private userService: UserService, 38 private userService: UserService,
40 private authService: AuthService, 39 private authService: AuthService,
41 private notifier: Notifier, 40 private notifier: Notifier,
42 private store: RecommendedVideosStore, 41 private store: RecommendedVideosStore
43 private sessionStorageService: SessionStorageService
44 ) { 42 ) {
45 this.videos$ = this.store.recommendations$ 43 this.videos$ = this.store.recommendations$
46 this.hasVideos$ = this.store.hasRecommendations$ 44 this.hasVideos$ = this.store.hasRecommendations$
47 this.videos$.subscribe(videos => this.gotRecommendations.emit(videos)) 45 this.videos$.subscribe(videos => this.gotRecommendations.emit(videos))
48 46
49 if (this.authService.isLoggedIn()) { 47 this.userService.getAnonymousOrLoggedUser()
50 this.autoPlayNextVideo = this.authService.getUser().autoPlayNextVideo 48 .subscribe(user => this.autoPlayNextVideo = user.autoPlayNextVideo)
51 } else {
52 this.autoPlayNextVideo = this.sessionStorageService.getItem(UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true'
53
54 this.sessionStorageService.watch([ UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO ]).subscribe(
55 () => {
56 this.autoPlayNextVideo = this.sessionStorageService.getItem(UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true'
57 }
58 )
59 }
60 49
61 this.autoPlayNextVideoTooltip = $localize`When active, the next video is automatically played after the current one.` 50 this.autoPlayNextVideoTooltip = $localize`When active, the next video is automatically played after the current one.`
62 } 51 }
@@ -77,13 +66,9 @@ export class RecommendedVideosComponent implements OnInit, OnChanges {
77 } 66 }
78 67
79 switchAutoPlayNextVideo () { 68 switchAutoPlayNextVideo () {
80 this.sessionStorageService.setItem(UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO, this.autoPlayNextVideo.toString()) 69 const details = { autoPlayNextVideo: this.autoPlayNextVideo }
81 70
82 if (this.authService.isLoggedIn()) { 71 if (this.authService.isLoggedIn()) {
83 const details = {
84 autoPlayNextVideo: this.autoPlayNextVideo
85 }
86
87 this.userService.updateMyProfile(details) 72 this.userService.updateMyProfile(details)
88 .subscribe({ 73 .subscribe({
89 next: () => { 74 next: () => {
@@ -92,6 +77,8 @@ export class RecommendedVideosComponent implements OnInit, OnChanges {
92 77
93 error: err => this.notifier.error(err.message) 78 error: err => this.notifier.error(err.message)
94 }) 79 })
80 } else {
81 this.userService.updateMyAnonymousProfile(details)
95 } 82 }
96 } 83 }
97} 84}
diff --git a/client/src/app/+videos/+video-watch/video-watch.component.ts b/client/src/app/+videos/+video-watch/video-watch.component.ts
index fd61bcbf0..d542f243c 100644
--- a/client/src/app/+videos/+video-watch/video-watch.component.ts
+++ b/client/src/app/+videos/+video-watch/video-watch.component.ts
@@ -1,5 +1,6 @@
1import { Hotkey, HotkeysService } from 'angular2-hotkeys' 1import { Hotkey, HotkeysService } from 'angular2-hotkeys'
2import { forkJoin, Subscription } from 'rxjs' 2import { forkJoin, Subscription } from 'rxjs'
3import { isP2PEnabled } from 'src/assets/player/utils'
3import { PlatformLocation } from '@angular/common' 4import { PlatformLocation } from '@angular/common'
4import { Component, ElementRef, Inject, LOCALE_ID, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core' 5import { Component, ElementRef, Inject, LOCALE_ID, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core'
5import { ActivatedRoute, Router } from '@angular/router' 6import { ActivatedRoute, Router } from '@angular/router'
@@ -14,6 +15,7 @@ import {
14 RestExtractor, 15 RestExtractor,
15 ScreenService, 16 ScreenService,
16 ServerService, 17 ServerService,
18 User,
17 UserService 19 UserService
18} from '@app/core' 20} from '@app/core'
19import { HooksService } from '@app/core/plugins/hooks.service' 21import { HooksService } from '@app/core/plugins/hooks.service'
@@ -237,31 +239,34 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
237 'filter:api.video-watch.video.get.result' 239 'filter:api.video-watch.video.get.result'
238 ) 240 )
239 241
240 forkJoin([ videoObs, this.videoCaptionService.listCaptions(videoId) ]) 242 forkJoin([
241 .subscribe({ 243 videoObs,
242 next: ([ video, captionsResult ]) => { 244 this.videoCaptionService.listCaptions(videoId),
243 const queryParams = this.route.snapshot.queryParams 245 this.userService.getAnonymousOrLoggedUser()
246 ]).subscribe({
247 next: ([ video, captionsResult, loggedInOrAnonymousUser ]) => {
248 const queryParams = this.route.snapshot.queryParams
244 249
245 const urlOptions = { 250 const urlOptions = {
246 resume: queryParams.resume, 251 resume: queryParams.resume,
247 252
248 startTime: queryParams.start, 253 startTime: queryParams.start,
249 stopTime: queryParams.stop, 254 stopTime: queryParams.stop,
250 255
251 muted: queryParams.muted, 256 muted: queryParams.muted,
252 loop: queryParams.loop, 257 loop: queryParams.loop,
253 subtitle: queryParams.subtitle, 258 subtitle: queryParams.subtitle,
254 259
255 playerMode: queryParams.mode, 260 playerMode: queryParams.mode,
256 peertubeLink: false 261 peertubeLink: false
257 } 262 }
258 263
259 this.onVideoFetched(video, captionsResult.data, urlOptions) 264 this.onVideoFetched({ video, videoCaptions: captionsResult.data, loggedInOrAnonymousUser, urlOptions })
260 .catch(err => this.handleGlobalError(err)) 265 .catch(err => this.handleGlobalError(err))
261 }, 266 },
262 267
263 error: err => this.handleRequestError(err) 268 error: err => this.handleRequestError(err)
264 }) 269 })
265 } 270 }
266 271
267 private loadPlaylist (playlistId: string) { 272 private loadPlaylist (playlistId: string) {
@@ -323,11 +328,14 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
323 this.notifier.error(errorMessage) 328 this.notifier.error(errorMessage)
324 } 329 }
325 330
326 private async onVideoFetched ( 331 private async onVideoFetched (options: {
327 video: VideoDetails, 332 video: VideoDetails
328 videoCaptions: VideoCaption[], 333 videoCaptions: VideoCaption[]
329 urlOptions: URLOptions 334 urlOptions: URLOptions
330 ) { 335 loggedInOrAnonymousUser: User
336 }) {
337 const { video, videoCaptions, urlOptions, loggedInOrAnonymousUser } = options
338
331 this.subscribeToLiveEventsIfNeeded(this.video, video) 339 this.subscribeToLiveEventsIfNeeded(this.video, video)
332 340
333 this.video = video 341 this.video = video
@@ -346,7 +354,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
346 if (res === false) return this.location.back() 354 if (res === false) return this.location.back()
347 } 355 }
348 356
349 this.buildPlayer(urlOptions) 357 this.buildPlayer(urlOptions, loggedInOrAnonymousUser)
350 .catch(err => console.error('Cannot build the player', err)) 358 .catch(err => console.error('Cannot build the player', err))
351 359
352 this.setOpenGraphTags() 360 this.setOpenGraphTags()
@@ -359,7 +367,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
359 this.hooks.runAction('action:video-watch.video.loaded', 'video-watch', hookOptions) 367 this.hooks.runAction('action:video-watch.video.loaded', 'video-watch', hookOptions)
360 } 368 }
361 369
362 private async buildPlayer (urlOptions: URLOptions) { 370 private async buildPlayer (urlOptions: URLOptions, loggedInOrAnonymousUser: User) {
363 // Flush old player if needed 371 // Flush old player if needed
364 this.flushPlayer() 372 this.flushPlayer()
365 373
@@ -380,6 +388,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
380 video: this.video, 388 video: this.video,
381 videoCaptions: this.videoCaptions, 389 videoCaptions: this.videoCaptions,
382 urlOptions, 390 urlOptions,
391 loggedInOrAnonymousUser,
383 user: this.user 392 user: this.user
384 } 393 }
385 const { playerMode, playerOptions } = await this.hooks.wrapFun( 394 const { playerMode, playerOptions } = await this.hooks.wrapFun(
@@ -517,9 +526,10 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
517 video: VideoDetails 526 video: VideoDetails
518 videoCaptions: VideoCaption[] 527 videoCaptions: VideoCaption[]
519 urlOptions: CustomizationOptions & { playerMode: PlayerMode } 528 urlOptions: CustomizationOptions & { playerMode: PlayerMode }
529 loggedInOrAnonymousUser: User
520 user?: AuthUser 530 user?: AuthUser
521 }) { 531 }) {
522 const { video, videoCaptions, urlOptions, user } = params 532 const { video, videoCaptions, urlOptions, loggedInOrAnonymousUser, user } = params
523 533
524 const getStartTime = () => { 534 const getStartTime = () => {
525 const byUrl = urlOptions.startTime !== undefined 535 const byUrl = urlOptions.startTime !== undefined
@@ -547,6 +557,8 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
547 const options: PeertubePlayerManagerOptions = { 557 const options: PeertubePlayerManagerOptions = {
548 common: { 558 common: {
549 autoplay: this.isAutoplay(), 559 autoplay: this.isAutoplay(),
560 p2pEnabled: isP2PEnabled(video, this.serverConfig, loggedInOrAnonymousUser.p2pEnabled),
561
550 nextVideo: () => this.playNextVideoInAngularZone(), 562 nextVideo: () => this.playNextVideoInAngularZone(),
551 563
552 playerElement: this.playerElement, 564 playerElement: this.playerElement,
diff --git a/client/src/app/app.component.ts b/client/src/app/app.component.ts
index 95af89b19..a60138af9 100644
--- a/client/src/app/app.component.ts
+++ b/client/src/app/app.component.ts
@@ -14,7 +14,8 @@ import {
14 ScrollService, 14 ScrollService,
15 ServerService, 15 ServerService,
16 ThemeService, 16 ThemeService,
17 User 17 User,
18 UserLocalStorageService
18} from '@app/core' 19} from '@app/core'
19import { HooksService } from '@app/core/plugins/hooks.service' 20import { HooksService } from '@app/core/plugins/hooks.service'
20import { PluginService } from '@app/core/plugins/plugin.service' 21import { PluginService } from '@app/core/plugins/plugin.service'
@@ -70,6 +71,7 @@ export class AppComponent implements OnInit, AfterViewInit {
70 private ngbConfig: NgbConfig, 71 private ngbConfig: NgbConfig,
71 private loadingBar: LoadingBarService, 72 private loadingBar: LoadingBarService,
72 private scrollService: ScrollService, 73 private scrollService: ScrollService,
74 private userLocalStorage: UserLocalStorageService,
73 public menu: MenuService 75 public menu: MenuService
74 ) { 76 ) {
75 this.ngbConfig.animation = false 77 this.ngbConfig.animation = false
@@ -86,6 +88,8 @@ export class AppComponent implements OnInit, AfterViewInit {
86 ngOnInit () { 88 ngOnInit () {
87 document.getElementById('incompatible-browser').className += ' browser-ok' 89 document.getElementById('incompatible-browser').className += ' browser-ok'
88 90
91 this.loadUser()
92
89 this.serverConfig = this.serverService.getHTMLConfig() 93 this.serverConfig = this.serverService.getHTMLConfig()
90 94
91 this.hooks.runAction('action:application.init', 'common') 95 this.hooks.runAction('action:application.init', 'common')
@@ -300,4 +304,15 @@ export class AppComponent implements OnInit, AfterViewInit {
300 }, undefined, $localize`Go to the videos upload page`) 304 }, undefined, $localize`Go to the videos upload page`)
301 ]) 305 ])
302 } 306 }
307
308 private loadUser () {
309 const tokens = this.userLocalStorage.getTokens()
310 if (!tokens) return
311
312 const user = this.userLocalStorage.getLoggedInUser()
313 if (!user) return
314
315 // Initialize user
316 this.authService.buildAuthUser(user, tokens)
317 }
303} 318}
diff --git a/client/src/app/core/auth/auth-user.model.ts b/client/src/app/core/auth/auth-user.model.ts
index f10b37e5a..cd9665e37 100644
--- a/client/src/app/core/auth/auth-user.model.ts
+++ b/client/src/app/core/auth/auth-user.model.ts
@@ -1,13 +1,7 @@
1import { Observable, of } from 'rxjs' 1import { Observable, of } from 'rxjs'
2import { map } from 'rxjs/operators' 2import { map } from 'rxjs/operators'
3import { User } from '@app/core/users/user.model' 3import { User } from '@app/core/users/user.model'
4import { 4import { UserTokens } from '@root-helpers/users'
5 flushUserInfoFromLocalStorage,
6 getUserInfoFromLocalStorage,
7 saveUserInfoIntoLocalStorage,
8 TokenOptions,
9 Tokens
10} from '@root-helpers/users'
11import { hasUserRight } from '@shared/core-utils/users' 5import { hasUserRight } from '@shared/core-utils/users'
12import { 6import {
13 MyUser as ServerMyUserModel, 7 MyUser as ServerMyUserModel,
@@ -19,31 +13,15 @@ import {
19} from '@shared/models' 13} from '@shared/models'
20 14
21export class AuthUser extends User implements ServerMyUserModel { 15export class AuthUser extends User implements ServerMyUserModel {
22 tokens: Tokens 16 tokens: UserTokens
23 specialPlaylists: MyUserSpecialPlaylist[] 17 specialPlaylists: MyUserSpecialPlaylist[]
24 18
25 canSeeVideosLink = true 19 canSeeVideosLink = true
26 20
27 static load () { 21 constructor (userHash: Partial<ServerMyUserModel>, hashTokens: Partial<UserTokens>) {
28 const tokens = Tokens.load()
29 if (!tokens) return null
30
31 const userInfo = getUserInfoFromLocalStorage()
32 if (!userInfo) return null
33
34 return new AuthUser(userInfo, tokens)
35 }
36
37 static flush () {
38 flushUserInfoFromLocalStorage()
39
40 Tokens.flush()
41 }
42
43 constructor (userHash: Partial<ServerMyUserModel>, hashTokens: TokenOptions) {
44 super(userHash) 22 super(userHash)
45 23
46 this.tokens = new Tokens(hashTokens) 24 this.tokens = new UserTokens(hashTokens)
47 this.specialPlaylists = userHash.specialPlaylists 25 this.specialPlaylists = userHash.specialPlaylists
48 } 26 }
49 27
@@ -77,20 +55,6 @@ export class AuthUser extends User implements ServerMyUserModel {
77 return user.role === UserRole.USER 55 return user.role === UserRole.USER
78 } 56 }
79 57
80 save () {
81 saveUserInfoIntoLocalStorage({
82 id: this.id,
83 username: this.username,
84 email: this.email,
85 role: this.role,
86 nsfwPolicy: this.nsfwPolicy,
87 webTorrentEnabled: this.webTorrentEnabled,
88 autoPlayVideo: this.autoPlayVideo
89 })
90
91 this.tokens.save()
92 }
93
94 computeCanSeeVideosLink (quotaObservable: Observable<UserVideoQuota>): Observable<boolean> { 58 computeCanSeeVideosLink (quotaObservable: Observable<UserVideoQuota>): Observable<boolean> {
95 if (!this.isUploadDisabled()) { 59 if (!this.isUploadDisabled()) {
96 this.canSeeVideosLink = true 60 this.canSeeVideosLink = true
diff --git a/client/src/app/core/auth/auth.service.ts b/client/src/app/core/auth/auth.service.ts
index 79239a17a..2ac88c185 100644
--- a/client/src/app/core/auth/auth.service.ts
+++ b/client/src/app/core/auth/auth.service.ts
@@ -5,7 +5,7 @@ import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'
5import { Injectable } from '@angular/core' 5import { Injectable } from '@angular/core'
6import { Router } from '@angular/router' 6import { Router } from '@angular/router'
7import { Notifier } from '@app/core/notification/notifier.service' 7import { Notifier } from '@app/core/notification/notifier.service'
8import { objectToUrlEncoded, peertubeLocalStorage } from '@root-helpers/index' 8import { objectToUrlEncoded, peertubeLocalStorage, UserTokens } from '@root-helpers/index'
9import { HttpStatusCode, MyUser as UserServerModel, OAuthClientLocal, User, UserLogin, UserRefreshToken } from '@shared/models' 9import { HttpStatusCode, MyUser as UserServerModel, OAuthClientLocal, User, UserLogin, UserRefreshToken } from '@shared/models'
10import { environment } from '../../../environments/environment' 10import { environment } from '../../../environments/environment'
11import { RestExtractor } from '../rest/rest-extractor.service' 11import { RestExtractor } from '../rest/rest-extractor.service'
@@ -34,6 +34,7 @@ export class AuthService {
34 34
35 loginChangedSource: Observable<AuthStatus> 35 loginChangedSource: Observable<AuthStatus>
36 userInformationLoaded = new ReplaySubject<boolean>(1) 36 userInformationLoaded = new ReplaySubject<boolean>(1)
37 tokensRefreshed = new ReplaySubject<void>(1)
37 hotkeys: Hotkey[] 38 hotkeys: Hotkey[]
38 39
39 private clientId: string = peertubeLocalStorage.getItem(AuthService.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_ID) 40 private clientId: string = peertubeLocalStorage.getItem(AuthService.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_ID)
@@ -52,9 +53,6 @@ export class AuthService {
52 this.loginChanged = new Subject<AuthStatus>() 53 this.loginChanged = new Subject<AuthStatus>()
53 this.loginChangedSource = this.loginChanged.asObservable() 54 this.loginChangedSource = this.loginChanged.asObservable()
54 55
55 // Return null if there is nothing to load
56 this.user = AuthUser.load()
57
58 // Set HotKeys 56 // Set HotKeys
59 this.hotkeys = [ 57 this.hotkeys = [
60 new Hotkey('m s', (event: KeyboardEvent): boolean => { 58 new Hotkey('m s', (event: KeyboardEvent): boolean => {
@@ -76,6 +74,10 @@ export class AuthService {
76 ] 74 ]
77 } 75 }
78 76
77 buildAuthUser (userInfo: Partial<User>, tokens: UserTokens) {
78 this.user = new AuthUser(userInfo, tokens)
79 }
80
79 loadClientCredentials () { 81 loadClientCredentials () {
80 // Fetch the client_id/client_secret 82 // Fetch the client_id/client_secret
81 this.http.get<OAuthClientLocal>(AuthService.BASE_CLIENT_URL) 83 this.http.get<OAuthClientLocal>(AuthService.BASE_CLIENT_URL)
@@ -180,8 +182,6 @@ Ensure you have correctly configured PeerTube (config/ directory), in particular
180 182
181 this.user = null 183 this.user = null
182 184
183 AuthUser.flush()
184
185 this.setStatus(AuthStatus.LoggedOut) 185 this.setStatus(AuthStatus.LoggedOut)
186 186
187 this.hotkeysService.remove(this.hotkeys) 187 this.hotkeysService.remove(this.hotkeys)
@@ -239,7 +239,6 @@ Ensure you have correctly configured PeerTube (config/ directory), in particular
239 .subscribe({ 239 .subscribe({
240 next: res => { 240 next: res => {
241 this.user.patch(res) 241 this.user.patch(res)
242 this.user.save()
243 242
244 this.userInformationLoaded.next(true) 243 this.userInformationLoaded.next(true)
245 } 244 }
@@ -262,7 +261,6 @@ Ensure you have correctly configured PeerTube (config/ directory), in particular
262 } 261 }
263 262
264 this.user = new AuthUser(obj, hashTokens) 263 this.user = new AuthUser(obj, hashTokens)
265 this.user.save()
266 264
267 this.setStatus(AuthStatus.LoggedIn) 265 this.setStatus(AuthStatus.LoggedIn)
268 this.userInformationLoaded.next(true) 266 this.userInformationLoaded.next(true)
@@ -272,7 +270,7 @@ Ensure you have correctly configured PeerTube (config/ directory), in particular
272 270
273 private handleRefreshToken (obj: UserRefreshToken) { 271 private handleRefreshToken (obj: UserRefreshToken) {
274 this.user.refreshTokens(obj.access_token, obj.refresh_token) 272 this.user.refreshTokens(obj.access_token, obj.refresh_token)
275 this.user.save() 273 this.tokensRefreshed.next()
276 } 274 }
277 275
278 private setStatus (status: AuthStatus) { 276 private setStatus (status: AuthStatus) {
diff --git a/client/src/app/core/core.module.ts b/client/src/app/core/core.module.ts
index 04be0671c..d80f95ed6 100644
--- a/client/src/app/core/core.module.ts
+++ b/client/src/app/core/core.module.ts
@@ -30,7 +30,7 @@ import { ServerConfigResolver } from './routing/server-config-resolver.service'
30import { ScopedTokensService } from './scoped-tokens' 30import { ScopedTokensService } from './scoped-tokens'
31import { ServerService } from './server' 31import { ServerService } from './server'
32import { ThemeService } from './theme' 32import { ThemeService } from './theme'
33import { UserService } from './users' 33import { UserLocalStorageService, UserService } from './users'
34import { LocalStorageService, ScreenService, SessionStorageService } from './wrappers' 34import { LocalStorageService, ScreenService, SessionStorageService } from './wrappers'
35 35
36@NgModule({ 36@NgModule({
@@ -79,6 +79,7 @@ import { LocalStorageService, ScreenService, SessionStorageService } from './wra
79 RestService, 79 RestService,
80 80
81 UserService, 81 UserService,
82 UserLocalStorageService,
82 83
83 ScreenService, 84 ScreenService,
84 LocalStorageService, 85 LocalStorageService,
diff --git a/client/src/app/core/users/index.ts b/client/src/app/core/users/index.ts
index 7b5a67bc7..e235a875b 100644
--- a/client/src/app/core/users/index.ts
+++ b/client/src/app/core/users/index.ts
@@ -1,2 +1,3 @@
1export * from './user-local-storage.service'
1export * from './user.model' 2export * from './user.model'
2export * from './user.service' 3export * from './user.service'
diff --git a/client/src/app/core/users/user-local-storage.service.ts b/client/src/app/core/users/user-local-storage.service.ts
new file mode 100644
index 000000000..85da46e0d
--- /dev/null
+++ b/client/src/app/core/users/user-local-storage.service.ts
@@ -0,0 +1,186 @@
1
2import { filter, throttleTime } from 'rxjs'
3import { Injectable } from '@angular/core'
4import { AuthService, AuthStatus } from '@app/core/auth'
5import { UserLocalStorageKeys, UserTokens } from '@root-helpers/users'
6import { getBoolOrDefault } from '@root-helpers/local-storage-utils'
7import { UserRole, UserUpdateMe } from '@shared/models'
8import { NSFWPolicyType } from '@shared/models/videos'
9import { ServerService } from '../server'
10import { LocalStorageService } from '../wrappers/storage.service'
11
12@Injectable()
13export class UserLocalStorageService {
14
15 constructor (
16 private authService: AuthService,
17 private server: ServerService,
18 private localStorageService: LocalStorageService
19 ) {
20 this.authService.userInformationLoaded.subscribe({
21 next: () => {
22 const user = this.authService.getUser()
23
24 this.setLoggedInUser(user)
25 this.setUserInfo(user)
26 this.setTokens(user.tokens)
27 }
28 })
29
30 this.authService.loginChangedSource
31 .pipe(filter(status => status === AuthStatus.LoggedOut))
32 .subscribe({
33 next: () => {
34 this.flushLoggedInUser()
35 this.flushUserInfo()
36 this.flushTokens()
37 }
38 })
39
40 this.authService.tokensRefreshed
41 .subscribe({
42 next: () => {
43 const user = this.authService.getUser()
44
45 this.setTokens(user.tokens)
46 }
47 })
48 }
49
50 // ---------------------------------------------------------------------------
51
52 getLoggedInUser () {
53 const usernameLocalStorage = this.localStorageService.getItem(UserLocalStorageKeys.USERNAME)
54
55 if (!usernameLocalStorage) return undefined
56
57 return {
58 id: parseInt(this.localStorageService.getItem(UserLocalStorageKeys.ID), 10),
59 username: this.localStorageService.getItem(UserLocalStorageKeys.USERNAME),
60 email: this.localStorageService.getItem(UserLocalStorageKeys.EMAIL),
61 role: parseInt(this.localStorageService.getItem(UserLocalStorageKeys.ROLE), 10) as UserRole,
62
63 ...this.getUserInfo()
64 }
65 }
66
67 setLoggedInUser (user: {
68 id: number
69 username: string
70 email: string
71 role: UserRole
72 }) {
73 this.localStorageService.setItem(UserLocalStorageKeys.ID, user.id.toString())
74 this.localStorageService.setItem(UserLocalStorageKeys.USERNAME, user.username)
75 this.localStorageService.setItem(UserLocalStorageKeys.EMAIL, user.email)
76 this.localStorageService.setItem(UserLocalStorageKeys.ROLE, user.role.toString())
77 }
78
79 flushLoggedInUser () {
80 this.localStorageService.removeItem(UserLocalStorageKeys.ID)
81 this.localStorageService.removeItem(UserLocalStorageKeys.USERNAME)
82 this.localStorageService.removeItem(UserLocalStorageKeys.EMAIL)
83 this.localStorageService.removeItem(UserLocalStorageKeys.ROLE)
84 }
85
86 // ---------------------------------------------------------------------------
87
88 getUserInfo () {
89 let videoLanguages: string[]
90
91 try {
92 const languagesString = this.localStorageService.getItem(UserLocalStorageKeys.VIDEO_LANGUAGES)
93 videoLanguages = languagesString && languagesString !== 'undefined'
94 ? JSON.parse(languagesString)
95 : null
96 } catch (err) {
97 videoLanguages = null
98 console.error('Cannot parse desired video languages from localStorage.', err)
99 }
100
101 const htmlConfig = this.server.getHTMLConfig()
102
103 const defaultNSFWPolicy = htmlConfig.instance.defaultNSFWPolicy
104 const defaultP2PEnabled = htmlConfig.defaults.p2p.enabled
105
106 return {
107 nsfwPolicy: this.localStorageService.getItem<NSFWPolicyType>(UserLocalStorageKeys.NSFW_POLICY) || defaultNSFWPolicy,
108 p2pEnabled: getBoolOrDefault(this.localStorageService.getItem(UserLocalStorageKeys.P2P_ENABLED), defaultP2PEnabled),
109 theme: this.localStorageService.getItem(UserLocalStorageKeys.THEME) || 'instance-default',
110 videoLanguages,
111
112 autoPlayVideo: getBoolOrDefault(this.localStorageService.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO), true),
113 autoPlayNextVideo: getBoolOrDefault(this.localStorageService.getItem(UserLocalStorageKeys.AUTO_PLAY_NEXT_VIDEO), false),
114 autoPlayNextVideoPlaylist: getBoolOrDefault(this.localStorageService.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST), true)
115 }
116 }
117
118 setUserInfo (profile: UserUpdateMe) {
119 const localStorageKeys: { [ id in keyof UserUpdateMe ]: string } = {
120 nsfwPolicy: UserLocalStorageKeys.NSFW_POLICY,
121 p2pEnabled: UserLocalStorageKeys.P2P_ENABLED,
122 autoPlayNextVideo: UserLocalStorageKeys.AUTO_PLAY_VIDEO,
123 autoPlayNextVideoPlaylist: UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST,
124 theme: UserLocalStorageKeys.THEME,
125 videoLanguages: UserLocalStorageKeys.VIDEO_LANGUAGES
126 }
127
128 const obj = Object.keys(localStorageKeys)
129 .filter(key => key in profile)
130 .map(key => ([ localStorageKeys[key], profile[key] ]))
131
132 for (const [ key, value ] of obj) {
133 try {
134 if (value === undefined) {
135 this.localStorageService.removeItem(key)
136 continue
137 }
138
139 const localStorageValue = typeof value === 'string'
140 ? value
141 : JSON.stringify(value)
142
143 this.localStorageService.setItem(key, localStorageValue)
144 } catch (err) {
145 console.error(`Cannot set ${key}->${value} in localStorage. Likely due to a value impossible to stringify.`, err)
146 }
147 }
148 }
149
150 flushUserInfo () {
151 this.localStorageService.removeItem(UserLocalStorageKeys.NSFW_POLICY)
152 this.localStorageService.removeItem(UserLocalStorageKeys.P2P_ENABLED)
153 this.localStorageService.removeItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO)
154 this.localStorageService.removeItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST)
155 this.localStorageService.removeItem(UserLocalStorageKeys.THEME)
156 this.localStorageService.removeItem(UserLocalStorageKeys.VIDEO_LANGUAGES)
157 }
158
159 listenUserInfoChange () {
160 return this.localStorageService.watch([
161 UserLocalStorageKeys.NSFW_POLICY,
162 UserLocalStorageKeys.P2P_ENABLED,
163 UserLocalStorageKeys.AUTO_PLAY_VIDEO,
164 UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST,
165 UserLocalStorageKeys.THEME,
166 UserLocalStorageKeys.VIDEO_LANGUAGES
167 ]).pipe(
168 throttleTime(200),
169 filter(() => this.authService.isLoggedIn() !== true)
170 )
171 }
172
173 // ---------------------------------------------------------------------------
174
175 getTokens () {
176 return UserTokens.getUserTokens(this.localStorageService)
177 }
178
179 setTokens (tokens: UserTokens) {
180 UserTokens.saveToLocalStorage(this.localStorageService, tokens)
181 }
182
183 flushTokens () {
184 UserTokens.flushLocalStorage(this.localStorageService)
185 }
186}
diff --git a/client/src/app/core/users/user.model.ts b/client/src/app/core/users/user.model.ts
index c0e5d3169..f211051ce 100644
--- a/client/src/app/core/users/user.model.ts
+++ b/client/src/app/core/users/user.model.ts
@@ -26,7 +26,11 @@ export class User implements UserServerModel {
26 autoPlayVideo: boolean 26 autoPlayVideo: boolean
27 autoPlayNextVideo: boolean 27 autoPlayNextVideo: boolean
28 autoPlayNextVideoPlaylist: boolean 28 autoPlayNextVideoPlaylist: boolean
29 webTorrentEnabled: boolean 29
30 p2pEnabled: boolean
31 // FIXME: deprecated in 4.1
32 webTorrentEnabled: never
33
30 videosHistoryEnabled: boolean 34 videosHistoryEnabled: boolean
31 videoLanguages: string[] 35 videoLanguages: string[]
32 36
@@ -84,7 +88,7 @@ export class User implements UserServerModel {
84 this.videoCommentsCount = hash.videoCommentsCount 88 this.videoCommentsCount = hash.videoCommentsCount
85 89
86 this.nsfwPolicy = hash.nsfwPolicy 90 this.nsfwPolicy = hash.nsfwPolicy
87 this.webTorrentEnabled = hash.webTorrentEnabled 91 this.p2pEnabled = hash.p2pEnabled
88 this.autoPlayVideo = hash.autoPlayVideo 92 this.autoPlayVideo = hash.autoPlayVideo
89 this.autoPlayNextVideo = hash.autoPlayNextVideo 93 this.autoPlayNextVideo = hash.autoPlayNextVideo
90 this.autoPlayNextVideoPlaylist = hash.autoPlayNextVideoPlaylist 94 this.autoPlayNextVideoPlaylist = hash.autoPlayNextVideoPlaylist
diff --git a/client/src/app/core/users/user.service.ts b/client/src/app/core/users/user.service.ts
index 632361e9b..a6a0474ab 100644
--- a/client/src/app/core/users/user.service.ts
+++ b/client/src/app/core/users/user.service.ts
@@ -1,11 +1,10 @@
1import { SortMeta } from 'primeng/api' 1import { SortMeta } from 'primeng/api'
2import { from, Observable, of } from 'rxjs' 2import { from, Observable, of } from 'rxjs'
3import { catchError, concatMap, filter, first, map, shareReplay, tap, throttleTime, toArray } from 'rxjs/operators' 3import { catchError, concatMap, first, map, shareReplay, tap, toArray } from 'rxjs/operators'
4import { HttpClient, HttpParams } from '@angular/common/http' 4import { HttpClient, HttpParams } from '@angular/common/http'
5import { Injectable } from '@angular/core' 5import { Injectable } from '@angular/core'
6import { AuthService } from '@app/core/auth' 6import { AuthService } from '@app/core/auth'
7import { getBytes } from '@root-helpers/bytes' 7import { getBytes } from '@root-helpers/bytes'
8import { UserLocalStorageKeys } from '@root-helpers/users'
9import { 8import {
10 ActorImage, 9 ActorImage,
11 ResultList, 10 ResultList,
@@ -17,10 +16,9 @@ import {
17 UserUpdateMe, 16 UserUpdateMe,
18 UserVideoQuota 17 UserVideoQuota
19} from '@shared/models' 18} from '@shared/models'
20import { ServerService } from '../'
21import { environment } from '../../../environments/environment' 19import { environment } from '../../../environments/environment'
22import { RestExtractor, RestPagination, RestService } from '../rest' 20import { RestExtractor, RestPagination, RestService } from '../rest'
23import { LocalStorageService, SessionStorageService } from '../wrappers/storage.service' 21import { UserLocalStorageService } from './'
24import { User } from './user.model' 22import { User } from './user.model'
25 23
26@Injectable() 24@Injectable()
@@ -33,12 +31,10 @@ export class UserService {
33 31
34 constructor ( 32 constructor (
35 private authHttp: HttpClient, 33 private authHttp: HttpClient,
36 private server: ServerService,
37 private authService: AuthService, 34 private authService: AuthService,
38 private restExtractor: RestExtractor, 35 private restExtractor: RestExtractor,
39 private restService: RestService, 36 private restService: RestService,
40 private localStorageService: LocalStorageService, 37 private userLocalStorageService: UserLocalStorageService
41 private sessionStorageService: SessionStorageService
42 ) { } 38 ) { }
43 39
44 hasSignupInThisSession () { 40 hasSignupInThisSession () {
@@ -73,6 +69,23 @@ export class UserService {
73 ) 69 )
74 } 70 }
75 71
72 // ---------------------------------------------------------------------------
73
74 updateMyAnonymousProfile (profile: UserUpdateMe) {
75 this.userLocalStorageService.setUserInfo(profile)
76 }
77
78 listenAnonymousUpdate () {
79 return this.userLocalStorageService.listenUserInfoChange()
80 .pipe(map(() => this.getAnonymousUser()))
81 }
82
83 getAnonymousUser () {
84 return new User(this.userLocalStorageService.getUserInfo())
85 }
86
87 // ---------------------------------------------------------------------------
88
76 updateMyProfile (profile: UserUpdateMe) { 89 updateMyProfile (profile: UserUpdateMe) {
77 const url = UserService.BASE_USERS_URL + 'me' 90 const url = UserService.BASE_USERS_URL + 'me'
78 91
@@ -83,53 +96,6 @@ export class UserService {
83 ) 96 )
84 } 97 }
85 98
86 updateMyAnonymousProfile (profile: UserUpdateMe) {
87 const localStorageKeys: { [ id in keyof UserUpdateMe ]: string } = {
88 nsfwPolicy: UserLocalStorageKeys.NSFW_POLICY,
89 webTorrentEnabled: UserLocalStorageKeys.WEBTORRENT_ENABLED,
90 autoPlayNextVideo: UserLocalStorageKeys.AUTO_PLAY_VIDEO,
91 autoPlayNextVideoPlaylist: UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST,
92 theme: UserLocalStorageKeys.THEME,
93 videoLanguages: UserLocalStorageKeys.VIDEO_LANGUAGES
94 }
95
96 const obj = Object.keys(localStorageKeys)
97 .filter(key => key in profile)
98 .map(key => ([ localStorageKeys[key], profile[key] ]))
99
100 for (const [ key, value ] of obj) {
101 try {
102 if (value === undefined) {
103 this.localStorageService.removeItem(key)
104 continue
105 }
106
107 const localStorageValue = typeof value === 'string'
108 ? value
109 : JSON.stringify(value)
110
111 this.localStorageService.setItem(key, localStorageValue)
112 } catch (err) {
113 console.error(`Cannot set ${key}->${value} in localStorage. Likely due to a value impossible to stringify.`, err)
114 }
115 }
116 }
117
118 listenAnonymousUpdate () {
119 return this.localStorageService.watch([
120 UserLocalStorageKeys.NSFW_POLICY,
121 UserLocalStorageKeys.WEBTORRENT_ENABLED,
122 UserLocalStorageKeys.AUTO_PLAY_VIDEO,
123 UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST,
124 UserLocalStorageKeys.THEME,
125 UserLocalStorageKeys.VIDEO_LANGUAGES
126 ]).pipe(
127 throttleTime(200),
128 filter(() => this.authService.isLoggedIn() !== true),
129 map(() => this.getAnonymousUser())
130 )
131 }
132
133 deleteMe () { 99 deleteMe () {
134 const url = UserService.BASE_USERS_URL + 'me' 100 const url = UserService.BASE_USERS_URL + 'me'
135 101
@@ -287,36 +253,6 @@ export class UserService {
287 .pipe(catchError(err => this.restExtractor.handleError(err))) 253 .pipe(catchError(err => this.restExtractor.handleError(err)))
288 } 254 }
289 255
290 getAnonymousUser () {
291 let videoLanguages: string[]
292
293 try {
294 const languagesString = this.localStorageService.getItem(UserLocalStorageKeys.VIDEO_LANGUAGES)
295 videoLanguages = languagesString && languagesString !== 'undefined'
296 ? JSON.parse(languagesString)
297 : null
298 } catch (err) {
299 videoLanguages = null
300 console.error('Cannot parse desired video languages from localStorage.', err)
301 }
302
303 const defaultNSFWPolicy = this.server.getHTMLConfig().instance.defaultNSFWPolicy
304
305 return new User({
306 // local storage keys
307 nsfwPolicy: this.localStorageService.getItem(UserLocalStorageKeys.NSFW_POLICY) || defaultNSFWPolicy,
308 webTorrentEnabled: this.localStorageService.getItem(UserLocalStorageKeys.WEBTORRENT_ENABLED) !== 'false',
309 theme: this.localStorageService.getItem(UserLocalStorageKeys.THEME) || 'instance-default',
310 videoLanguages,
311
312 autoPlayNextVideoPlaylist: this.localStorageService.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST) !== 'false',
313 autoPlayVideo: this.localStorageService.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO) === 'true',
314
315 // session storage keys
316 autoPlayNextVideo: this.sessionStorageService.getItem(UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true'
317 })
318 }
319
320 getUsers (parameters: { 256 getUsers (parameters: {
321 pagination: RestPagination 257 pagination: RestPagination
322 sort: SortMeta 258 sort: SortMeta
diff --git a/client/src/app/menu/menu.component.html b/client/src/app/menu/menu.component.html
index 9ea991042..48b3fdc85 100644
--- a/client/src/app/menu/menu.component.html
+++ b/client/src/app/menu/menu.component.html
@@ -60,7 +60,7 @@
60 <my-global-icon iconName="p2p" aria-hidden="true"></my-global-icon> 60 <my-global-icon iconName="p2p" aria-hidden="true"></my-global-icon>
61 <ng-container i18n>Help share videos</ng-container> 61 <ng-container i18n>Help share videos</ng-container>
62 62
63 <my-input-switch class="ml-auto" [checked]="user.webTorrentEnabled"></my-input-switch> 63 <my-input-switch class="ml-auto" [checked]="user.p2pEnabled"></my-input-switch>
64 </a> 64 </a>
65 65
66 <div class="dropdown-divider"></div> 66 <div class="dropdown-divider"></div>
diff --git a/client/src/app/menu/menu.component.ts b/client/src/app/menu/menu.component.ts
index d5ddc29cb..983f0a938 100644
--- a/client/src/app/menu/menu.component.ts
+++ b/client/src/app/menu/menu.component.ts
@@ -196,9 +196,9 @@ export class MenuComponent implements OnInit {
196 196
197 toggleUseP2P () { 197 toggleUseP2P () {
198 if (!this.user) return 198 if (!this.user) return
199 this.user.webTorrentEnabled = !this.user.webTorrentEnabled 199 this.user.p2pEnabled = !this.user.p2pEnabled
200 200
201 this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled }) 201 this.userService.updateMyProfile({ p2pEnabled: this.user.p2pEnabled })
202 .subscribe(() => this.authService.refreshUserInformation()) 202 .subscribe(() => this.authService.refreshUserInformation())
203 } 203 }
204 204
diff --git a/client/src/app/shared/shared-search/search.service.ts b/client/src/app/shared/shared-search/search.service.ts
index 61acfb466..ad2de0f37 100644
--- a/client/src/app/shared/shared-search/search.service.ts
+++ b/client/src/app/shared/shared-search/search.service.ts
@@ -4,7 +4,6 @@ import { HttpClient, HttpParams } from '@angular/common/http'
4import { Injectable } from '@angular/core' 4import { Injectable } from '@angular/core'
5import { ComponentPaginationLight, RestExtractor, RestPagination, RestService } from '@app/core' 5import { ComponentPaginationLight, RestExtractor, RestPagination, RestService } from '@app/core'
6import { Video, VideoChannel, VideoChannelService, VideoService } from '@app/shared/shared-main' 6import { Video, VideoChannel, VideoChannelService, VideoService } from '@app/shared/shared-main'
7import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage'
8import { 7import {
9 ResultList, 8 ResultList,
10 Video as VideoServerModel, 9 Video as VideoServerModel,
@@ -25,11 +24,7 @@ export class SearchService {
25 private restService: RestService, 24 private restService: RestService,
26 private videoService: VideoService, 25 private videoService: VideoService,
27 private playlistService: VideoPlaylistService 26 private playlistService: VideoPlaylistService
28 ) { 27 ) { }
29 // Add ability to override search endpoint if the user updated this local storage key
30 const searchUrl = peertubeLocalStorage.getItem('search-url')
31 if (searchUrl) SearchService.BASE_SEARCH_URL = searchUrl
32 }
33 28
34 searchVideos (parameters: { 29 searchVideos (parameters: {
35 search?: string 30 search?: string
diff --git a/client/src/app/shared/shared-user-settings/user-video-settings.component.html b/client/src/app/shared/shared-user-settings/user-video-settings.component.html
index bc9dd0f7f..4843f65b9 100644
--- a/client/src/app/shared/shared-user-settings/user-video-settings.component.html
+++ b/client/src/app/shared/shared-user-settings/user-video-settings.component.html
@@ -38,7 +38,7 @@
38 38
39 <div class="form-group"> 39 <div class="form-group">
40 <my-peertube-checkbox 40 <my-peertube-checkbox
41 inputName="webTorrentEnabled" formControlName="webTorrentEnabled" [recommended]="true" 41 inputName="p2pEnabled" formControlName="p2pEnabled" [recommended]="true"
42 i18n-labelText labelText="Help share videos being played" 42 i18n-labelText labelText="Help share videos being played"
43 > 43 >
44 <ng-container ngProjectAs="description"> 44 <ng-container ngProjectAs="description">
diff --git a/client/src/app/shared/shared-user-settings/user-video-settings.component.ts b/client/src/app/shared/shared-user-settings/user-video-settings.component.ts
index 0cd889a8a..7d6b69469 100644
--- a/client/src/app/shared/shared-user-settings/user-video-settings.component.ts
+++ b/client/src/app/shared/shared-user-settings/user-video-settings.component.ts
@@ -34,7 +34,7 @@ export class UserVideoSettingsComponent extends FormReactive implements OnInit,
34 ngOnInit () { 34 ngOnInit () {
35 this.buildForm({ 35 this.buildForm({
36 nsfwPolicy: null, 36 nsfwPolicy: null,
37 webTorrentEnabled: null, 37 p2pEnabled: null,
38 autoPlayVideo: null, 38 autoPlayVideo: null,
39 autoPlayNextVideo: null, 39 autoPlayNextVideo: null,
40 videoLanguages: null 40 videoLanguages: null
@@ -48,7 +48,7 @@ export class UserVideoSettingsComponent extends FormReactive implements OnInit,
48 48
49 this.form.patchValue({ 49 this.form.patchValue({
50 nsfwPolicy: this.user.nsfwPolicy || this.defaultNSFWPolicy, 50 nsfwPolicy: this.user.nsfwPolicy || this.defaultNSFWPolicy,
51 webTorrentEnabled: this.user.webTorrentEnabled, 51 p2pEnabled: this.user.p2pEnabled,
52 autoPlayVideo: this.user.autoPlayVideo === true, 52 autoPlayVideo: this.user.autoPlayVideo === true,
53 autoPlayNextVideo: this.user.autoPlayNextVideo, 53 autoPlayNextVideo: this.user.autoPlayNextVideo,
54 videoLanguages: this.user.videoLanguages 54 videoLanguages: this.user.videoLanguages
@@ -65,7 +65,7 @@ export class UserVideoSettingsComponent extends FormReactive implements OnInit,
65 65
66 updateDetails (onlyKeys?: string[]) { 66 updateDetails (onlyKeys?: string[]) {
67 const nsfwPolicy = this.form.value['nsfwPolicy'] 67 const nsfwPolicy = this.form.value['nsfwPolicy']
68 const webTorrentEnabled = this.form.value['webTorrentEnabled'] 68 const p2pEnabled = this.form.value['p2pEnabled']
69 const autoPlayVideo = this.form.value['autoPlayVideo'] 69 const autoPlayVideo = this.form.value['autoPlayVideo']
70 const autoPlayNextVideo = this.form.value['autoPlayNextVideo'] 70 const autoPlayNextVideo = this.form.value['autoPlayNextVideo']
71 71
@@ -80,7 +80,7 @@ export class UserVideoSettingsComponent extends FormReactive implements OnInit,
80 80
81 let details: UserUpdateMe = { 81 let details: UserUpdateMe = {
82 nsfwPolicy, 82 nsfwPolicy,
83 webTorrentEnabled, 83 p2pEnabled,
84 autoPlayVideo, 84 autoPlayVideo,
85 autoPlayNextVideo, 85 autoPlayNextVideo,
86 videoLanguages 86 videoLanguages