From 4a62fde705d3d2b2a29f9ee66457908b5ab83e2b Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Thu, 13 Sep 2018 00:28:13 -0400 Subject: [PATCH] fetching first messages --- src/app/components/toot/toot.component.scss | 2 +- src/app/models/stream.models.ts | 118 ++++++++++-------- .../streams-main-display.component.ts | 5 +- src/app/services/accounts.service.ts | 1 - 4 files changed, 69 insertions(+), 57 deletions(-) diff --git a/src/app/components/toot/toot.component.scss b/src/app/components/toot/toot.component.scss index a81e1d96..d687f8f9 100644 --- a/src/app/components/toot/toot.component.scss +++ b/src/app/components/toot/toot.component.scss @@ -20,7 +20,7 @@ #toot-avatar img { width: 50px; height: 50px; - border-radius: 4px; + border-radius: 2px; margin: 0; } diff --git a/src/app/models/stream.models.ts b/src/app/models/stream.models.ts index cc0134c7..a66402a0 100644 --- a/src/app/models/stream.models.ts +++ b/src/app/models/stream.models.ts @@ -1,60 +1,72 @@ -import { Http, Headers, Response } from "@angular/http"; +import { HttpClient, HttpHeaders } from "@angular/common/http"; +import { Store } from "@ngxs/store"; import { BehaviorSubject } from "rxjs"; import { AccountWrapper } from "./account.models"; -// import { LocalAccount } from "../services/accounts.service"; import { ApiRoutes } from "../services/models/api.settings"; import { Account, Status } from "../services/models/mastodon.interfaces"; import { StreamingService, StreamingWrapper } from "../services/streaming.service"; import { StreamTypeEnum } from "../states/streams.state"; +import { AccountInfo } from "../states/accounts.state"; + export class Stream { - private apiRoutes = new ApiRoutes(); + private apiRoutes = new ApiRoutes(); + private account: AccountInfo; - statuses = new BehaviorSubject([]); - - constructor( - private readonly httpService: Http, - public streamName: string, - private readonly type: StreamTypeEnum) { + statuses = new BehaviorSubject([]); - this.retrieveToots(); //TODO change this for WebSockets - } + constructor( + private readonly httpClient: HttpClient, + private readonly store: Store, + public streamName: string, + private readonly type: StreamTypeEnum, + username: string) { - private test: StreamingWrapper; - private retrieveToots(): void { - // //TEST - // const service = new StreamingService(); - // this.test = service.getStreaming(this.account.mastodonInstance, this.account.tokenData.access_token); - // //END TEST - - const route = this.getTimelineRoute(); + const splitedUserName = username.split('@'); + const user = splitedUserName[0]; + const instance = splitedUserName[1]; + this.account = this.getRegisteredAccounts().find(x => x.username == user && x.instance == instance); - const header = new Headers(); - // header.append("Authorization", `Bearer ${this.account.tokenData.access_token}`); - - // this.httpService.get(this.account.mastodonInstance + route, { headers: header }).toPromise() - // .then((res: Response) => { - // const statuses = (res.json() as Status[]) - // .map((status: Status) => { - // return new TootWrapper(status); - // }); - - // this.statuses.next(statuses); - // }); - - } - - 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`; + this.retrieveToots(); //TODO change this for WebSockets + } + + private getRegisteredAccounts(): AccountInfo[] { + var regAccounts = this.store.snapshot().registeredaccounts.accounts; + return regAccounts; + } + + private test: StreamingWrapper; + private retrieveToots(): void { + // //TEST + // const service = new StreamingService(); + // this.test = service.getStreaming(this.account.mastodonInstance, this.account.tokenData.access_token); + // //END TEST + + const route = `https://${this.account.instance}${this.getTimelineRoute()}`; + + const headers = new HttpHeaders({ 'Authorization': `Bearer ${this.account.token.access_token}` }); + this.httpClient.get(route, { headers: headers }).toPromise() + .then((results: Status[]) => { + var statuses = results.map((status: Status) => { + return new TootWrapper(status); + }); + + this.statuses.next(statuses); + }); + + } + + 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`; + } } - } } @@ -63,18 +75,18 @@ export class Stream { // 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; + 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; - } + this.content = status.content; + } - account: AccountWrapper; //TODO change to Account - content: string; + account: AccountWrapper; //TODO change to Account + content: string; } diff --git a/src/app/pages/streams-main-display/streams-main-display.component.ts b/src/app/pages/streams-main-display/streams-main-display.component.ts index 30a90a4f..5ddc1ff6 100644 --- a/src/app/pages/streams-main-display/streams-main-display.component.ts +++ b/src/app/pages/streams-main-display/streams-main-display.component.ts @@ -6,6 +6,7 @@ import { StreamElement } from "../../states/streams.state"; import { Store } from "@ngxs/store"; import { Http } from "@angular/http"; import { NavigationService } from "../../services/navigation.service"; +import { HttpClient } from "@angular/common/http"; @Component({ @@ -23,7 +24,7 @@ export class StreamsMainDisplayComponent implements OnInit, OnDestroy { constructor( private readonly navigationService: NavigationService, - private readonly http: Http, + private readonly httpClient: HttpClient, private readonly store: Store) { this.streams$ = this.store.select(state => state.streamsstatemodel.streams); @@ -33,7 +34,7 @@ export class StreamsMainDisplayComponent implements OnInit, OnDestroy { this.streamsStateSub = this.streams$.subscribe((streams: StreamElement[]) => { this.streams.length = 0; for (const stream of streams) { - const newStream = new Stream(this.http, stream.name, stream.type); + const newStream = new Stream(this.httpClient, this.store, stream.name, stream.type, stream.username); this.streams.push(newStream); } diff --git a/src/app/services/accounts.service.ts b/src/app/services/accounts.service.ts index 4d0c7282..b2b3df8f 100644 --- a/src/app/services/accounts.service.ts +++ b/src/app/services/accounts.service.ts @@ -17,7 +17,6 @@ export class AccountsService { retrieveAccountDetails(account: AccountInfo): Promise { const headers = new HttpHeaders({'Authorization':`Bearer ${account.token.access_token}`}); - // const headers = new HttpHeaders({'Bearer':`${account.token}`}); return this.httpClient.get('https://' + account.instance + this.apiRoutes.getCurrentAccount, {headers: headers}).toPromise(); }