diff --git a/src/app/components/stream/user-profile/user-profile.component.html b/src/app/components/stream/user-profile/user-profile.component.html index 7c026065..df5b6500 100644 --- a/src/app/components/stream/user-profile/user-profile.component.html +++ b/src/app/components/stream/user-profile/user-profile.component.html @@ -9,23 +9,24 @@

{{account.display_name}}

@{{account.acct}}

-
+
-
-
follows you
-
blocked
+
+
follows you
+
blocked
+
muted
diff --git a/src/app/components/stream/user-profile/user-profile.component.ts b/src/app/components/stream/user-profile/user-profile.component.ts index 112dbd76..68d268ab 100644 --- a/src/app/components/stream/user-profile/user-profile.component.ts +++ b/src/app/components/stream/user-profile/user-profile.component.ts @@ -2,12 +2,17 @@ import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core'; import { HttpErrorResponse } from '@angular/common/http'; import { faUser, faHourglassHalf, faUserCheck } from "@fortawesome/free-solid-svg-icons"; import { faUser as faUserRegular } from "@fortawesome/free-regular-svg-icons"; +import { Observable, Subscription } from 'rxjs'; +import { Store } from '@ngxs/store'; -import { Account, Status } from "../../../services/models/mastodon.interfaces"; +import { Account, Status, Relationship } from "../../../services/models/mastodon.interfaces"; import { MastodonService } from '../../../services/mastodon.service'; import { ToolsService, OpenThreadEvent } from '../../../services/tools.service'; import { StatusWrapper } from '../stream.component'; import { NotificationService } from '../../../services/notification.service'; +import { AccountInfo } from '../../../states/accounts.state'; + + @Component({ selector: 'app-user-profile', @@ -27,10 +32,15 @@ export class UserProfileComponent implements OnInit { statusLoading: boolean; error: string; + relationship: Relationship; statuses: StatusWrapper[] = []; private lastAccountName: string; + private currentlyUsedAccount: AccountInfo; + private accounts$: Observable; + private accountSub: Subscription; + @Output() browseAccountEvent = new EventEmitter(); @Output() browseHashtagEvent = new EventEmitter(); @Output() browseThreadEvent = new EventEmitter(); @@ -43,22 +53,45 @@ export class UserProfileComponent implements OnInit { } constructor( + private readonly store: Store, private readonly notificationService: NotificationService, private readonly mastodonService: MastodonService, - private readonly toolsService: ToolsService) { } + private readonly toolsService: ToolsService) { + + this.accounts$ = this.store.select(state => state.registeredaccounts.accounts); + } ngOnInit() { + // this.currentlyUsedAccount = this.toolsService.getSelectedAccounts()[0]; + this.accountSub = this.accounts$.subscribe((accounts: AccountInfo[]) => { + const userAccount = accounts.filter(x => x.isSelected)[0]; + // this.load() + }); + } + + ngOnDestroy() { + this.accountSub.unsubscribe(); } private load(accountName: string) { this.statuses.length = 0; - this.isLoading = true; - this.loadAccount(accountName) + this.account = null; + this.isLoading = true; + this.statusLoading = true; + + this.currentlyUsedAccount = this.toolsService.getSelectedAccounts()[0]; + return this.toolsService.findAccount(this.currentlyUsedAccount, accountName) .then((account: Account) => { + this.isLoading = false; + this.account = account; this.hasNote = account && account.note && account.note !== '

