clean up, less boilerplates

This commit is contained in:
Nicolas Constant 2018-09-15 21:57:00 -04:00
parent 9095699158
commit 1137e7f69e
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
6 changed files with 95 additions and 137 deletions

View File

@ -1,9 +1,9 @@
<div class="stream-column">
<div class="stream-column__stream-header">
<a href title="return to top" (click)="goToTop()"><h1>{{ stream.streamName.toUpperCase() }}</h1></a>
<a href title="return to top" (click)="goToTop()"><h1>{{ streamElement.name.toUpperCase() }}</h1></a>
</div>
<div class="stream-toots" data-simplebar>
<div *ngFor="let toot of toots">
<div *ngFor="let toot of statuses">
<app-toot [toot]="toot"></app-toot>
</div>
</div>

View File

@ -1,10 +1,12 @@
import { Component, OnInit, Input } from "@angular/core";
import { Stream, TootWrapper } from "../../models/stream.models";
import { AccountWrapper } from "../../models/account.models";
import { StreamElement } from "../../states/streams.state";
import { StreamingService } from "../../services/streaming.service";
import { HttpClient } from "@angular/common/http";
import { StreamElement, StreamTypeEnum } from "../../states/streams.state";
import { StreamingService, StreamingWrapper, EventEnum, StatusUpdate } from "../../services/streaming.service";
import { HttpClient, HttpHeaders } from "@angular/common/http";
import { Store } from "@ngxs/store";
import { AccountInfo } from "../../states/accounts.state";
import { ApiRoutes } from "../../services/models/api.settings";
import { Status } from "../../services/models/mastodon.interfaces";
@Component({
selector: "app-stream",
@ -12,21 +14,25 @@ import { Store } from "@ngxs/store";
styleUrls: ["./stream.component.scss"]
})
export class StreamComponent implements OnInit {
stream: Stream;
private apiRoutes = new ApiRoutes();
private account: AccountInfo;
private websocketStreaming: StreamingWrapper;
private type: StreamTypeEnum;
statuses: TootWrapper[] = [];
private _streamElement: StreamElement;
@Input()
set streamElement(streamElement: StreamElement) {
this._streamElement = streamElement;
console.log('streamElement');
console.log(streamElement);
this.stream = new Stream(this.streamingService, this.httpClient, this.store, streamElement.name, streamElement.type, streamElement.username);
this.stream.statuses.subscribe((results: TootWrapper[]) => {
for (let t of results) {
this.toots.unshift(t);
}
});
const splitedUserName = streamElement.username.split('@');
const user = splitedUserName[0];
const instance = splitedUserName[1];
this.account = this.getRegisteredAccounts().find(x => x.username == user && x.instance == instance);
this.type = streamElement.type;
this.retrieveToots(); //TODO change this for WebSockets
this.launchWebsocket();
}
get streamElement(): StreamElement {
@ -49,4 +55,77 @@ export class StreamComponent implements OnInit {
return false;
}
private getTimelineRoute(): string {
switch (this.type) {
case StreamTypeEnum.personnal:
return this.apiRoutes.getHomeTimeline;
case StreamTypeEnum.local:
return this.apiRoutes.getPublicTimeline + `?Local=true`;
case StreamTypeEnum.global:
return this.apiRoutes.getPublicTimeline + `?Local=false`;
}
}
private getRegisteredAccounts(): AccountInfo[] {
var regAccounts = <AccountInfo[]>this.store.snapshot().registeredaccounts.accounts;
return regAccounts;
}
private retrieveToots(): void {
const route = `https://${this.account.instance}${this.getTimelineRoute()}`;
const headers = new HttpHeaders({ 'Authorization': `Bearer ${this.account.token.access_token}` });
this.httpClient.get<Status[]>(route, { headers: headers }).toPromise()
.then((results: Status[]) => {
var statuses = results.map((status: Status) => {
return new TootWrapper(status);
});
for (const s of statuses) {
this.statuses.push(s);
}
});
}
private launchWebsocket(): void {
//Web socket
let streamRequest: string;
switch (this.type) {
case StreamTypeEnum.global:
streamRequest = 'public';
break;
case StreamTypeEnum.local:
streamRequest = 'public:local';
break;
case StreamTypeEnum.personnal:
streamRequest = 'user';
break;
}
this.websocketStreaming = this.streamingService.getStreaming(this.account.instance, this.account.token.access_token, streamRequest);
this.websocketStreaming.statusUpdateSubjet.subscribe((update: StatusUpdate) => {
if (update) {
if (update.type === EventEnum.update) {
this.statuses.unshift(new TootWrapper(update.status));
}
}
});
}
}
export class TootWrapper {
constructor(status: Status) {
this.account = new AccountWrapper();
this.account.username = status.account.username;
this.account.display_name = status.account.display_name;
this.account.avatar = status.account.avatar;
this.content = status.content;
}
account: AccountWrapper; //TODO change to Account
content: string;
}

View File

@ -1,5 +1,5 @@
import { Component, OnInit, Input } from "@angular/core";
import { TootWrapper } from "../../../models/stream.models";
import { TootWrapper } from "../stream.component";
@Component({
selector: "app-toot",

View File

@ -26,9 +26,7 @@ export class StreamsSelectionFooterComponent implements OnInit {
}
onColumnSelection(index: number): boolean {
console.warn(`column selected: ${index}`);
this.navigationService.columnSelected(index);
return false;
}
}

View File

@ -1,117 +0,0 @@
import { HttpClient, HttpHeaders } from "@angular/common/http";
import { Store } from "@ngxs/store";
import { BehaviorSubject } from "rxjs";
import { AccountWrapper } from "./account.models";
import { ApiRoutes } from "../services/models/api.settings";
import { Account, Status } from "../services/models/mastodon.interfaces";
import { StreamingService, StreamingWrapper, StatusUpdate, EventEnum } from "../services/streaming.service";
import { StreamTypeEnum } from "../states/streams.state";
import { AccountInfo } from "../states/accounts.state";
export class Stream {
private apiRoutes = new ApiRoutes();
private account: AccountInfo;
private websocketStreaming: StreamingWrapper;
statuses = new BehaviorSubject<TootWrapper[]>([]);
constructor(
private readonly streamingService: StreamingService,
private readonly httpClient: HttpClient,
private readonly store: Store,
public streamName: string,
private readonly type: StreamTypeEnum,
username: string) {
const splitedUserName = username.split('@');
const user = splitedUserName[0];
const instance = splitedUserName[1];
this.account = this.getRegisteredAccounts().find(x => x.username == user && x.instance == instance);
this.retrieveToots(); //TODO change this for WebSockets
this.launchWebsocket();
}
private getRegisteredAccounts(): AccountInfo[] {
var regAccounts = <AccountInfo[]>this.store.snapshot().registeredaccounts.accounts;
return regAccounts;
}
private retrieveToots(): void {
const route = `https://${this.account.instance}${this.getTimelineRoute()}`;
const headers = new HttpHeaders({ 'Authorization': `Bearer ${this.account.token.access_token}` });
this.httpClient.get<Status[]>(route, { headers: headers }).toPromise()
.then((results: Status[]) => {
var statuses = results.map((status: Status) => {
return new TootWrapper(status);
});
this.statuses.next(statuses);
});
}
private launchWebsocket(): void {
//Web socket
let streamRequest: string;
switch (this.type) {
case StreamTypeEnum.global:
streamRequest = 'public';
break;
case StreamTypeEnum.local:
streamRequest = 'public:local';
break;
case StreamTypeEnum.personnal:
streamRequest = 'user';
break;
}
this.websocketStreaming = this.streamingService.getStreaming(this.account.instance, this.account.token.access_token, streamRequest);
this.websocketStreaming.statusUpdateSubjet.subscribe((update: StatusUpdate) => {
if (update) {
if (update.type === EventEnum.update) {
this.statuses.next([new TootWrapper(update.status)]);
}
}
});
}
private getTimelineRoute(): string {
switch (this.type) {
case StreamTypeEnum.personnal:
return this.apiRoutes.getHomeTimeline;
case StreamTypeEnum.local:
return this.apiRoutes.getPublicTimeline + `?Local=true`;
case StreamTypeEnum.global:
return this.apiRoutes.getPublicTimeline + `?Local=false`;
}
}
}
// export enum StreamTypeEnum {
// Home,
// Public,
// Local
// }
export class TootWrapper {
constructor(status: Status) {
this.account = new AccountWrapper();
this.account.username = status.account.username;
this.account.display_name = status.account.display_name;
this.account.avatar = status.account.avatar;
this.content = status.content;
}
account: AccountWrapper; //TODO change to Account
content: string;
}

View File

@ -2,7 +2,6 @@ import { Component, OnInit, OnDestroy, QueryList, ViewChildren, ElementRef } fro
import { Observable, Subscription } from "rxjs";
import { Select } from "@ngxs/store";
import { Stream } from "../../models/stream.models";
import { StreamElement } from "../../states/streams.state";
import { NavigationService } from "../../services/navigation.service";
@ -14,7 +13,6 @@ import { NavigationService } from "../../services/navigation.service";
export class StreamsMainDisplayComponent implements OnInit, OnDestroy {
@Select(state => state.streamsstatemodel.streams) streamElements$: Observable<StreamElement[]>;
streams: Stream[] = [];
private columnSelectedSub: Subscription;
constructor(