# Postiz anti-duplicate-post patch (2026-09-26)
# G1: POST/PUT returning 5xx with unrecognised body = outcome unknown -> never replay, throw.
# G2: workflow v1.1.2 treats 'unknown' activity errors like timeouts -> markUnconfirmed, no loop retry.
# Build fails if either patch does not apply (upstream code changed).
FROM ghcr.io/gitroomhq/postiz-app@sha256:01b24a4fc1055f6833122c99614805bea702fdc0ee30c9b5d09330b521fa1efa
RUN set -e; cd /app; \
  for f in apps/backend/dist/libraries/nestjs-libraries/src/integrations/social.abstract.js apps/orchestrator/dist/libraries/nestjs-libraries/src/integrations/social.abstract.js; do \
    sed -i "s#^\(\s*\)const handleError = this.handleErrors(json || '{}', request.status);#&\n\1if (request.status >= 500 \&\& !handleError \&\& (options.method || 'GET').toUpperCase() !== 'GET') { throw new Error('PATCH-G1 unconfirmed: platform returned ' + request.status + ' ' + json.slice(0, 300)); }#" "$f"; \
    grep -q "PATCH-G1" "$f"; \
    node --check "$f"; \
  done; \
  w=apps/orchestrator/dist/apps/orchestrator/src/workflows/post-workflows/post.workflow.v1.1.2.js; \
  sed -i "s#if (handle.type === 'timeout') {#if (handle.type === 'timeout' || handle.type === 'unknown') { /*PATCH-G2*/#" "$w"; \
  test "$(grep -c PATCH-G2 $w)" = 1; \
  node --check "$w"
# G3 (2026-09-30): TikTok app review - redirect on app.postabroad.com, request only scopes the app has.
RUN set -e; cd /app; \
  for f in apps/backend/dist/libraries/nestjs-libraries/src/integrations/social/tiktok.provider.js apps/orchestrator/dist/libraries/nestjs-libraries/src/integrations/social/tiktok.provider.js; do \
    sed -i "/^\s*'video.list',$/d; /^\s*'user.info.stats',$/d" "$f"; \
    sed -i 's#${process?.env?.FRONTEND_URL}/integrations/social/tiktok#https://app.postabroad.com/integrations/social/tiktok#g' "$f"; \
    test "$(grep -c app.postabroad.com/integrations/social/tiktok $f)" = 2; \
    ! grep -q "'user.info.stats',\|'video.list'," "$f"; \
    node --check "$f"; \
  done
# G4 (2026-10-06): upstream emails a duplicated "post published" notice (subject=body, digest repeats it). Keep in-app notification, drop the email.
RUN set -e; cd /app/apps/orchestrator/dist/apps/orchestrator/src/workflows/post-workflows; \
  for f in post.workflow.v*.js; do \
    sed -i '/has been published on/s/, true, true);/, false, false);/' "$f"; \
    ! grep 'has been published on' "$f" | grep -q 'true, true'; \
    node --check "$f"; \
  done; \
  grep -l 'has been published on.*false, false' post.workflow.v1.1.2.js
# G6 (2026-10-09): Instagram long-lived token exchange (GET graph.instagram.com/access_token?grant_type=ig_exchange_token)
# now fails with "Unsupported request - method type: get" when client_id is sent; without it the exchange succeeds (verified live on lyvoi_usa).
# Symptom upstream: connect page "Could not add provider. Invalid API key" (id empty after failed exchange).
RUN set -e; cd /app; \
  for f in apps/backend/dist/libraries/nestjs-libraries/src/integrations/social/instagram.standalone.provider.js apps/orchestrator/dist/libraries/nestjs-libraries/src/integrations/social/instagram.standalone.provider.js; do \
    [ -f "$f" ] || continue; \
    grep -q "grant_type=ig_exchange_token" "$f" || continue; \
    sed -i '/^\s*`&client_id=${process.env.INSTAGRAM_APP_ID}` +$/d' "$f"; \
    ! grep -A3 "grant_type=ig_exchange_token" "$f" | grep -q "client_id"; \
    node --check "$f"; \
  done; \
  ! grep -A3 "grant_type=ig_exchange_token" apps/backend/dist/libraries/nestjs-libraries/src/integrations/social/instagram.standalone.provider.js | grep -q client_id
# G5 (2026-10-07, NO.1926): auth pages rebrand to PostAbroad, drop fake 20,000+/testimonials and unconfigured Google button.
COPY g5-rebrand.js /tmp/g5-rebrand.js
RUN node /tmp/g5-rebrand.js && rm /tmp/g5-rebrand.js
# G7-G9 (2026-10-09): Instagram asks for manage_messages (Studio inbox); Facebook asks for 3 Page scopes only and skips the
# Postiz-only "pick a Page" step when exactly one Page was granted. Details in g7-meta.js.
COPY g7-meta.js /tmp/g7-meta.js
RUN node /tmp/g7-meta.js && rm /tmp/g7-meta.js \
  && grep -q instagram_business_manage_messages /app/apps/backend/dist/libraries/nestjs-libraries/src/integrations/social/instagram.standalone.provider.js \
  && ! grep -q "'business_management'" /app/apps/backend/dist/libraries/nestjs-libraries/src/integrations/social/facebook.provider.js
