update detech new message and auto sync conversations
This commit is contained in:
		
							parent
							
								
									cf29ee8348
								
							
						
					
					
						commit
						6befd5f2a7
					
				
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							| 
						 | 
					@ -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();
 | 
					initSocket();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					initDailyReloadAlarm();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -542,7 +542,7 @@ export class ContentService {
 | 
				
			||||||
        () => {
 | 
					        () => {
 | 
				
			||||||
          this.getConversations();
 | 
					          this.getConversations();
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        20000
 | 
					        60000
 | 
				
			||||||
      );
 | 
					      );
 | 
				
			||||||
      console.log("✅ startSyncConversations running");
 | 
					      console.log("✅ startSyncConversations running");
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
| 
						 | 
					@ -583,7 +583,7 @@ export class ContentService {
 | 
				
			||||||
            console.error("❌ autoSyncConversationPrefixMessages error:", err);
 | 
					            console.error("❌ autoSyncConversationPrefixMessages error:", err);
 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
      }, 10000);
 | 
					      }, 20000);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      console.log("✅ autoSyncConversationPrefixMessages running with PQueue");
 | 
					      console.log("✅ autoSyncConversationPrefixMessages running with PQueue");
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -48,7 +48,9 @@ export class MessagesService {
 | 
				
			||||||
    return AppResponse.toPagination<Message>(result, true, Message);
 | 
					    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 time = new Date(dto.time);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const existing = await this.repo.findOne({
 | 
					    const existing = await this.repo.findOne({
 | 
				
			||||||
| 
						 | 
					@ -56,7 +58,7 @@ export class MessagesService {
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (existing) {
 | 
					    if (existing) {
 | 
				
			||||||
      return existing;
 | 
					      return { data: existing, exit: true };
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const conversation = await this.conversationRepo.findOne({
 | 
					    const conversation = await this.conversationRepo.findOne({
 | 
				
			||||||
| 
						 | 
					@ -87,14 +89,19 @@ export class MessagesService {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      if (!conversation) return;
 | 
					      if (!conversation) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      const content = `** :rocket: ${result.name} sent:**
 | 
				
			||||||
 | 
					\`\`\`
 | 
				
			||||||
 | 
					${result.message}
 | 
				
			||||||
 | 
					\`\`\``;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      await this.zulupService.sendMessageToTopic(
 | 
					      await this.zulupService.sendMessageToTopic(
 | 
				
			||||||
        process.env.ZULIP_STREAMS_NAME,
 | 
					        process.env.ZULIP_STREAMS_NAME,
 | 
				
			||||||
        conversation.name,
 | 
					        conversation.name,
 | 
				
			||||||
        result.message,
 | 
					        content,
 | 
				
			||||||
      );
 | 
					      );
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return result;
 | 
					    return { data: result, exit: false };
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async bulkCreate(dtos: CreateMessageDto[]): Promise<Message[]> {
 | 
					  async bulkCreate(dtos: CreateMessageDto[]): Promise<Message[]> {
 | 
				
			||||||
| 
						 | 
					@ -198,15 +205,35 @@ export class MessagesService {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async createAndSendToZulip({ data }: CreateBulkMessageDto) {
 | 
					  async createAndSendToZulip({ data }: CreateBulkMessageDto) {
 | 
				
			||||||
    const results = [];
 | 
					    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);
 | 
					      const result = await this.create(mes);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      if (result) {
 | 
					      if (result) {
 | 
				
			||||||
        results.push(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
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue