Skip to content

refactor(tiktok): align webhook routing with shared dispatcher - #6261

Merged
BillLeoutsakosvl346 merged 10 commits into
stagingfrom
fix/tiktok-webhook-routing
Aug 8, 2026
Merged

refactor(tiktok): align webhook routing with shared dispatcher#6261
BillLeoutsakosvl346 merged 10 commits into
stagingfrom
fix/tiktok-webhook-routing

Conversation

@BillLeoutsakosvl346

Copy link
Copy Markdown
Contributor

Summary

  • route TikTok app webhooks by the standard routingKey lookup and shared dispatcher
  • remove TikTok-specific ingress jobs, target workers, and async-job registrations
  • migrate existing deployments to routing_key with workspace/account verification
  • retain a temporary null-routing-key fallback and supporting index for safe rolling deployment
  • remove low-signal assertion-only tests while preserving behavioral coverage

Why

TikTok previously used provider-specific background ingress and target-resolution infrastructure. This aligns it with the established Slack-style shared-app webhook architecture while preserving authentication, filtering, idempotency, account isolation, and queued workflow execution.

The temporary fallback handles registrations written by old pods after the migration runs. A follow-up contract PR can remove that fallback and index after one full rollout and final backfill.

Validation

  • 16 focused TikTok/Slack/shared suites: 175 tests
  • independent validate-trigger and validate-integration review: clean approval
  • type-check, format, lint, strict API validation
  • client, tool-registry, and monorepo boundary checks
  • migration safety plus isolated valid/malformed/foreign/fresh-database validation
  • git diff --check

No trigger options, tool behavior, OAuth scopes, webhook payloads, or user-facing fields change.

@vercel

vercel Bot commented Aug 4, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
docs Ready Ready Preview Aug 8, 2026 10:14pm

Request Review

@cursor

cursor Bot commented Aug 4, 2026

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes TikTok webhook delivery from async ingress to synchronous in-request fanout on shared routing infrastructure; mis-routing or dispatch failures would affect workflow triggers, though verification and Slack-parity patterns are preserved.

Overview
TikTok app webhook deliveries are handled in the HTTP route instead of a durable ingress job. After signature and client_key checks, the handler resolves targets with findWebhooksByRoutingKey(envelope.user_openid, …, 'tiktok') and fans out through dispatchResolvedWebhookTarget, returning 503 when lookup or dispatch fails so TikTok can retry.

The TikTok-specific background stack (tiktok-webhook-ingress, paginated tiktok-webhook-targets, and the tiktok-webhook-ingress job type) is removed so routing matches the Slack-style shared processor.

Deploy now derives TikTok routing_key from the connected account’s open_id (UUID suffix stripped), stores providerConfig.credentialId, and uses a null trigger path. Credential deletion deactivates TikTok webhook rows bound via providerConfig.credentialId, alongside Slack.

A migration drops the obsolete webhook_tiktok_credential_id_idx (no routing-key backfill in this diff). Several assertion-only tests are removed; route and deploy tests cover the new behavior.

Reviewed by Cursor Bugbot for commit cc4ca66. Bugbot is set up for automated code reviews on this repo. Configure here.

@greptile-apps

greptile-apps Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR replaces TikTok-specific ingress and target-resolution jobs with the shared routing-key webhook dispatcher while retaining rollout compatibility through a routing-key migration.

  • Routes verified TikTok events by user_openid and returns a retryable response when lookup or dispatch fails.
  • Migrates existing TikTok registrations to routing_key with account and workspace validation.
  • Removes the obsolete TikTok ingress jobs, target workers, and async-job registrations.
  • Deactivates TikTok webhook registrations when their bound credential is removed.

Confidence Score: 5/5

The PR appears safe to merge.

The previously reported failed-dispatch issue is fixed: every failed shared-dispatch outcome is counted and causes a retryable 503 response, while intentionally ignored outcomes remain acknowledged.

Important Files Changed

Filename Overview
apps/sim/app/api/webhooks/tiktok/route.ts Replaces provider-specific durable ingress with shared routing-key lookup and dispatch, and now returns 503 when any target dispatch fails.
apps/sim/lib/webhooks/deploy.ts Persists the TikTok account open ID as the shared dispatcher routing key while retaining the temporary legacy fallback.
apps/sim/lib/credentials/deletion.ts Extends credential cleanup to deactivate TikTok registrations bound through provider configuration.
packages/db/migrations/0282_tiktok_routing_key.sql Backfills validated TikTok routing keys and adds the temporary fallback index needed during rolling deployment.
packages/db/schema.ts Aligns the webhook schema indexes with TikTok routing-key lookup and rollout fallback behavior.

Sequence Diagram

sequenceDiagram
  participant TikTok
  participant Route as TikTok webhook route
  participant DB as Webhook lookup
  participant Dispatcher as Shared dispatcher
  participant Queue as Webhook execution queue

  TikTok->>Route: Signed event
  Route->>Route: Verify signature and envelope
  Route->>DB: Find targets by user_openid + provider
  DB-->>Route: Active workflow targets
  loop Each target
    Route->>Dispatcher: Dispatch resolved target
    Dispatcher->>Queue: Enqueue webhook execution
    Dispatcher-->>Route: queued / ignored / failed
  end
  alt Any dispatch failed
    Route-->>TikTok: 503 retryable response
  else All targets accepted or ignored
    Route-->>TikTok: 200 OK
  end
Loading

Reviews (2): Last reviewed commit: "refactor(tiktok): remove rollout compati..." | Re-trigger Greptile

Comment thread apps/sim/app/api/webhooks/tiktok/route.ts
Comment thread apps/sim/app/api/webhooks/tiktok/route.ts Outdated
@BillLeoutsakosvl346

Copy link
Copy Markdown
Contributor Author

@greptile

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit df6065e. Configure here.

Comment thread apps/sim/app/api/webhooks/tiktok/route.ts
@BillLeoutsakosvl346

Copy link
Copy Markdown
Contributor Author

TikTok architecture classification

This classifies the final TikTok integration after the routing cleanup.

  • Standard: files expected for an ordinary Sim integration.
  • Semi-standard: established extension points needed only by integrations with OAuth, binary uploads, or app-level webhooks.
  • Unique: TikTok-specific infrastructure without a broader established pattern.

Standard

Block

  • apps/sim/blocks/blocks/tiktok.ts

The normal integration block: operations, trigger options, fields, parameter mapping, and outputs.

Tools

  • apps/sim/tools/tiktok/api-schemas.ts
  • apps/sim/tools/tiktok/get_post_status.ts
  • apps/sim/tools/tiktok/get_user.ts
  • apps/sim/tools/tiktok/index.ts
  • apps/sim/tools/tiktok/list_videos.ts
  • apps/sim/tools/tiktok/query_videos.ts
  • apps/sim/tools/tiktok/types.ts
  • apps/sim/tools/tiktok/upload_video_draft.ts
  • apps/sim/tools/tiktok/utils.ts

These follow the usual tools/<provider> structure: one file per operation, provider-local types/helpers, and a barrel export.

Triggers

  • apps/sim/triggers/tiktok/authorization_removed.ts
  • apps/sim/triggers/tiktok/post_inbox_delivered.ts
  • apps/sim/triggers/tiktok/post_no_longer_public.ts
  • apps/sim/triggers/tiktok/post_publicly_available.ts
  • apps/sim/triggers/tiktok/post_publish_complete.ts
  • apps/sim/triggers/tiktok/post_publish_failed.ts
  • apps/sim/triggers/tiktok/utils.ts
  • apps/sim/triggers/tiktok/index.ts

These are conventional trigger definitions registered through Sim’s trigger registry.

Registration and catalogs

  • apps/sim/blocks/registry-maps.ts
  • apps/sim/tools/registry.ts
  • apps/sim/triggers/registry.ts
  • apps/sim/lib/integrations/integrations.json
  • apps/sim/lib/integrations/icon-mapping.ts
  • apps/sim/components/icons.tsx
  • apps/sim/tools/generated/tool-ids.ts
  • apps/sim/tools/generated/tool-metadata.ts
  • apps/sim/tools/generated/tool-outputs.ts
  • apps/docs/content/docs/en/integrations/tiktok.mdx

These are the normal central registration, icon, generated-catalog, and documentation files used by other integrations.

Semi-standard

