import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, Subscription, switchMap, timer } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class MsgService {

  msgPolling: Observable<any>;  // 轮询查阅消息定时器

  constructor(private http : HttpClient) { 
    this.msgPolling = timer(1, 5 * 60 * 1000)   // 5分钟拉取一次消息即可
      .pipe(
        switchMap(() => this.getMsgs())
      );
  }


  // 获取消息
  getMsgs() {
    return this.http.get('/napi/web/message/all');
  }

  // 将消息置为已读
  setRead(ids: string[]) {
    return this.http.patch('/napi/web/message/read', ids);
  }

  setAllRead() {
    return this.http.patch('/napi/web/message/allread', '');
  }

}
