first iteration (and very candid) toots retrieval

This commit is contained in:
Nicolas Constant 2018-03-21 00:21:15 -04:00
parent 7e78496214
commit 7239dfa0fa
12 changed files with 204 additions and 47 deletions

View File

@ -111,6 +111,7 @@
<TypeScriptCompile Include="src\app\services\auth.service.ts" />
<TypeScriptCompile Include="src\app\services\models\api.settings.ts" />
<TypeScriptCompile Include="src\app\services\models\mastodon.interfaces.ts" />
<TypeScriptCompile Include="src\app\services\streams.service.ts" />
<TypeScriptCompile Include="src\environments\environment.prod.ts" />
<TypeScriptCompile Include="src\environments\environment.ts" />
<TypeScriptCompile Include="src\main.ts" />

View File

@ -1,7 +1,7 @@
import { BrowserModule } from "@angular/platform-browser";
import { FormsModule } from "@angular/forms";
import { HttpModule } from "@angular/http";
import { NgModule } from "@angular/core";
import { NgModule, APP_INITIALIZER } from "@angular/core";
import { RouterModule, Routes } from "@angular/router";
import { NgxElectronModule } from "ngx-electron";
@ -15,6 +15,7 @@ import { TootComponent } from "./components/toot/toot.component";
import { RegisterNewAccountComponent } from "./pages/register-new-account/register-new-account.component";
import { AuthService } from "./services/auth.service";
import { AccountsService } from "./services/accounts.service";
import { StreamsService } from "./services/streams.service";
const routes: Routes = [
{ path: "", redirectTo: "home", pathMatch: "full" },
@ -40,7 +41,11 @@ const routes: Routes = [
NgxElectronModule,
RouterModule.forRoot(routes)
],
providers: [AuthService, AccountsService],
providers: [AuthService, AccountsService, StreamsService, { provide: APP_INITIALIZER, useFactory: settingsServiceFactory, deps: [AccountsService], multi: true }],
bootstrap: [AppComponent]
})
export class AppModule { }
function settingsServiceFactory(service: AccountsService) {
return () => service.load();
}

View File

