2023-01-05 12:11:50 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
import * as FIFOProcessQueue from 'fifo-process-queue';
|
|
|
|
|
import { RoomService } from './room.service';
|
|
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
|
|
|
|
export class ViewedMessageService {
|
|
|
|
|
|
2023-01-09 10:49:58 +01:00
|
|
|
constructor() {
|
|
|
|
|
}
|
2023-01-05 12:11:50 +01:00
|
|
|
|
|
|
|
|
viewQueue = FIFOProcessQueue(async ({room, userId, statusNum, statusText}, callback) => {
|
|
|
|
|
|
|
|
|
|
if(room.membersExcludeMe?.map) {
|
|
|
|
|
const membersIds = room.membersExcludeMe.map((user)=> user._id)
|
|
|
|
|
|
|
|
|
|
if(membersIds.includes(userId)) {
|
|
|
|
|
|
|
|
|
|
if(statusText != 'offline') {
|
|
|
|
|
room.deleteMessageToReceive(userId)
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-09 14:51:00 +01:00
|
|
|
let n = 0
|
2023-01-09 10:49:58 +01:00
|
|
|
for (const message of room.messages) {
|
|
|
|
|
if(message.online) {
|
|
|
|
|
for(let id of membersIds) {
|
|
|
|
|
if(message.addReceived(id)) {
|
2023-01-09 14:51:00 +01:00
|
|
|
n++
|
|
|
|
|
setTimeout(async() => {
|
|
|
|
|
await message.saveChanges()
|
|
|
|
|
}, 100 * n)
|
2023-01-05 12:11:50 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-09 10:49:58 +01:00
|
|
|
}
|
2023-01-05 12:11:50 +01:00
|
|
|
|
|
|
|
|
setTimeout(function () {
|
|
|
|
|
callback();
|
|
|
|
|
}, 100);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
})
|
2023-01-09 10:49:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
viewReadQueue = FIFOProcessQueue(async ({room}, callback) => {
|
|
|
|
|
|
|
|
|
|
const membersIds: string[] = room.membersExcludeMe.map((user)=> user._id)
|
|
|
|
|
|
2023-01-09 14:51:00 +01:00
|
|
|
let n = 0
|
2023-01-09 10:49:58 +01:00
|
|
|
for (const message of room.messages) {
|
|
|
|
|
if(message.online) {
|
|
|
|
|
for(let id of membersIds) {
|
|
|
|
|
|
|
|
|
|
if(message.addViewed(id)) {
|
|
|
|
|
message.addReceived(id)
|
2023-01-09 14:51:00 +01:00
|
|
|
n++
|
|
|
|
|
setTimeout(async() => {
|
|
|
|
|
await message.saveChanges()
|
|
|
|
|
}, 100 * n)
|
|
|
|
|
|
2023-01-09 10:49:58 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
callback()
|
|
|
|
|
},100)
|
|
|
|
|
|
|
|
|
|
})
|
2023-01-05 12:11:50 +01:00
|
|
|
|
|
|
|
|
request(room:RoomService, userId, statusNum, statusText) {
|
|
|
|
|
this.viewQueue.push({room, userId, statusNum, statusText})
|
|
|
|
|
}
|
2023-01-09 10:49:58 +01:00
|
|
|
|
|
|
|
|
requestReadAll(room:RoomService) {
|
|
|
|
|
this.viewReadQueue.push({room})
|
|
|
|
|
}
|
2023-01-05 12:11:50 +01:00
|
|
|
}
|