]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/users/user-notification.service.ts
Add bulk comment actions on account dropdown
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / users / user-notification.service.ts
index 2dfee8060fdee17ad1ab518ca12c90327340bf17..e525a1d587f8e42da7eb5fb82803839a8d34bdab 100644 (file)
@@ -1,33 +1,29 @@
 import { Injectable } from '@angular/core'
 import { HttpClient, HttpParams } from '@angular/common/http'
-import { RestExtractor, RestService } from '@app/shared/rest'
+import { RestExtractor, RestService } from '../rest'
 import { catchError, map, tap } from 'rxjs/operators'
 import { environment } from '../../../environments/environment'
 import { ResultList, UserNotification as UserNotificationServer, UserNotificationSetting } from '../../../../../shared'
-import { UserNotification } from '@app/shared/users/user-notification.model'
-import { Subject } from 'rxjs'
-import * as io from 'socket.io-client'
-import { AuthService } from '@app/core'
-import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
-import { User } from '@app/shared'
+import { UserNotification } from './user-notification.model'
+import { AuthService } from '../../core'
+import { ComponentPaginationLight } from '../rest/component-pagination.model'
+import { User } from '../users/user.model'
+import { UserNotificationSocket } from '@app/core/notification/user-notification-socket.service'
 
 @Injectable()
 export class UserNotificationService {
   static BASE_NOTIFICATIONS_URL = environment.apiUrl + '/api/v1/users/me/notifications'
   static BASE_NOTIFICATION_SETTINGS = environment.apiUrl + '/api/v1/users/me/notification-settings'
 
-  private notificationSubject = new Subject<{ type: 'new' | 'read' | 'read-all', notification?: UserNotification }>()
-
-  private socket: SocketIOClient.Socket
-
   constructor (
     private auth: AuthService,
     private authHttp: HttpClient,
     private restExtractor: RestExtractor,
-    private restService: RestService
+    private restService: RestService,
+    private userNotificationSocket: UserNotificationSocket
   ) {}
 
-  listMyNotifications (pagination: ComponentPagination, unread?: boolean, ignoreLoadingBar = false) {
+  listMyNotifications (pagination: ComponentPaginationLight, unread?: boolean, ignoreLoadingBar = false) {
     let params = new HttpParams()
     params = this.restService.addRestGetParams(params, this.restService.componentPaginationToRestPagination(pagination))
 
@@ -48,16 +44,6 @@ export class UserNotificationService {
       .pipe(map(n => n.total))
   }
 
-  getMyNotificationsSocket () {
-    const socket = this.getSocket()
-
-    socket.on('new-notification', (n: UserNotificationServer) => {
-      this.notificationSubject.next({ type: 'new', notification: new UserNotification(n) })
-    })
-
-    return this.notificationSubject.asObservable()
-  }
-
   markAsRead (notification: UserNotification) {
     const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read'
 
@@ -67,7 +53,7 @@ export class UserNotificationService {
     return this.authHttp.post(url, body, { headers })
                .pipe(
                  map(this.restExtractor.extractDataBool),
-                 tap(() => this.notificationSubject.next({ type: 'read' })),
+                 tap(() => this.userNotificationSocket.dispatch('read')),
                  catchError(res => this.restExtractor.handleError(res))
                )
   }
@@ -79,7 +65,7 @@ export class UserNotificationService {
     return this.authHttp.post(url, {}, { headers })
                .pipe(
                  map(this.restExtractor.extractDataBool),
-                 tap(() => this.notificationSubject.next({ type: 'read-all' })),
+                 tap(() => this.userNotificationSocket.dispatch('read-all')),
                  catchError(res => this.restExtractor.handleError(res))
                )
   }
@@ -94,16 +80,6 @@ export class UserNotificationService {
                )
   }
 
-  private getSocket () {
-    if (this.socket) return this.socket
-
-    this.socket = io(environment.apiUrl + '/user-notifications', {
-      query: { accessToken: this.auth.getAccessToken() }
-    })
-
-    return this.socket
-  }
-
   private formatNotification (notification: UserNotificationServer) {
     return new UserNotification(notification)
   }