socialnetworknews-web

A open-source Paper.li clone
git clone git://archive.git.mtrnord.blog/SocialNetworkNews/socialnetworknews-web.git
Log | Files | Refs | README | LICENSE

twitter.component.ts (2951B)


      1 import {throwError as observableThrowError, of} from 'rxjs';
      2 import {delay, mergeMap, retryWhen, take, concat} from 'rxjs/operators';
      3 import {Component, Inject, OnInit} from '@angular/core';
      4 import {ActivatedRoute, Router} from '@angular/router';
      5 import {ToastrService} from 'ngx-toastr';
      6 
      7 import {HttpClient} from '@angular/common/http';
      8 import {DOCUMENT} from '@angular/common';
      9 import {ApiService} from '../../api.service';
     10 @Component({
     11   selector: 'app-twitter',
     12   templateUrl: './twitter.component.html',
     13   styleUrls: ['./twitter.component.scss']
     14 })
     15 export class TwitterComponent implements OnInit {
     16   url: string;
     17   oauth_token: string;
     18   oauth_verifier: string;
     19 
     20   constructor(private apiService: ApiService, private toastr: ToastrService, private route: ActivatedRoute, @Inject(DOCUMENT) private document, private http: HttpClient, private router: Router) {
     21     this.url = document.location.protocol + '//' + document.location.hostname + '/api';
     22     this.apiService.inCallback = true;
     23   }
     24 
     25   ngOnInit() {
     26     this.route.queryParams
     27       .subscribe(params => {
     28         console.log(params);
     29 
     30         this.oauth_token = params.oauth_token;
     31         this.oauth_verifier = params.oauth_verifier;
     32 
     33         this.http.get(`${this.url}/login/twitter/callback?oauth_token=${this.oauth_token}&oauth_verifier=${this.oauth_verifier}`, { observe: 'response' })
     34           .pipe(
     35             // TODO: Better variable naming
     36             retryWhen(oerror => {
     37               return oerror
     38                 .pipe(
     39                   mergeMap((error: any) => {
     40                     if (String(error.status).startsWith('50')) {
     41                       return of(error.status)
     42                         .pipe(
     43                           delay(1000)
     44                         );
     45                     } else if (error.status === 404) {
     46                       return observableThrowError({error: 'Sorry, there was an error. The Server just doesn\'t find any data for the requested time :('});
     47                     }
     48                     return observableThrowError({error: 'Unknown error'});
     49                   }),
     50                   take(5),
     51                   // TODO: Allow to link to a Status Page
     52                   concat(observableThrowError({error: 'Sorry, there was an error (after 5 retries). This probably means we can\'t reach our API Server :('}))
     53                 );
     54             })
     55           )
     56           .subscribe(
     57           data => {
     58             this.apiService.userUUID = data.headers.get('UUID');
     59 
     60             // TODO redirect to the Profile instead of home page
     61             this.apiService.inCallback = false;
     62             this.router.navigate(['/']);
     63           },
     64           err => {
     65             this.toastr.error(err['error'], 'Error connecting API', {
     66               positionClass: 'toast-top-center',
     67               disableTimeOut: true
     68             });
     69             throw err;
     70           },
     71           () => console.log('done making callback')
     72         );
     73       });
     74   }
     75 
     76 }