update detech new message and auto sync conversations
This commit is contained in:
parent
9dfa87e2c0
commit
83785fdf0a
|
|
@ -28,4 +28,4 @@ dist-ssr
|
||||||
*.ntvs*
|
*.ntvs*
|
||||||
*.njsproj
|
*.njsproj
|
||||||
*.sln
|
*.sln
|
||||||
*.sw?
|
*.sw?
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
|
|
@ -44,3 +44,7 @@ contentService.detectNewMessage();
|
||||||
|
|
||||||
// SYNC CONVERSASIONS (INTERVAL)
|
// SYNC CONVERSASIONS (INTERVAL)
|
||||||
contentService.startSyncConversations();
|
contentService.startSyncConversations();
|
||||||
|
|
||||||
|
// const a = new TeamsChatService();
|
||||||
|
|
||||||
|
// a.start();
|
||||||
|
|
|
||||||
|
|
@ -447,49 +447,90 @@ export class ContentService {
|
||||||
|
|
||||||
async detectNewMessage(interval = 2000) {
|
async detectNewMessage(interval = 2000) {
|
||||||
console.log("[Monitor] Starting...");
|
console.log("[Monitor] Starting...");
|
||||||
// this.initialHistories = this.extractAllMessages();
|
|
||||||
// this.lastMessage = this.initialHistories.pop();
|
|
||||||
|
|
||||||
// await messageApi.sendBulkMessages(this.initialHistories);
|
// Lưu lịch sử ban đầu
|
||||||
setInterval(async () => {
|
this.service.initialHistories = this.service.extractAllMessages();
|
||||||
const aria_value = document
|
this.service.lastMessage = this.service.initialHistories.pop();
|
||||||
.querySelector(
|
|
||||||
'[aria-labelledby^="cn-normal-notification-main-content-"]'
|
|
||||||
)
|
|
||||||
?.getAttribute("aria-labelledby");
|
|
||||||
|
|
||||||
if (!aria_value) {
|
|
||||||
console.log("No new message...");
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const room_id = aria_value
|
|
||||||
.split(" ")[0]
|
|
||||||
.replaceAll("cn-normal-notification-main-content-", "");
|
|
||||||
|
|
||||||
if (!room_id) return;
|
|
||||||
|
|
||||||
console.log({ room_id, aria_value });
|
|
||||||
|
|
||||||
|
setInterval(() => {
|
||||||
queue.add(async () => {
|
queue.add(async () => {
|
||||||
console.log("[Queue] Handling SYNC NEW MESSAGE");
|
try {
|
||||||
|
const currentRoom = this.service.getCurrentRoomInfo();
|
||||||
|
|
||||||
this._clickToConversation(room_id);
|
// Lấy attribute aria-labelledby
|
||||||
|
const ariaValue = document
|
||||||
|
.querySelector(
|
||||||
|
'[aria-labelledby^="cn-normal-notification-main-content-"]'
|
||||||
|
)
|
||||||
|
?.getAttribute("aria-labelledby");
|
||||||
|
|
||||||
await delay(2000);
|
// **CASE 1: Không có popup notification**
|
||||||
|
if (!ariaValue) {
|
||||||
|
const newCurrentMessage = await this.service.detectNewMessages();
|
||||||
|
|
||||||
const allMessages = this.service.extractAllMessages();
|
if (!newCurrentMessage) {
|
||||||
|
console.log("[Monitor] No new message...");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const lastMessage = allMessages.at(-1);
|
if (currentRoom.room_id !== newCurrentMessage?.room_id) {
|
||||||
|
console.log("[Monitor] Message from another room → skip");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!lastMessage) return;
|
console.log("[Monitor] Found new message:", newCurrentMessage);
|
||||||
|
await this._sendLastMessage(newCurrentMessage);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
await messageApi.sendSingleMessage(lastMessage);
|
// **CASE 2: Có popup → cần click để sync**
|
||||||
|
const roomId = ariaValue
|
||||||
|
.split(" ")[0]
|
||||||
|
.replace("cn-normal-notification-main-content-", "");
|
||||||
|
|
||||||
|
if (!roomId) {
|
||||||
|
console.warn(
|
||||||
|
"[Monitor] Could not extract room_id from aria-labelledby"
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("[Monitor] Notification detected:", {
|
||||||
|
roomId,
|
||||||
|
ariaValue,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Click vào conversation và xử lý
|
||||||
|
this._clickToConversation(roomId);
|
||||||
|
await delay(2000); // chờ load messages
|
||||||
|
|
||||||
|
const allMessages = this.service.extractAllMessages();
|
||||||
|
const lastMessage = allMessages.at(-1);
|
||||||
|
|
||||||
|
if (!lastMessage) {
|
||||||
|
console.log("[Queue] No last message found after sync");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("[Queue] Sending last message:", lastMessage);
|
||||||
|
await this._sendLastMessage(lastMessage);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("[Monitor] Error detecting new message:", error);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}, interval);
|
}, interval);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper gửi tin nhắn cuối cùng
|
||||||
|
async _sendLastMessage(message: IMessage) {
|
||||||
|
try {
|
||||||
|
await messageApi.sendSingleMessage(message);
|
||||||
|
console.log("[API] Sent message successfully:", message);
|
||||||
|
} catch (err) {
|
||||||
|
console.error("[API] Failed to send message:", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
startSyncConversations() {
|
startSyncConversations() {
|
||||||
setInterval(() => this.getConversations(), 20000);
|
setInterval(() => this.getConversations(), 20000);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,7 @@
|
||||||
import { messageApi } from "@/api/message-api.service";
|
|
||||||
|
|
||||||
export class TeamsChatService {
|
export class TeamsChatService {
|
||||||
private readonly MY_NAME = "Apactech com";
|
private readonly MY_NAME = "Apactech com";
|
||||||
private lastMessage?: IMessage;
|
public lastMessage?: IMessage;
|
||||||
private initialHistories: IMessage[] = [];
|
public initialHistories: IMessage[] = [];
|
||||||
|
|
||||||
public elTags = {
|
public elTags = {
|
||||||
container_scroll:
|
container_scroll:
|
||||||
|
|
@ -124,7 +122,7 @@ export class TeamsChatService {
|
||||||
console.log("%c[New incoming message]", "color: #007acc;", message);
|
console.log("%c[New incoming message]", "color: #007acc;", message);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async detectNewMessages() {
|
public async detectNewMessages() {
|
||||||
const allMessages = this.extractAllMessages();
|
const allMessages = this.extractAllMessages();
|
||||||
|
|
||||||
const lastIndex = allMessages.findIndex(
|
const lastIndex = allMessages.findIndex(
|
||||||
|
|
@ -139,16 +137,18 @@ export class TeamsChatService {
|
||||||
|
|
||||||
const newMsg = newMessages[0];
|
const newMsg = newMessages[0];
|
||||||
|
|
||||||
if (newMsg.name === this.MY_NAME) {
|
// if (newMsg.name === this.MY_NAME) {
|
||||||
console.log("[Monitor] My new message:", newMsg);
|
// console.log("[Monitor] My new message:", newMsg);
|
||||||
await messageApi.sendSingleMessage(newMsg);
|
// await messageApi.sendSingleMessage(newMsg);
|
||||||
} else {
|
// } else {
|
||||||
console.log("[Monitor] New incoming message:", newMsg);
|
// console.log("[Monitor] New incoming message:", newMsg);
|
||||||
this.handleNewMessage(newMsg);
|
// this.handleNewMessage(newMsg);
|
||||||
messageApi.sendSingleMessage(newMsg);
|
// messageApi.sendSingleMessage(newMsg);
|
||||||
}
|
// }
|
||||||
|
|
||||||
this.lastMessage = allMessages.pop();
|
this.lastMessage = allMessages.pop();
|
||||||
|
|
||||||
|
return newMsg;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async start(interval = 10000) {
|
public async start(interval = 10000) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue