FROM --platform=$BUILDPLATFORM node:22-bullseye@sha256:9e34ba52e1f3c31ed9bd4d0bcf784f5909db17cda61c220e29c8d7a8ebfb402e AS source-builder
WORKDIR /src
# Не скачиваем release bundle и не ищем webpack globals: adapter собирается
# вместе с actual MatrixChat/RoomViewStore из проверенного immutable commit.
RUN git init . \
  && git remote add origin https://github.com/element-hq/element-web.git \
  && git fetch --depth=1 origin tag v1.11.112 \
  && git checkout --detach v1.11.112 \
  && test "$(git rev-parse HEAD)" = 87d40ab0e0d38a064912ee73ffaa8684cb1e79b1
# Immutable upstream dependencies are cached independently from maintained overlays.
RUN corepack enable \
  && corepack prepare yarn@1.22.22 --activate \
  && yarn --frozen-lockfile --network-timeout=200000 install
COPY direct-target-*.ts patch-direct-target-source.mjs patch-encryption-source.mjs /abz/
COPY direct-target-source.test.tsx encryption-source.test.tsx /abz/
COPY Dockerfile inject-mobile-embed.sh mobile-embed.js mobile-embed.css notification-bridge.js sso-callback.html session-vault.html notification-permission.html abz-element-source.html direct-target-*.test.mjs encryption-*.test.mjs /src/abz-distribution/
RUN node /abz/patch-encryption-source.mjs /src \
  && node /abz/patch-direct-target-source.mjs /src \
  && mkdir -p /src/test/unit-tests/abz \
  && cp /abz/direct-target-source.test.tsx /src/test/unit-tests/abz/DirectTarget-test.tsx \
  && cp /abz/encryption-source.test.tsx /src/test/unit-tests/abz/Encryption-test.tsx \
  && cp -R /abz /src/abz-distribution/source-adapter \
  && NODE_OPTIONS=--max-old-space-size=4096 yarn lint:types \
  && yarn jest test/unit-tests/abz/DirectTarget-test.tsx test/unit-tests/abz/Encryption-test.tsx --runInBand \
  && printf '%s\n' '{"upstream":"element-hq/element-web","tag":"v1.11.112","commit":"87d40ab0e0d38a064912ee73ffaa8684cb1e79b1","source":"/abz-element-source.tar.gz","directTargetAuthorization":"v2 correlated one-use server redemption before immutable expiresAt; local commit may follow expiry; ACK requires source-owned selected state","encryptionUi":"eight uniquely asserted source transformations"}' > /abz-element-source.json \
  && tar --exclude=.git --exclude=node_modules --exclude=coverage -czf /abz-element-source.tar.gz . \
  && VERSION=v1.11.112-abz-authorization-v2 yarn build \
  && cp config.sample.json webapp/config.json

FROM vectorim/element-web:v1.11.112@sha256:6e38aeb4d30c6e6fd06db85dc20b07facfdad2de0312f352a1c35d516cdf7367

# Тонкий слой гасит окна заверения и тосты шифрования. Шифрование комнаты не запрещает.
USER root
# Runtime/nginx остаются upstream; весь JS заменён source-built webapp.
RUN rm -rf /app/*
COPY --from=source-builder /src/webapp /app
# AGPL corresponding source включает upstream LICENSE, lockfile и все изменения.
COPY --from=source-builder /abz-element-source.tar.gz /app/abz-element-source.tar.gz
COPY --from=source-builder /abz-element-source.json /app/abz-element-source.json
COPY abz-element-source.html /app/abz-element-source.html
COPY sso-callback.html /app/sso-callback.html
COPY session-vault.html /app/session-vault.html
COPY notification-permission.html /app/notification-permission.html
COPY mobile-embed.css /app/mobile-embed.css
COPY mobile-embed.js /app/mobile-embed.js
COPY notification-bridge.js /app/notification-bridge.js
COPY inject-mobile-embed.sh /tmp/inject-mobile-embed.sh
# Source-built bundle already includes the eight asserted encryption UI changes.
# Rename it as before so an existing chat cannot retain the previous JavaScript.
RUN chmod +x /tmp/inject-mobile-embed.sh \
  && /tmp/inject-mobile-embed.sh \
  && old=$(find /app/bundles -mindepth 1 -maxdepth 1 -type d) \
  && [ "$(printf '%s\n' "$old" | wc -l | tr -d ' ')" = 1 ] \
  && hash=$(basename "$old") \
  && new="${hash}-nags" \
  && find /app -type f \( -name '*.html' -o -name '*.js' -o -name '*.css' -o -name '*.json' \) | while IFS= read -r file; do \
       grep -F -q -- "$hash" "$file" || continue; \
       sed -i "s/${hash}/${new}/g" "$file" || exit 1; \
     done \
  && mv "$old" "/app/bundles/${new}" \
  && rm /tmp/inject-mobile-embed.sh \
  && chown -R nginx:nginx /app/mobile-embed.css /app/mobile-embed.js /app/notification-bridge.js /app/index.html
USER nginx
