adapt-authoring-content 3.3.0 → 3.4.0

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.
@@ -2,7 +2,7 @@ import { AbstractApiModule } from 'adapt-authoring-api'
2
2
  import { Hook, stringifyValues } from 'adapt-authoring-core'
3
3
  import { createObjectId, parseObjectId } from 'adapt-authoring-mongodb'
4
4
  import { ObjectId } from 'mongodb'
5
- import { ContentTree, buildAssetUsagePipeline, computeSortOrderOps, contentTypeToSchemaName, extractAssetIds, fieldsToProjection, formatFriendlyId, parseMaxSeq, treeEtag } from './utils.js'
5
+ import { ContentTree, buildAssetUsagePipeline, computeSortOrderOps, contentTypeToSchemaName, excludeIdsFromQuery, extractAssetIds, fieldsToProjection, formatFriendlyId, parseMaxSeq, treeEtag } from './utils.js'
6
6
  /**
7
7
  * Module which handles course content
8
8
  * @memberof content
@@ -58,6 +58,7 @@ class ContentModule extends AbstractApiModule {
58
58
  this.postDeleteHook.tap(this.touchCourse.bind(this))
59
59
 
60
60
  assets.preDeleteHook.tap(this.enforceAssetNotInUse.bind(this))
61
+ assets.queryHook.tap(this.onAssetQueryHook, this)
61
62
 
62
63
  // block builds when the course structure has empty containers. adaptframework depends on
63
64
  // content (not vice-versa), so tap its hook once available rather than awaiting it here.
@@ -149,6 +150,27 @@ class ContentModule extends AbstractApiModule {
149
150
  }
150
151
  }
151
152
 
153
+ /**
154
+ * "Unused assets" filter for the asset manager. The UI sends a `?unused`
155
+ * query-string flag (see adapt-authoring-ui Assets page); restrict the assets
156
+ * query to those not referenced by any content document. Read from req.query
157
+ * because the flag is not part of the asset schema.
158
+ *
159
+ * Lives here, tapping the assets module's queryHook, so the `_assetIds`
160
+ * mechanism stays owned by content and assets remains usage-agnostic. Uses
161
+ * queryHook (not accessQueryHook) because it's a user-driven filter that must
162
+ * apply to every role.
163
+ * @param {external:ExpressRequest} req
164
+ * @return {Promise}
165
+ */
166
+ async onAssetQueryHook (req) {
167
+ if (!req.query.unused) return
168
+ // distinct can surface a null when legacy docs carry a null in _assetIds;
169
+ // those would crash the mongodb layer's ObjectId conversion of the $nin array
170
+ const usedIds = (await this.mongodb.getCollection(this.collectionName).distinct('_assetIds')).filter(Boolean)
171
+ excludeIdsFromQuery(req.apiData.query, usedIds)
172
+ }
173
+
152
174
  /** @override */
153
175
  async getSchemaName (data) {
154
176
  const { contentplugin } = this
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Mutates a mongo query to exclude the given `_id`s. If the query already
3
+ * has an `_id` constraint, both are preserved via `$and` so existing filters
4
+ * are not silently dropped.
5
+ * @param {Object} query The mongo query object to mutate
6
+ * @param {Array} ids Document `_id`s to exclude. Falsy or empty → no-op
7
+ * @memberof content
8
+ */
9
+ export function excludeIdsFromQuery (query, ids) {
10
+ if (!ids?.length) return
11
+ const existing = query._id
12
+ if (existing) {
13
+ query.$and = [
14
+ ...(query.$and ?? []),
15
+ { _id: existing },
16
+ { _id: { $nin: ids } }
17
+ ]
18
+ delete query._id
19
+ } else {
20
+ query._id = { $nin: ids }
21
+ }
22
+ }
package/lib/utils.js CHANGED
@@ -1,6 +1,7 @@
1
1
  export { default as ContentTree } from './ContentTree.js'
2
2
  export { default as buildAssetUsagePipeline } from './utils/buildAssetUsagePipeline.js'
3
3
  export { default as computeSortOrderOps } from './utils/computeSortOrderOps.js'
4
+ export { excludeIdsFromQuery } from './utils/excludeIdsFromQuery.js'
4
5
  export { extractAssetIds } from './utils/extractAssetIds.js'
5
6
  export { default as contentTypeToSchemaName } from './utils/contentTypeToSchemaName.js'
6
7
  export { default as fieldsToProjection } from './utils/fieldsToProjection.js'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adapt-authoring-content",
3
- "version": "3.3.0",
3
+ "version": "3.4.0",
4
4
  "description": "Module for managing Adapt content",
5
5
  "homepage": "https://github.com/adapt-security/adapt-authoring-content",
6
6
  "license": "GPL-3.0",
@@ -0,0 +1,40 @@
1
+ import { describe, it } from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+ import { excludeIdsFromQuery } from '../lib/utils/excludeIdsFromQuery.js'
4
+
5
+ describe('excludeIdsFromQuery', () => {
6
+ it('is a no-op for empty/falsy ids', () => {
7
+ const query = { type: 'image' }
8
+ excludeIdsFromQuery(query, [])
9
+ excludeIdsFromQuery(query, undefined)
10
+ assert.deepEqual(query, { type: 'image' })
11
+ })
12
+
13
+ it('adds a $nin _id constraint when none exists', () => {
14
+ const query = { type: 'image' }
15
+ excludeIdsFromQuery(query, ['a', 'b'])
16
+ assert.deepEqual(query, { type: 'image', _id: { $nin: ['a', 'b'] } })
17
+ })
18
+
19
+ it('preserves an existing _id constraint via $and', () => {
20
+ const query = { _id: { $in: ['keep'] } }
21
+ excludeIdsFromQuery(query, ['drop'])
22
+ assert.deepEqual(query, {
23
+ $and: [
24
+ { _id: { $in: ['keep'] } },
25
+ { _id: { $nin: ['drop'] } }
26
+ ]
27
+ })
28
+ assert.ok(!('_id' in query))
29
+ })
30
+
31
+ it('appends to an existing $and', () => {
32
+ const query = { $and: [{ a: 1 }], _id: 'x' }
33
+ excludeIdsFromQuery(query, ['y'])
34
+ assert.deepEqual(query.$and, [
35
+ { a: 1 },
36
+ { _id: 'x' },
37
+ { _id: { $nin: ['y'] } }
38
+ ])
39
+ })
40
+ })