mirror of
https://github.com/NicolasConstant/sengi
synced 2025-02-08 07:58:47 +01:00
added websocket wirering
This commit is contained in:
parent
9e3389ed11
commit
de20c8f4f4
@ -1,12 +1,11 @@
|
||||
import { Injectable } from "@angular/core";
|
||||
import { BehaviorSubject } from "rxjs";
|
||||
|
||||
import { Status } from "./models/mastodon.interfaces";
|
||||
import { Status, Notification } from "./models/mastodon.interfaces";
|
||||
import { ApiRoutes } from "./models/api.settings";
|
||||
import { StreamTypeEnum, StreamElement } from "../states/streams.state";
|
||||
import { MastodonWrapperService } from "./mastodon-wrapper.service";
|
||||
import { AccountInfo } from "../states/accounts.state";
|
||||
import { AccountIconComponent } from '../components/left-side-bar/account-icon/account-icon.component';
|
||||
|
||||
@Injectable()
|
||||
export class StreamingService {
|
||||
@ -18,8 +17,8 @@ export class StreamingService {
|
||||
|
||||
getStreaming(accountInfo: AccountInfo, stream: StreamElement): StreamingWrapper {
|
||||
|
||||
console.warn('EventSourceStreaminWrapper');
|
||||
new EventSourceStreaminWrapper(accountInfo, stream);
|
||||
//console.warn('EventSourceStreaminWrapper');
|
||||
//new EventSourceStreaminWrapper(accountInfo, stream);
|
||||
|
||||
return new StreamingWrapper(this.mastodonService, accountInfo, stream, this.nbStatusPerIteration);
|
||||
}
|
||||
@ -118,6 +117,10 @@ export class StreamingWrapper {
|
||||
newUpdate.messageId = event.payload;
|
||||
newUpdate.account = this.account;
|
||||
break;
|
||||
case 'notification':
|
||||
newUpdate.type = EventEnum.notification;
|
||||
newUpdate.notification = <Notification>JSON.parse(event.payload);
|
||||
break;
|
||||
default:
|
||||
newUpdate.type = EventEnum.unknow;
|
||||
}
|
||||
@ -242,10 +245,12 @@ export class StatusUpdate {
|
||||
status: Status;
|
||||
messageId: string;
|
||||
account: AccountInfo;
|
||||
notification: Notification;
|
||||
}
|
||||
|
||||
export enum EventEnum {
|
||||
unknow = 0,
|
||||
update = 1,
|
||||
delete = 2
|
||||
delete = 2,
|
||||
notification = 3
|
||||
}
|
||||
|
@ -7,8 +7,9 @@ import { MastodonWrapperService } from './mastodon-wrapper.service';
|
||||
import { AccountInfo } from '../states/accounts.state';
|
||||
import { NotificationService } from './notification.service';
|
||||
import { ToolsService } from './tools.service';
|
||||
import { StreamingService } from './streaming.service';
|
||||
import { StreamingService, StatusUpdate, EventEnum } from './streaming.service';
|
||||
import { NotificationsComponent } from '../components/floating-column/manage-account/notifications/notifications.component';
|
||||
import { StreamElement, StreamTypeEnum } from '../states/streams.state';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
@ -56,7 +57,13 @@ export class UserNotificationService {
|
||||
});;
|
||||
|
||||
//TODO: start streaming services
|
||||
|
||||
let streamElement = new StreamElement(StreamTypeEnum.personnal, 'activity', account.id, null, null, null, account.instance);
|
||||
let streaming = this.streamingService.getStreaming(account, streamElement);
|
||||
streaming.statusUpdateSubjet.subscribe((notification: StatusUpdate) => {
|
||||
if (notification && notification.type === EventEnum.notification) {
|
||||
this.processNewUpdate(account, notification);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
@ -79,6 +86,14 @@ export class UserNotificationService {
|
||||
// });
|
||||
}
|
||||
|
||||
private processNewUpdate(account: AccountInfo, notification: StatusUpdate) {
|
||||
if (notification.notification.type === 'mention') {
|
||||
this.processMentionsAndNotifications(account, [notification.notification], NotificationTypeEnum.UserMention);
|
||||
} else {
|
||||
this.processMentionsAndNotifications(account, [notification.notification], NotificationTypeEnum.UserNotification);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private processMentionsAndNotifications(account: AccountInfo, notifications: Notification[], type: NotificationTypeEnum) {
|
||||
if (notifications.length === 0) {
|
||||
@ -112,24 +127,15 @@ export class UserNotificationService {
|
||||
}
|
||||
}
|
||||
|
||||
private hasNewNotifications(lastNotification: Notification, lastCreationDate: string): boolean {
|
||||
|
||||
if(!lastNotification) return false;
|
||||
if(!lastCreationDate) return false;
|
||||
private hasNewNotifications(lastNotification: Notification, lastCreationDate: string): boolean {
|
||||
if (!lastNotification) return false;
|
||||
if (!lastCreationDate) return false;
|
||||
return new Date(lastNotification.created_at) > new Date(lastCreationDate);
|
||||
}
|
||||
|
||||
private analyseNotifications(account: AccountInfo, userNotification: UserNotification, newNotifications: Notification[], type: NotificationTypeEnum): UserNotification {
|
||||
console.group();
|
||||
console.warn(account.username);
|
||||
console.warn(newNotifications);
|
||||
console.groupEnd();
|
||||
// if (userNotification.allNotifications.length > 30) {
|
||||
// userNotification.allNotifications.length = 30;
|
||||
// }
|
||||
|
||||
let lastNotificationId = newNotifications[newNotifications.length - 1].id; //FIXME: wtf? check the id retrieval
|
||||
//let lastNotificationId = newNotifications[0].id; //FIXME: wtf? check the id retrieval
|
||||
|
||||
let lastNotificationId = newNotifications[newNotifications.length - 1].id;
|
||||
|
||||
const accountSettings = this.toolsService.getAccountSettings(account);
|
||||
|
||||
@ -160,7 +166,7 @@ export class UserNotificationService {
|
||||
// userNotification.notifications = [...newNotifications, ...currentNotifications];
|
||||
// userNotification.mentions = [...newMentions, ...currentMentions];
|
||||
|
||||
|
||||
|
||||
|
||||
if (type === NotificationTypeEnum.UserMention && !accountSettings.lastMentionCreationDate && newNotifications.length > 0) {
|
||||
accountSettings.lastMentionCreationDate = newNotifications[0].created_at;
|
||||
|
Loading…
x
Reference in New Issue
Block a user