Skip to content

Commit 2413e7a

Browse files
committed
fix: provide certificate_ok initial state and fix file action callback signatures for stable32
Signed-off-by: Clément Christiaens <clement.christiaens@xwiki.com>
1 parent a1a657d commit 2413e7a

File tree

5 files changed

+164
-209
lines changed

5 files changed

+164
-209
lines changed

lib/Listener/LoadAdditionalListener.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use OCA\Libresign\AppInfo\Application;
1313
use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory;
1414
use OCP\App\IAppManager;
15+
use OCP\AppFramework\Services\IInitialState;
1516
use OCP\EventDispatcher\Event;
1617
use OCP\EventDispatcher\IEventListener;
1718
use OCP\Util;
@@ -23,6 +24,7 @@ class LoadAdditionalListener implements IEventListener {
2324
public function __construct(
2425
private IAppManager $appManager,
2526
private CertificateEngineFactory $certificateEngineFactory,
27+
private IInitialState $initialState,
2628
) {
2729
}
2830
#[\Override]
@@ -40,6 +42,7 @@ public function handle(Event $event): void {
4042
}
4143

4244
if (class_exists('\OCA\Files\App')) {
45+
$this->initialState->provideInitialState('certificate_ok', true);
4346
Util::addInitScript(Application::APP_ID, 'libresign-init');
4447
}
4548
}

