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;
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']);
}
}
}
makeFriends() {
- this._friendService.makeFriends().subscribe(
+ this.friendService.makeFriends().subscribe(
status => {
if (status === 409) {
alert('Already made friends!');
}
quitFriends() {
- this._friendService.quitFriends().subscribe(
+ this.friendService.quitFriends().subscribe(
status => {
alert('Quit friends!');
},
@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 => <number> 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 => <number> 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<number> {
console.error(error);
return Observable.throw(error.json().error || 'Server error');
}
})
export class SearchComponent {
- @Output() search: EventEmitter<Search> = new EventEmitter<Search>();
+ @Output() search = new EventEmitter<Search>();
searchCriterias: Search = {
field: 'name',
value: ''
};
+
fieldChoices = {
name: 'Name',
author: 'Author',
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);
}
})
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') {
@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<AuthStatus>;
- constructor (private http: Http) {
- this._loginChanged = new Subject<AuthStatus>();
- this.loginChanged$ = this._loginChanged.asObservable();
+ private loginChanged: Subject<AuthStatus>;
+ private clientId: string;
+ private clientSecret: string;
+
+ constructor(private http: Http) {
+ this.loginChanged = new Subject<AuthStatus>();
+ 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 => {
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');
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);
}
// TODO make HTTP request
}
- getRequestHeader(): Headers {
+ getRequestHeader() {
return new Headers({ 'Authorization': `${this.getTokenType()} ${this.getToken()}` });
}
return new RequestOptions({ headers: this.getRequestHeader() });
}
- getToken(): string {
+ getToken() {
return localStorage.getItem('access_token');
}
- getTokenType(): string {
+ getTokenType() {
return localStorage.getItem('token_type');
}
return user;
}
- isLoggedIn(): boolean {
+ isLoggedIn() {
if (this.getToken()) {
return true;
} else {
}
setStatus(status: AuthStatus) {
- this._loginChanged.next(status);
+ this.loginChanged.next(status);
}
private handleError (error: Response) {
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'),
});
}
- 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 {
}
}
- save():void {
+ save() {
localStorage.setItem('access_token', this.access_token);
localStorage.setItem('refresh_token', this.refresh_token);
localStorage.setItem('token_type', this.token_type);
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();
}
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';
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') {
this.by = Video.createByString(hash.author, hash.podUrl);
}
- isRemovableBy(user): boolean {
+ isRemovableBy(user) {
return this.isLocal === true && user && this.author === user.username;
}
}
@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 => <Video> res.json())
.catch(this.handleError);
}
removeVideo(id: string) {
- const options = this._authService.getAuthRequestOptions();
- return this.http.delete(this._baseVideoUrl + id, options)
+ const options = this.authService.getAuthRequestOptions();
+ return this.http.delete(VideoService.BASE_VIDEO_URL + id, options)
.map(res => <number> res.status)
.catch(this.handleError);
}
if (search.field) params.set('field', search.field);
if (sort) params.set('sort', sort);
- return this.http.get(this._baseVideoUrl + 'search/' + encodeURIComponent(search.value), { search: params })
+ return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params })
.map(res => res.json())
.map(this.extractVideos)
.catch(this.handleError);
}
- private extractVideos (body: any) {
+ private extractVideos(body: any) {
const videos_json = body.data;
const totalVideos = body.total;
const videos = [];
return { videos, totalVideos };
}
- private handleError (error: Response) {
+ private handleError(error: Response) {
console.error(error);
return Observable.throw(error.json().error || 'Server error');
}
import { AuthService, User } from '../../users/index';
// TODO: import it with systemjs
-declare var jQuery:any;
+declare var jQuery: any;
@Component({
selector: 'my-videos-add',
total: 0
};
sort: SortField;
- loading: boolean = false;
+ loading = false;
private search: Search;
constructor(
- private _authService: AuthService,
- private _videoService: VideoService,
- private _routeParams: RouteParams,
- private _router: Router
+ private authService: AuthService,
+ private videoService: VideoService,
+ private routeParams: RouteParams,
+ private router: Router
) {
this.search = {
- value: this._routeParams.get('search'),
- field: <SearchField>this._routeParams.get('field')
+ value: this.routeParams.get('search'),
+ field: <SearchField>this.routeParams.get('field')
};
- this.sort = <SortField>this._routeParams.get('sort') || '-createdDate';
+ this.sort = <SortField>this.routeParams.get('sort') || '-createdDate';
}
ngOnInit() {
- if (this._authService.isLoggedIn()) {
+ if (this.authService.isLoggedIn()) {
this.user = User.load();
}
let observable = null;
if (this.search.value !== null) {
- observable = this._videoService.searchVideos(this.search, this.pagination, this.sort);
+ observable = this.videoService.searchVideos(this.search, this.pagination, this.sort);
} else {
- observable = this._videoService.getVideos(this.pagination, this.sort);
+ observable = this.videoService.getVideos(this.pagination, this.sort);
}
observable.subscribe(
);
}
- onRemoved(video: Video): void {
+ onRemoved(video: Video) {
this.videos.splice(this.videos.indexOf(video), 1);
}
params.field = this.search.field;
}
- this._router.navigate(['VideosList', params]);
+ this.router.navigate(['VideosList', params]);
this.getVideos();
}
}
@Input() video: Video;
@Input() user: User;
- hovering: boolean = false;
+ hovering = false;
- constructor(private _videoService: VideoService) {}
+ constructor(private videoService: VideoService) {}
onHover() {
this.hovering = true;
this.hovering = false;
}
- displayRemoveIcon(): boolean {
+ displayRemoveIcon() {
return this.hovering && this.video.isRemovableBy(this.user);
}
removeVideo(id: string) {
if (confirm('Do you really want to remove this video?')) {
- this._videoService.removeVideo(id).subscribe(
+ this.videoService.removeVideo(id).subscribe(
status => this.removed.emit(true),
error => alert(error)
);
return Object.keys(this.sortChoices);
}
- getStringChoice(choiceKey: SortField): string {
+ getStringChoice(choiceKey: SortField) {
return this.sortChoices[choiceKey];
}
numPeers: number;
loading: boolean = false;
- private _interval: NodeJS.Timer;
+ private interval: NodeJS.Timer;
private client: any;
constructor(
- private _videoService: VideoService,
- private _routeParams: RouteParams,
- private _elementRef: ElementRef
+ private videoService: VideoService,
+ private routeParams: RouteParams,
+ private elementRef: ElementRef
) {
// TODO: use a service
this.client = new WebTorrent({ dht: false });
}
ngOnInit() {
- let id = this._routeParams.get('id');
- this._videoService.getVideo(id).subscribe(
+ let id = this.routeParams.get('id');
+ this.videoService.getVideo(id).subscribe(
video => this.loadVideo(video),
error => alert(error)
);
this.client.add(this.video.magnetUri, (torrent) => {
this.loading = false;
console.log('Added ' + this.video.magnetUri + '.');
- torrent.files[0].appendTo(this._elementRef.nativeElement.querySelector('.embed-responsive'), (err) => {
+ torrent.files[0].appendTo(this.elementRef.nativeElement.querySelector('.embed-responsive'), (err) => {
if (err) {
alert('Cannot append the file.');
console.error(err);
});
// Refresh each second
- this._interval = setInterval(() => {
+ this.interval = setInterval(() => {
this.downloadSpeed = torrent.downloadSpeed;
this.uploadSpeed = torrent.uploadSpeed;
this.numPeers = torrent.numPeers;
});
}
- routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) : any {
+ routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) {
console.log('Removing video from webtorrent.');
- clearInterval(this._interval);
+ clearInterval(this.interval);
this.client.remove(this.video.magnetUri);
return true;
}
"use-pipe-transform-interface": true,
"pipe-naming": [true, "camelCase", "my"],
"component-class-suffix": true,
- "directive-class-suffix": true
+ "directive-class-suffix": true,
+
+ "typedef-whitespace": [ true,
+ {
+ "call-signature": "nospace",
+ "index-signature": "nospace",
+ "parameter": "nospace",
+ "property-declaration": "nospace",
+ "variable-declaration": "nospace"
+ }
+ ],
+ "whitespace": [ true,
+ "check-branch",
+ "check-decl",
+ "check-operator",
+ "check-separator",
+ "check-type"
+ ]
}
}