2019-02-11 06:56:52 +01:00
|
|
|
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';
|
2019-02-11 06:56:52 +01:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class NotificationService {
|
|
|
|
public notifactionStream = new Subject<NotificatioData>();
|
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>();
|
2019-07-06 05:41:03 +02:00
|
|
|
public deletedStatusStream = new Subject<StatusWrapper>();
|
2019-02-11 06:56:52 +01:00
|
|
|
|
2019-09-07 23:52:07 +02:00
|
|
|
constructor(private readonly toolsService: ToolsService) {
|
2019-02-11 06:56:52 +01:00
|
|
|
}
|
|
|
|
|
2019-09-07 23:52:07 +02:00
|
|
|
public notify(avatar: string, errorCode: number, message: string, isError: boolean) {
|
|
|
|
let newNotification = new NotificatioData(avatar, errorCode, message, isError);
|
2019-02-11 06:56:52 +01:00
|
|
|
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;
|
|
|
|
|
|
|
|
try {
|
|
|
|
code = err.status;
|
|
|
|
message = err.error.error; //Mastodon
|
|
|
|
if (!message) {
|
|
|
|
message = err.error.errors.detail; //Pleroma
|
|
|
|
}
|
|
|
|
} 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-07-06 05:41:03 +02:00
|
|
|
|
2019-09-07 23:52:07 +02:00
|
|
|
public deleteStatus(status: StatusWrapper) {
|
2019-07-06 05:41:03 +02:00
|
|
|
this.deletedStatusStream.next(status);
|
|
|
|
}
|
2019-02-11 06:56:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export class NotificatioData {
|
|
|
|
public id: string;
|
|
|
|
|
|
|
|
constructor(
|
2019-09-07 23:52:07 +02:00
|
|
|
public avatar: string,
|
|
|
|
public errorCode: number,
|
2019-02-11 06:56:52 +01:00
|
|
|
public message: string,
|
|
|
|
public isError: boolean
|
2019-09-07 23:52:07 +02:00
|
|
|
) {
|
2019-02-11 06:56:52 +01: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
|
|
|
|
|
|
|
}
|
|
|
|
}
|