{
  "name": "Community Hub - Mention Notification Processor",
  "nodes": [
    { "parameters": { "httpMethod": "POST", "path": "new-message-event", "responseMode": "lastNode", "options": {} }, "name": "Webhook Trigger", "type": "n8n-nodes-base.webhook", "typeVersion": 1, "id": "webhookTrigger" },
    { "parameters": { "functionCode": "// Extract message content from webhook data\nconst messageContent = $json.body.content;\n\n// Regex to find @mentions\nconst mentionRegex = /@(\\w+)/g;\nlet match;\nconst mentions = [];\n\nwhile ((match = mentionRegex.exec(messageContent)) !== null) {\n  mentions.push(match[1]); // push the username without '@'\n}\n\n// If mentions are found, prepare for further processing\nif (mentions.length > 0) {\n  return [{ json: { ...$json.body, mentions: mentions, hasMentions: true } }];\n} else {\n  // No mentions, just increment unread dots for relevant users\n  return [{ json: { ...$json.body, hasMentions: false } }];\n}\n" }, "name": "Parse Mentions", "type": "n8n-nodes-base.function", "typeVersion": 1, "id": "parseMentions" },
    { "parameters": { "conditions": { "boolean": [ { "value": "={{$json.hasMentions}}" } ] } }, "name": "If Has Mentions", "type": "n8n-nodes-base.if", "typeVersion": 1, "id": "ifHasMentions" },
    { "parameters": { "authentication": "credentials", "credentials": { "mySql": { "id": "YOUR_MYSQL_CREDENTIAL_ID", "name": "MySQL Dev" } }, "query": "SELECT id, firebase_token FROM users WHERE username IN ('{{ $json.mentions.join(\"', '\") }}');", "options": {} }, "name": "Lookup Mentioned Users", "type": "n8n-nodes-base.mySql", "typeVersion": 1, "id": "lookupUsers" },
    { "parameters": { "url": "https://fcm.googleapis.com/fcm/send", "method": "POST", "jsonParameters": true, "body": { "to": "={{$json.firebase_token}}", "notification": { "title": "New mention in Community Hub!", "body": "={{$json.content}}", "icon": "https://mysportmanager.ie/favicon.ico" }, "data": { "conversation_id": "={{$json.conversation_id}}", "message_id": "={{$json.id}}" } }, "headerParameters": [ { "name": "Authorization", "value": "Bearer YOUR_FIREBASE_SERVER_KEY" } ], "sendOnlySetParameters": true, "options": {} }, "name": "Send Firebase Push Notification", "type": "n8n-nodes-base.httpRequest", "typeVersion": 3, "id": "sendFirebasePush" },
    { "parameters": { "authentication": "credentials", "credentials": { "mySql": { "id": "YOUR_MYSQL_CREDENTIAL_ID", "name": "MySQL Dev" } }, "query": "UPDATE participants SET mention_badge = mention_badge + 1 WHERE user_id = {{ $json.id }} AND conversation_id = {{ $json.conversation_id }};", "options": {} }, "name": "Increment Mention Badge", "type": "n8n-nodes-base.mySql", "typeVersion": 1, "id": "incrementMentionBadge" },
    { "parameters": { "url": "YOUR_WEBSOCKET_SERVICE_API_ENDPOINT/broadcast", "method": "POST", "jsonParameters": true, "body": { "conversation_id": "={{$json.conversation_id}}", "event": "mention_badge_update", "user_id": "={{$json.id}}", "badge_count": "increment" }, "sendOnlySetParameters": true, "options": {} }, "name": "WebSocket Broadcast (Mention Badge)", "type": "n8n-nodes-base.httpRequest", "typeVersion": 3, "id": "wsBroadcastMention" },
    { "parameters": { "authentication": "credentials", "credentials": { "mySql": { "id": "YOUR_MYSQL_CREDENTIAL_ID", "name": "MySQL Dev" } }, "query": "UPDATE participants SET unread_count = unread_count + 1 WHERE conversation_id = {{ $json.conversation_id }} AND user_id != {{ $json.sender_id }} AND user_id NOT IN (SELECT id FROM users WHERE username IN ('{{ $json.mentions.join(\"', '\") }}'));", "options": {} }, "name": "Increment Unread Dot (No Mentions)", "type": "n8n-nodes-base.mySql", "typeVersion": 1, "id": "incrementUnreadDot" },
    { "parameters": { "url": "YOUR_WEBSOCKET_SERVICE_API_ENDPOINT/broadcast", "method": "POST", "jsonParameters": true, "body": { "conversation_id": "={{$json.conversation_id}}", "event": "unread_dot_update" }, "sendOnlySetParameters": true, "options": {} }, "name": "WebSocket Broadcast (Unread Dot)", "type": "n8n-nodes-base.httpRequest", "typeVersion": 3, "id": "wsBroadcastUnread" },
    { "parameters": { "resource": "messages", "operation": "sendText", "chatId": "YOUR_ADMIN_TELEGRAM_CHAT_ID", "text": "New message event received. Mentions: ={{$json.mentions.join(', ') || 'None'}}", "allowHtml": false, "disableWebPagePreview": false, "disableNotification": false, "options": {} }, "name": "Debug Telegram Notification", "type": "n8n-nodes-base.telegram", "typeVersion": 1, "id": "debugTelegram", "credentials": { "telegramApi": { "id": "WkHKUk8lgUqCv1PL", "name": "Telegram Bot" } }, "alwaysOutputData": true }
  ],
  "connections": {
    "Webhook Trigger": { "main": [ [ { "node": "Parse Mentions", "type": "main", "index": 0 } ] ] },
    "Parse Mentions": { "main": [ [ { "node": "If Has Mentions", "type": "main", "index": 0 } ] ] },
    "If Has Mentions": { "main": [ [ { "node": "Lookup Mentioned Users", "type": "main", "index": 0 }, { "node": "Increment Unread Dot (No Mentions)", "type": "main", "index": 0 } ] ] },
    "Lookup Mentioned Users": { "main": [ [ { "node": "Send Firebase Push Notification", "type": "main", "index": 0 }, { "node": "Increment Mention Badge", "type": "main", "index": 0 } ] ] },
    "Send Firebase Push Notification": { "main": [ [ { "node": "WebSocket Broadcast (Mention Badge)", "type": "main", "index": 0 } ] ] },
    "Increment Unread Dot (No Mentions)": { "main": [ [ { "node": "WebSocket Broadcast (Unread Dot)", "type": "main", "index": 0 } ] ] },
    "WebSocket Broadcast (Unread Dot)": { "main": [ [ { "node": "Debug Telegram Notification", "type": "main", "index": 0 } ] ] },
    "WebSocket Broadcast (Mention Badge)": { "main": [ [ { "node": "Debug Telegram Notification", "type": "main", "index": 0 } ] ] }
  },
  "settings": {},
  "staticData": {}
}
