Sengi-Windows-MacOS-Linux/src/app/services/notification.service.ts

91 lines
2.8 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
2019-02-12 04:28:15 +01:00
import { HttpErrorResponse } from '@angular/common/http';
2019-09-07 23:52:07 +02:00
2019-04-13 19:47:20 +02:00
import { StatusWrapper } from '../models/common.model';
2019-07-04 00:55:46 +02:00
import { Account } from './models/mastodon.interfaces';
2019-09-07 23:52:07 +02:00
import { AccountInfo } from '../states/accounts.state';
import { ToolsService } from './tools.service';
@Injectable()
export class NotificationService {
public restartNotificationStream = new Subject<string>();
2020-12-22 03:14:32 +01:00
public notifactionStream = new Subject<NotificationData>();
2019-04-13 19:47:20 +02:00
public newRespondPostedStream = new Subject<NewReplyData>();
2019-07-04 00:55:46 +02:00
public hideAccountUrlStream = new Subject<string>();
public deletedStatusStream = new Subject<StatusWrapper>();
2019-09-07 23:52:07 +02:00
constructor(private readonly toolsService: ToolsService) {
}
2019-09-07 23:52:07 +02:00
public notify(avatar: string, errorCode: number, message: string, isError: boolean) {
2020-12-22 03:14:32 +01:00
let newNotification = new NotificationData(avatar, errorCode, message, isError);
this.notifactionStream.next(newNotification);
}
2019-02-12 04:28:15 +01:00
2019-09-07 23:52:07 +02:00
public notifyHttpError(err: HttpErrorResponse, account: AccountInfo) {
let message = 'Oops, Unknown Error';
let code: number;
2023-04-24 00:40:42 +02:00
2019-09-07 23:52:07 +02:00
try {
code = err.status;
2023-04-24 00:40:42 +02:00
if(err.error && err.error.error) {
2020-06-09 01:10:24 +02:00
message = err.error.error; //Mastodon
} else if(err.error && err.error.errors && err.error.errors.detail){
2019-09-07 23:52:07 +02:00
message = err.error.errors.detail; //Pleroma
2023-04-24 00:40:42 +02:00
} else if(err.message){
message = err.message;
2019-09-07 23:52:07 +02:00
}
} catch (err) { }
if (account) {
this.toolsService.getAvatar(account)
.then((avatar: string) => {
this.notify(avatar, code, message, true);
})
.catch(err => {
});
} else {
this.notify(null, code, message, true);
}
2019-02-12 04:28:15 +01:00
}
2019-04-13 19:47:20 +02:00
// public newStatusPosted(status: StatusWrapper){
2019-09-07 23:52:07 +02:00
public newStatusPosted(uiStatusRepliedToId: string, response: StatusWrapper) {
2019-04-13 19:47:20 +02:00
const notification = new NewReplyData(uiStatusRepliedToId, response);
this.newRespondPostedStream.next(notification);
}
2019-07-04 00:55:46 +02:00
2019-09-07 23:52:07 +02:00
public hideAccount(account: Account) {
2019-07-04 00:55:46 +02:00
this.hideAccountUrlStream.next(account.url);
}
2019-09-07 23:52:07 +02:00
public deleteStatus(status: StatusWrapper) {
this.deletedStatusStream.next(status);
}
public notifyRestartNotification(label: string){
this.restartNotificationStream.next(label);
}
}
2020-12-22 03:14:32 +01:00
export class NotificationData {
public id: string;
constructor(
2019-09-07 23:52:07 +02:00
public avatar: string,
public errorCode: number,
public message: string,
public isError: boolean
2019-09-07 23:52:07 +02:00
) {
this.id = `${message}${new Date().getTime()}`;
}
}
2019-04-13 19:47:20 +02:00
export class NewReplyData {
2019-09-07 23:52:07 +02:00
constructor(public uiStatusId: string, public response: StatusWrapper) {
2019-04-13 19:47:20 +02:00
}
}