update detech new message and auto sync conversations

This commit is contained in:
Admin 2025-09-09 10:08:11 +07:00
parent cf29ee8348
commit 6befd5f2a7
5 changed files with 82 additions and 10 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -80,4 +80,49 @@ function broadcastToPorts(message: any) {
});
}
function initDailyReloadAlarm() {
// Tên alarm
const alarmName = "dailyReload";
// Xóa alarm cũ nếu có
chrome.alarms.clear(alarmName, () => {
// Lấy thời gian hiện tại
const now = new Date();
// Thiết lập 2 giờ sáng ngày hôm nay hoặc ngày mai nếu đã quá giờ
const firstTrigger = new Date();
firstTrigger.setHours(2, 0, 0, 0); // 2:00:00
if (firstTrigger.getTime() <= now.getTime()) {
// Nếu đã qua 2h sáng hôm nay, đặt vào ngày mai
firstTrigger.setDate(firstTrigger.getDate() + 1);
}
const delayInMinutes = (firstTrigger.getTime() - now.getTime()) / 1000 / 60;
// Tạo alarm
chrome.alarms.create(alarmName, {
delayInMinutes,
periodInMinutes: 24 * 60, // lặp lại mỗi 24h
});
console.log(`⏰ Daily reload alarm set for ${firstTrigger}`);
});
// Lắng nghe alarm
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === "dailyReload") {
console.log("🔄 Reloading page (2 AM alarm)");
// Reload tất cả tab đang mở (hoặc tab cụ thể nếu muốn)
chrome.tabs.query({}, (tabs) => {
tabs.forEach((tab) => {
if (tab.id) chrome.tabs.reload(tab.id);
});
});
}
});
}
initSocket();
initDailyReloadAlarm();

View File

@ -542,7 +542,7 @@ export class ContentService {
() => {
this.getConversations();
},
20000
60000
);
console.log("✅ startSyncConversations running");
} else {
@ -583,7 +583,7 @@ export class ContentService {
console.error("❌ autoSyncConversationPrefixMessages error:", err);
}
});
}, 10000);
}, 20000);
console.log("✅ autoSyncConversationPrefixMessages running with PQueue");
} else {

View File

@ -48,7 +48,9 @@ export class MessagesService {
return AppResponse.toPagination<Message>(result, true, Message);
}
async create(dto: CreateMessageDto): Promise<Message> {
async create(
dto: CreateMessageDto,
): Promise<{ data: Message; exit: boolean }> {
const time = new Date(dto.time);
const existing = await this.repo.findOne({
@ -56,7 +58,7 @@ export class MessagesService {
});
if (existing) {
return existing;
return { data: existing, exit: true };
}
const conversation = await this.conversationRepo.findOne({
@ -87,14 +89,19 @@ export class MessagesService {
if (!conversation) return;
const content = `** :rocket: ${result.name} sent:**
\`\`\`
${result.message}
\`\`\``;
await this.zulupService.sendMessageToTopic(
process.env.ZULIP_STREAMS_NAME,
conversation.name,
result.message,
content,
);
}
return result;
return { data: result, exit: false };
}
async bulkCreate(dtos: CreateMessageDto[]): Promise<Message[]> {
@ -198,15 +205,35 @@ export class MessagesService {
async createAndSendToZulip({ data }: CreateBulkMessageDto) {
const results = [];
let iterationCount = 0; // Đếm số lần lặp
// Đảo ngược array trước khi xử lý
const reversedData = [...data].reverse();
for (const mes of reversedData) {
iterationCount++; // Tăng biến đếm mỗi lần lặp
for (const mes of data) {
const result = await this.create(mes);
if (result) {
results.push(result);
// Nếu result có exit = true thì dừng vòng lặp
if (result.exit === true) {
console.log(
`⛔ Stopped because exit = true after ${iterationCount} iterations`,
result,
);
break;
}
}
}
return results;
console.log(`🔁 Total iterations: ${iterationCount}`);
return {
results,
iterationCount, // Trả về kèm số lần lặp
};
}
}