From ccf6ed16f1eeb05b77103bd44bc06ccbbbba9bdd Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 27 May 2016 17:25:52 +0200 Subject: [PATCH] Do not prefix private attributes --- client/app/app.component.ts | 21 +++++----- client/app/friends/friend.service.ts | 12 +++--- client/app/shared/search.component.ts | 9 ++-- client/app/users/login/login.component.ts | 8 ++-- client/app/users/shared/auth.service.ts | 41 ++++++++++--------- client/app/users/shared/token.model.ts | 7 ++-- client/app/users/shared/user.model.ts | 6 +-- client/app/videos/shared/video.model.ts | 6 +-- client/app/videos/shared/video.service.ts | 18 ++++---- .../videos/video-add/video-add.component.ts | 2 +- .../videos/video-list/video-list.component.ts | 26 ++++++------ .../video-list/video-miniature.component.ts | 8 ++-- .../videos/video-list/video-sort.component.ts | 2 +- .../video-watch/video-watch.component.ts | 20 ++++----- client/tslint.json | 19 ++++++++- 15 files changed, 112 insertions(+), 93 deletions(-) diff --git a/client/app/app.component.ts b/client/app/app.component.ts index c94ff79a7..20c8c8724 100644 --- a/client/app/app.component.ts +++ b/client/app/app.component.ts @@ -51,16 +51,15 @@ import { export class AppComponent { isLoggedIn: boolean; search_field: string = name; - choices = [ ]; - - constructor(private _friendService: FriendService, - private _authService: AuthService, - private _router: Router + choices = []; + constructor(private friendService: FriendService, + private authService: AuthService, + private router: Router ) { - this.isLoggedIn = this._authService.isLoggedIn(); + this.isLoggedIn = this.authService.isLoggedIn(); - this._authService.loginChanged$.subscribe( + this.authService.loginChangedSource.subscribe( status => { if (status === AuthStatus.LoggedIn) { this.isLoggedIn = true; @@ -75,9 +74,9 @@ export class AppComponent { search: search.value, field: search.field }; - this._router.navigate(['VideosList', params]); + this.router.navigate(['VideosList', params]); } else { - this._router.navigate(['VideosList']); + this.router.navigate(['VideosList']); } } @@ -86,7 +85,7 @@ export class AppComponent { } makeFriends() { - this._friendService.makeFriends().subscribe( + this.friendService.makeFriends().subscribe( status => { if (status === 409) { alert('Already made friends!'); @@ -99,7 +98,7 @@ export class AppComponent { } quitFriends() { - this._friendService.quitFriends().subscribe( + this.friendService.quitFriends().subscribe( status => { alert('Quit friends!'); }, diff --git a/client/app/friends/friend.service.ts b/client/app/friends/friend.service.ts index d143ec40d..bdfa7baec 100644 --- a/client/app/friends/friend.service.ts +++ b/client/app/friends/friend.service.ts @@ -4,23 +4,23 @@ import { Observable } from 'rxjs/Rx'; @Injectable() export class FriendService { - private _baseFriendsUrl = '/api/v1/pods/'; + private static BASE_FRIEND_URL: string = '/api/v1/pods/'; constructor (private http: Http) {} makeFriends() { - return this.http.get(this._baseFriendsUrl + 'makefriends') - .map(res => res.status) + return this.http.get(FriendService.BASE_FRIEND_URL + 'makefriends') + .map(res => res.status) .catch(this.handleError); } quitFriends() { - return this.http.get(this._baseFriendsUrl + 'quitfriends') - .map(res => res.status) + return this.http.get(FriendService.BASE_FRIEND_URL + 'quitfriends') + .map(res => res.status) .catch(this.handleError); } - private handleError (error: Response) { + private handleError (error: Response): Observable { console.error(error); return Observable.throw(error.json().error || 'Server error'); } diff --git a/client/app/shared/search.component.ts b/client/app/shared/search.component.ts index 519810f9b..674518aba 100644 --- a/client/app/shared/search.component.ts +++ b/client/app/shared/search.component.ts @@ -12,12 +12,13 @@ import { SearchField } from './search-field.type'; }) export class SearchComponent { - @Output() search: EventEmitter = new EventEmitter(); + @Output() search = new EventEmitter(); searchCriterias: Search = { field: 'name', value: '' }; + fieldChoices = { name: 'Name', author: 'Author', @@ -29,18 +30,18 @@ export class SearchComponent { return Object.keys(this.fieldChoices); } - getStringChoice(choiceKey: SearchField): string { + getStringChoice(choiceKey: SearchField) { return this.fieldChoices[choiceKey]; } - choose($event:MouseEvent, choice: SearchField) { + choose($event: MouseEvent, choice: SearchField) { $event.preventDefault(); $event.stopPropagation(); this.searchCriterias.field = choice; } - doSearch(): void { + doSearch() { this.search.emit(this.searchCriterias); } diff --git a/client/app/users/login/login.component.ts b/client/app/users/login/login.component.ts index 33590ad4c..8e369541d 100644 --- a/client/app/users/login/login.component.ts +++ b/client/app/users/login/login.component.ts @@ -10,17 +10,17 @@ import { AuthService, AuthStatus, User } from '../shared/index'; }) export class UserLoginComponent { - constructor(private _authService: AuthService, private _router: Router) {} + constructor(private authService: AuthService, private router: Router) {} login(username: string, password: string) { - this._authService.login(username, password).subscribe( + this.authService.login(username, password).subscribe( result => { const user = new User(username, result); user.save(); - this._authService.setStatus(AuthStatus.LoggedIn); + this.authService.setStatus(AuthStatus.LoggedIn); - this._router.navigate(['VideosList']); + this.router.navigate(['VideosList']); }, error => { if (error.error === 'invalid_grant') { diff --git a/client/app/users/shared/auth.service.ts b/client/app/users/shared/auth.service.ts index 1cb042db5..b1da94436 100644 --- a/client/app/users/shared/auth.service.ts +++ b/client/app/users/shared/auth.service.ts @@ -7,27 +7,28 @@ import { User } from './user.model'; @Injectable() export class AuthService { - loginChanged$; + private static BASE_LOGIN_URL = '/api/v1/users/token'; + private static BASE_CLIENT_URL = '/api/v1/users/client'; - private _loginChanged; - private _baseLoginUrl = '/api/v1/users/token'; - private _baseClientUrl = '/api/v1/users/client'; - private _clientId = ''; - private _clientSecret = ''; + loginChangedSource: Observable; - constructor (private http: Http) { - this._loginChanged = new Subject(); - this.loginChanged$ = this._loginChanged.asObservable(); + private loginChanged: Subject; + private clientId: string; + private clientSecret: string; + + constructor(private http: Http) { + this.loginChanged = new Subject(); + this.loginChangedSource = this.loginChanged.asObservable(); // Fetch the client_id/client_secret // FIXME: save in local storage? - this.http.get(this._baseClientUrl) + this.http.get(AuthService.BASE_CLIENT_URL) .map(res => res.json()) .catch(this.handleError) .subscribe( result => { - this._clientId = result.client_id; - this._clientSecret = result.client_secret; + this.clientId = result.client_id; + this.clientSecret = result.client_secret; console.log('Client credentials loaded.'); }, error => { @@ -38,8 +39,8 @@ export class AuthService { login(username: string, password: string) { let body = new URLSearchParams(); - body.set('client_id', this._clientId); - body.set('client_secret', this._clientSecret); + body.set('client_id', this.clientId); + body.set('client_secret', this.clientSecret); body.set('response_type', 'code'); body.set('grant_type', 'password'); body.set('scope', 'upload'); @@ -53,7 +54,7 @@ export class AuthService { headers: headers }; - return this.http.post(this._baseLoginUrl, body.toString(), options) + return this.http.post(AuthService.BASE_LOGIN_URL, body.toString(), options) .map(res => res.json()) .catch(this.handleError); } @@ -62,7 +63,7 @@ export class AuthService { // TODO make HTTP request } - getRequestHeader(): Headers { + getRequestHeader() { return new Headers({ 'Authorization': `${this.getTokenType()} ${this.getToken()}` }); } @@ -70,11 +71,11 @@ export class AuthService { return new RequestOptions({ headers: this.getRequestHeader() }); } - getToken(): string { + getToken() { return localStorage.getItem('access_token'); } - getTokenType(): string { + getTokenType() { return localStorage.getItem('token_type'); } @@ -88,7 +89,7 @@ export class AuthService { return user; } - isLoggedIn(): boolean { + isLoggedIn() { if (this.getToken()) { return true; } else { @@ -97,7 +98,7 @@ export class AuthService { } setStatus(status: AuthStatus) { - this._loginChanged.next(status); + this.loginChanged.next(status); } private handleError (error: Response) { diff --git a/client/app/users/shared/token.model.ts b/client/app/users/shared/token.model.ts index b7872e74a..021c83fad 100644 --- a/client/app/users/shared/token.model.ts +++ b/client/app/users/shared/token.model.ts @@ -3,7 +3,7 @@ export class Token { refresh_token: string; token_type: string; - static load(): Token { + static load() { return new Token({ access_token: localStorage.getItem('access_token'), refresh_token: localStorage.getItem('refresh_token'), @@ -11,10 +11,11 @@ export class Token { }); } - constructor (hash?: any) { + constructor(hash?: any) { if (hash) { this.access_token = hash.access_token; this.refresh_token = hash.refresh_token; + if (hash.token_type === 'bearer') { this.token_type = 'Bearer'; } else { @@ -23,7 +24,7 @@ export class Token { } } - save():void { + save() { localStorage.setItem('access_token', this.access_token); localStorage.setItem('refresh_token', this.refresh_token); localStorage.setItem('token_type', this.token_type); diff --git a/client/app/users/shared/user.model.ts b/client/app/users/shared/user.model.ts index 73fd4ddc0..ca0a5f26c 100644 --- a/client/app/users/shared/user.model.ts +++ b/client/app/users/shared/user.model.ts @@ -4,16 +4,16 @@ export class User { username: string; token: Token; - static load(): User { + static load() { return new User(localStorage.getItem('username'), Token.load()); } - constructor (username: string, hash_token: any) { + constructor(username: string, hash_token: any) { this.username = username; this.token = new Token(hash_token); } - save(): void { + save() { localStorage.setItem('username', this.username); this.token.save(); } diff --git a/client/app/videos/shared/video.model.ts b/client/app/videos/shared/video.model.ts index eec537c9e..2b018ad86 100644 --- a/client/app/videos/shared/video.model.ts +++ b/client/app/videos/shared/video.model.ts @@ -11,7 +11,7 @@ export class Video { by: string; duration: string; - private static createDurationString(duration: number): string { + private static createDurationString(duration: number) { const minutes = Math.floor(duration / 60); const seconds = duration % 60; const minutes_padding = minutes >= 10 ? '' : '0'; @@ -20,7 +20,7 @@ export class Video { return minutes_padding + minutes.toString() + ':' + seconds_padding + seconds.toString(); } - private static createByString(author: string, podUrl: string): string { + private static createByString(author: string, podUrl: string) { let [ host, port ] = podUrl.replace(/^https?:\/\//, '').split(':'); if (port === '80' || port === '443') { @@ -57,7 +57,7 @@ export class Video { this.by = Video.createByString(hash.author, hash.podUrl); } - isRemovableBy(user): boolean { + isRemovableBy(user) { return this.isLocal === true && user && this.author === user.username; } } diff --git a/client/app/videos/shared/video.service.ts b/client/app/videos/shared/video.service.ts index 78789c3cc..b6e0800a0 100644 --- a/client/app/videos/shared/video.service.ts +++ b/client/app/videos/shared/video.service.ts @@ -10,30 +10,30 @@ import { Video } from './video.model'; @Injectable() export class VideoService { - private _baseVideoUrl = '/api/v1/videos/'; + private static BASE_VIDEO_URL = '/api/v1/videos/'; - constructor (private http: Http, private _authService: AuthService) {} + constructor(private http: Http, private authService: AuthService) {} getVideos(pagination: Pagination, sort: SortField) { const params = this.createPaginationParams(pagination); if (sort) params.set('sort', sort); - return this.http.get(this._baseVideoUrl, { search: params }) + return this.http.get(VideoService.BASE_VIDEO_URL, { search: params }) .map(res => res.json()) .map(this.extractVideos) .catch(this.handleError); } getVideo(id: string) { - return this.http.get(this._baseVideoUrl + id) + return this.http.get(VideoService.BASE_VIDEO_URL + id) .map(res =>