socialnetworknews-web

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

commit 1cee5b9432cb0aae3438f6495c9f7b565d3db3bc
parent 8978f233479bd8e55bddd0d697d50f3b75766571
Author: MTRNord <mtrnord1@gmail.com>
Date:   Sat, 31 Mar 2018 20:57:34 +0200

Add hopefully working callback wrapper

Diffstat:
Msrc/app/api.service.ts | 2+-
Msrc/app/app.module.ts | 7++++---
Msrc/app/app.routing.ts | 7++++++-
Asrc/app/callback/twitter/twitter.component.html | 0
Asrc/app/callback/twitter/twitter.component.scss | 0
Asrc/app/callback/twitter/twitter.component.spec.ts | 25+++++++++++++++++++++++++
Asrc/app/callback/twitter/twitter.component.ts | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/app/login/login.component.scss | 6+++++-
Msrc/app/login/login.component.ts | 11++---------
9 files changed, 114 insertions(+), 15 deletions(-)

diff --git a/src/app/api.service.ts b/src/app/api.service.ts @@ -43,7 +43,7 @@ export interface Author { @Injectable() export class ApiService { - private url; + url: string; constructor(@Inject(DOCUMENT) private document, private http: HttpClient) { this.url = document.location.protocol + '//' + document.location.hostname + '/api'; } diff --git a/src/app/app.module.ts b/src/app/app.module.ts @@ -12,10 +12,11 @@ import { PapersComponent } from './papers/papers.component'; import { TweetListComponent } from './tweet-list/tweet-list.component'; import { PapersListComponent } from './papers-list/papers-list.component'; import { SpinnerComponent } from './spinner/spinner.component'; +import { LoginComponent } from './login/login.component'; +import { TwitterComponent as CallbackTwitterComponent } from './callback/twitter/twitter.component'; import {MatCardModule} from '@angular/material/card'; import {MatButtonModule} from '@angular/material/button'; -import {FlexLayoutModule} from '@angular/flex-layout'; import { ApiService } from './api.service'; @@ -25,7 +26,7 @@ import { CardComponent } from './card/card.component'; import {NgHttpLoaderModule} from 'ng-http-loader/ng-http-loader.module'; import {Lightbox} from './utils/lightbox'; import {LinkifyPipe} from './utils/linkifier'; -import { LoginComponent } from './login/login.component'; + Raven .config('https://b760c9f9035c472998ada3a02dcc81d3@sentry.io/294520', { environment: 'development', @@ -56,6 +57,7 @@ export class RavenErrorHandler implements ErrorHandler { CardComponent, LinkifyPipe, LoginComponent, + CallbackTwitterComponent, ], imports: [ BrowserModule.withServerTransition({appId: 'snn-app'}), @@ -67,7 +69,6 @@ export class RavenErrorHandler implements ErrorHandler { ToastrModule.forRoot(), MatCardModule, MatButtonModule, - FlexLayoutModule, ], providers: [ ApiService, diff --git a/src/app/app.routing.ts b/src/app/app.routing.ts @@ -1,9 +1,10 @@ +import {NgModule} from '@angular/core'; import {RouterModule, Routes} from '@angular/router'; import {MainComponent} from './main/main.component'; -import {NgModule} from '@angular/core'; import {PaperComponent} from './paper/paper.component'; import {PapersComponent} from './papers/papers.component'; import {LoginComponent} from './login/login.component'; +import { TwitterComponent as CallbackTwitterComponent } from './callback/twitter/twitter.component'; const routes: Routes = [ { @@ -22,6 +23,10 @@ const routes: Routes = [ path: 'login', component: LoginComponent, }, + { + path: 'login/twitter/callback', + component: CallbackTwitterComponent, + }, ]; @NgModule({ diff --git a/src/app/callback/twitter/twitter.component.html b/src/app/callback/twitter/twitter.component.html diff --git a/src/app/callback/twitter/twitter.component.scss b/src/app/callback/twitter/twitter.component.scss diff --git a/src/app/callback/twitter/twitter.component.spec.ts b/src/app/callback/twitter/twitter.component.spec.ts @@ -0,0 +1,25 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; + +import { TwitterComponent } from './twitter.component'; + +describe('TwitterComponent', () => { + let component: TwitterComponent; + let fixture: ComponentFixture<TwitterComponent>; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ TwitterComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(TwitterComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/callback/twitter/twitter.component.ts b/src/app/callback/twitter/twitter.component.ts @@ -0,0 +1,71 @@ +import {Component, Inject, OnInit} from '@angular/core'; +import {ActivatedRoute} from '@angular/router'; +import {ToastrService} from 'ngx-toastr'; + +import {Observable} from 'rxjs/Observable'; +import 'rxjs/add/observable/of'; +import 'rxjs/add/operator/retryWhen'; +import 'rxjs/add/operator/mergeMap'; +import 'rxjs/add/operator/delay'; +import 'rxjs/add/operator/take'; +import 'rxjs/add/operator/concat'; +import 'rxjs/add/observable/throw'; +import {HttpClient} from '@angular/common/http'; +import {DOCUMENT} from '@angular/common'; + +@Component({ + selector: 'app-twitter', + templateUrl: './twitter.component.html', + styleUrls: ['./twitter.component.scss'] +}) +export class TwitterComponent implements OnInit { + url: string; + accessToken: string; + accessSecret: string; + + constructor(private toastr: ToastrService, private route: ActivatedRoute, @Inject(DOCUMENT) private document, private http: HttpClient) { + this.url = document.location.protocol + '//' + document.location.hostname + '/api'; + } + + ngOnInit() { + this.route.queryParams + .filter(params => params.order) + .subscribe(params => { + console.log(params); + + this.accessToken = params.accessToken; + this.accessSecret = params.accessSecret; + + this.http.get(`${this.url}/login/twitter/callback`) + // TODO: Better variable naming + .retryWhen(oerror => { + return oerror + .mergeMap((error: any) => { + if (String(error.status).startsWith('50')) { + return Observable.of(error.status).delay(1000); + } else if (error.status === 404) { + return Observable.throw({error: 'Sorry, there was an error. The Server just doesn\'t find any data for the requested time :('}); + } + return Observable.throw({error: 'Unknown error'}); + }) + .take(5) + // TODO: Allow to link to a Status Page + .concat(Observable.throw({error: 'Sorry, there was an error (after 5 retries). This probably means we can\'t reach our API Server :('})); + }) + .subscribe( + data => { + // TODO get accessToken Header + }, + err => { + this.toastr.error(err['error'], 'Error connecting API', { + positionClass: 'toast-top-center', + disableTimeOut: true + }); + throw err; + }, + () => console.log('done loading Yesterday') + ); + }); + } + +} diff --git a/src/app/login/login.component.scss b/src/app/login/login.component.scss @@ -1,7 +1,11 @@ -body { +main { background: #4688F1; padding: 0; margin: 0; + height: 100%; + width: 100%; + position: absolute; + top: 0; } .login-box { diff --git a/src/app/login/login.component.ts b/src/app/login/login.component.ts @@ -1,18 +1,11 @@ -import {Component, ViewEncapsulation} from '@angular/core'; +import {Component} from '@angular/core'; @Component({ selector: 'app-login', templateUrl: './login.component.html', styleUrls: ['./login.component.scss'], - encapsulation: ViewEncapsulation.None, }) export class LoginComponent { - - constructor() { } - - login() { - // TODO implement "/api/login/twitter" - console.log('TODO'); - } + constructor() {} }