From 4d175d9aa1716ebb23e85c6ed02f64716298ac6c Mon Sep 17 00:00:00 2001
From: Naiel <109038805+naielv@users.noreply.github.com>
Date: Wed, 15 Oct 2025 11:07:03 +0000
Subject: [PATCH] update
---
src/page/avisos.js | 1 +
src/page/buscar.js | 1 +
src/page/chat.js | 255 ++++++++++++++++++++++++++++++++++++++++++++-
3 files changed, 256 insertions(+), 1 deletion(-)
diff --git a/src/page/avisos.js b/src/page/avisos.js
index 277c464..a052b0a 100644
--- a/src/page/avisos.js
+++ b/src/page/avisos.js
@@ -4,6 +4,7 @@ PAGES.avisos = {
navcss: "btn5",
icon: "static/appico/File_Plugin.svg",
AccessControl: true,
+ Esconder: true,
Title: "Avisos",
edit: function (mid) {
if (!checkRole("avisos:edit")) {setUrlHash("avisos");return}
diff --git a/src/page/buscar.js b/src/page/buscar.js
index 7da3b63..140dd8d 100644
--- a/src/page/buscar.js
+++ b/src/page/buscar.js
@@ -3,6 +3,7 @@ PAGES.buscar = {
icon: "static/appico/File_Plugin.svg",
Title: "Buscar",
AccessControl: true,
+ Esconder: true,
index: function() {
const searchInput = safeuuid();
diff --git a/src/page/chat.js b/src/page/chat.js
index 22a2d63..dd27431 100644
--- a/src/page/chat.js
+++ b/src/page/chat.js
@@ -1,13 +1,178 @@
PERMS["chat"] = "Chat"
PERMS["chat:edit"] = "> Escribir"
+// Global chat notification system
+PAGES.chat_notifications = {
+ listener: null,
+ Esconder: true,
+ lastMessageTime: null, // Start with null to allow all messages initially
+ isActive: false,
+
+ init: function() {
+ console.log("Initializing chat notifications...");
+ console.log("isActive:", this.isActive);
+ console.log("checkRole available:", typeof checkRole);
+ console.log("checkRole('chat'):", typeof checkRole === 'function' ? checkRole("chat") : "function not available");
+
+ if (this.isActive) {
+ console.log("Chat notifications already active");
+ return;
+ }
+
+ // Check if user has chat permissions (with fallback)
+ if (typeof checkRole === 'function' && !checkRole("chat")) {
+ console.log("No chat permissions");
+ return;
+ }
+
+ this.isActive = true;
+ console.log("Starting chat notifications listener");
+
+ var today = new Date().toISOString().split('T')[0];
+ var dayPath = `chat_${today}`;
+
+ console.log("Listening for notifications on:", dayPath);
+ console.log("TABLE:", TABLE);
+
+ // Set initial timestamp to current time to only notify for new messages from now on
+ if (!this.lastMessageTime) {
+ this.lastMessageTime = new Date().toISOString();
+ console.log("Set initial lastMessageTime:", this.lastMessageTime);
+ }
+
+ // Listen for new messages on today's chat
+ this.listener = gun.get(TABLE).get(dayPath).map().on((data, messageId, _msg, _ev) => {
+ console.log("Notification listener received data:", data, "messageId:", messageId);
+
+ if (data === null) return; // Ignore deletions
+
+ if (typeof data === "string") {
+ // Encrypted message
+ console.log("Decrypting notification message...");
+ TS_decrypt(data, SECRET, (decryptedData) => {
+ console.log("Decrypted notification data:", decryptedData);
+ this.handleNewMessage(decryptedData, messageId);
+ });
+ } else {
+ // Unencrypted message
+ console.log("Processing unencrypted notification:", data);
+ this.handleNewMessage(data, messageId);
+ }
+ });
+
+ // Add to cleanup listeners
+ EventListeners.GunJS.push(this.listener);
+ console.log("Chat notifications initialized successfully");
+ },
+
+ handleNewMessage: function(messageData, messageId) {
+ console.log("Handling new message for notification:", messageData);
+ console.log("Current lastMessageTime:", this.lastMessageTime);
+ console.log("Message timestamp:", messageData.timestamp);
+ console.log("Message authorId:", messageData.authorId);
+ console.log("Current user ID:", SUB_LOGGED_IN_ID);
+
+ // Don't notify for our own messages
+ if (messageData.authorId === SUB_LOGGED_IN_ID) {
+ console.log("Skipping notification - own message");
+ return;
+ }
+
+ // Don't notify for old messages (only if we have a baseline)
+ if (this.lastMessageTime && messageData.timestamp && messageData.timestamp <= this.lastMessageTime) {
+ console.log("Skipping notification - old message");
+ return;
+ }
+
+ // Don't notify if user is currently viewing the chat page
+ var currentHash = location.hash.replace("#", "");
+ console.log("Current page hash:", currentHash);
+ if (currentHash === 'chat') {
+ console.log("Skipping notification - user viewing chat");
+ return;
+ }
+
+ // Update last message time
+ if (messageData.timestamp) {
+ this.lastMessageTime = messageData.timestamp;
+ console.log("Updated lastMessageTime to:", this.lastMessageTime);
+ }
+
+ // Show notification
+ var author = messageData.author || 'Usuario Anónimo';
+ var preview = messageData.text.length > 50 ?
+ messageData.text.substring(0, 50) + '...' :
+ messageData.text;
+
+ console.log("Showing notification for:", author, "-", preview);
+ console.log("Testing toastr availability:", typeof toastr);
+
+ // Test notification to verify toastr works
+ console.log("Attempting to show notification...");
+
+ toastr.info(
+ `${author}:
${preview}`,
+ '💬 Nuevo mensaje en Chat',
+ {
+ onclick: function() {
+ console.log("Notification clicked, navigating to chat");
+ setUrlHash('chat');
+ },
+ timeOut: 8000,
+ extendedTimeOut: 2000,
+ closeButton: true,
+ progressBar: true,
+ positionClass: 'toast-top-right',
+ preventDuplicates: true,
+ newestOnTop: true,
+ escapeHtml: false
+ }
+ );
+
+ console.log("Notification call completed");
+ },
+
+ destroy: function() {
+ if (this.listener) {
+ this.listener.off();
+ }
+ this.isActive = false;
+ },
+
+ // Test function to manually trigger a notification
+ testNotification: function() {
+ console.log("Testing notification manually...");
+ console.log("toastr available:", typeof toastr);
+
+ toastr.success("¡Funciona! Esta es una notificación de prueba.", "Test de Notificación", {
+ timeOut: 5000,
+ closeButton: true,
+ progressBar: true,
+ positionClass: 'toast-top-right'
+ });
+ }
+};
+
PAGES.chat = {
navcss: "btn4",
icon: "static/appico/Chat.svg",
AccessControl: true,
Title: "Chat",
index: function() {
- if (!checkRole("chat")) {setUrlHash("index");return}
+ console.log("Chat index function called");
+ console.log("SUB_LOGGED_IN:", SUB_LOGGED_IN);
+ console.log("checkRole function exists:", typeof checkRole);
+
+ if (!checkRole("chat")) {
+ console.log("No chat permission, redirecting to index");
+ setUrlHash("index");
+ return;
+ }
+
+ console.log("Chat permission granted, initializing chat");
+
+ // Stop global notifications when viewing chat
+ PAGES.chat_notifications.destroy();
var messagesList = safeuuid();
var messageInput = safeuuid();
@@ -15,6 +180,8 @@ PAGES.chat = {
var daySelector = safeuuid();
var currentDay = new Date().toISOString().split('T')[0]; // YYYY-MM-DD format
+ console.log("Creating chat UI with currentDay:", currentDay);
+
container.innerHTML = `