@tobycaimf/dsh-archived-sessions 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.js CHANGED
@@ -284,6 +284,17 @@ export function apply(ctx) {
284
284
  return out
285
285
  }
286
286
 
287
+ // Archive (hide) one session: adds its id to the durable archive set so it
288
+ // is dropped out of the sidebar. DSH requires the session to exist (live or
289
+ // persisted) — a genuine miss surfaces as an error.
290
+ async function archiveOne(sid) {
291
+ const state = await archivedState()
292
+ const list = state.archivedSessionIds.map(String)
293
+ if (list.includes(sid)) return { ok: true, archived: false }
294
+ await w.archiveSession(sid)
295
+ return { ok: true, archived: true }
296
+ }
297
+
287
298
  async function allSessionItems() {
288
299
  let materialized = new Set()
289
300
  let live = ctx.get('sessions')
@@ -454,6 +465,43 @@ export function apply(ctx) {
454
465
  },
455
466
  }))
456
467
 
468
+ // Archive (hide) one session.
469
+ disposers.push(ctx.webServer.register({
470
+ kind: 'exact',
471
+ path: '/archived-sessions/archive',
472
+ handler: async (req, res) => {
473
+ try {
474
+ const body = await readJsonBody(req)
475
+ const sid = body && typeof body.sessionId === 'string' ? body.sessionId : null
476
+ if (!sid) return json(res, { ok: false, error: 'missing sessionId' }, 400)
477
+ json(res, { sessionId: sid, ...(await archiveOne(sid)) })
478
+ } catch (e) {
479
+ json(res, { ok: false, error: String((e && e.message) || e) }, 500)
480
+ }
481
+ },
482
+ }))
483
+
484
+ // Archive (hide) many sessions.
485
+ disposers.push(ctx.webServer.register({
486
+ kind: 'exact',
487
+ path: '/archived-sessions/archive-many',
488
+ handler: async (req, res) => {
489
+ try {
490
+ const body = await readJsonBody(req)
491
+ const ids = parseIds(body)
492
+ if (!ids || ids.length === 0) return json(res, { ok: false, error: 'missing sessionIds' }, 400)
493
+ const results = []
494
+ for (const sid of ids) {
495
+ try { results.push({ sessionId: sid, ok: true, ...(await archiveOne(sid)) }) }
496
+ catch (e) { results.push({ sessionId: sid, ok: false, error: String((e && e.message) || e) }) }
497
+ }
498
+ json(res, { ok: true, archived: results.filter((r) => r.ok).length, results })
499
+ } catch (e) {
500
+ json(res, { ok: false, error: String((e && e.message) || e) }, 500)
501
+ }
502
+ },
503
+ }))
504
+
457
505
  return () => { for (const d of disposers) d() }
458
506
  }, 'dsh-archived-sessions: routes')
459
507
  }