Video-upload route

  • apps/sim/app/api/tools/tiktok/upload-video-draft/route.ts
  • apps/sim/app/api/tools/tiktok/upload-video-draft/upload.ts
  • apps/sim/lib/api/contracts/tiktok-tools.ts

Uploading a video requires authenticated workspace-file access and streaming binary data to TikTok. That cannot be handled safely as an ordinary JSON tool request, so it uses the same internal-route pattern used by other file-backed integrations.

App-level webhook handling

  • apps/sim/app/api/webhooks/tiktok/route.ts
  • apps/sim/lib/webhooks/providers/tiktok.ts
  • apps/sim/lib/webhooks/providers/registry.ts
  • apps/sim/lib/api/contracts/webhooks.ts

TikTok sends every application event to one shared callback rather than creating a URL per workflow. The provider-local route verifies TikTok’s signature and then uses Sim’s existing webhook dispatcher.

Slack-style deployment and lifecycle integration

  • apps/sim/lib/webhooks/deploy.ts
  • apps/sim/lib/credentials/deletion.ts

At deployment, Sim derives TikTok’s open_id from the selected credential and stores it in the existing shared routingKey column. The callback looks up matching deployments by that key.

Credential deletion also deactivates TikTok deployments, matching the existing Slack lifecycle pattern. These shared files are touched only to register TikTok with established platform behavior; no new shared framework was introduced.

OAuth and environment registration

  • apps/sim/lib/oauth/oauth.ts
  • apps/sim/lib/oauth/types.ts
  • apps/sim/lib/oauth/utils.ts
  • apps/sim/lib/core/config/env.ts

These are established provider-registration points needed for TikTok OAuth credentials and webhook verification.

Unique

There is now no TikTok-specific runtime architecture under apps/sim/background, no custom target worker, no TikTok execution queue, and no provider-specific routing table.

The only remaining unique files are one-time database history and cleanup:

  • packages/db/migrations/0258_gigantic_lady_mastermind.sql
  • packages/db/migrations/0282_tiktok_routing_key.sql
  • packages/db/migrations/meta/0258_snapshot.json
  • packages/db/migrations/meta/0282_snapshot.json
  • packages/db/migrations/meta/_journal.json
  • packages/db/schema.ts

Migration 0258 is immutable history from the original implementation. Migration 0282 removes the now-unused TikTok credential-expression index after routing moved to the standard routingKey column.

This belongs in the routing PR because the dead index and schema helper exist specifically because of the architecture being removed. Splitting it out would temporarily leave obsolete TikTok-only database infrastructure behind. The snapshot and journal changes are generated migration bookkeeping, not additional runtime design.

Tests retained

  • apps/sim/app/api/tools/tiktok/upload-video-draft/route.test.ts
  • apps/sim/app/api/tools/tiktok/upload-video-draft/upload.test.ts
  • apps/sim/app/api/webhooks/tiktok/route.test.ts
  • apps/sim/blocks/blocks/tiktok.test.ts
  • apps/sim/lib/credentials/__tests__/webhook-deactivation.test.ts
  • apps/sim/lib/webhooks/deploy.test.ts
  • apps/sim/lib/webhooks/providers/tiktok.test.ts
  • apps/sim/tools/tiktok/api-schemas.test.ts
  • apps/sim/tools/tiktok/get_user.test.ts
  • apps/sim/tools/tiktok/utils.test.ts
  • apps/sim/tools/tiktok/videos.test.ts

These retain behavioral coverage for request/response transformation, bounded responses, upload streaming, authentication, routing, deployment isolation, idempotency, and credential cleanup.

Low-value scope/output/registry restatements and all obsolete background-worker tests were removed.

Result

The final TikTok implementation is standard or semi-standard except for immutable migration history and the one-time removal of its obsolete index. Runtime webhook routing now follows the existing Slack-style dispatcher pattern.

…-routing

# Conflicts:
#	packages/db/migrations/meta/0282_snapshot.json
#	packages/db/migrations/meta/_journal.json
@BillLeoutsakosvl346
BillLeoutsakosvl346 merged commit fb6e18f into staging Aug 8, 2026
29 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/tiktok-webhook-routing branch August 9, 2026 00:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant