added capability to follow/unfollow

This commit is contained in:
Nicolas Constant 2019-02-23 01:00:33 -05:00
parent dca563e176
commit 3dd75cf99e
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
3 changed files with 42 additions and 19 deletions

View File

@ -62,16 +62,16 @@ export class UserProfileComponent implements OnInit {
ngOnInit() {
this.accountSub = this.accounts$.subscribe((accounts: AccountInfo[]) => {
if (this.account) {
this.relationship = null;
const userAccount = accounts.filter(x => x.isSelected)[0];
//this.relationship = null;
this.currentlyUsedAccount = accounts.filter(x => x.isSelected)[0];
this.toolsService.findAccount(userAccount, this.lastAccountName)
this.toolsService.findAccount(this.currentlyUsedAccount, this.lastAccountName)
.then((account: Account) => {
this.getFollowStatus(userAccount, account);
this.getFollowStatus(this.currentlyUsedAccount, account);
})
.catch((err: HttpErrorResponse) => {
this.notificationService.notifyHttpError(err);
});
});
}
});
}
@ -124,7 +124,7 @@ export class UserProfileComponent implements OnInit {
}
private getFollowStatus(userAccount: AccountInfo, account: Account): Promise<void> {
this.relationship = null;
// this.relationship = null;
return this.mastodonService.getRelationships(userAccount, [account])
.then((result: Relationship[]) => {
this.relationship = result.filter(x => x.id === account.id)[0];
@ -148,17 +148,32 @@ export class UserProfileComponent implements OnInit {
}
follow(): boolean {
this.mastodonService.follow(this.currentlyUsedAccount, this.account)
.then(() => {})
.catch(() => {});
this.currentlyUsedAccount = this.toolsService.getSelectedAccounts()[0];
this.toolsService.findAccount(this.currentlyUsedAccount, this.lastAccountName)
.then((account: Account) => {
return this.mastodonService.follow(this.currentlyUsedAccount, account);
})
.then((relationship: Relationship) => {
this.relationship = relationship;
})
.catch((err: HttpErrorResponse) => {
this.notificationService.notifyHttpError(err);
});
return false;
}
unfollow(): boolean {
this.mastodonService.unfollow(this.currentlyUsedAccount, this.account)
.then(() => {})
.catch(() => {});
this.currentlyUsedAccount = this.toolsService.getSelectedAccounts()[0];
this.toolsService.findAccount(this.currentlyUsedAccount, this.lastAccountName)
.then((account: Account) => {
return this.mastodonService.unfollow(this.currentlyUsedAccount, account);
})
.then((relationship: Relationship) => {
this.relationship = relationship;
})
.catch((err: HttpErrorResponse) => {
this.notificationService.notifyHttpError(err);
});
return false;
}
}

View File

@ -179,12 +179,19 @@ export class MastodonService {
return this.httpClient.get<Relationship[]>(route, { headers: headers }).toPromise();
}
unfollow(currentlyUsedAccount: AccountInfo, account: Account): any {
throw new Error("Method not implemented.");
follow(currentlyUsedAccount: AccountInfo, account: Account): Promise<Relationship> {
const route = `https://${currentlyUsedAccount.instance}${this.apiRoutes.follow}`.replace('{0}', account.id.toString());
const headers = new HttpHeaders({ 'Authorization': `Bearer ${currentlyUsedAccount.token.access_token}` });
return this.httpClient.post<Relationship>(route, null, { headers: headers }).toPromise();
}
unfollow(currentlyUsedAccount: AccountInfo, account: Account): Promise<Relationship> {
const route = `https://${currentlyUsedAccount.instance}${this.apiRoutes.unfollow}`.replace('{0}', account.id.toString());
const headers = new HttpHeaders({ 'Authorization': `Bearer ${currentlyUsedAccount.token.access_token}` });
return this.httpClient.post<Relationship>(route, null, { headers: headers }).toPromise();
}
follow(currentlyUsedAccount: AccountInfo, account: Account): any {
throw new Error("Method not implemented.");
}
}
export enum VisibilityEnum {

View File

@ -16,7 +16,8 @@ export class NotificationService {
public notifyHttpError(err: HttpErrorResponse){
console.error(err.message);
let message = `${err.status}: ${err.statusText}`;
// let message = `${err.status}: ${err.statusText}`;
let message = `${err.statusText}`;
this.notify(message, true);
}
}