Sengi-Windows-MacOS-Linux/src/app/components/floating-column/manage-account/manage-account.component.ts

136 lines
5.0 KiB
TypeScript
Raw Normal View History

import { Component, OnInit, OnDestroy, Input, Output, EventEmitter } from '@angular/core';
import { faAt, faUserPlus } from "@fortawesome/free-solid-svg-icons";
import { faBell, faEnvelope, faUser, faStar } from "@fortawesome/free-regular-svg-icons";
import { Subscription } from 'rxjs';
2019-03-19 05:21:00 +01:00
2018-09-11 07:54:23 +02:00
import { AccountWrapper } from '../../../models/account.models';
2019-03-25 06:09:53 +01:00
import { UserNotificationService, UserNotification } from '../../../services/user-notification.service';
2019-11-17 04:35:20 +01:00
import { OpenThreadEvent, ToolsService } from '../../../services/tools.service';
import { MastodonWrapperService } from '../../../services/mastodon-wrapper.service';
2019-06-15 20:03:29 +02:00
import { Account } from "../../../services/models/mastodon.interfaces";
import { NotificationService } from '../../../services/notification.service';
import { AccountInfo } from '../../../states/accounts.state';
2018-09-10 03:55:16 +02:00
@Component({
2019-01-28 06:46:37 +01:00
selector: 'app-manage-account',
templateUrl: './manage-account.component.html',
styleUrls: ['./manage-account.component.scss']
2018-09-10 03:55:16 +02:00
})
2019-11-17 04:35:20 +01:00
export class ManageAccountComponent implements OnInit, OnDestroy {
2019-03-19 05:21:00 +01:00
faAt = faAt;
faBell = faBell;
faEnvelope = faEnvelope;
faUser = faUser;
faStar = faStar;
faUserPlus = faUserPlus;
2019-03-19 05:21:00 +01:00
2019-11-17 04:35:20 +01:00
subPanel: 'account' | 'notifications' | 'mentions' | 'dm' | 'favorites' = 'account';
hasNotifications = false;
hasMentions = false;
2018-09-10 03:55:16 +02:00
2019-06-15 20:03:29 +02:00
userAccount: Account;
@Output() browseAccountEvent = new EventEmitter<string>();
@Output() browseHashtagEvent = new EventEmitter<string>();
@Output() browseThreadEvent = new EventEmitter<OpenThreadEvent>();
@Input('account')
set account(acc: AccountWrapper) {
this._account = acc;
this.checkNotifications();
2019-06-15 20:03:29 +02:00
this.getUserUrl(acc.info);
}
get account(): AccountWrapper {
return this._account;
}
private userNotificationServiceSub: Subscription;
private _account: AccountWrapper;
constructor(
2019-11-17 04:35:20 +01:00
private readonly toolsService: ToolsService,
private readonly mastodonService: MastodonWrapperService,
2019-06-15 20:03:29 +02:00
private readonly notificationService: NotificationService,
2019-03-25 06:09:53 +01:00
private readonly userNotificationService: UserNotificationService) { }
ngOnInit() {
}
ngOnDestroy(): void {
this.userNotificationServiceSub.unsubscribe();
}
2019-01-30 04:33:49 +01:00
2019-11-17 04:35:20 +01:00
private getUserUrl(account: AccountInfo) {
2019-06-15 20:03:29 +02:00
this.mastodonService.retrieveAccountDetails(this.account.info)
.then((acc: Account) => {
this.userAccount = acc;
})
.catch(err => {
2019-09-07 23:52:07 +02:00
this.notificationService.notifyHttpError(err, this.account.info);
2019-06-15 20:03:29 +02:00
});
}
2019-11-17 04:35:20 +01:00
private checkNotifications() {
if (this.userNotificationServiceSub) {
2019-03-25 05:52:30 +01:00
this.userNotificationServiceSub.unsubscribe();
}
this.userNotificationServiceSub = this.userNotificationService.userNotifications.subscribe((userNotifications: UserNotification[]) => {
const userNotification = userNotifications.find(x => x.account.id === this.account.info.id);
2019-11-17 04:35:20 +01:00
if (userNotification) {
let settings = this.toolsService.getSettings();
let accSettings = this.toolsService.getAccountSettings(this.account.info);
if (!settings.disableAvatarNotifications && !accSettings.disableAvatarNotifications) {
this.hasNotifications = userNotification.hasNewNotifications;
this.hasMentions = userNotification.hasNewMentions;
}
}
});
2019-11-17 04:35:20 +01:00
let current = this.userNotificationService.userNotifications.value;
const userNotification = current.find(x => x.account.id === this.account.info.id);
if (userNotification) {
let settings = this.toolsService.getSettings();
let accSettings = this.toolsService.getAccountSettings(this.account.info);
if (!settings.disableAutofocus && !settings.disableAvatarNotifications && !accSettings.disableAvatarNotifications) {
if (userNotification.hasNewNotifications) {
this.loadSubPanel('notifications');
} else if (userNotification.hasNewMentions) {
this.loadSubPanel('mentions');
}
}
}
2019-01-30 04:33:49 +01:00
}
2019-11-17 04:35:20 +01:00
loadSubPanel(subpanel: 'account' | 'notifications' | 'mentions' | 'dm' | 'favorites'): boolean {
this.subPanel = subpanel;
return false;
}
browseAccount(accountName: string): void {
this.browseAccountEvent.next(accountName);
}
browseLocalAccount(): boolean {
var accountName = `@${this.account.info.username}@${this.account.info.instance}`;
this.browseAccountEvent.next(accountName);
return false;
}
openLocalAccount(): boolean {
window.open(this.userAccount.url, '_blank');
return false;
}
browseHashtag(hashtag: string): void {
this.browseHashtagEvent.next(hashtag);
}
browseThread(openThreadEvent: OpenThreadEvent): void {
this.browseThreadEvent.next(openThreadEvent);
}
2018-09-10 03:55:16 +02:00
}