{
  "name": "Detect Volunteers and award badges",
  "nodes": [
    {
      "id": "853e8b25-b42b-437c-bfd6-188341357c1d",
      "type": "trigger:post_created",
      "typeVersion": "1.0",
      "name": "When a post is created in Volunteer shoutout thread",
      "parameters": {
        "post_type": "regular",
        "topic_ids": [
          14977
        ],
        "topic_type": "all",
        "category_ids": []
      },
      "credentials": {},
      "webhookId": null,
      "position": {
        "x": -83.84452413405212,
        "y": -3.177857467385441
      },
      "notes": "",
      "notesInFlow": false,
      "alwaysOutputData": false
    },
    {
      "id": "f798b294-4dac-4328-9334-3b1bdcd845e6",
      "type": "action:code",
      "typeVersion": "1.0",
      "name": "Extract mentioned usernames",
      "parameters": {
        "code": "var items = $input.all();\nvar output = [];\n\nfunction emit(username, post, seen) {\n  if (seen[username]) return;\n  seen[username] = true;\n  output.push({\n    json: {\n      username: username,\n      post_id: post.id,\n      post_url: post.post_url,\n      topic_title: post.topic_title\n    }\n  });\n}\n\nitems.forEach(function(item) {\n  var post = item.json.post;\n  if (!post) return;\n\n  var seen = {};\n  var cooked = post.cooked;\n\n  if (typeof cooked === \"string\" && cooked.length > 0) {\n    // Authoritative path: Discourse renders every real mention as an anchor\n    // with class \"mention\" and href .../u/<username>.\n    var anchorRe = /<a\\b[^>]*\\bhref=\"[^\"]*\\/u\\/([^\"\\/]+)\"/g;\n    var m;\n    while ((m = anchorRe.exec(cooked)) !== null) {\n      if (m[0].indexOf(\"mention\") === -1) continue;\n      emit(m[1], post, seen);\n    }\n  } else {\n    // Fallback: no cooked HTML available, parse plain @mentions from raw.\n    // Require the @ to start a token (start, whitespace or open paren) so\n    // emails like foo@bar.com are not extracted.\n    var usernameRe = /(?:^|[\\s(])@([A-Za-z0-9][A-Za-z0-9_.-]*)/g;\n    var raw = post.raw;\n    if (typeof raw !== \"string\") return;\n    var m2;\n    while ((m2 = usernameRe.exec(raw)) !== null) {\n      emit(m2[1], post, seen);\n    }\n  }\n});\n\nreturn output;",
        "mode": "runOnceForAllItems"
      },
      "credentials": {},
      "webhookId": null,
      "position": {
        "x": 306.7038637925448,
        "y": 0.8726107127389371
      },
      "notes": "",
      "notesInFlow": false,
      "alwaysOutputData": false
    },
    {
      "id": "f694f2b8-495f-4358-8fd6-830159557592",
      "type": "action:badge",
      "typeVersion": "1.0",
      "name": "Grant Volunteer Bronze",
      "parameters": {
        "reason": "=post:{{ $(\"When a post is created in Volunteer shoutout thread\").first().json.post.id }}",
        "badge_id": 156,
        "username": "={{ $json.username }}",
        "operation": "grant",
        "actor_username": "system"
      },
      "credentials": {},
      "webhookId": null,
      "position": {
        "x": 439.9838099406634,
        "y": 0.7621456344550639
      },
      "notes": "",
      "notesInFlow": false,
      "alwaysOutputData": false
    },
    {
      "id": "55d93fe0-b4be-4226-848c-34e5239a0001",
      "type": "flow:wait",
      "typeVersion": "1.0",
      "name": "Wait 3 minutes for grace period edits",
      "parameters": {
        "resume": "time_interval",
        "wait_unit": "seconds",
        "wait_amount": 181
      },
      "credentials": {},
      "webhookId": null,
      "position": {
        "x": 55.10332490717933,
        "y": -1.304269841677507
      },
      "notes": "",
      "notesInFlow": false,
      "alwaysOutputData": false
    },
    {
      "id": "93dd94b5-955b-43db-aa84-4ac380b79036",
      "type": "action:post",
      "typeVersion": "1.0",
      "name": "Fetch fresh copy of post",
      "parameters": {
        "order": "latest",
        "status": "any",
        "post_id": "={{ $json.post.id }}",
        "operation": "get",
        "post_type": "regular",
        "include_raw": true,
        "actor_username": "system",
        "include_cooked": true,
        "body_character_limit": 0,
        "exact_category_match": false
      },
      "credentials": {},
      "webhookId": null,
      "position": {
        "x": 182.5372671019409,
        "y": 0.7894153873046346
      },
      "notes": "",
      "notesInFlow": false,
      "alwaysOutputData": false
    },
    {
      "id": "eaac1185-a3c7-427a-9ec9-6b8c9d2594fa",
      "type": "trigger:post_edited",
      "typeVersion": "2.0",
      "name": "When a post is edited in Volunteer shoutout thread",
      "parameters": {
        "topic_ids": [
          14977
        ],
        "post_scope": "all_posts",
        "category_ids": []
      },
      "credentials": {},
      "webhookId": null,
      "position": {
        "x": -84.33227673753834,
        "y": 114.8771146729049
      },
      "notes": "",
      "notesInFlow": false,
      "alwaysOutputData": false
    },
    {
      "id": "97995056-6bfe-480b-9cc9-9437698c17ec",
      "type": "action:code",
      "typeVersion": "1.0",
      "name": "Extract newly mentioned usernames",
      "parameters": {
        "code": "// Extract usernames mentioned in the NEW post content minus usernames\n// mentioned in the PREVIOUS version (revision.previous_raw). The result is\n// the set of usernames newly mentioned by this edit.\n//\n// Edits within the grace period do not create a revision (revision is null),\n// so the previous content is unknown; output nothing for those edits.\n\nfunction extractRawUsernames(raw) {\n  var seen = {};\n  var usernames = [];\n  if (typeof raw !== \"string\" || raw.length === 0) return usernames;\n  // Parse plain @mentions that start a token (start, whitespace or open\n  // paren) so emails like foo@bar.com are not extracted.\n  var usernameRe = /(?:^|[\\s(])@([A-Za-z0-9][A-Za-z0-9_.-]*)/g;\n  var m;\n  while ((m = usernameRe.exec(raw)) !== null) {\n    var key = m[1].toLowerCase();\n    if (!seen[key]) {\n      seen[key] = true;\n      usernames.push(m[1]);\n    }\n  }\n  return usernames;\n}\n\nfunction extractMentions(post) {\n  var seen = {};\n  var usernames = [];\n  function add(name) {\n    var key = name.toLowerCase();\n    if (seen[key]) return;\n    seen[key] = true;\n    usernames.push(name);\n  }\n  var cooked = post.cooked;\n  if (typeof cooked === \"string\" && cooked.length > 0) {\n    // Authoritative path: Discourse renders every real mention as an anchor\n    // with class \"mention\" and href .../u/<username>.\n    var anchorRe = /<a\\b[^>]*\\bhref=\"[^\"]*\\/u\\/([^\"\\/]+)\"/g;\n    var m;\n    while ((m = anchorRe.exec(cooked)) !== null) {\n      if (m[0].indexOf(\"mention\") === -1) continue;\n      add(m[1]);\n    }\n  } else {\n    // Fallback: no cooked HTML available, parse plain @mentions from raw.\n    var raw = post.raw;\n    if (typeof raw === \"string\" && raw.length > 0) {\n      var usernameRe = /(?:^|[\\s(])@([A-Za-z0-9][A-Za-z0-9_.-]*)/g;\n      var m2;\n      while ((m2 = usernameRe.exec(raw)) !== null) {\n        add(m2[1]);\n      }\n    }\n  }\n  return usernames;\n}\n\nvar items = $input.all();\nvar output = [];\n\nitems.forEach(function(item) {\n  var post = item.json.post;\n  if (!post) return;\n\n  // No revision means the edit happened within the grace period and the\n  // previous content is unknown; emit nothing for it.\n  if (!item.json.revision) return;\n\n  var previousRaw = null;\n  if (typeof item.json.revision.previous_raw === \"string\") {\n    previousRaw = item.json.revision.previous_raw;\n  }\n\n  var before = {};\n  extractRawUsernames(previousRaw).forEach(function(name) {\n    before[name.toLowerCase()] = true;\n  });\n\n  var seen = {};\n  extractMentions(post).forEach(function(username) {\n    var key = username.toLowerCase();\n    if (before[key]) return; // already mentioned before this edit\n    if (seen[key]) return;\n    seen[key] = true;\n    output.push({\n      json: {\n        username: username,\n        post_id: post.id,\n        post_url: post.post_url,\n        topic_title: post.topic_title\n      }\n    });\n  });\n});\n\nreturn output;",
        "mode": "runOnceForAllItems"
      },
      "credentials": {},
      "webhookId": null,
      "position": {
        "x": 307.0653240001522,
        "y": 117.9477365383893
      },
      "notes": "",
      "notesInFlow": false,
      "alwaysOutputData": false
    },
    {
      "id": "cb9238da-e0dc-4979-a6a6-d02fa3999e88",
      "type": "action:badge",
      "typeVersion": "1.0",
      "name": "Grant Volunteer Bronze 1",
      "parameters": {
        "reason": "=post:{{ $json.post_id }}",
        "badge_id": 156,
        "username": "={{ $json.username }}",
        "operation": "grant",
        "actor_username": "system"
      },
      "credentials": {},
      "webhookId": null,
      "position": {
        "x": 444.8187623047817,
        "y": 117.8584211107569
      },
      "notes": "",
      "notesInFlow": false,
      "alwaysOutputData": false
    },
    {
      "id": "f70c03c1-6e9c-4679-abd0-ad4f213b775e",
      "type": "flow:sticky_note",
      "typeVersion": "1.0",
      "name": "Sticky Note",
      "parameters": {
        "content": "Posts edited within 3 minutes are in the grace period and won't create a 'revision' attribute (revision=null). These will be caught by the 3 minute timer for the original post.\n\nEdit path name extraction ignores detected names if the revision is null. If there is a revision text, subtract the original post from the detected names to determine newly mentioned names in the edit.",
        "width": 200,
        "height": 150,
        "color": "yellow"
      },
      "credentials": {},
      "webhookId": null,
      "position": {
        "x": 64.53000877067944,
        "y": 90.35722562961476
      }
    }
  ],
  "connections": {
    "Fetch fresh copy of post": {
      "main": [
        [
          {
            "node": "Extract mentioned usernames",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract mentioned usernames": {
      "main": [
        [
          {
            "node": "Grant Volunteer Bronze",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract newly mentioned usernames": {
      "main": [
        [
          {
            "node": "Grant Volunteer Bronze 1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait 3 minutes for grace period edits": {
      "main": [
        [
          {
            "node": "Fetch fresh copy of post",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When a post is edited in Volunteer shoutout thread": {
      "main": [
        [
          {
            "node": "Extract newly mentioned usernames",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When a post is created in Volunteer shoutout thread": {
      "main": [
        [
          {
            "node": "Wait 3 minutes for grace period edits",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {},
  "variables": [],
  "staticData": {}
}