fetching first messages

This commit is contained in:
Nicolas Constant 2018-09-13 00:28:13 -04:00
parent bcf26ee1fc
commit 4a62fde705
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
4 changed files with 69 additions and 57 deletions

View File

@ -20,7 +20,7 @@
#toot-avatar img {
width: 50px;
height: 50px;
border-radius: 4px;
border-radius: 2px;
margin: 0;
}

View File

@ -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<TootWrapper[]>([]);
constructor(
private readonly httpService: Http,
public streamName: string,
private readonly type: StreamTypeEnum) {
statuses = new BehaviorSubject<TootWrapper[]>([]);
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 = <AccountInfo[]>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<Status[]>(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;
}

View File

@ -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);
}

View File

@ -17,7 +17,6 @@ export class AccountsService {
retrieveAccountDetails(account: AccountInfo): Promise<Account> {
const headers = new HttpHeaders({'Authorization':`Bearer ${account.token.access_token}`});
// const headers = new HttpHeaders({'Bearer':`${account.token}`});
return this.httpClient.get<Account>('https://' + account.instance + this.apiRoutes.getCurrentAccount, {headers: headers}).toPromise();
}