← Back to Pull requests

feat(channel): Pinterest board sections (#1120) #1127

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Oct 23, 2026 01:56 UTC
Author

Closes #1120. Step 2 of 3: the RLS matrix factory landed in #1123, and step 3 drops the PENDING_TABLES entry after this merges.

Teams pin into board sections, and Tailwind and Later let them pick one. Crove connects each board as a channel (#812) and pinned at the board level only. Only the worker may open a channel's token (ADR-0025), so the composer cannot ask Pinterest itself. The worker fills a cache that the composer reads, following the link preview pattern (#641). The cache is the generic table planned in #1123, so #1030 (the YouTube playlist picker) can use it later.

Pinterest API v5, as confirmed in the docs


Sources: the official OpenAPI description (pinterest/api-description, v5/openapi.yaml) and Pinterest's generated client docs (pinterest/pinterest-python-generated-api-client: docs/BoardsApi.md, docs/PinCreate.md, docs/PinsApi.md, docs/Error.md). developers.pinterest.com renders in the browser only and could not be read directly.
  • GET /v5/boards/{board_id}/sections (board_sections/list):

  • - takes bookmark and page_size (default 25), and answers { items: BoardSection[] (required), bookmark: string | null };
    - needs scope boards:read, which Crove already requests, so there is no new consent;
    - can answer 400, 401, 403, 404 and 429, all as Pinterest.Lib.Error.
  • PinCreate.board_section_id: an optional string or null, "The board section to which this Pin belongs."

  • POST /v5/pins 404: "Board or section not found".

  • Errors have the shape { code: int, message: string }, which PinterestError already reads.

  • Not confirmed: a maximum page_size. Crove asks for 250 per page, as it already does for boards.
  • What it does


  • Adapter (packages/channel/src/pinterest.ts, options.ts):

  • - Listing sections: options({ kind: 'pinterest_section' }) lists the board's sections, following the bookmark from page to page, at most 500. It cleans names: one line, no control, zero-width or direction marks, at most 200 characters. It tells a dead token, a refusal, a gone board and an outage apart.
    - Pinning into a section: with settings.pinterest.sectionId set, publish lists the sections again before any visible call and before a video's upload. It sends board_section_id only for a section the board still has.
    - Refusals, with nothing posted:
    - an unknown or deleted section, or a 404 from POST /v5/pins after the check: PINTEREST_SECTION_NOT_FOUND (bad_request);
    - settings it cannot read: PINTEREST_SECTION_INVALID;
    - a list that does not come: PINTEREST_SECTIONS_UNREADABLE (transient, so the attempt is retried);
    - a token or permission refusal while listing: token_expired or permission;
    - a board that is gone: BOARD_NOT_FOUND.
    - No section: a pin without one is sent exactly as before, with no extra call. The existing recorded-answer test still checks the body with toEqual.
  • Settings (modules/social/src/domain/channels.ts, validate.ts): settings.pinterest = { sectionId }, a numeric id with the same shape and reading as the adapter's. It is refused on other channels (PINTEREST_NOT_SUPPORTED) and when malformed (PINTEREST_SECTION_INVALID). It is not stored without a section, so content hashes do not change.

  • Cache (modules/social/src/channel-options.ts):

  • - social.channel_option.request (members): checks the caller's channel scope (#1081) and an active Pinterest connection. A fresh answer (under a day old) or a pending request (under 2 minutes old) is returned as it is. Otherwise the request is queued, within 20 requests per org per minute counted under an advisory lock.
    - social.channel_option.get (members): the same checks; options are returned only when the list is ready.
    - social.channel_option.load and social.channel_option.record (system only):
    - load reads the connection and kind from the pending row, on a connection that is still active;
    - record updates only WHERE status = 'pending', zod-checks ids, names (trimmed, no invisible marks, at most 200 characters), distinct ids and the 500 cap, and a failure stores items = '[]'.
    - Worker (runChannelOptionJob, queue social-channel-option, one job per request): opens the token only through kernel.channel.credential (credential_get) in the org's system context and hands it to the adapter. A credential that will not open is recorded as permission. No token reaches a command input, an event payload, an error or an answer, and the DB test checks the event and the stored row for it.
  • Composer (apps/app/src/routes/social-post.tsx, vi and en):

  • - a "Phần trong bảng" (Board section) picker on Pinterest variants: none (the board itself) or one of the listed sections;
    - while the list loads it says so, then it shows the reason a list failed, with a reload button;
    - it warns when the chosen section is no longer on the board;
    - names are rendered as text only, and only design tokens are used;
    - the post page words PINTEREST_SECTION_NOT_FOUND, and the kernel's new codes (CHANNEL_OPTION_LIMIT, CHANNEL_OPTIONS_NOT_OFFERED) are worded in both languages.

    Migration 20261029060000_crove_social_channel_option.sql


  • The table, its index, tenant RLS and grants are exactly as in #1123's body.

  • channel_option_before_update, as portal_invoice_list_before_update does it:

  • - the list's org, id, connection, kind and created_at never change;
    - an update that changes nothing passes, as the matrix's status = status probe needs;
    - outside the system context, the only change allowed is asking again: pending, error null, a later requested_at, with items and fetched_at unchanged;
    - a failed answer keeps no items, in any context.
  • channel_option_before_insert: a row of the context's own org starts pending with no items, error or fetched_at. Another org's row is left to RLS, which refuses it. A pending insert passes in the system context too.

  • Queue: social-channel-option.

  • crove_kernel.org_purge: regenerated with purge-add.mjs 1120 crove_social.channel_option, copied verbatim from 20261029050000_crove_social_weekly_digest.sql. The only line that differs:

  • -    'crove_social.short_link', 'crove_social.snippet', 'crove_social.watermark', 'crove_social.report_branding', 'crove_social.goal', 'crove_social.banned_word', 'crove_social.external_import', 'crove_social.external_post', 'crove_social.channel_access', 'crove_social.review_check', 'crove_social.digest_week'];
    +    'crove_social.short_link', 'crove_social.snippet', 'crove_social.watermark', 'crove_social.report_branding', 'crove_social.goal', 'crove_social.banned_word', 'crove_social.external_import', 'crove_social.external_post', 'crove_social.channel_access', 'crove_social.review_check', 'crove_social.digest_week', 'crove_social.channel_option'];
    

  • Unions and types: SOCIAL_CHANNEL_OPTION_KINDS, _STATUSES and _ERRORS are in packages/db/src/schema.ts and mapped in check-unions.db.test.ts. pnpm db:types was regenerated and is unchanged on a second run. node tools/ci/migration-order.mjs origin/main: OK, newest on main is 20261029050000.
  • Checks


  • pnpm -s lint: pass.

  • pnpm -s typecheck: pass.

  • pnpm docs:check: 82 files OK.

  • pnpm exec vitest run packages/channel/test modules/social/test apps/app/test: 147 files, 1222 passed.

  • Unit project (vitest run --project unit): 2474 passed, 14 failed. All 14 failures are in the known Windows-only files (tools/test/invariant-summary, backup-lib, apps/kernel/test/status-page, invariant/route-coverage).

  • New tests:

  • - Pinterest adapter, recorded answers: 16 in pinterest.test.ts, 6 of them new. They cover paging by bookmark, the 500 cap, name cleaning, the error mapping, board_section_id on image and video pins, the refusals with no POST /v5/pins, and an unchanged pin without a section.
    - Domain: 1 test of the settings shape, checked against the adapter's reading.
  • DB tests on PostgreSQL 17, in a private container, one file at a time, after merging the current main:
  • | Test file | Passed |
    | --- | --- |
    | apps/kernel/test/social-channel-option.db.test.ts (new) | 8 |
    | apps/kernel/test/invariant/rls-matrix.db.test.ts | 542 |
    | apps/kernel/test/invariant/org-purge.db.test.ts | 3 |
    | apps/kernel/test/org-purge.db.test.ts | 7 |
    | packages/db/test/invariant/migrate.db.test.ts | 9 |
    | packages/db/test/check-unions.db.test.ts | 3 |
    | apps/kernel/test/social-publish.db.test.ts | 54 |
    | apps/kernel/test/social-link-preview.db.test.ts | 3 |

    The new DB test covers:
    - a request, a repeat, the worker's record with the board's own token, freshness;
    - failures by kind, no items kept on failure, asking again, a credential that will not open;
    - refusals for a viewer, a channel outside the member's scope, another org, an expired connection, a channel that lists nothing;
    - the per-minute budget and that other orgs are unaffected;
    - the guard refusals and a member asking again;
    - the system-only, pending-only, zod-checked record;
    - publishing: listing the sections again, pinning into the section, refusing a gone section with nothing pinned, retrying when the list is down, and a pin without a section unchanged.

    Protected path: migration; needs an independent security review. test/invariant is untouched; the protected-path script lists only supabase/migrations/20261029060000_crove_social_channel_option.sql.

    Not tried in a browser: it needs a connected Pinterest board, and a real Pinterest app and account are not available here. The picker is covered by typecheck and by the API and DB tests only.

    Notes:

  • A second commit restores the CRLF line endings that modules/social/src/posts.ts has on main. The first commit had rewritten them; the net change to that file is 3 lines.

  • The merge commit from main has git's default message, without the Co-Authored-By trailer.
  • 🤖 Generated with Claude Code

    ---

    <!-- dos-pr-summary -->

    📌 TL;DR


    This PR introduces a "Channel Options" feature that allows users to select specific Pinterest board sections when composing a social post. It implements a secure, worker-driven architecture where the frontend requests options, the backend worker fetches them using the channel's token, and the publishing process validates the section's existence before posting.

    🎯 Type of Change


  • 🚀 New feature

  • 🐛 Bugfix

  • 🧹 Refactor

  • ⚡ Performance

  • 📚 Documentation

  • ⚙️ CI / Configuration
  • 🔍 Changes Walkthrough


    | File | Summary of Changes |
    | :--- | :--- |
    | apps/app/src/i18n/messages/en.ts | Added English translations for Pinterest section selection UI, error states (e.g., section not found, permission errors), and new channel option limits. |
    | apps/app/src/i18n/messages/vi.ts | Added Vietnamese translations corresponding to the new English strings. |
    | apps/app/src/lib/kernel-errors.ts | Registered new error codes CHANNEL_OPTION_LIMIT and CHANNEL_OPTIONS_NOT_OFFERED in the kernel error list. |
    | apps/app/src/routes/social-post.tsx | Added PinterestSettings component to the social post composer. This component handles fetching board sections via the new channelOptions API, displays a dropdown for selection, and handles loading/error states. Integrated into the ProviderSettings flow. |
    | apps/kernel/src/jobs-schema.ts | Defined a new job queue socialChannelOption for processing channel option requests. |
    | apps/kernel/src/rpc.ts | Exposed new RPC endpoints social.channelOptions.request and social.channelOptions.get to the frontend. |
    | apps/kernel/src/worker.ts | Implemented the worker logic to process socialChannelOption jobs. It listens for social.channel_option.requested events, executes the job with the channel's token, and handles retries. |
    | apps/kernel/test/social-channel-option.db.test.ts | Added comprehensive integration tests for the channel option feature, covering permission checks, rate limiting, token security, and publishing validation against live (mocked) Pinterest API responses. |
    | modules/social/src/channel-options.ts | Core business logic module. Defines the social.channel_option.request command (with rate limiting and freshness checks) and social.channel_option.record command (worker-only). Ensures tokens never leave the worker context. |

    📊 Architectural Flow


    sequenceDiagram
        participant UI as Social Post UI
        participant API as Kernel RPC
        participant DB as Database
        participant Worker as Background Worker
        participant Pinterest as Pinterest API
    
        Note over UI, Pinterest: Phase 1: Fetching Board Sections
    
        UI->>API: social.channelOptions.request(connectionId)
        API->>DB: Check existing options (freshness/limit)
        alt Fresh or Pending
            API-->>UI: Return cached/pending status
        else Stale or New
            API->>DB: Insert/Update status to 'pending'
            API->>DB: Emit 'social.channel_option.requested' event
            API-->>UI: Return 'pending' status
        end
    
        Worker->>DB: Poll for 'social.channel_option.requested'
        Worker->>DB: Fetch channel token (secure context)
        Worker->>Pinterest: GET /v5/boards/{id}/sections
        Pinterest-->>Worker: Return sections list
        Worker->>DB: social.channel_option.record(options)
        DB->>DB: Update status to 'ready'
    
        UI->>API: social.channelOptions.get(connectionId)
        API->>DB: Fetch options
        API-->>UI: Return sections list
    
        Note over UI, Pinterest: Phase 2: Publishing with Section
    
        UI->>API: Submit Post (with pinterest.sectionId)
        API->>DB: Queue Publish Job
        Worker->>DB: Fetch Publish Job
        Worker->>Pinterest: GET /v5/boards/{id}/sections (Validation)
        Pinterest-->>Worker: Confirm section exists
        Worker->>Pinterest: POST /v5/pins (with board_section_id)
        Pinterest-->>Worker: Success
        Worker->>DB: Mark Post as Published
    

    <!-- /dos-pr-summary -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: claude[bot] Review duration: 13m
    All Vouch review confidence checks passed
    Residual risk 0.32 = Change Risk (0.52) × (1 − Review Confidence (0.38)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 81% · Reviewer familiarity: 35%
    38%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #1127"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 38.3% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 38.3% · Lower = rushed or shallow review
    38.3%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 32.1% · Safe to merge
    32.1%
    Repository