Skip to content

Commit 7ca8ae2

Browse files
committed
implement static getCourseTagStubs
1 parent b9bcdc3 commit 7ca8ae2

File tree

1 file changed

+70
-10
lines changed

1 file changed

+70
-10
lines changed

packages/db/src/impl/static/courseDB.ts

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { DataLayerResult } from '../../core/types/db';
1515
import { ContentNavigationStrategyData } from '../../core/types/contentNavigationStrategy';
1616
import { ScheduledCard } from '../../core/types/user';
1717
import { Navigators } from '../../core/navigators';
18+
import { logger } from '../../util/logger';
1819

1920
export class StaticCourseDB implements CourseDBInterface {
2021
constructor(
@@ -41,10 +42,9 @@ export class StaticCourseDB implements CourseDBInterface {
4142
}
4243

4344
async getCourseInfo(): Promise<CourseInfo> {
44-
// This would need to be pre-computed in the manifest
4545
return {
46-
cardCount: 0, // Would come from manifest
47-
registeredUsers: 0,
46+
cardCount: this.manifest.documentCount || 0,
47+
registeredUsers: 0, // Always 0 in static mode
4848
};
4949
}
5050

@@ -160,6 +160,7 @@ export class StaticCourseDB implements CourseDBInterface {
160160

161161
async getAppliedTags(_cardId: string): Promise<PouchDB.Query.Response<TagStub>> {
162162
// Would need to query the tag index
163+
logger.warn(`getAppliedTags not implemented`);
163164
return {
164165
total_rows: 0,
165166
offset: 0,
@@ -188,12 +189,71 @@ export class StaticCourseDB implements CourseDBInterface {
188189
}
189190

190191
async getCourseTagStubs(): Promise<PouchDB.Core.AllDocsResponse<Tag>> {
191-
// Would query all tag documents
192-
return {
193-
total_rows: 0,
194-
offset: 0,
195-
rows: [],
196-
};
192+
try {
193+
const tagsIndex = await this.unpacker.getTagsIndex();
194+
195+
if (!tagsIndex || !tagsIndex.byTag) {
196+
logger.warn('Tags index not found or empty');
197+
return {
198+
total_rows: 0,
199+
offset: 0,
200+
rows: [],
201+
};
202+
}
203+
204+
// Create tag stubs from the index
205+
const tagNames = Object.keys(tagsIndex.byTag);
206+
const rows = await Promise.all(
207+
tagNames.map(async (tagName) => {
208+
const cardIds = tagsIndex.byTag[tagName] || [];
209+
const tagId = `${DocType.TAG}-${tagName}`;
210+
211+
try {
212+
// Try to get the full tag document
213+
const tagDoc = await this.unpacker.getDocument(tagId);
214+
return {
215+
id: tagId,
216+
key: tagId,
217+
value: { rev: '1-static' },
218+
doc: tagDoc,
219+
};
220+
} catch (error) {
221+
// If tag document not found, create a minimal stub
222+
logger.warn(`Tag document not found for ${tagName}, creating stub`);
223+
const stubDoc = {
224+
_id: tagId,
225+
_rev: '1-static',
226+
course: this.courseId,
227+
docType: DocType.TAG,
228+
name: tagName,
229+
snippet: `Tag: ${tagName}`,
230+
wiki: '',
231+
taggedCards: cardIds,
232+
author: 'system',
233+
};
234+
return {
235+
id: tagId,
236+
key: tagId,
237+
value: { rev: '1-static' },
238+
doc: stubDoc,
239+
};
240+
}
241+
})
242+
);
243+
244+
return {
245+
total_rows: rows.length,
246+
offset: 0,
247+
rows,
248+
};
249+
} catch (error) {
250+
logger.error('Failed to get course tag stubs:', error);
251+
return {
252+
total_rows: 0,
253+
offset: 0,
254+
rows: [],
255+
};
256+
}
197257
}
198258

199259
async addNote(
@@ -256,7 +316,7 @@ export class StaticCourseDB implements CourseDBInterface {
256316
}
257317

258318
// Attachment helper methods (internal use, not part of interface)
259-
319+
260320
/**
261321
* Get attachment URL for a document and attachment name
262322
* Internal helper method for static attachment serving

0 commit comments

Comments
 (0)