{
  "name": "MADIAD Hub - Retry and unpublish",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 10
            }
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        -160,
        0
      ],
      "id": "Every 10 minutes",
      "name": "Every 10 minutes"
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "",
          "mode": "list",
          "cachedResultName": "[MADIAD Template] Publishing Schedule"
        },
        "sheetName": {
          "__rl": true,
          "value": "Config",
          "mode": "name"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        60,
        0
      ],
      "id": "Read Config",
      "name": "Read Config"
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "",
          "mode": "list",
          "cachedResultName": "[MADIAD Template] Publishing Schedule"
        },
        "sheetName": {
          "__rl": true,
          "value": "Schedule",
          "mode": "name"
        },
        "options": {
          "dataLocationOnSheet": {
            "values": {
              "rangeDefinition": "specifyRange",
              "headerRow": 1,
              "firstDataRow": 3
            }
          }
        }
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        280,
        0
      ],
      "id": "Read Schedule",
      "name": "Read Schedule"
    },
    {
      "parameters": {
        "jsCode": "\nconst cfg = {};\nfor (const it of $('Read Config').all()) {\n  const k = String(it.json['Key'] ?? '').trim();\n  if (k) cfg[k] = String(it.json['Value'] ?? '').trim();\n}\nif (!cfg.profile_id) throw new Error('No profile_id in the Config tab. Copy it from your MADIAD Hub dashboard.');\nconst KENH = { 'Facebook': 'facebook', 'Instagram': 'instagram', 'TikTok': 'tiktok', 'YouTube': 'youtube', 'LinkedIn': 'linkedin', 'X': 'x', 'Threads': 'threads', 'Pinterest': 'pinterest', 'Reddit': 'reddit', 'Bluesky': 'bluesky', 'Google Business': 'google_business', 'Telegram': 'telegram', 'Discord': 'discord', 'Slack': 'slack', 'Mastodon': 'mastodon', 'Nostr': 'nostr', 'Lemmy': 'lemmy', 'dev.to': 'devto', 'Hashnode': 'hashnode', 'WordPress': 'wordpress', 'Whop': 'whop', 'listmonk': 'listmonk' };\nconst IMG = ['.jpg', '.jpeg', '.png', '.webp', '.gif'];\nconst VID = ['.mp4', '.mov', '.m4v', '.webm'];\nconst NEEDS_TITLE = ['youtube', 'reddit', 'pinterest'];\nconst OPEN = ['', 'Draft', 'Queued'];\n\nconst out = [];\nfor (const it of $input.all()) {\n  const r = it.json;\n  const act = String(r['Action'] ?? '').trim();\n  const st = String(r['Status'] ?? '').trim();\n\n  if (act === 'Retry') {\n    const rid = String(r['Request ID'] ?? '').trim();\n    if (!rid) { out.push({ json: { _row: r.row_number, _err: 'Retry needs a Request ID and this row has none. Set Action back to Publish now to send it fresh.' } }); continue; }\n    out.push({ json: { _row: r.row_number, _op: 'retry', _body: { request_id: rid }, _st: 'Publishing' } });\n    continue;\n  }\n\n  if (act === 'Unpublish') {\n    if (st !== 'Published') continue;\n    const ids = String(r['Platform Post IDs'] ?? '').trim();\n    if (!ids) { out.push({ json: { _row: r.row_number, _err: 'Cannot unpublish: this row has no platform post IDs. Delete it on the platform itself.' } }); continue; }\n    for (const pair of ids.split(';')) {\n      const [platform, postId] = pair.split('=');\n      if (!platform || !postId) continue;\n      out.push({ json: { _row: r.row_number, _op: 'unpublish', _st: 'Draft',\n        _body: { profile_id: cfg.profile_id, platform, post_id: postId } } });\n    }\n  }\n}\nreturn out;\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        500,
        0
      ],
      "id": "Build the command",
      "name": "Build the command"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "version": 2
          },
          "conditions": [
            {
              "id": "c",
              "operator": {
                "type": "string",
                "operation": "notExists"
              },
              "leftValue": "={{ $json._err }}",
              "rightValue": ""
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        720,
        0
      ],
      "id": "Command usable?",
      "name": "Command usable?"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://api.madiad.com/v1/posts/{{ $json._op }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-Madiad-Source",
              "value": "tpl-retry-unpublish-v1"
            }
          ]
        },
        "options": {
          "response": {
            "response": {
              "neverError": true,
              "fullResponse": true
            }
          }
        },
        "sendBody": true,
        "contentType": "json",
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json._body) }}"
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        940,
        -120
      ],
      "id": "Send the command",
      "name": "Send the command",
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "version": 2
          },
          "conditions": [
            {
              "id": "c",
              "operator": {
                "type": "number",
                "operation": "lt"
              },
              "leftValue": "={{ $json.statusCode }}",
              "rightValue": 300
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        1160,
        -120
      ],
      "id": "Accepted?",
      "name": "Accepted?"
    },
    {
      "parameters": {
        "operation": "update",
        "documentId": {
          "__rl": true,
          "value": "",
          "mode": "list",
          "cachedResultName": "[MADIAD Template] Publishing Schedule"
        },
        "sheetName": {
          "__rl": true,
          "value": "Schedule",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "row_number": "={{ $('Build the command').item.json._row }}",
            "Status": "={{ $('Build the command').item.json._st }}",
            "Action": "",
            "Error": ""
          },
          "matchingColumns": [
            "row_number"
          ],
          "schema": []
        },
        "options": {
          "dataLocationOnSheet": {
            "values": {
              "rangeDefinition": "specifyRange",
              "headerRow": 1,
              "firstDataRow": 3
            }
          }
        }
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        1380,
        -220
      ],
      "id": "Write done",
      "name": "Write done",
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "operation": "update",
        "documentId": {
          "__rl": true,
          "value": "",
          "mode": "list",
          "cachedResultName": "[MADIAD Template] Publishing Schedule"
        },
        "sheetName": {
          "__rl": true,
          "value": "Schedule",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "row_number": "={{ $('Build the command').item.json._row }}",
            "Error": "={{ $json.body?.error?.message ?? ('HTTP ' + ($json.statusCode ?? '?')) }}"
          },
          "matchingColumns": [
            "row_number"
          ],
          "schema": []
        },
        "options": {
          "dataLocationOnSheet": {
            "values": {
              "rangeDefinition": "specifyRange",
              "headerRow": 1,
              "firstDataRow": 3
            }
          }
        }
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        1380,
        -20
      ],
      "id": "Write failure",
      "name": "Write failure",
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "operation": "update",
        "documentId": {
          "__rl": true,
          "value": "",
          "mode": "list",
          "cachedResultName": "[MADIAD Template] Publishing Schedule"
        },
        "sheetName": {
          "__rl": true,
          "value": "Schedule",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "row_number": "={{ $json._row }}",
            "Error": "={{ $json._err }}",
            "Action": ""
          },
          "matchingColumns": [
            "row_number"
          ],
          "schema": []
        },
        "options": {
          "dataLocationOnSheet": {
            "values": {
              "rangeDefinition": "specifyRange",
              "headerRow": 1,
              "firstDataRow": 3
            }
          }
        }
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        940,
        140
      ],
      "id": "Write missing data",
      "name": "Write missing data",
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "content": "## MADIAD Hub - Retry and unpublish\n\n1. Pick your own copy of the sheet in EVERY Google Sheets node.\n2. Create a **Header Auth** credential named `MADIAD Hub`:",
        "height": 300,
        "width": 430,
        "color": 2
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -620,
        -160
      ],
      "id": "Setup",
      "name": "Setup"
    }
  ],
  "connections": {
    "Every 10 minutes": {
      "main": [
        [
          {
            "node": "Read Config",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Config": {
      "main": [
        [
          {
            "node": "Read Schedule",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Schedule": {
      "main": [
        [
          {
            "node": "Build the command",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build the command": {
      "main": [
        [
          {
            "node": "Command usable?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Command usable?": {
      "main": [
        [
          {
            "node": "Send the command",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Write missing data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send the command": {
      "main": [
        [
          {
            "node": "Accepted?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Accepted?": {
      "main": [
        [
          {
            "node": "Write done",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Write failure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false
  }
}