src/actions/openInLibreSignAction.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const action = {
3232
displayName: () => t('libresign', 'Open in LibreSign'),
3333
iconSvgInline: () => SvgIcon,
3434

35-
enabled({ nodes }) {
35+
enabled(nodes) {
3636
const getNodeMime = (node) => node?.mime || node?.mimetype || ''
3737

3838
if (!loadState('libresign', 'certificate_ok', false)) {
@@ -62,8 +62,7 @@ export const action = {
6262
/**
6363
* Single file or folder: open in sidebar
6464
*/
65-
async exec({ nodes }) {
66-
const node = nodes[0]
65+
async exec(node) {
6766
await window.OCA.Files.Sidebar.open(node.path)
6867
window.OCA.Files.Sidebar.setActiveTab('libresign')
6968
return null
@@ -73,11 +72,11 @@ export const action = {
7372
* Multiple files: prepare envelope data and delegate to sidebar
7473
* Similar to exec, but passes multiple files to the sidebar for processing
7574
*/
76-
async execBatch({ nodes }) {
75+
async execBatch(nodes) {
7776
const getNodeFileId = (node) => node?.fileid ?? node?.id
7877

7978
if (nodes.length === 1) {
80-
await this.exec({ nodes })
79+
await this.exec(nodes[0])
8180
return [null]
8281
}
8382

src/actions/showStatusInlineAction.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const getNodeMime = (node) => node?.mime || node?.mimetype || ''
1515
const action = {
1616
id: 'show-status-inline',
1717
displayName: () => '',
18-
title: ({ nodes }) => {
18+
title: (nodes) => {
1919
const node = nodes?.[0]
2020
if (!node || !node.attributes) return ''
2121

@@ -28,14 +28,13 @@ const action = {
2828

2929
return t('libresign', 'original file')
3030
},
31-
exec: async ({ nodes }) => {
32-
const node = nodes?.[0]
31+
exec: async (node) => {
3332
if (!node) return null
3433
window.OCA.Files.Sidebar.open(node.path)
3534
window.OCA.Files.Sidebar.setActiveTab('libresign')
3635
return null
3736
},
38-
iconSvgInline: ({ nodes }) => {
37+
iconSvgInline: (nodes) => {
3938
const node = nodes?.[0]
4039
if (!node || !node.attributes) return ''
4140

@@ -49,7 +48,7 @@ const action = {
4948
return getStatusSvgInline(FILE_STATUS.DRAFT) || ''
5049
},
5150
inline: () => true,
52-
enabled: ({ nodes }) => {
51+
enabled: (nodes) => {
5352
const certificateOk = loadState('libresign', 'certificate_ok')
5453
const allHaveStatus = nodes?.every(node => node.attributes?.['libresign-signature-status'] !== undefined)
5554

src/tests/actions/openInLibreSignAction.spec.ts

Lines changed: 44 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ describe('openInLibreSignAction rules', () => {
9595
order: number
9696
displayName: unknown
9797
iconSvgInline: unknown
98-
enabled: (context: { nodes: unknown }) => boolean
99-
exec: (context: { nodes: unknown }) => Promise<unknown>
100-
execBatch: (context: { nodes: unknown }) => Promise<unknown>
98+
enabled: (nodes: unknown[]) => boolean
99+
exec: (node: unknown) => Promise<unknown>
100+
execBatch: (nodes: unknown[]) => Promise<unknown>
101101
}
102102
let loadState: { mockReturnValue: (value: unknown) => unknown }
103103
let getCapabilities: { mockReturnValue: (value: unknown) => unknown }
@@ -151,19 +151,15 @@ describe('openInLibreSignAction rules', () => {
151151
it('disables action when certificate not configured', () => {
152152
loadState.mockReturnValue(false)
153153

154-
const enabled = action.enabled({
155-
nodes: [{ type: 'file', mime: 'application/pdf' }],
156-
})
154+
const enabled = action.enabled([{ type: 'file', mime: 'application/pdf' }])
157155

158156
expect(enabled).toBe(false)
159157
})
160158

161159
it('enables action when certificate configured', () => {
162160
loadState.mockReturnValue(true)
163161

164-
const enabled = action.enabled({
165-
nodes: [{ type: 'file', mime: 'application/pdf' }],
166-
})
162+
const enabled = action.enabled([{ type: 'file', mime: 'application/pdf' }])
167163

168164
expect(enabled).toBe(true)
169165
})
@@ -175,47 +171,37 @@ describe('openInLibreSignAction rules', () => {
175171
})
176172

177173
it('enables for single PDF file', () => {
178-
const enabled = action.enabled({
179-
nodes: [{ type: 'file', mime: 'application/pdf' }],
180-
})
174+
const enabled = action.enabled([{ type: 'file', mime: 'application/pdf' }])
181175

182176
expect(enabled).toBe(true)
183177
})
184178

185179
it('disables for single non-PDF file', () => {
186-
const enabled = action.enabled({
187-
nodes: [{ type: 'file', mime: 'image/png' }],
188-
})
180+
const enabled = action.enabled([{ type: 'file', mime: 'image/png' }])
189181

190182
expect(enabled).toBe(false)
191183
})
192184

193185
it('enables for single PDF when only mimetype is provided', () => {
194-
const enabled = action.enabled({
195-
nodes: [{ type: 'file', mimetype: 'application/pdf' }],
196-
})
186+
const enabled = action.enabled([{ type: 'file', mimetype: 'application/pdf' }])
197187

198188
expect(enabled).toBe(true)
199189
})
200190

201191
it('enables for folder with signature status', () => {
202-
const enabled = action.enabled({
203-
nodes: [{
204-
type: 'folder',
205-
attributes: { 'libresign-signature-status': 'signed' },
206-
}],
207-
})
192+
const enabled = action.enabled([{
193+
type: 'folder',
194+
attributes: { 'libresign-signature-status': 'signed' },
195+
}])
208196

209197
expect(enabled).toBe(true)
210198
})
211199

212200
it('disables for folder without signature status', () => {
213-
const enabled = action.enabled({
214-
nodes: [{
215-
type: 'folder',
216-
attributes: {},
217-
}],
218-
})
201+
const enabled = action.enabled([{
202+
type: 'folder',
203+
attributes: {},
204+
}])
219205

220206
expect(enabled).toBe(false)
221207
})
@@ -235,12 +221,10 @@ describe('openInLibreSignAction rules', () => {
235221
},
236222
})
237223

238-
const enabled = action.enabled({
239-
nodes: [
240-
{ type: 'file', mime: 'application/pdf' },
241-
{ type: 'file', mime: 'application/pdf' },
242-
],
243-
})
224+
const enabled = action.enabled([
225+
{ type: 'file', mime: 'application/pdf' },
226+
{ type: 'file', mime: 'application/pdf' },
227+
])
244228

245229
expect(enabled).toBe(true)
246230
})
@@ -254,12 +238,10 @@ describe('openInLibreSignAction rules', () => {
254238
},
255239
})
256240

257-
const enabled = action.enabled({
258-
nodes: [
259-
{ type: 'file', mime: 'application/pdf' },
260-
{ type: 'file', mime: 'application/pdf' },
261-
],
262-
})
241+
const enabled = action.enabled([
242+
{ type: 'file', mime: 'application/pdf' },
243+
{ type: 'file', mime: 'application/pdf' },
244+
])
263245

264246
expect(enabled).toBe(false)
265247
})
@@ -273,12 +255,10 @@ describe('openInLibreSignAction rules', () => {
273255
},
274256
})
275257

276-
const enabled = action.enabled({
277-
nodes: [
278-
{ type: 'file', mime: 'application/pdf' },
279-
{ type: 'file', mime: 'image/png' },
280-
],
281-
})
258+
const enabled = action.enabled([
259+
{ type: 'file', mime: 'application/pdf' },
260+
{ type: 'file', mime: 'image/png' },
261+
])
282262

283263
expect(enabled).toBe(false)
284264
})
@@ -291,7 +271,7 @@ describe('openInLibreSignAction rules', () => {
291271

292272
window.OCA.Libresign = {}
293273

294-
await action.execBatch({ nodes })
274+
await action.execBatch(nodes)
295275

296276
const pending = getPendingEnvelope()
297277
expect(pending.files[0].fileId).toBe(999)
@@ -305,17 +285,19 @@ describe('openInLibreSignAction rules', () => {
305285
})
306286

307287
it('disables when nodes array is empty', () => {
308-
const enabled = action.enabled({ nodes: [] })
288+
const enabled = action.enabled([])
309289
expect(enabled).toBe(false)
310290
})
311291

312292
it('disables when nodes is null', () => {
313-
const enabled = action.enabled({ nodes: null })
293+
// @ts-expect-error Testing invalid input
294+
const enabled = action.enabled(null)
314295
expect(enabled).toBe(false)
315296
})
316297

317298
it('disables when nodes is undefined', () => {
318-
const enabled = action.enabled({ nodes: undefined })
299+
// @ts-expect-error Testing invalid input
300+
const enabled = action.enabled(undefined)
319301
expect(enabled).toBe(false)
320302
})
321303
})
@@ -328,7 +310,7 @@ describe('openInLibreSignAction rules', () => {
328310
it('opens sidebar with file', async () => {
329311
const node = { type: 'file', mime: 'application/pdf', fileid: 123, path: '/test.pdf' }
330312

331-
await action.exec({ nodes: [node] })
313+
await action.exec(node)
332314

333315
expect(mockSidebar.open).toHaveBeenCalledWith('/test.pdf')
334316
expect(mockSidebar.setActiveTab).toHaveBeenCalledWith('libresign')
@@ -342,14 +324,14 @@ describe('openInLibreSignAction rules', () => {
342324
path: '/Documents',
343325
}
344326

345-
await action.exec({ nodes: [node] })
327+
await action.exec(node)
346328

347329
expect(mockSidebar.open).toHaveBeenCalledWith('/Documents')
348330
})
349331

350332
it('returns null after execution', async () => {
351333
const node = { type: 'file', mime: 'application/pdf', path: '/test.pdf' }
352-
const result = await action.exec({ nodes: [node] })
334+
const result = await action.exec(node)
353335

354336
expect(result).toBeNull()
355337
})
@@ -398,7 +380,7 @@ let spawnDialog: typeof import('@nextcloud/vue/functions/dialog').spawnDialog
398380
{ type: 'file', mime: 'application/pdf', fileid: 2, dirname: '/Docs' },
399381
]
400382

401-
const result = await action.execBatch({ nodes })
383+
const result = await action.execBatch(nodes)
402384

403385
expect(result).toEqual([null, null])
404386
})
@@ -411,7 +393,7 @@ let spawnDialog: typeof import('@nextcloud/vue/functions/dialog').spawnDialog
411393

412394
window.OCA = { Libresign: {}, Files: { Sidebar: mockSidebar as OCAFilesSidebar } }
413395

414-
await action.execBatch({ nodes })
396+
await action.execBatch(nodes)
415397

416398
expect(mockSidebar.open).toHaveBeenCalled()
417399
expect(mockSidebar.setActiveTab).toHaveBeenCalledWith('libresign')
@@ -425,7 +407,7 @@ let spawnDialog: typeof import('@nextcloud/vue/functions/dialog').spawnDialog
425407

426408
window.OCA = { Libresign: {}, Files: { Sidebar: mockSidebar as OCAFilesSidebar } }
427409

428-
await action.execBatch({ nodes })
410+
await action.execBatch(nodes)
429411

430412
const pending = getPendingEnvelope()
431413
expect(pending.nodeType).toBe('envelope')
@@ -442,7 +424,7 @@ let spawnDialog: typeof import('@nextcloud/vue/functions/dialog').spawnDialog
442424

443425
window.OCA = { Libresign: {}, Files: { Sidebar: mockSidebar as OCAFilesSidebar } }
444426

445-
await action.execBatch({ nodes })
427+
await action.execBatch(nodes)
446428

447429
const pending = getPendingEnvelope()
448430
expect(pending.settings.path).toBe('/Documents/Contracts/Test Envelope')
@@ -456,7 +438,7 @@ let spawnDialog: typeof import('@nextcloud/vue/functions/dialog').spawnDialog
456438

457439
window.OCA = { Libresign: {}, Files: { Sidebar: mockSidebar as OCAFilesSidebar } }
458440

459-
await action.execBatch({ nodes })
441+
await action.execBatch(nodes)
460442

461443
const pending = getPendingEnvelope()
462444
expect(pending.settings.path).toBe('/Test Envelope')
@@ -470,7 +452,7 @@ let spawnDialog: typeof import('@nextcloud/vue/functions/dialog').spawnDialog
470452

471453
window.OCA = { Libresign: {}, Files: { Sidebar: mockSidebar as OCAFilesSidebar } }
472454

473-
await action.execBatch({ nodes })
455+
await action.execBatch(nodes)
474456

475457
const pending = getPendingEnvelope()
476458
expect(pending.settings.path).toBe('/Docs/Test Envelope')
@@ -498,7 +480,7 @@ let spawnDialog: typeof import('@nextcloud/vue/functions/dialog').spawnDialog
498480

499481
window.OCA = { Libresign: {}, Files: { Sidebar: mockSidebar as OCAFilesSidebar } }
500482

501-
await action.execBatch({ nodes })
483+
await action.execBatch(nodes)
502484

503485
const pending = getPendingEnvelope()
504486
expect(pending.files[0].fileId).toBe(123)

0 commit comments

Comments
 (0)