From 24eca60e59ee2d578c938c3226f228b2f72b7437 Mon Sep 17 00:00:00 2001 From: Hoang Pham Date: Wed, 4 Nov 2026 15:01:31 +0700 Subject: [PATCH] feat(comparison): summarize the changes between two versions The comparison in Files and Collectives can now summarize the changes between the two versions shown, with the instance's AI provider. "Summarize changes" sits in the comparison's header, on the row of Previous and Next, and says that both complete versions are sent to the AI provider; nothing starts without a click. The summary shows its place in the queue, then a card labelled as AI-generated, with both versions, the gist and the changes grouped by meaning, Copy and collapse. A stored summary of the same two texts shows at once for anyone who opens them. Side by side it spans both documents; in the list of changes it sits above the list. Every state that stops a summary says why and what to do, and a server that does not answer is asked again. The comparison loads both files as bytes, so the texts it fingerprints are the ones the server reads; it fingerprints them only for a summary. The summary code is a separate chunk, loaded only where summaries are on, which pages learn from initial state. The Files sidebar gets "Summarize recent changes": it opens the list of changes from the newest version at least an hour older than the latest change, or the oldest kept one, ready to summarize. Resolves #8904 Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Hoang Pham --- lib/Listeners/LoadViewerListener.php | 15 + playwright/e2e/version-summaries.spec.ts | 421 ++++++++++++++++ src/comparison/decode.ts | 74 +++ src/comparison/session.ts | 1 + src/comparison/sharedComparison.ts | 33 +- src/comparison/versions.ts | 6 +- src/components/VersionSummary.vue | 467 ++++++++++++++++++ src/components/VersionSummaryStart.vue | 101 ++++ src/components/ViewerVersionComparison.vue | 153 +++++- src/init.js | 3 + src/tests/comparison/decode.spec.ts | 73 +++ src/tests/comparison/sharedComparison.spec.ts | 22 +- src/tests/components/VersionSummary.spec.ts | 113 +++++ .../components/VersionSummaryStart.spec.ts | 53 ++ .../ViewerVersionComparison.spec.ts | 70 ++- src/tests/versionSummary/copy.spec.ts | 38 ++ src/tests/versionSummary/format.spec.ts | 56 +++ src/tests/versionSummary/sidebarEntry.spec.ts | 109 ++++ src/tests/versionSummary/states.spec.ts | 69 +++ src/tests/versionSummary/summaries.spec.ts | 446 +++++++++++++++++ src/versionSummary/copy.ts | 37 ++ src/versionSummary/format.ts | 60 +++ src/versionSummary/sidebarEntry.ts | 80 +++ src/versionSummary/states.ts | 257 ++++++++++ src/versionSummary/summaries.ts | 308 ++++++++++++ src/versionSummary/summarizeRecentChanges.ts | 68 +++ .../unit/Listeners/LoadViewerListenerTest.php | 60 +++ 27 files changed, 3135 insertions(+), 58 deletions(-) create mode 100644 playwright/e2e/version-summaries.spec.ts create mode 100644 src/comparison/decode.ts create mode 100644 src/components/VersionSummary.vue create mode 100644 src/components/VersionSummaryStart.vue create mode 100644 src/tests/comparison/decode.spec.ts create mode 100644 src/tests/components/VersionSummary.spec.ts create mode 100644 src/tests/components/VersionSummaryStart.spec.ts create mode 100644 src/tests/versionSummary/copy.spec.ts create mode 100644 src/tests/versionSummary/format.spec.ts create mode 100644 src/tests/versionSummary/sidebarEntry.spec.ts create mode 100644 src/tests/versionSummary/states.spec.ts create mode 100644 src/tests/versionSummary/summaries.spec.ts create mode 100644 src/versionSummary/copy.ts create mode 100644 src/versionSummary/format.ts create mode 100644 src/versionSummary/sidebarEntry.ts create mode 100644 src/versionSummary/states.ts create mode 100644 src/versionSummary/summaries.ts create mode 100644 src/versionSummary/summarizeRecentChanges.ts create mode 100644 tests/unit/Listeners/LoadViewerListenerTest.php diff --git a/lib/Listeners/LoadViewerListener.php b/lib/Listeners/LoadViewerListener.php index 4a9fe962f3a..3ec71beba71 100644 --- a/lib/Listeners/LoadViewerListener.php +++ b/lib/Listeners/LoadViewerListener.php @@ -8,14 +8,24 @@ namespace OCA\Text\Listeners; +use OCA\Text\Service\ConfigService; use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; use OCP\AppFramework\Http\TemplateResponse; +use OCP\AppFramework\Services\IInitialState; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; +use OCP\IUserSession; use OCP\Util; /** @implements IEventListener */ class LoadViewerListener implements IEventListener { + public function __construct( + private readonly IInitialState $initialState, + private readonly IUserSession $userSession, + private readonly ConfigService $configService, + ) { + } + public function handle(Event $event): void { if (!($event instanceof BeforeTemplateRenderedEvent)) { return; @@ -27,5 +37,10 @@ public function handle(Event $event): void { // Only registers the handler: the editor loads when a text file opens Util::addInitScript('text', 'text-viewer'); + // Whether comparisons offer summaries, known without a request + $this->initialState->provideLazyInitialState( + 'version_summaries', + fn () => $this->configService->isVersionSummariesAvailableFor($this->userSession->getUser()), + ); } } diff --git a/playwright/e2e/version-summaries.spec.ts b/playwright/e2e/version-summaries.spec.ts new file mode 100644 index 00000000000..5794f40c920 --- /dev/null +++ b/playwright/e2e/version-summaries.spec.ts @@ -0,0 +1,421 @@ +/** + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +import type { Page, Request } from '@playwright/test' +import type { ViewerSection } from '../support/sections/ViewerSection.ts' + +import { runOcc } from '@nextcloud/e2e-test-server/docker' +import { createRandomUser, login } from '@nextcloud/e2e-test-server/playwright' +import { expect, mergeTests, request } from '@playwright/test' +import { test as randomUserTest } from '../support/fixtures/random-user.ts' +import { test as uploadFileTest } from '../support/fixtures/upload-file.ts' + +const test = mergeTests(randomUserTest, uploadFileTest) + +// Core's testing provider answers with this, then the input it got +const SUMMARY = /^This is a fake response message:/ +const now = Math.floor(Date.now() / 1000) + +async function providerFails(fail: boolean) { + await runOcc(['config:app:set', 'testing', 'fail-testing-text2textchat', '--type=boolean', `--value=${fail}`]) +} + +// As an admin does, so the web server sees it at once +async function turnSummaries(enabled: boolean) { + const admin = await request.newContext({ + baseURL: process.env.baseURL ?? 'http://localhost:8089/index.php/', + extraHTTPHeaders: { 'OCS-APIRequest': 'true', Authorization: 'Basic ' + btoa('admin:admin') }, + }) + await admin.put('../ocs/v2.php/apps/text/settings/version-summaries', { data: { enabled }, failOnStatusCode: true }) + await admin.dispose() +} + +async function processTasks() { + await runOcc(['taskprocessing:worker', '--timeout=4', '--interval=1']) +} + +function countSummaryRequests(page: Page) { + const requests: string[] = [] + page.on('request', (request: Request) => { + if (request.url().includes('/apps/text/version-summaries/')) { + requests.push(request.method()) + } + }) + return { + lookups: () => requests.filter((method) => method === 'GET').length, + starts: () => requests.filter((method) => method === 'POST').length, + } +} + +async function compareToCurrent(page: Page, viewer: ViewerSection, index = 1) { + const versions = await viewer.openSidebarTab('Versions') + await versions.getByRole('button').nth(index).click() + await page.getByRole('menuitem', { name: 'Compare to current version' }).click() + await expect(page.locator('[data-text-version-summary]')).toBeAttached() +} + +const summarize = (page: Page) => page.getByRole('button', { name: 'Summarize changes' }) +const summaryText = (page: Page) => page.locator('[data-text-version-summary-text]') +// Beside the button in the header, or below it where the header has no room +const disclosure = (page: Page) => page.getByText('Both complete versions are sent to the AI provider of this Nextcloud.').filter({ visible: true }) +const underWay = (page: Page) => page.locator('.text-version-summary__title', { hasText: 'Summarizing changes…' }) + +// What the suite changes, as it was before, to put back afterwards +const CHANGED = [['text', 'version_summaries_hourly_limit'], ['text', 'version_summaries_user_hourly_limit'], ['core', 'ai.taskprocessing_provider_preferences']] as const +const before: { testing: boolean, enabled: boolean, values: (string | null)[] } = { testing: false, enabled: false, values: [] } + +test.beforeAll(async () => { + const { stdout: apps } = await runOcc(['app:list', '--enabled', '--output=json']) + before.testing = 'testing' in (JSON.parse(apps).enabled ?? {}) + const enabled = await runOcc(['config:app:get', 'text', 'version_summaries_enabled'], { failOnError: false }) + before.enabled = enabled.exitCode === 0 && ['1', 'true'].includes(enabled.stdout.trim()) + before.values = await Promise.all(CHANGED.map(async ([app, key]) => { + const value = await runOcc(['config:app:get', app, key], { failOnError: false }) + return value.exitCode === 0 ? value.stdout.trim() : null + })) + + // Only here: its providers add the assistant to every editor's menu + await runOcc(['app:enable', 'testing']) + // More summaries start here in an hour than the default allows + await runOcc(['config:app:set', 'text', 'version_summaries_hourly_limit', '--type=integer', '--value=1000']) + await runOcc(['config:app:set', 'text', 'version_summaries_user_hourly_limit', '--type=integer', '--value=1000']) + await turnSummaries(true) + await runOcc(['config:app:set', 'core', 'ai.taskprocessing_provider_preferences', '--value={"core:text2text:chat":"testing-text2textchat"}']) +}) + +test.afterAll(async () => { + // Every step on its own, so one that fails leaves none of the others as the suite set them + const steps = [ + () => turnSummaries(before.enabled), + ...CHANGED.map(([app, key], index) => () => before.values[index] === null + ? runOcc(['config:app:delete', app, key]) + : runOcc(['config:app:set', app, key, `--value=${before.values[index]}`])), + () => (before.testing ? Promise.resolve() : runOcc(['app:disable', 'testing'])), + ] + const failures: unknown[] = [] + for (const step of steps) { + await step().catch((error: unknown) => failures.push(error)) + } + if (failures.length > 0) { + throw failures[0] + } +}) + +test.beforeEach(async () => { + await providerFails(false) + await processTasks() +}) + +test.describe('Summaries of changes in Files', () => { + test.use({ + fileName: 'plan.md', + fileContent: '# Plan\n\nThe pilot runs 3 weeks.', + oldVersions: [[{ content: '# Plan\n\nThe pilot runs 2 weeks.', mtime: now - 7200 }], { scope: 'test' }], + }) + + test('summarizes the compared versions on request, once', async ({ open, page, viewer }) => { + const count = countSummaryRequests(page) + await open() + await compareToCurrent(page, viewer) + await expect(summarize(page)).toBeEnabled() + await expect(disclosure(page)).toBeVisible() + // Opening asks once and starts nothing + expect([count.lookups(), count.starts()]).toEqual([1, 0]) + + await summarize(page).click() + await expect(underWay(page)).toBeVisible() + await expect(summarize(page)).toHaveCount(0) + await processTasks() + await expect(summaryText(page)).toHaveText(SUMMARY) + await expect(page.getByText('AI-generated, may omit or misstate changes')).toBeVisible() + expect(count.starts()).toBe(1) + + // Switching the view and the filter asks for nothing + const lookups = count.lookups() + await page.keyboard.press('d') + await expect(summaryText(page)).toHaveText(SUMMARY) + await page.keyboard.press('d') + await expect(summaryText(page)).toHaveText(SUMMARY) + await page.waitForTimeout(500) + expect([count.lookups(), count.starts()]).toEqual([lookups, 1]) + }) + + test('collapses the summary and copies it with its label', async ({ open, page, viewer }) => { + await page.context().grantPermissions(['clipboard-read', 'clipboard-write']) + await open() + await compareToCurrent(page, viewer) + await summarize(page).click() + await processTasks() + await expect(summaryText(page)).toHaveText(SUMMARY) + + await page.getByRole('button', { name: 'Copy summary' }).click() + await expect(page.getByRole('button', { name: 'Copied' })).toBeVisible() + const copied = await page.evaluate(() => navigator.clipboard.readText()) + expect(copied).toMatch(/^AI summary of changes in plan\.md, From .* to the current version \(.*\) \(AI-generated, may omit or misstate changes\):\n\nThis is a fake response message/) + + const toggle = page.locator('.text-version-summary__actions button[aria-expanded]') + await toggle.focus() + await page.keyboard.press('Enter') + await expect(toggle).toHaveAttribute('aria-expanded', 'false') + await expect(summaryText(page)).toBeHidden() + await page.keyboard.press('Space') + await expect(toggle).toHaveAttribute('aria-expanded', 'true') + }) + + test('keeps a summary under way when the comparison closes and opens again', async ({ open, page, viewer }) => { + const count = countSummaryRequests(page) + await open() + await compareToCurrent(page, viewer) + await summarize(page).click() + await expect(underWay(page)).toBeVisible() + await page.locator('.viewer__comparison').locator('xpath=ancestor::*[@role="dialog"][1]').getByRole('button', { name: 'Close', exact: true }).first().click() + + await page.reload() + await open() + await compareToCurrent(page, viewer) + await expect(underWay(page)).toBeVisible() + await processTasks() + await expect(summaryText(page)).toHaveText(SUMMARY) + expect(count.starts()).toBe(1) + }) + + test('says what failed, and tries again on request', async ({ open, page, viewer }) => { + await providerFails(true) + await open() + await compareToCurrent(page, viewer) + await summarize(page).click() + await processTasks() + await expect(page.locator('.text-version-summary__note')).toContainText('The summary could not be created.') + const tryAgain = page.getByRole('button', { name: 'Try again' }) + await expect(tryAgain).toBeFocused() + + await providerFails(false) + await tryAgain.click() + await processTasks() + await expect(summaryText(page)).toHaveText(SUMMARY) + }) + + test('refuses texts that changed after the comparison opened, and reloads them', async ({ open, page, user, viewer }) => { + await open() + await compareToCurrent(page, viewer) + await user.uploadFile({ name: 'plan.md', content: '# Plan\n\nThe pilot runs 4 weeks.', mtime: now + 10 }) + + await summarize(page).click() + await expect(page.locator('.text-version-summary__note')).toContainText('This file changed after you opened the comparison.') + await page.getByRole('button', { name: 'Reload comparison' }).click() + await expect(page.getByRole('article', { name: 'After' }).getByText('4 weeks')).toBeVisible() + await expect(summarize(page)).toBeEnabled() + }) + + test('shows a stored summary to everyone who can open both versions, at once', async ({ browser, baseURL, open, page, viewer }) => { + await open() + await compareToCurrent(page, viewer) + await summarize(page).click() + await processTasks() + await expect(summaryText(page)).toHaveText(SUMMARY) + + const reader = await createRandomUser() + await page.request.post('/ocs/v2.php/apps/files_sharing/api/v1/shares', { + data: { path: '/plan.md', shareType: 0, shareWith: reader.userId, permissions: 1 }, + headers: { Accept: 'application/json', 'OCS-APIRequest': 'true' }, + failOnStatusCode: true, + }) + const readerPage = await browser.newPage({ storageState: undefined, baseURL }) + await login(readerPage.request, reader) + const count = countSummaryRequests(readerPage) + await readerPage.goto('apps/files') + await readerPage.locator('[data-cy-files-list-row-name="plan.md"]').click({ button: 'right' }) + await readerPage.getByRole('menuitem', { name: 'Details' }).click() + await readerPage.getByRole('tab', { name: 'Versions' }).click() + await readerPage.getByRole('tabpanel', { name: 'Versions' }).getByRole('button').nth(1).click() + await readerPage.getByRole('menuitem', { name: 'Compare to current version' }).click() + + await expect(summaryText(readerPage)).toHaveText(SUMMARY) + expect([count.lookups(), count.starts()]).toEqual([1, 0]) + await readerPage.close() + }) +}) + +test.describe('A file written by another app', () => { + test.use({ + fileName: 'elsewhere.md', + // Text saves it without the final line break when its editor closes + fileContent: '# Plan\n\nThe pilot runs 3 weeks.\n', + oldVersions: [[{ content: '# Plan\n\nThe pilot runs 2 weeks.\n', mtime: now - 7200 }], { scope: 'test' }], + }) + + test('compares the file as the closing editor saved it', async ({ open, page, viewer }) => { + await open() + await expect(viewer.el.locator('.ProseMirror')).toContainText('The pilot runs 3 weeks') + await compareToCurrent(page, viewer) + + await expect(summarize(page)).toBeEnabled() + await expect(page.getByText('This file changed after you opened the comparison.')).toHaveCount(0) + }) +}) + +test.describe('The setting', () => { + test('cannot be changed by someone who is not an administrator', async ({ page }) => { + const response = await page.request.put('../ocs/v2.php/apps/text/settings/version-summaries', { + data: { enabled: false }, + headers: { 'OCS-APIRequest': 'true' }, + }) + + expect(response.status()).toBe(403) + const enabled = await runOcc(['config:app:get', 'text', 'version_summaries_enabled']) + expect(['1', 'true']).toContain(enabled.stdout.trim()) + }) +}) + +test.describe('Versions that look the same', () => { + test.use({ + fileName: 'spacing.md', + fileContent: '# Plan\n\nThe pilot runs 3 weeks.', + oldVersions: [[{ content: '# Plan\n\n\nThe pilot runs 3 weeks.\n', mtime: now - 7200 }], { scope: 'test' }], + }) + + test('offers no summary', async ({ open, page, viewer }) => { + await open() + await compareToCurrent(page, viewer) + + await expect(page.locator('.text-version-summary-start__note')).toHaveText('The two versions look the same. Only their Markdown source differs.') + await expect(summarize(page)).toHaveCount(0) + }) +}) + +test.describe('Summaries of each step', () => { + test.use({ + fileName: 'steps.md', + fileContent: '# Plan\n\nThe pilot runs 4 weeks.', + oldVersions: [[ + { content: '# Plan\n\nThe pilot runs 2 weeks.', mtime: now - 7200 }, + { content: '# Plan\n\nThe pilot runs 3 weeks.', mtime: now - 1800 }, + ], { scope: 'test' }], + }) + + test('summarizes another pair after changing an end', async ({ open, page, viewer }) => { + const count = countSummaryRequests(page) + await open() + await compareToCurrent(page, viewer, 2) + await summarize(page).click() + await processTasks() + // Core's testing provider answers with the texts it got + await expect(summaryText(page)).toContainText('The pilot runs 2 weeks.') + + // Changing the pair asks about the new pair and starts nothing + await page.locator('[data-comparison-picker="from"]').getByRole('button').click() + await page.getByRole('menuitemradio').nth(1).click() + await expect(summarize(page)).toBeEnabled() + expect(count.starts()).toBe(1) + await summarize(page).click() + await processTasks() + await expect(summaryText(page)).toContainText('The pilot runs 3 weeks.') + await expect(summaryText(page)).not.toContainText('The pilot runs 2 weeks.') + expect(count.starts()).toBe(2) + }) + + test('summarizes the recent changes from the Files sidebar', async ({ open, page, viewer }) => { + const count = countSummaryRequests(page) + await open() + await viewer.openSidebarTab('Versions') + await page.locator('.app-sidebar-header').getByRole('button', { name: 'Actions' }).click() + await page.getByRole('menuitem', { name: 'Summarize recent changes' }).click() + + // From the newest version at least an hour older: the one of two hours ago + await expect(page.locator('[data-comparison-select]')).toHaveCount(1) + await expect(page.locator('[data-comparison-select] del')).toHaveText('2') + await expect(page.locator('[data-comparison-picker="from"]')).toBeVisible() + // Nothing is sent before the user saw what is sent + await expect(disclosure(page)).toBeVisible() + expect(count.starts()).toBe(0) + await summarize(page).click() + await processTasks() + await expect(summaryText(page)).toHaveText(SUMMARY) + expect(count.starts()).toBe(1) + }) +}) + +test.describe('A file without older versions', () => { + test.use({ fileName: 'new.md', fileContent: '# New' }) + + test('says there is nothing to summarize yet', async ({ open, page, viewer }) => { + await open() + await viewer.openSidebarTab('Versions') + await page.locator('.app-sidebar-header').getByRole('button', { name: 'Actions' }).click() + await page.getByRole('menuitem', { name: 'Summarize recent changes' }).click() + + await expect(page.getByText('This file has no earlier versions yet.').first()).toBeVisible() + }) +}) + +test.describe('On a phone', () => { + test.use({ + viewport: { width: 390, height: 844 }, + fileName: 'phone.md', + fileContent: '# Plan\n\nThe pilot runs 3 weeks.', + oldVersions: [[{ content: '# Plan\n\nThe pilot runs 2 weeks.', mtime: now - 7200 }], { scope: 'test' }], + }) + + test('summarizes the recent changes from the Files sidebar', async ({ file, page }) => { + await page.goto('apps/files') + await page.locator(`[data-cy-files-list-row-name="${file.name}"]`).getByRole('button', { name: 'Actions' }).click() + await page.getByRole('menuitem', { name: 'Details' }).click() + await page.locator('.app-sidebar-header').getByRole('button', { name: 'Actions' }).click() + await page.getByRole('menuitem', { name: 'Summarize recent changes' }).click() + + await expect(page.locator('[data-comparison-picker]')).toHaveCount(2) + await summarize(page).click() + await processTasks() + await expect(summaryText(page)).toHaveText(SUMMARY) + await expect(summaryText(page)).toBeInViewport() + }) +}) + +test.describe('Without a chat provider', () => { + test.use({ + fileName: 'plan.md', + fileContent: '# Plan\n\nThe pilot runs 3 weeks.', + oldVersions: [[{ content: '# Plan\n\nThe pilot runs 2 weeks.', mtime: now - 7200 }], { scope: 'test' }], + }) + test.afterEach(async () => { + await runOcc(['app:enable', 'testing']) + }) + + test('says why summaries cannot be made', async ({ open, page, viewer }) => { + await runOcc(['app:disable', 'testing']) + await open() + await compareToCurrent(page, viewer) + await expect(summarize(page)).toBeDisabled() + await expect(page.locator('.text-version-summary-start__note')).toHaveText('Summaries are not available: no AI provider is set up. Ask your administrator.') + }) +}) + +test.describe('With summaries turned off', () => { + test.use({ + fileName: 'off.md', + fileContent: '# Plan\n\nThe pilot runs 3 weeks.', + oldVersions: [[{ content: '# Plan\n\nThe pilot runs 2 weeks.', mtime: now - 7200 }], { scope: 'test' }], + }) + + test.afterEach(async () => { + await turnSummaries(true) + }) + + test('loads and asks for nothing', async ({ open, page, viewer }) => { + await turnSummaries(false) + const loaded: string[] = [] + page.on('request', (request) => loaded.push(request.url())) + await open() + const versions = await viewer.openSidebarTab('Versions') + await versions.getByRole('button').nth(1).click() + await page.getByRole('menuitem', { name: 'Compare to current version' }).click() + await expect(page.getByRole('article', { name: 'After' })).toBeVisible() + + expect(loaded.filter((url) => url.includes('VersionSummary') || url.includes('version-summaries')).map((url) => url.split('/').pop())).toEqual([]) + await expect(page.locator('[data-text-version-summary]')).toHaveCount(0) + await page.locator('.app-sidebar-header').getByRole('button', { name: 'Actions' }).click() + await expect(page.getByRole('menuitem', { name: 'Summarize recent changes' })).toHaveCount(0) + }) +}) diff --git a/src/comparison/decode.ts b/src/comparison/decode.ts new file mode 100644 index 00000000000..ee316d07469 --- /dev/null +++ b/src/comparison/decode.ts @@ -0,0 +1,74 @@ +/** + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +import { toHexString } from 'lib0/buffer' +import { digest } from 'lib0/hash/sha256' + +/** What the comparison knows of one end beyond its text */ +export interface LoadedText { + text: string + /** Hex SHA-256 of the raw bytes, as the server computes it */ + sha256: string + /** Whether the bytes are valid UTF-8, the only encoding summaries take */ + utf8: boolean + /** When the server last changed it, in seconds, if it said */ + modified?: number +} + +/** + * Read a fetched file the way a browser reads a text response: a byte order + * mark decides the encoding, then the charset of the response, then UTF-8. + * + * @param bytes The raw bytes. + * @param contentType The `Content-Type` of the response. + */ +export function decodeText(bytes: Uint8Array, contentType?: string): string { + const boms: [number[], string][] = [[[0xEF, 0xBB, 0xBF], 'utf-8'], [[0xFE, 0xFF], 'utf-16be'], [[0xFF, 0xFE], 'utf-16le']] + for (const [bom, encoding] of boms) { + if (bom.every((byte, i) => bytes[i] === byte)) { + return new TextDecoder(encoding, { ignoreBOM: true }).decode(bytes.subarray(bom.length)) + } + } + const charset = contentType?.match(/charset=["']?([^;"'\s]+)/i)?.[1] + try { + return new TextDecoder(charset ?? 'utf-8', { ignoreBOM: true }).decode(bytes) + } catch { + // An unknown label + return new TextDecoder('utf-8', { ignoreBOM: true }).decode(bytes) + } +} + +/** + * The text of a fetched file and what identifies it. + * + * @param buffer The raw response. + * @param headers The response headers. + */ +export function loadText(buffer: ArrayBuffer, headers: Record = {}): LoadedText { + const bytes = new Uint8Array(buffer) + const lastModified = Date.parse(String(headers['last-modified'] ?? '')) + // Worked out only for a summary, which most comparisons never ask for + let sha256: string | undefined + let utf8: boolean | undefined + return { + text: decodeText(bytes, headers['content-type'] as string | undefined), + get sha256() { + sha256 ??= toHexString(digest(bytes)) + return sha256 + }, + get utf8() { + if (utf8 === undefined) { + try { + new TextDecoder('utf-8', { fatal: true }).decode(bytes) + utf8 = true + } catch { + utf8 = false + } + } + return utf8 + }, + modified: Number.isNaN(lastModified) ? undefined : lastModified / 1000, + } +} diff --git a/src/comparison/session.ts b/src/comparison/session.ts index 889ec58b53f..016a2bb18e0 100644 --- a/src/comparison/session.ts +++ b/src/comparison/session.ts @@ -278,6 +278,7 @@ export function createComparisonSession(beforeContent: string, afterContent: str editors, labels, legends, + changeCount: changes.length, formattingCount: changes.filter(({ category }) => category === 'formatting').length, ...state, select, diff --git a/src/comparison/sharedComparison.ts b/src/comparison/sharedComparison.ts index ab4aa987164..094b9d83d26 100644 --- a/src/comparison/sharedComparison.ts +++ b/src/comparison/sharedComparison.ts @@ -5,23 +5,18 @@ import type { IFile } from '@nextcloud/files' import type { Ref, ShallowRef } from 'vue' +import type { LoadedText } from './decode.ts' import type { ComparisonSession } from './session.ts' import axios from '@nextcloud/axios' import { nextTick, onScopeDispose, ref, shallowRef } from 'vue' import { closingSaveOf } from '../helpers/closingSaves.ts' import { logger } from '../helpers/logger.ts' +import { loadText } from './decode.ts' import { createComparisonSession } from './session.ts' import { comparedEnds, endOf, isCurrent } from './versionEnds.ts' import { useFileVersions } from './versions.ts' -/** A file as it was loaded */ -interface LoadedText { - text: string - /** When the server last changed it, in seconds, if it said */ - modified?: number -} - /** Both texts compared, as they were loaded */ export interface ComparedTexts { before: LoadedText @@ -39,6 +34,10 @@ interface SharedComparison { error: Ref /** Whether the error is that the editor it replaces could not save */ unsaved: Ref + /** Height of the summary in one pane, which the other pane leaves free so both documents start level */ + summaryHeight: Ref + /** The newer document's pane side by side, which the summary spans too */ + afterPane: ShallowRef /** Etag of the newer file as loaded */ etag?: string } @@ -70,7 +69,7 @@ export function useSharedComparison(base: IFile, file: IFile) { comparison = undefined } if (!comparison) { - comparison = { users: 0, loads: 0, session: shallowRef(), texts: shallowRef(), path: ref(), error: ref(false), unsaved: ref(false) } + comparison = { users: 0, loads: 0, session: shallowRef(), texts: shallowRef(), path: ref(), error: ref(false), unsaved: ref(false), summaryHeight: ref(0), afterPane: shallowRef() } comparisons.set(key, comparison) load(key, comparison, base, file) } @@ -105,7 +104,7 @@ export function useSharedComparison(base: IFile, file: IFile) { load(key, shared, base, file) } - return { session: shared.session, texts: shared.texts, path: shared.path, error: shared.error, unsaved: shared.unsaved, reload } + return { session: shared.session, texts: shared.texts, path: shared.path, error: shared.error, unsaved: shared.unsaved, summaryHeight: shared.summaryHeight, afterPane: shared.afterPane, reload } } /** @@ -134,16 +133,19 @@ async function load(key: string, comparison: SharedComparison, base: IFile, file return } // Files are served without caching rules, so browsers may reuse an old copy - const options = { responseType: 'text', headers: { 'Cache-Control': 'no-cache' }, timeout: 120_000 } as const + const options = { responseType: 'arraybuffer', headers: { 'Cache-Control': 'no-cache' }, timeout: 120_000 } as const const [before, after, path] = await Promise.all([ - axios.get(base.encodedSource, options), - axios.get(file.encodedSource, options), + axios.get(base.encodedSource, options), + axios.get(file.encodedSource, options), documentPath(base, file), ]) if (stale()) { return } - const texts = { before: loaded(before), after: loaded(after) } + const texts = { + before: loadText(before.data, before.headers), + after: loadText(after.data, after.headers), + } comparison.path.value = path comparison.texts.value = texts comparison.etag = etagOf(after.headers.etag) @@ -157,11 +159,6 @@ async function load(key: string, comparison: SharedComparison, base: IFile, file } } -function loaded(response: { data: string, headers: Record }): LoadedText { - const modified = Date.parse(String(response.headers['last-modified'] ?? '')) - return { text: response.data, modified: Number.isNaN(modified) ? undefined : modified / 1000 } -} - /** * Path of the file compared, as the current file has it: the node of an old * version has only its revision for a path. diff --git a/src/comparison/versions.ts b/src/comparison/versions.ts index 1e31c02d0f2..20eaf496e7a 100644 --- a/src/comparison/versions.ts +++ b/src/comparison/versions.ts @@ -17,6 +17,8 @@ export interface FileVersions { versions: Ref /** Path of the file in the user's files */ path: Ref + /** Whether this Nextcloud keeps versions at all */ + available: Ref failed: Ref /** Fetch them, unless they are here; resolves once they are, or the fetch failed */ load: () => Promise @@ -67,6 +69,7 @@ export function invalidateFileVersions(fileId: number) { function createFileVersions(fileId: number): CachedVersions { const versions = ref() const path = ref() + const available = ref(true) const failed = ref(false) /** Counts invalidations: a fetch started before one is stale */ let generation = 0 @@ -89,6 +92,7 @@ function createFileVersions(fileId: number): CachedVersions { if (started === generation) { versions.value = data.ocs.data.versions path.value = data.ocs.data.path + available.value = data.ocs.data.versionsAvailable } } catch (exception) { if (started === generation) { @@ -114,5 +118,5 @@ function createFileVersions(fileId: number): CachedVersions { return load() } - return { versions, path, failed, load, refresh, invalidate } + return { versions, path, available, failed, load, refresh, invalidate } } diff --git a/src/components/VersionSummary.vue b/src/components/VersionSummary.vue new file mode 100644 index 00000000000..de2ee5f1904 --- /dev/null +++ b/src/components/VersionSummary.vue @@ -0,0 +1,467 @@ + + + + + + + diff --git a/src/components/VersionSummaryStart.vue b/src/components/VersionSummaryStart.vue new file mode 100644 index 00000000000..d1eecf0f9de --- /dev/null +++ b/src/components/VersionSummaryStart.vue @@ -0,0 +1,101 @@ + + + + + + + diff --git a/src/components/ViewerVersionComparison.vue b/src/components/ViewerVersionComparison.vue index 875c9fa6f84..d50f2bfdbc7 100644 --- a/src/components/ViewerVersionComparison.vue +++ b/src/components/ViewerVersionComparison.vue @@ -5,9 +5,11 @@