@ -18,17 +18,16 @@ export class LeftSideBarComponent implements OnInit, OnDestroy {
private readonly accountsService: AccountsService) { }
ngOnInit() {
this.accountsService.init.then(() => {
this.sub = this.accountsService.accountsSubject.subscribe((accounts: LocalAccount[]) => {
this.accounts.length = 0;
this.sub = this.accountsService.accountsSubject.subscribe((accounts: LocalAccount[]) => {
this.accounts.length = 0;
for (let acc of accounts) {
const acc1 = new AccountWrapper();
acc1.username = acc.mastodonAccount.username;
acc1.avatar = acc.mastodonAccount.avatar;
this.accounts.push(acc1);
}
});
for (let acc of accounts) {
const accWrapper = new AccountWrapper();
console.warn(acc);
accWrapper.username = `${acc.mastodonAccount.username}@${acc.mastodonInstance.replace("https://", "")}`;
accWrapper.avatar = acc.mastodonAccount.avatar;
this.accounts.push(accWrapper);
}
});
//const acc1 = new AccountWrapper();

View File

@ -1,4 +1,4 @@
import { Component, OnInit } from "@angular/core";
import { Component, OnInit, Input } from "@angular/core";
import { Stream, TootWrapper } from "../../models/stream.models";
import { AccountWrapper } from "../../models/account.models";
@ -8,27 +8,44 @@ import { AccountWrapper } from "../../models/account.models";
styleUrls: ["./stream.component.css"]
})
export class StreamComponent implements OnInit {
stream: Stream;
private _stream: Stream;
@Input()
set stream(stream: Stream) {
console.warn(stream);
this._stream = stream;
this._stream.statuses.subscribe((toots: TootWrapper[]) => {
for (let t of toots) {
this.toots.push(t);
}
});
}
get stream(): Stream {
return this._stream;
}
toots: TootWrapper[] = [];
constructor() { }
ngOnInit() {
//Stubs
const newStream = new Stream();
newStream.streamName = "Stream Name";
this.stream = newStream;
//const newStream = new Stream();
//newStream.streamName = "Stream Name";
//this.stream = newStream;
const acc1 = new AccountWrapper();
acc1.username = "@mastodon.social@Gargron";
acc1.avatar = "https://files.mastodon.social/accounts/avatars/000/000/001/original/4df197532c6b768c.png";
//const acc1 = new AccountWrapper();
//acc1.username = "@mastodon.social@Gargron";
//acc1.avatar = "https://files.mastodon.social/accounts/avatars/000/000/001/original/4df197532c6b768c.png";
for (let i = 0; i < 20; i++) {
const newToot = new TootWrapper();
newToot.account = acc1;
newToot.content = "Lorem Elsass ipsum tristique semper elit jetz gehts los lacus habitant Hans sagittis baeckeoffe condimentum id, salu bredele ch'ai libero, ftomi! hop Pfourtz ! id munster auctor, Miss Dahlias rhoncus Yo dû. Salu bissame turpis ante amet non sed gal Spätzle Gal !";
this.toots.push(newToot);
}
//for (let i = 0; i < 20; i++) {
// const newToot = new TootWrapper();
// newToot.account = acc1;
// newToot.content = "Lorem Elsass ipsum tristique semper elit jetz gehts los lacus habitant Hans sagittis baeckeoffe condimentum id, salu bredele ch'ai libero, ftomi! hop Pfourtz ! id munster auctor, Miss Dahlias rhoncus Yo dû. Salu bissame turpis ante amet non sed gal Spätzle Gal !";
// this.toots.push(newToot);
//}
}
goToTop(): boolean {

View File

@ -2,7 +2,6 @@
<div id="mam-toot-avatar">
<img src="{{ toot.account.avatar }}" />
</div>
<div id="mam-toot-content">
<p>{{ toot.content }}</p>
<div id="mam-toot-content" innerHTML="{{toot.content}}">
</div>
</div>

View File

@ -1,4 +1,10 @@
import { Account } from "../services/models/mastodon.interfaces";
export class AccountWrapper {
constructor() {
}
id: number;
username: string;
display_name: string;

View File

@ -1,10 +1,75 @@
import { Http, Headers, Response } from "@angular/http";
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";
export class Stream {
streamName: string;
private apiRoutes = new ApiRoutes();
statuses = new BehaviorSubject<TootWrapper[]>([]);
constructor(
private readonly httpService: Http,
public streamName: string,
private readonly type: StreamTypeEnum,
private readonly account: LocalAccount) {
this.retrieveToots(); //TODO change this for WebSockets
}
private retrieveToots(): void {
const route = this.getTimelineRoute();
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.Home:
return this.apiRoutes.getHomeTimeline;
case StreamTypeEnum.Local:
return this.apiRoutes.getPublicTimeline + `?Local=true`;
case StreamTypeEnum.Public:
return this.apiRoutes.getPublicTimeline + `?Local=false`;
}
}
}
export enum StreamTypeEnum {
Home,
Public,
Local
}
export class TootWrapper {
account: AccountWrapper;
constructor(status: Status) {
console.warn(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,6 +1,6 @@
<div id="mam-main-display">
<div *ngFor="let s of streams" class="mam-stream-column">
<app-stream></app-stream>
<app-stream [stream]="s"></app-stream>
</div>
</div>

View File

@ -1,20 +1,32 @@
import { Component, OnInit } from '@angular/core';
import { Stream } from 'stream';
import { Component, OnInit } from "@angular/core";
import { Stream } from "../../models/stream.models";
import { StreamsService } from "../../services/streams.service";
@Component({
selector: 'app-streams-main-display',
templateUrl: './streams-main-display.component.html',
styleUrls: ['./streams-main-display.component.css']
selector: "app-streams-main-display",
templateUrl: "./streams-main-display.component.html",
styleUrls: ["./streams-main-display.component.css"]
})
export class StreamsMainDisplayComponent implements OnInit {
streams: Stream[] = [];
constructor() { }
constructor(private readonly streamService: StreamsService) {
}
ngOnInit() {
for (let i = 0; i < 3; i++) {
this.streams.push(new Stream());
}
this.streamService.streamsSubject.subscribe((streams: Stream[]) => {
for (let s of streams) {
this.streams.push(s);
}
});
//for (let i = 0; i < 3; i++) {
// this.streams.push(new Stream());
//}
}
}

View File

@ -10,13 +10,16 @@ export class AccountsService {
private localAccountKey = "localAccounts";
private apiRoutes = new ApiRoutes();
accountsSubject: Subject<LocalAccount[]>;
init: Promise<any>; //TODO load this service before any UI action
accountsSubject: BehaviorSubject<LocalAccount[]>;
constructor(private readonly httpService: Http) {
this.init = this.getAllLocalAccount().then((accounts) => {
this.accountsSubject = new BehaviorSubject<LocalAccount[]>(accounts);
});
constructor(private readonly httpService: Http) {}
load(): Promise<boolean> {
return this.getAllLocalAccount()
.then((accounts) => {
this.accountsSubject = new BehaviorSubject<LocalAccount[]>(accounts);
return true;
});
}
addNewAccount(mastodonInstance: string, email: string, token: TokenData) {

View File

@ -99,7 +99,7 @@ export interface Status {
id: string;
uri: string;
url: string;
account: string;
account: Account;
in_reply_to_id: string;
in_reply_to_account_id: string;
reblog: string;

View File

@ -0,0 +1,50 @@
import { Injectable } from "@angular/core";
import { Http } from "@angular/http";
import { BehaviorSubject } from "rxjs";
import { Stream, StreamTypeEnum } from "../models/stream.models";
import { AccountsService, LocalAccount } from "./accounts.service";
@Injectable()
export class StreamsService {
streamsSubject = new BehaviorSubject<Stream[]>([]);
constructor(
private readonly httpService: Http,
private readonly accountsService: AccountsService) {
// Return home/local/public of all accounts
this.accountsService.accountsSubject
.subscribe((accounts: LocalAccount[]) => {
const streams: Stream[] = [];
for (let acc of accounts) {
const homeStream = new Stream(this.httpService, "Home", StreamTypeEnum.Home, acc);
const localStream = new Stream(this.httpService, "Local", StreamTypeEnum.Local, acc);
const publicStream = new Stream(this.httpService, "Public", StreamTypeEnum.Public, acc);
streams.push(homeStream);
streams.push(localStream);
streams.push(publicStream);
}
this.streamsSubject.next(streams);
});
}
//getStreams(): void {
// // Return home/local/public of all accounts
// this.accountsService.accountsSubject
// .map((accounts: LocalAccount[]) => {
// const streams: Stream[] = [];
// for (let acc of accounts) {
// const homeStream = new Stream(this.httpService, "Home", StreamTypeEnum.Home, acc);
// const localStream = new Stream(this.httpService, "Local", StreamTypeEnum.Local, acc);
// const publicStream = new Stream(this.httpService, "Public", StreamTypeEnum.Public, acc);
// streams.push(homeStream);
// streams.push(localStream);
// streams.push(publicStream);
// }
// this.streamsSubject.next(streams);
// });
}