'; - return this.getStatuses(this.account); + + const getFollowStatusPromise = this.getFollowStatus(this.currentlyUsedAccount, this.account); + const getStatusesPromise = this.getStatuses(this.currentlyUsedAccount, this.account); + + return Promise.all([getFollowStatusPromise, getStatusesPromise]); }) .catch((err: HttpErrorResponse) => { this.notificationService.notifyHttpError(err); @@ -68,6 +101,26 @@ export class UserProfileComponent implements OnInit { this.statusLoading = false; }); } + + private getStatuses(userAccount: AccountInfo, account: Account): Promise { + this.statusLoading = true; + return this.mastodonService.getAccountStatuses(userAccount, account.id, false, false, true, null, null, 40) + .then((result: Status[]) => { + for (const status of result) { + const wrapper = new StatusWrapper(status, userAccount); + this.statuses.push(wrapper); + } + this.statusLoading = false; + }); + } + + private getFollowStatus(userAccount: AccountInfo, account: Account):Promise { + this.relationship = null; + return this.mastodonService.getRelationships(userAccount, [account]) + .then((result: Relationship[])=> { + this.relationship = result.filter(x => x.id === account.id)[0]; + }); + } refresh(): any { this.load(this.lastAccountName); @@ -83,39 +136,5 @@ export class UserProfileComponent implements OnInit { browseThread(openThreadEvent: OpenThreadEvent): void { this.browseThreadEvent.next(openThreadEvent); - } - - private loadAccount(accountName: string): Promise { - this.account = null; - - let selectedAccounts = this.toolsService.getSelectedAccounts(); - - if (selectedAccounts.length === 0) { - this.error = 'no user selected'; - console.error(this.error); - return Promise.resolve(null); - } - - this.isLoading = true; - return this.toolsService.findAccount(selectedAccounts[0], accountName) - .then((result) => { - this.isLoading = false; - return result; - }); - } - - private getStatuses(account: Account): Promise { - let selectedAccounts = this.toolsService.getSelectedAccounts(); - if (selectedAccounts.length === 0) return; - - this.statusLoading = true; - return this.mastodonService.getAccountStatuses(selectedAccounts[0], account.id, false, false, true, null, null, 40) - .then((result: Status[]) => { - for (const status of result) { - const wrapper = new StatusWrapper(status, selectedAccounts[0]); - this.statuses.push(wrapper); - } - this.statusLoading = false; - }); - } + } } diff --git a/src/app/services/mastodon.service.ts b/src/app/services/mastodon.service.ts index ab81b988..dc914e4b 100644 --- a/src/app/services/mastodon.service.ts +++ b/src/app/services/mastodon.service.ts @@ -2,14 +2,14 @@ import { Injectable } from '@angular/core'; import { HttpHeaders, HttpClient } from '@angular/common/http'; import { ApiRoutes } from './models/api.settings'; -import { Account, Status, Results, Context } from "./models/mastodon.interfaces"; +import { Account, Status, Results, Context, Relationship } from "./models/mastodon.interfaces"; import { AccountInfo } from '../states/accounts.state'; import { StreamTypeEnum } from '../states/streams.state'; import { stat } from 'fs'; +import { forEach } from '@angular/router/src/utils/collection'; @Injectable() -export class MastodonService { - +export class MastodonService { private apiRoutes = new ApiRoutes(); constructor(private readonly httpClient: HttpClient) { } @@ -166,6 +166,18 @@ export class MastodonService { const headers = new HttpHeaders({ 'Authorization': `Bearer ${account.token.access_token}` }); return this.httpClient.post(route, null, { headers: headers }).toPromise() } + + getRelationships(account: AccountInfo, accountsToRetrieve: Account[]): Promise { + let params = "?"; + accountsToRetrieve.forEach(x => { + if(params.includes('id')) params += '&'; + params += `id[]=${x.id}`; + }); + + const route = `https://${account.instance}${this.apiRoutes.getAccountRelationships}${params}`; + const headers = new HttpHeaders({ 'Authorization': `Bearer ${account.token.access_token}` }); + return this.httpClient.get(route, { headers: headers }).toPromise(); + } } export enum VisibilityEnum { diff --git a/src/app/services/models/mastodon.interfaces.ts b/src/app/services/models/mastodon.interfaces.ts index c07620cc..3e184b04 100644 --- a/src/app/services/models/mastodon.interfaces.ts +++ b/src/app/services/models/mastodon.interfaces.ts @@ -85,12 +85,12 @@ export interface Notification { } export interface Relationship { - id: string; - following: string; - followed_by: string; - blocking: string; - muting: string; - requested: string; + id: number; + following: boolean; + followed_by: boolean; + blocking: boolean; + muting: boolean; + requested: boolean; } export interface Report {