diff --git a/.agent/logs/20251223_card_url_type_migration.md b/.agent/logs/20251223_card_url_type_migration.md new file mode 100644 index 000000000..ec4036377 --- /dev/null +++ b/.agent/logs/20251223_card_url_type_migration.md @@ -0,0 +1,58 @@ +# Card URL Type Migration Plan + +**Date:** 2025-12-23 +**Status:** Planning Phase + +## Overview + +Adding a dedicated `urlType` field to the cards table to enable efficient querying by URL type (AUDIO, VIDEO, SOCIAL, etc.) while maintaining backward compatibility. + +## Approach: Incremental Migration + +### Phase 1: Add Optional Field (Immediate) +- Add `url_type` TEXT column to cards table (nullable) +- Update CardMapper to populate urlType for new cards +- Add indexes for performance +- New cards will be immediately queryable by type + +### Phase 2: Hybrid Queries (Temporary) +- Update query services to handle mixed data: + - Fast indexed lookup for new cards (urlType field) + - Fallback JSON query for old cards (contentData.metadata.type) +- Use OR conditions to support both data states + +### Phase 3: Backfill Migration (Later) +- Run SQL update to populate urlType for existing cards +- Remove fallback JSON queries once migration complete +- Simplify query logic to use only indexed field + +## Benefits + +✅ **Zero downtime** - No breaking changes +✅ **Immediate value** - New cards queryable by type right away +✅ **Gradual migration** - Can backfill when convenient +✅ **Performance** - Indexed queries for new data + +## Implementation Notes + +- URL type classification will use regex-based classifier service +- Known patterns: Spotify episodes → AUDIO, YouTube → VIDEO, etc. +- Fallback to LINK type for unclassified URLs + +## Migration SQL (for Phase 3) + +```sql +UPDATE cards +SET url_type = content_data #>> '{metadata,type}' +WHERE type = 'URL' + AND url_type IS NULL + AND content_data #>> '{metadata,type}' IS NOT NULL; +``` + +## Next Steps + +1. Implement URL classifier service +2. Add urlType field to schema +3. Update CardMapper and query services +4. Test with new cards +5. Plan backfill migration timing -- knotmirror 0.0.1 From c37d75a8241e08e672a79bceced5832495467705 Mon Sep 17 00:00:00 2001 From: Wesley Finck Date: Tue, 23 Dec 2025 20:03:46 +0800 Subject: [PATCH 02/18] feat: add optional urlType filtering to card query methods Co-authored-by: aider (anthropic/claude-sonnet-4-20250514) --- .../cards/domain/ICardQueryRepository.ts | 2 + .../repositories/mappers/CardMapper.ts | 3 + .../CollectionCardQueryService.ts | 25 +++++---- .../query-services/UrlCardQueryService.ts | 23 +++++--- .../repositories/schema/card.sql.ts | 3 + .../utils/InMemoryCardQueryRepository.ts | 55 ++++++++++++------- 6 files changed, 71 insertions(+), 40 deletions(-) diff --git a/src/modules/cards/domain/ICardQueryRepository.ts b/src/modules/cards/domain/ICardQueryRepository.ts index 31ea05369..bb9a471d5 100644 --- a/src/modules/cards/domain/ICardQueryRepository.ts +++ b/src/modules/cards/domain/ICardQueryRepository.ts @@ -1,10 +1,12 @@ import { CardTypeEnum } from './value-objects/CardType'; +import { UrlType } from './value-objects/UrlType'; export interface CardQueryOptions { page: number; limit: number; sortBy: CardSortField; sortOrder: SortOrder; + urlType?: UrlType; } export interface PaginatedQueryResult { diff --git a/src/modules/cards/infrastructure/repositories/mappers/CardMapper.ts b/src/modules/cards/infrastructure/repositories/mappers/CardMapper.ts index 66700bf4b..8bcaa598f 100644 --- a/src/modules/cards/infrastructure/repositories/mappers/CardMapper.ts +++ b/src/modules/cards/infrastructure/repositories/mappers/CardMapper.ts @@ -46,6 +46,7 @@ export interface CardPersistenceData { type: string; contentData: CardContentData; url?: string; + urlType?: string; parentCardId?: string; viaCardId?: string; libraryCount: number; @@ -101,6 +102,7 @@ export interface CardDTO { type: string; contentData: CardContentData; // Type-safe JSON data for the content url?: string; + urlType?: string; parentCardId?: string; viaCardId?: string; publishedRecordId?: { @@ -344,6 +346,7 @@ export class CardMapper { type: card.type.value, contentData, url: card.url?.value, + urlType: content.type === CardTypeEnum.URL ? content.urlContent?.metadata?.type : undefined, parentCardId: card.parentCardId?.getStringValue(), viaCardId: card.viaCardId?.getStringValue(), libraryCount: card.libraryCount, diff --git a/src/modules/cards/infrastructure/repositories/query-services/CollectionCardQueryService.ts b/src/modules/cards/infrastructure/repositories/query-services/CollectionCardQueryService.ts index 0754a9e6d..b4abd3111 100644 --- a/src/modules/cards/infrastructure/repositories/query-services/CollectionCardQueryService.ts +++ b/src/modules/cards/infrastructure/repositories/query-services/CollectionCardQueryService.ts @@ -1,4 +1,5 @@ import { eq, desc, asc, count, countDistinct, inArray, and } from 'drizzle-orm'; +import { UrlType } from '../../../domain/value-objects/UrlType'; import { PostgresJsDatabase } from 'drizzle-orm/postgres-js'; import { CardQueryOptions, @@ -48,6 +49,16 @@ export class CollectionCardQueryService { const collectionAuthorId = collectionResult[0]!.authorId; + // Build where conditions + const whereConditions = [ + eq(collectionCards.collectionId, collectionId), + eq(cards.type, CardTypeEnum.URL), + ]; + + if (options.urlType) { + whereConditions.push(eq(cards.urlType, options.urlType)); + } + // Get cards in collection - use standard query for all sort fields const cardsQuery = this.db .select({ @@ -61,12 +72,7 @@ export class CollectionCardQueryService { }) .from(cards) .innerJoin(collectionCards, eq(cards.id, collectionCards.cardId)) - .where( - and( - eq(collectionCards.collectionId, collectionId), - eq(cards.type, CardTypeEnum.URL), - ), - ); + .where(and(...whereConditions)); // For LIBRARY_COUNT sorting, we need to sort by urlLibraryCount after calculating it let cardsWithUrlLibraryCount: Array<{ @@ -270,12 +276,7 @@ export class CollectionCardQueryService { .select({ count: count() }) .from(cards) .innerJoin(collectionCards, eq(cards.id, collectionCards.cardId)) - .where( - and( - eq(collectionCards.collectionId, collectionId), - eq(cards.type, CardTypeEnum.URL), - ), - ); + .where(and(...whereConditions)); const totalCount = totalCountResult[0]?.count || 0; // const offset = (page - 1) * limit; diff --git a/src/modules/cards/infrastructure/repositories/query-services/UrlCardQueryService.ts b/src/modules/cards/infrastructure/repositories/query-services/UrlCardQueryService.ts index aaaece5f1..90fcdb9a1 100644 --- a/src/modules/cards/infrastructure/repositories/query-services/UrlCardQueryService.ts +++ b/src/modules/cards/infrastructure/repositories/query-services/UrlCardQueryService.ts @@ -1,4 +1,5 @@ import { eq, desc, asc, count, countDistinct, inArray, and } from 'drizzle-orm'; +import { UrlType } from '../../../domain/value-objects/UrlType'; import { UrlCardView } from '../../../domain/ICardQueryRepository'; import { PostgresJsDatabase } from 'drizzle-orm/postgres-js'; import { @@ -31,6 +32,16 @@ export class UrlCardQueryService { // Build the sort order const orderDirection = sortOrder === SortOrder.ASC ? asc : desc; + // Build where conditions + const whereConditions = [ + eq(cards.authorId, userId), + eq(cards.type, CardTypeEnum.URL), + ]; + + if (options.urlType) { + whereConditions.push(eq(cards.urlType, options.urlType)); + } + // For LIBRARY_COUNT sorting, we need to handle urlLibraryCount calculation and sorting separately if (sortBy === CardSortField.LIBRARY_COUNT) { // Get all URL cards for the user first @@ -45,9 +56,7 @@ export class UrlCardQueryService { updatedAt: cards.updatedAt, }) .from(cards) - .where( - and(eq(cards.authorId, userId), eq(cards.type, CardTypeEnum.URL)), - ); + .where(and(...whereConditions)); const allUrlCardsResult = await allUrlCardsQuery; @@ -259,9 +268,7 @@ export class UrlCardQueryService { updatedAt: cards.updatedAt, }) .from(cards) - .where( - and(eq(cards.authorId, userId), eq(cards.type, CardTypeEnum.URL)), - ) + .where(and(...whereConditions)) .orderBy(orderDirection(this.getSortColumn(sortBy))) .limit(limit) .offset(offset); @@ -368,9 +375,7 @@ export class UrlCardQueryService { const totalCountResult = await this.db .select({ count: count() }) .from(cards) - .where( - and(eq(cards.authorId, userId), eq(cards.type, CardTypeEnum.URL)), - ); + .where(and(...whereConditions)); const totalCount = totalCountResult[0]?.count || 0; const hasMore = offset + urlCardsResult.length < totalCount; diff --git a/src/modules/cards/infrastructure/repositories/schema/card.sql.ts b/src/modules/cards/infrastructure/repositories/schema/card.sql.ts index 3ff525c95..4939fb735 100644 --- a/src/modules/cards/infrastructure/repositories/schema/card.sql.ts +++ b/src/modules/cards/infrastructure/repositories/schema/card.sql.ts @@ -19,6 +19,7 @@ export const cards: PgTableWithColumns = pgTable( type: text('type').notNull(), // URL, NOTE, HIGHLIGHT contentData: jsonb('content_data').notNull(), url: text('url'), // Optional URL field for all card types + urlType: text('url_type'), // Optional URL type field (article, video, etc.) parentCardId: uuid('parent_card_id').references(() => cards.id), viaCardId: uuid('via_card_id').references(() => cards.id), publishedRecordId: uuid('published_record_id').references( @@ -44,6 +45,8 @@ export const cards: PgTableWithColumns = pgTable( ), // Index for getLibrariesForUrl and getCollectionsWithUrl - fast URL+type lookups urlTypeIdx: index('idx_cards_url_type').on(table.url, table.type), + // Index for filtering by URL type + urlTypeFilterIdx: index('idx_cards_url_type_filter').on(table.urlType), // Partial index for finding NOTE cards by parent - only indexes NOTE type cards parentTypeIdx: index('idx_cards_parent_type') .on(table.parentCardId, table.type) diff --git a/src/modules/cards/tests/utils/InMemoryCardQueryRepository.ts b/src/modules/cards/tests/utils/InMemoryCardQueryRepository.ts index 9013c2f7d..8655c60ce 100644 --- a/src/modules/cards/tests/utils/InMemoryCardQueryRepository.ts +++ b/src/modules/cards/tests/utils/InMemoryCardQueryRepository.ts @@ -32,17 +32,27 @@ export class InMemoryCardQueryRepository implements ICardQueryRepository { try { // Get all cards and filter by user's library membership const allCards = this.cardRepository.getAllCards(); - const userCards = allCards + let userCards = allCards .filter( (card) => card.isUrlCard && card.isInLibrary(CuratorId.create(userId).unwrap()), - ) - .map((card) => this.cardToUrlCardQueryResult(card, callingUserId)); + ); + + // Filter by urlType if specified + if (options.urlType) { + userCards = userCards.filter( + (card) => card.content.urlContent?.metadata?.type === options.urlType, + ); + } + + const userCardResults = userCards.map((card) => + this.cardToUrlCardQueryResult(card, callingUserId), + ); // Sort cards const sortedCards = this.sortCards( - userCards, + userCardResults, options.sortBy, options.sortOrder, ); @@ -54,8 +64,8 @@ export class InMemoryCardQueryRepository implements ICardQueryRepository { return { items: paginatedCards, - totalCount: userCards.length, - hasMore: endIndex < userCards.length, + totalCount: userCardResults.length, + hasMore: endIndex < userCardResults.length, }; } catch (error) { throw new Error( @@ -232,21 +242,28 @@ export class InMemoryCardQueryRepository implements ICardQueryRepository { const collectionCardIds = new Set( collection.cardIds.map((id) => id.getStringValue()), ); - const collectionCards = allCards - .filter( - (card) => - collectionCardIds.has(card.cardId.getStringValue()) && - card.isUrlCard, - ) - .map((card) => - this.toCollectionCardQueryResult( - this.cardToUrlCardQueryResult(card, callingUserId), - ), + let collectionCards = allCards.filter( + (card) => + collectionCardIds.has(card.cardId.getStringValue()) && + card.isUrlCard, + ); + + // Filter by urlType if specified + if (options.urlType) { + collectionCards = collectionCards.filter( + (card) => card.content.urlContent?.metadata?.type === options.urlType, ); + } + + const collectionCardResults = collectionCards.map((card) => + this.toCollectionCardQueryResult( + this.cardToUrlCardQueryResult(card, callingUserId), + ), + ); // Sort cards const sortedCards = this.sortCollectionCards( - collectionCards, + collectionCardResults, options.sortBy, options.sortOrder, ); @@ -258,8 +275,8 @@ export class InMemoryCardQueryRepository implements ICardQueryRepository { return { items: paginatedCards, - totalCount: collectionCards.length, - hasMore: endIndex < collectionCards.length, + totalCount: collectionCardResults.length, + hasMore: endIndex < collectionCardResults.length, }; } catch (error) { throw new Error( -- knotmirror 0.0.1 From ceaeeca1dc5f773b70de32a52877f4d78362a833 Mon Sep 17 00:00:00 2001 From: Wesley Finck Date: Tue, 23 Dec 2025 20:10:53 +0800 Subject: [PATCH 03/18] test: add URL type filtering tests for card query methods Co-authored-by: aider (anthropic/claude-sonnet-4-20250514) --- ...y.getCardsInCollection.integration.test.ts | 127 ++++++++++++++++++ ...tory.getUrlCardsOfUser.integration.test.ts | 99 ++++++++++++++ 2 files changed, 226 insertions(+) diff --git a/src/modules/cards/tests/infrastructure/DrizzleCardQueryRepository.getCardsInCollection.integration.test.ts b/src/modules/cards/tests/infrastructure/DrizzleCardQueryRepository.getCardsInCollection.integration.test.ts index 6a23244d7..c1ce79ff2 100644 --- a/src/modules/cards/tests/infrastructure/DrizzleCardQueryRepository.getCardsInCollection.integration.test.ts +++ b/src/modules/cards/tests/infrastructure/DrizzleCardQueryRepository.getCardsInCollection.integration.test.ts @@ -23,6 +23,7 @@ import { UrlMetadata } from '../../domain/value-objects/UrlMetadata'; import { CardSortField, SortOrder } from '../../domain/ICardQueryRepository'; import { createTestSchema } from '../test-utils/createTestSchema'; import { CardTypeEnum } from '../../domain/value-objects/CardType'; +import { UrlType } from '../../domain/value-objects/UrlType'; describe('DrizzleCardQueryRepository - getCardsInCollection', () => { let container: StartedPostgreSqlContainer; @@ -1059,4 +1060,130 @@ describe('DrizzleCardQueryRepository - getCardsInCollection', () => { expect(result.items[0]?.urlLibraryCount).toBe(2); // But 2 users have it }); }); + + describe('URL type filtering', () => { + it('should filter collection cards by urlType', async () => { + // Create URL cards with different types + const articleUrl = URL.create('https://example.com/article').unwrap(); + const articleMetadata = UrlMetadata.create({ + url: articleUrl.value, + title: 'Test Article', + type: UrlType.ARTICLE, + }).unwrap(); + + const videoUrl = URL.create('https://example.com/video').unwrap(); + const videoMetadata = UrlMetadata.create({ + url: videoUrl.value, + title: 'Test Video', + type: UrlType.VIDEO, + }).unwrap(); + + const articleCard = new CardBuilder() + .withCuratorId(curatorId.value) + .withUrlCard(articleUrl, articleMetadata) + .buildOrThrow(); + + const videoCard = new CardBuilder() + .withCuratorId(curatorId.value) + .withUrlCard(videoUrl, videoMetadata) + .buildOrThrow(); + + await cardRepository.save(articleCard); + await cardRepository.save(videoCard); + + // Create collection and add both cards + const collection = Collection.create( + { + authorId: curatorId, + name: 'Mixed Collection', + accessType: CollectionAccessType.OPEN, + collaboratorIds: [], + createdAt: new Date(), + updatedAt: new Date(), + }, + new UniqueEntityID(), + ).unwrap(); + + collection.addCard(articleCard.cardId, curatorId); + collection.addCard(videoCard.cardId, curatorId); + await collectionRepository.save(collection); + + // Query for only article type + const articleResult = await queryRepository.getCardsInCollection( + collection.collectionId.getStringValue(), + { + page: 1, + limit: 10, + sortBy: CardSortField.UPDATED_AT, + sortOrder: SortOrder.DESC, + urlType: UrlType.ARTICLE, + }, + ); + + expect(articleResult.items).toHaveLength(1); + expect(articleResult.items[0]?.url).toBe(articleUrl.value); + + // Query for only video type + const videoResult = await queryRepository.getCardsInCollection( + collection.collectionId.getStringValue(), + { + page: 1, + limit: 10, + sortBy: CardSortField.UPDATED_AT, + sortOrder: SortOrder.DESC, + urlType: UrlType.VIDEO, + }, + ); + + expect(videoResult.items).toHaveLength(1); + expect(videoResult.items[0]?.url).toBe(videoUrl.value); + }); + + it('should return empty result when no collection cards match the urlType filter', async () => { + const articleUrl = URL.create('https://example.com/article').unwrap(); + const articleMetadata = UrlMetadata.create({ + url: articleUrl.value, + title: 'Test Article', + type: UrlType.ARTICLE, + }).unwrap(); + + const articleCard = new CardBuilder() + .withCuratorId(curatorId.value) + .withUrlCard(articleUrl, articleMetadata) + .buildOrThrow(); + + await cardRepository.save(articleCard); + + // Create collection and add article card + const collection = Collection.create( + { + authorId: curatorId, + name: 'Article Collection', + accessType: CollectionAccessType.OPEN, + collaboratorIds: [], + createdAt: new Date(), + updatedAt: new Date(), + }, + new UniqueEntityID(), + ).unwrap(); + + collection.addCard(articleCard.cardId, curatorId); + await collectionRepository.save(collection); + + // Query for video type when only article exists + const result = await queryRepository.getCardsInCollection( + collection.collectionId.getStringValue(), + { + page: 1, + limit: 10, + sortBy: CardSortField.UPDATED_AT, + sortOrder: SortOrder.DESC, + urlType: UrlType.VIDEO, + }, + ); + + expect(result.items).toHaveLength(0); + expect(result.totalCount).toBe(0); + }); + }); }); diff --git a/src/modules/cards/tests/infrastructure/DrizzleCardQueryRepository.getUrlCardsOfUser.integration.test.ts b/src/modules/cards/tests/infrastructure/DrizzleCardQueryRepository.getUrlCardsOfUser.integration.test.ts index 5aa333ed7..dd3ab7387 100644 --- a/src/modules/cards/tests/infrastructure/DrizzleCardQueryRepository.getUrlCardsOfUser.integration.test.ts +++ b/src/modules/cards/tests/infrastructure/DrizzleCardQueryRepository.getUrlCardsOfUser.integration.test.ts @@ -23,6 +23,7 @@ import { UrlMetadata } from '../../domain/value-objects/UrlMetadata'; import { CardSortField, SortOrder } from '../../domain/ICardQueryRepository'; import { createTestSchema } from '../test-utils/createTestSchema'; import { CardTypeEnum } from '../../domain/value-objects/CardType'; +import { UrlType } from '../../domain/value-objects/UrlType'; describe('DrizzleCardQueryRepository - getUrlCardsOfUser', () => { let container: StartedPostgreSqlContainer; @@ -1100,4 +1101,102 @@ describe('DrizzleCardQueryRepository - getUrlCardsOfUser', () => { expect(result.items[0]?.urlLibraryCount).toBe(2); // But 2 users have it }); }); + + describe('URL type filtering', () => { + it('should filter URL cards by urlType', async () => { + // Create URL cards with different types + const articleUrl = URL.create('https://example.com/article').unwrap(); + const articleMetadata = UrlMetadata.create({ + url: articleUrl.value, + title: 'Test Article', + type: UrlType.ARTICLE, + }).unwrap(); + + const videoUrl = URL.create('https://example.com/video').unwrap(); + const videoMetadata = UrlMetadata.create({ + url: videoUrl.value, + title: 'Test Video', + type: UrlType.VIDEO, + }).unwrap(); + + const articleCard = new CardBuilder() + .withCuratorId(curatorId.value) + .withUrlCard(articleUrl, articleMetadata) + .buildOrThrow(); + + const videoCard = new CardBuilder() + .withCuratorId(curatorId.value) + .withUrlCard(videoUrl, videoMetadata) + .buildOrThrow(); + + await cardRepository.save(articleCard); + await cardRepository.save(videoCard); + + articleCard.addToLibrary(curatorId); + videoCard.addToLibrary(curatorId); + + await cardRepository.save(articleCard); + await cardRepository.save(videoCard); + + // Query for only article type + const articleResult = await queryRepository.getUrlCardsOfUser( + curatorId.value, + { + page: 1, + limit: 10, + sortBy: CardSortField.UPDATED_AT, + sortOrder: SortOrder.DESC, + urlType: UrlType.ARTICLE, + }, + ); + + expect(articleResult.items).toHaveLength(1); + expect(articleResult.items[0]?.url).toBe(articleUrl.value); + + // Query for only video type + const videoResult = await queryRepository.getUrlCardsOfUser( + curatorId.value, + { + page: 1, + limit: 10, + sortBy: CardSortField.UPDATED_AT, + sortOrder: SortOrder.DESC, + urlType: UrlType.VIDEO, + }, + ); + + expect(videoResult.items).toHaveLength(1); + expect(videoResult.items[0]?.url).toBe(videoUrl.value); + }); + + it('should return empty result when no cards match the urlType filter', async () => { + const articleUrl = URL.create('https://example.com/article').unwrap(); + const articleMetadata = UrlMetadata.create({ + url: articleUrl.value, + title: 'Test Article', + type: UrlType.ARTICLE, + }).unwrap(); + + const articleCard = new CardBuilder() + .withCuratorId(curatorId.value) + .withUrlCard(articleUrl, articleMetadata) + .buildOrThrow(); + + await cardRepository.save(articleCard); + articleCard.addToLibrary(curatorId); + await cardRepository.save(articleCard); + + // Query for video type when only article exists + const result = await queryRepository.getUrlCardsOfUser(curatorId.value, { + page: 1, + limit: 10, + sortBy: CardSortField.UPDATED_AT, + sortOrder: SortOrder.DESC, + urlType: UrlType.VIDEO, + }); + + expect(result.items).toHaveLength(0); + expect(result.totalCount).toBe(0); + }); + }); }); -- knotmirror 0.0.1 From 75ffd28906aae935de3a50c13fc83f3534e5ed4f Mon Sep 17 00:00:00 2001 From: Wesley Finck Date: Tue, 23 Dec 2025 20:11:55 +0800 Subject: [PATCH 04/18] feat: add url_type column and index to test schema for cards Co-authored-by: aider (anthropic/claude-sonnet-4-20250514) --- src/modules/cards/tests/test-utils/createTestSchema.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/modules/cards/tests/test-utils/createTestSchema.ts b/src/modules/cards/tests/test-utils/createTestSchema.ts index 362196749..61fee8bb1 100644 --- a/src/modules/cards/tests/test-utils/createTestSchema.ts +++ b/src/modules/cards/tests/test-utils/createTestSchema.ts @@ -23,6 +23,7 @@ export async function createTestSchema(db: PostgresJsDatabase) { type TEXT NOT NULL, content_data JSONB NOT NULL, url TEXT, + url_type TEXT, parent_card_id UUID REFERENCES cards(id), via_card_id UUID REFERENCES cards(id), published_record_id UUID REFERENCES published_records(id), @@ -127,6 +128,12 @@ export async function createTestSchema(db: PostgresJsDatabase) { ON cards(url, type) INCLUDE (id) `); + // Index for filtering by URL type + await db.execute(sql` + CREATE INDEX IF NOT EXISTS idx_cards_url_type_filter + ON cards(url_type) + `); + // Covering index for getCardsInCollection - sorted by add time with cardId included await db.execute(sql` CREATE INDEX IF NOT EXISTS idx_collection_cards_collection_added -- knotmirror 0.0.1 From e4a1e1d13119d2185de58907a3ad6d84e04a51ca Mon Sep 17 00:00:00 2001 From: Wesley Finck Date: Tue, 23 Dec 2025 20:21:11 +0800 Subject: [PATCH 05/18] update migration files --- .../migrations/0010_boring_blazing_skull.sql | 2 + .../migrations/meta/0010_snapshot.json | 1174 +++++++++++++++++ .../database/migrations/meta/_journal.json | 9 +- 3 files changed, 1184 insertions(+), 1 deletion(-) create mode 100644 src/shared/infrastructure/database/migrations/0010_boring_blazing_skull.sql create mode 100644 src/shared/infrastructure/database/migrations/meta/0010_snapshot.json diff --git a/src/shared/infrastructure/database/migrations/0010_boring_blazing_skull.sql b/src/shared/infrastructure/database/migrations/0010_boring_blazing_skull.sql new file mode 100644 index 000000000..91edace9a --- /dev/null +++ b/src/shared/infrastructure/database/migrations/0010_boring_blazing_skull.sql @@ -0,0 +1,2 @@ +ALTER TABLE "cards" ADD COLUMN "url_type" text;--> statement-breakpoint +CREATE INDEX "idx_cards_url_type_filter" ON "cards" USING btree ("url_type"); \ No newline at end of file diff --git a/src/shared/infrastructure/database/migrations/meta/0010_snapshot.json b/src/shared/infrastructure/database/migrations/meta/0010_snapshot.json new file mode 100644 index 000000000..b8ae3341c --- /dev/null +++ b/src/shared/infrastructure/database/migrations/meta/0010_snapshot.json @@ -0,0 +1,1174 @@ +{ + "id": "b6b80ebe-71aa-4189-bedd-1ee7cb6d9e46", + "prevId": "b2271318-d2d3-46c6-bcba-1a7d61adec8a", + "version": "7", + "dialect": "postgresql", + "tables": { + "public.app_password_sessions": { + "name": "app_password_sessions", + "schema": "", + "columns": { + "did": { + "name": "did", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "session_data": { + "name": "session_data", + "type": "jsonb", + "primaryKey": false, + "notNull": true + }, + "app_password": { + "name": "app_password", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.cards": { + "name": "cards", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true + }, + "author_id": { + "name": "author_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "content_data": { + "name": "content_data", + "type": "jsonb", + "primaryKey": false, + "notNull": true + }, + "url": { + "name": "url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "url_type": { + "name": "url_type", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "parent_card_id": { + "name": "parent_card_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "via_card_id": { + "name": "via_card_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "published_record_id": { + "name": "published_record_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "library_count": { + "name": "library_count", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "cards_author_url_idx": { + "name": "cards_author_url_idx", + "columns": [ + { + "expression": "author_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "url", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "cards_author_id_idx": { + "name": "cards_author_id_idx", + "columns": [ + { + "expression": "author_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_cards_type_updated_at": { + "name": "idx_cards_type_updated_at", + "columns": [ + { + "expression": "type", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "updated_at", + "isExpression": false, + "asc": false, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_cards_url_type": { + "name": "idx_cards_url_type", + "columns": [ + { + "expression": "url", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "type", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_cards_url_type_filter": { + "name": "idx_cards_url_type_filter", + "columns": [ + { + "expression": "url_type", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_cards_parent_type": { + "name": "idx_cards_parent_type", + "columns": [ + { + "expression": "parent_card_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "type", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "where": "type = 'NOTE'", + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "cards_parent_card_id_cards_id_fk": { + "name": "cards_parent_card_id_cards_id_fk", + "tableFrom": "cards", + "tableTo": "cards", + "columnsFrom": [ + "parent_card_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "cards_via_card_id_cards_id_fk": { + "name": "cards_via_card_id_cards_id_fk", + "tableFrom": "cards", + "tableTo": "cards", + "columnsFrom": [ + "via_card_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "cards_published_record_id_published_records_id_fk": { + "name": "cards_published_record_id_published_records_id_fk", + "tableFrom": "cards", + "tableTo": "published_records", + "columnsFrom": [ + "published_record_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.collection_cards": { + "name": "collection_cards", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true + }, + "collection_id": { + "name": "collection_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "card_id": { + "name": "card_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "added_by": { + "name": "added_by", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "added_at": { + "name": "added_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "via_card_id": { + "name": "via_card_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "published_record_id": { + "name": "published_record_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "collection_cards_card_id_idx": { + "name": "collection_cards_card_id_idx", + "columns": [ + { + "expression": "card_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "collection_cards_collection_id_idx": { + "name": "collection_cards_collection_id_idx", + "columns": [ + { + "expression": "collection_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_collection_cards_collection_added": { + "name": "idx_collection_cards_collection_added", + "columns": [ + { + "expression": "collection_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "added_at", + "isExpression": false, + "asc": false, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_collection_cards_card_collection": { + "name": "idx_collection_cards_card_collection", + "columns": [ + { + "expression": "card_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "collection_cards_collection_id_collections_id_fk": { + "name": "collection_cards_collection_id_collections_id_fk", + "tableFrom": "collection_cards", + "tableTo": "collections", + "columnsFrom": [ + "collection_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "collection_cards_card_id_cards_id_fk": { + "name": "collection_cards_card_id_cards_id_fk", + "tableFrom": "collection_cards", + "tableTo": "cards", + "columnsFrom": [ + "card_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "collection_cards_via_card_id_cards_id_fk": { + "name": "collection_cards_via_card_id_cards_id_fk", + "tableFrom": "collection_cards", + "tableTo": "cards", + "columnsFrom": [ + "via_card_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "collection_cards_published_record_id_published_records_id_fk": { + "name": "collection_cards_published_record_id_published_records_id_fk", + "tableFrom": "collection_cards", + "tableTo": "published_records", + "columnsFrom": [ + "published_record_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.collection_collaborators": { + "name": "collection_collaborators", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true + }, + "collection_id": { + "name": "collection_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "collaborator_id": { + "name": "collaborator_id", + "type": "text", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": { + "collection_collaborators_collection_id_collections_id_fk": { + "name": "collection_collaborators_collection_id_collections_id_fk", + "tableFrom": "collection_collaborators", + "tableTo": "collections", + "columnsFrom": [ + "collection_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.collections": { + "name": "collections", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true + }, + "author_id": { + "name": "author_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "access_type": { + "name": "access_type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "card_count": { + "name": "card_count", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "published_record_id": { + "name": "published_record_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "collections_author_id_idx": { + "name": "collections_author_id_idx", + "columns": [ + { + "expression": "author_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "collections_author_updated_at_idx": { + "name": "collections_author_updated_at_idx", + "columns": [ + { + "expression": "author_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "updated_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "collections_published_record_id_published_records_id_fk": { + "name": "collections_published_record_id_published_records_id_fk", + "tableFrom": "collections", + "tableTo": "published_records", + "columnsFrom": [ + "published_record_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.library_memberships": { + "name": "library_memberships", + "schema": "", + "columns": { + "card_id": { + "name": "card_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "added_at": { + "name": "added_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "published_record_id": { + "name": "published_record_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "idx_user_cards": { + "name": "idx_user_cards", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_card_users": { + "name": "idx_card_users", + "columns": [ + { + "expression": "card_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_library_memberships_user_type_covering": { + "name": "idx_library_memberships_user_type_covering", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "added_at", + "isExpression": false, + "asc": false, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "library_memberships_card_id_cards_id_fk": { + "name": "library_memberships_card_id_cards_id_fk", + "tableFrom": "library_memberships", + "tableTo": "cards", + "columnsFrom": [ + "card_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "library_memberships_published_record_id_published_records_id_fk": { + "name": "library_memberships_published_record_id_published_records_id_fk", + "tableFrom": "library_memberships", + "tableTo": "published_records", + "columnsFrom": [ + "published_record_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "library_memberships_card_id_user_id_pk": { + "name": "library_memberships_card_id_user_id_pk", + "columns": [ + "card_id", + "user_id" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.published_records": { + "name": "published_records", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true + }, + "uri": { + "name": "uri", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "cid": { + "name": "cid", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "recorded_at": { + "name": "recorded_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "uri_cid_unique_idx": { + "name": "uri_cid_unique_idx", + "columns": [ + { + "expression": "uri", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "cid", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "published_records_uri_idx": { + "name": "published_records_uri_idx", + "columns": [ + { + "expression": "uri", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.feed_activities": { + "name": "feed_activities", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true + }, + "actor_id": { + "name": "actor_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.notifications": { + "name": "notifications", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true + }, + "recipient_user_id": { + "name": "recipient_user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "actor_user_id": { + "name": "actor_user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "primaryKey": false, + "notNull": true + }, + "read": { + "name": "read", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "notifications_recipient_idx": { + "name": "notifications_recipient_idx", + "columns": [ + { + "expression": "recipient_user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "notifications_recipient_created_at_idx": { + "name": "notifications_recipient_created_at_idx", + "columns": [ + { + "expression": "recipient_user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": false, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "notifications_recipient_read_idx": { + "name": "notifications_recipient_read_idx", + "columns": [ + { + "expression": "recipient_user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "read", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.auth_session": { + "name": "auth_session", + "schema": "", + "columns": { + "key": { + "name": "key", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "session": { + "name": "session", + "type": "text", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.auth_state": { + "name": "auth_state", + "schema": "", + "columns": { + "key": { + "name": "key", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "state": { + "name": "state", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.auth_refresh_tokens": { + "name": "auth_refresh_tokens", + "schema": "", + "columns": { + "token_id": { + "name": "token_id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "user_did": { + "name": "user_did", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "refresh_token": { + "name": "refresh_token", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "issued_at": { + "name": "issued_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true + }, + "expires_at": { + "name": "expires_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true + }, + "revoked": { + "name": "revoked", + "type": "boolean", + "primaryKey": false, + "notNull": false, + "default": false + } + }, + "indexes": {}, + "foreignKeys": { + "auth_refresh_tokens_user_did_users_id_fk": { + "name": "auth_refresh_tokens_user_did_users_id_fk", + "tableFrom": "auth_refresh_tokens", + "tableTo": "users", + "columnsFrom": [ + "user_did" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.users": { + "name": "users", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "handle": { + "name": "handle", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "linked_at": { + "name": "linked_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true + }, + "last_login_at": { + "name": "last_login_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + } + }, + "enums": {}, + "schemas": {}, + "sequences": {}, + "roles": {}, + "policies": {}, + "views": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/src/shared/infrastructure/database/migrations/meta/_journal.json b/src/shared/infrastructure/database/migrations/meta/_journal.json index d39a8a35e..4441f6d0e 100644 --- a/src/shared/infrastructure/database/migrations/meta/_journal.json +++ b/src/shared/infrastructure/database/migrations/meta/_journal.json @@ -71,6 +71,13 @@ "when": 1765356227606, "tag": "0009_stormy_mastermind", "breakpoints": true + }, + { + "idx": 10, + "version": "7", + "when": 1766492444234, + "tag": "0010_boring_blazing_skull", + "breakpoints": true } ] -} +} \ No newline at end of file -- knotmirror 0.0.1 From be2ee1ef9a86040c78d914be70f6b7287adb22e7 Mon Sep 17 00:00:00 2001 From: Wesley Finck Date: Tue, 23 Dec 2025 20:26:15 +0800 Subject: [PATCH 06/18] feat: add optional urlType query parameter to card and collection endpoints Co-authored-by: aider (anthropic/claude-sonnet-4-20250514) --- .../queries/GetCollectionPageByAtUriUseCase.ts | 3 +++ .../useCases/queries/GetCollectionPageUseCase.ts | 4 ++++ .../application/useCases/queries/GetUrlCardsUseCase.ts | 4 ++++ .../controllers/GetCollectionPageByAtUriController.ts | 3 ++- .../http/controllers/GetCollectionPageController.ts | 3 ++- .../http/controllers/GetMyUrlCardsController.ts | 3 ++- .../http/controllers/GetUserUrlCardsController.ts | 3 ++- src/types/src/api/requests.ts | 10 ++++++++-- src/webapp/api-client/clients/QueryClient.ts | 4 ++++ 9 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/modules/cards/application/useCases/queries/GetCollectionPageByAtUriUseCase.ts b/src/modules/cards/application/useCases/queries/GetCollectionPageByAtUriUseCase.ts index 45c1626ff..f5ee92dee 100644 --- a/src/modules/cards/application/useCases/queries/GetCollectionPageByAtUriUseCase.ts +++ b/src/modules/cards/application/useCases/queries/GetCollectionPageByAtUriUseCase.ts @@ -13,6 +13,7 @@ import { CardSortField, SortOrder, } from 'src/modules/cards/domain/ICardQueryRepository'; +import { UrlType } from '../../../domain/value-objects/UrlType'; export interface GetCollectionPageByAtUriQuery { handle: string; @@ -22,6 +23,7 @@ export interface GetCollectionPageByAtUriQuery { limit?: number; sortBy?: CardSortField; sortOrder?: SortOrder; + urlType?: UrlType; } export class GetCollectionPageByAtUriUseCase @@ -93,6 +95,7 @@ export class GetCollectionPageByAtUriUseCase limit: query.limit, sortBy: query.sortBy, sortOrder: query.sortOrder, + urlType: query.urlType, }); } } diff --git a/src/modules/cards/application/useCases/queries/GetCollectionPageUseCase.ts b/src/modules/cards/application/useCases/queries/GetCollectionPageUseCase.ts index 93c9c4a32..4a7ca3674 100644 --- a/src/modules/cards/application/useCases/queries/GetCollectionPageUseCase.ts +++ b/src/modules/cards/application/useCases/queries/GetCollectionPageUseCase.ts @@ -7,6 +7,7 @@ import { SortOrder, UrlCardView, } from '../../../domain/ICardQueryRepository'; +import { UrlType } from '../../../domain/value-objects/UrlType'; import { ICollectionRepository } from '../../../domain/ICollectionRepository'; import { IProfileService } from '../../../domain/services/IProfileService'; @@ -17,6 +18,7 @@ export interface GetCollectionPageQuery { limit?: number; sortBy?: CardSortField; sortOrder?: SortOrder; + urlType?: UrlType; } export type CollectionPageUrlCardDTO = UrlCardView; @@ -76,6 +78,7 @@ export class GetCollectionPageUseCase const limit = Math.min(query.limit || 20, 100); // Cap at 100 const sortBy = query.sortBy || CardSortField.UPDATED_AT; const sortOrder = query.sortOrder || SortOrder.DESC; + const urlType = query.urlType; // Validate collection ID const collectionIdResult = CollectionId.createFromString( @@ -132,6 +135,7 @@ export class GetCollectionPageUseCase limit, sortBy, sortOrder, + urlType, }, query.callingUserId, ); diff --git a/src/modules/cards/application/useCases/queries/GetUrlCardsUseCase.ts b/src/modules/cards/application/useCases/queries/GetUrlCardsUseCase.ts index 196662341..541bebdbd 100644 --- a/src/modules/cards/application/useCases/queries/GetUrlCardsUseCase.ts +++ b/src/modules/cards/application/useCases/queries/GetUrlCardsUseCase.ts @@ -7,6 +7,7 @@ import { WithCollections, UrlCardView, } from '../../../domain/ICardQueryRepository'; +import { UrlType } from '../../../domain/value-objects/UrlType'; import { DIDOrHandle } from 'src/modules/atproto/domain/DIDOrHandle'; import { IIdentityResolutionService } from 'src/modules/atproto/domain/services/IIdentityResolutionService'; import { IProfileService } from '../../../domain/services/IProfileService'; @@ -19,6 +20,7 @@ export interface GetUrlCardsQuery { limit?: number; sortBy?: CardSortField; sortOrder?: SortOrder; + urlType?: UrlType; } // Enriched data for the final use case result @@ -62,6 +64,7 @@ export class GetUrlCardsUseCase const limit = Math.min(query.limit || 20, 100); // Cap at 100 const sortBy = query.sortBy || CardSortField.UPDATED_AT; const sortOrder = query.sortOrder || SortOrder.DESC; + const urlType = query.urlType; // Parse and validate user identifier const identifierResult = DIDOrHandle.create(query.userId); @@ -90,6 +93,7 @@ export class GetUrlCardsUseCase limit, sortBy, sortOrder, + urlType, }, query.callingUserId, ); diff --git a/src/modules/cards/infrastructure/http/controllers/GetCollectionPageByAtUriController.ts b/src/modules/cards/infrastructure/http/controllers/GetCollectionPageByAtUriController.ts index a9d81e22c..745314af5 100644 --- a/src/modules/cards/infrastructure/http/controllers/GetCollectionPageByAtUriController.ts +++ b/src/modules/cards/infrastructure/http/controllers/GetCollectionPageByAtUriController.ts @@ -17,7 +17,7 @@ export class GetCollectionPageByAtUriController extends Controller { if (!handle || !recordKey) { return this.badRequest(res, 'Handle and recordKey are required'); } - const { page, limit, sortBy, sortOrder } = req.query; + const { page, limit, sortBy, sortOrder, urlType } = req.query; const callerDid = req.did; const result = await this.getCollectionPageByAtUriUseCase.execute({ @@ -28,6 +28,7 @@ export class GetCollectionPageByAtUriController extends Controller { limit: limit ? parseInt(limit as string) : undefined, sortBy: sortBy as CardSortField, sortOrder: sortOrder as SortOrder, + urlType: urlType as any, }); if (result.isErr()) { diff --git a/src/modules/cards/infrastructure/http/controllers/GetCollectionPageController.ts b/src/modules/cards/infrastructure/http/controllers/GetCollectionPageController.ts index be7cadd8b..ed2f82a70 100644 --- a/src/modules/cards/infrastructure/http/controllers/GetCollectionPageController.ts +++ b/src/modules/cards/infrastructure/http/controllers/GetCollectionPageController.ts @@ -12,7 +12,7 @@ export class GetCollectionPageController extends Controller { async executeImpl(req: AuthenticatedRequest, res: Response): Promise { try { const { collectionId } = req.params; - const { page, limit, sortBy, sortOrder } = req.query; + const { page, limit, sortBy, sortOrder, urlType } = req.query; const callerDid = req.did; if (!collectionId) { @@ -26,6 +26,7 @@ export class GetCollectionPageController extends Controller { limit: limit ? parseInt(limit as string) : undefined, sortBy: sortBy as CardSortField, sortOrder: sortOrder as SortOrder, + urlType: urlType as any, }); if (result.isErr()) { diff --git a/src/modules/cards/infrastructure/http/controllers/GetMyUrlCardsController.ts b/src/modules/cards/infrastructure/http/controllers/GetMyUrlCardsController.ts index 6f2ef281e..6ecd65573 100644 --- a/src/modules/cards/infrastructure/http/controllers/GetMyUrlCardsController.ts +++ b/src/modules/cards/infrastructure/http/controllers/GetMyUrlCardsController.ts @@ -12,7 +12,7 @@ export class GetMyUrlCardsController extends Controller { async executeImpl(req: AuthenticatedRequest, res: Response): Promise { try { const callerDid = req.did; - const { page, limit, sortBy, sortOrder } = req.query; + const { page, limit, sortBy, sortOrder, urlType } = req.query; if (!callerDid) { return this.unauthorized(res); @@ -25,6 +25,7 @@ export class GetMyUrlCardsController extends Controller { limit: limit ? parseInt(limit as string) : undefined, sortBy: sortBy as CardSortField, sortOrder: sortOrder as SortOrder, + urlType: urlType as any, }); if (result.isErr()) { diff --git a/src/modules/cards/infrastructure/http/controllers/GetUserUrlCardsController.ts b/src/modules/cards/infrastructure/http/controllers/GetUserUrlCardsController.ts index 653c6e8cd..325a32a8c 100644 --- a/src/modules/cards/infrastructure/http/controllers/GetUserUrlCardsController.ts +++ b/src/modules/cards/infrastructure/http/controllers/GetUserUrlCardsController.ts @@ -12,7 +12,7 @@ export class GetUserUrlCardsController extends Controller { async executeImpl(req: AuthenticatedRequest, res: Response): Promise { try { const { identifier } = req.params; - const { page, limit, sortBy, sortOrder } = req.query; + const { page, limit, sortBy, sortOrder, urlType } = req.query; const callerDid = req.did; if (!identifier) { @@ -26,6 +26,7 @@ export class GetUserUrlCardsController extends Controller { limit: limit ? parseInt(limit as string) : undefined, sortBy: sortBy as CardSortField, sortOrder: sortOrder as SortOrder, + urlType: urlType as any, }); if (result.isErr()) { diff --git a/src/types/src/api/requests.ts b/src/types/src/api/requests.ts index a037d8f67..5908b19e2 100644 --- a/src/types/src/api/requests.ts +++ b/src/types/src/api/requests.ts @@ -69,13 +69,18 @@ export interface DeleteCollectionRequest { } // Query parameters -export interface GetMyUrlCardsParams extends PaginatedSortedParams {} +export interface GetMyUrlCardsParams extends PaginatedSortedParams { + urlType?: string; +} export interface GetUrlCardsParams extends PaginatedSortedParams { identifier: string; // Can be DID or handle + urlType?: string; } -export interface GetCollectionPageParams extends PaginatedSortedParams {} +export interface GetCollectionPageParams extends PaginatedSortedParams { + urlType?: string; +} export interface GetMyCollectionsParams extends PaginatedSortedParams { searchText?: string; @@ -89,6 +94,7 @@ export interface GetCollectionsParams extends PaginatedSortedParams { export interface GetCollectionPageByAtUriParams extends PaginatedSortedParams { handle: string; recordKey: string; + urlType?: string; } export interface GetGlobalFeedParams extends PaginationParams { diff --git a/src/webapp/api-client/clients/QueryClient.ts b/src/webapp/api-client/clients/QueryClient.ts index c958dd1c0..08ccd894f 100644 --- a/src/webapp/api-client/clients/QueryClient.ts +++ b/src/webapp/api-client/clients/QueryClient.ts @@ -43,6 +43,7 @@ export class QueryClient extends BaseClient { if (params?.limit) searchParams.set('limit', params.limit.toString()); if (params?.sortBy) searchParams.set('sortBy', params.sortBy); if (params?.sortOrder) searchParams.set('sortOrder', params.sortOrder); + if (params?.urlType) searchParams.set('urlType', params.urlType); const queryString = searchParams.toString(); const endpoint = queryString @@ -60,6 +61,7 @@ export class QueryClient extends BaseClient { if (params.limit) searchParams.set('limit', params.limit.toString()); if (params.sortBy) searchParams.set('sortBy', params.sortBy); if (params.sortOrder) searchParams.set('sortOrder', params.sortOrder); + if (params.urlType) searchParams.set('urlType', params.urlType); const queryString = searchParams.toString(); const endpoint = queryString @@ -102,6 +104,7 @@ export class QueryClient extends BaseClient { if (params?.limit) searchParams.set('limit', params.limit.toString()); if (params?.sortBy) searchParams.set('sortBy', params.sortBy); if (params?.sortOrder) searchParams.set('sortOrder', params.sortOrder); + if (params?.urlType) searchParams.set('urlType', params.urlType); const queryString = searchParams.toString(); const endpoint = queryString @@ -123,6 +126,7 @@ export class QueryClient extends BaseClient { if (queryParams.sortBy) searchParams.set('sortBy', queryParams.sortBy); if (queryParams.sortOrder) searchParams.set('sortOrder', queryParams.sortOrder); + if (queryParams.urlType) searchParams.set('urlType', queryParams.urlType); const queryString = searchParams.toString(); const endpoint = queryString -- knotmirror 0.0.1 From ce76781aad9303e70f9382506ff08163d30cd3b9 Mon Sep 17 00:00:00 2001 From: Wesley Finck Date: Tue, 23 Dec 2025 20:30:12 +0800 Subject: [PATCH 07/18] refactor: replicate UrlType enum in request types Co-authored-by: aider (anthropic/claude-sonnet-4-20250514) --- src/types/src/api/requests.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/types/src/api/requests.ts b/src/types/src/api/requests.ts index 5908b19e2..904b01e38 100644 --- a/src/types/src/api/requests.ts +++ b/src/types/src/api/requests.ts @@ -13,6 +13,19 @@ export interface PaginatedSortedParams extends PaginationParams, SortingParams {} +// URL Type enum for filtering +export enum UrlType { + ARTICLE = 'article', + LINK = 'link', + BOOK = 'book', + RESEARCH = 'research', + AUDIO = 'audio', + VIDEO = 'video', + SOCIAL = 'social', + EVENT = 'event', + SOFTWARE = 'software', +} + // Command request types export interface AddUrlToLibraryRequest { url: string; @@ -70,16 +83,16 @@ export interface DeleteCollectionRequest { // Query parameters export interface GetMyUrlCardsParams extends PaginatedSortedParams { - urlType?: string; + urlType?: UrlType; } export interface GetUrlCardsParams extends PaginatedSortedParams { identifier: string; // Can be DID or handle - urlType?: string; + urlType?: UrlType; } export interface GetCollectionPageParams extends PaginatedSortedParams { - urlType?: string; + urlType?: UrlType; } export interface GetMyCollectionsParams extends PaginatedSortedParams { @@ -94,7 +107,7 @@ export interface GetCollectionsParams extends PaginatedSortedParams { export interface GetCollectionPageByAtUriParams extends PaginatedSortedParams { handle: string; recordKey: string; - urlType?: string; + urlType?: UrlType; } export interface GetGlobalFeedParams extends PaginationParams { -- knotmirror 0.0.1 From 1f490fc7c2dba4bb723fbb7a5c3c1d8073498e1a Mon Sep 17 00:00:00 2001 From: Wesley Finck Date: Tue, 23 Dec 2025 21:02:25 +0800 Subject: [PATCH 08/18] feat: add urlType support to feed activities schema and related components Co-authored-by: aider (anthropic/claude-sonnet-4-20250514) --- .../tests/test-utils/createTestSchema.ts | 1 + .../commands/AddActivityToFeedUseCase.ts | 21 ++++++- .../queries/GetGemActivityFeedUseCase.ts | 9 +++ .../useCases/queries/GetGlobalFeedUseCase.ts | 9 +++ src/modules/feeds/domain/FeedActivity.ts | 8 +++ src/modules/feeds/domain/IFeedRepository.ts | 2 + .../GetGemActivityFeedController.ts | 2 + .../controllers/GetGlobalFeedController.ts | 2 + .../repositories/DrizzleFeedRepository.ts | 63 +++++++++++++++---- .../mappers/FeedActivityMapper.ts | 9 +++ .../repositories/schema/feedActivity.sql.ts | 1 + .../infrastructure/InMemoryFeedRepository.ts | 18 +++++- .../http/factories/UseCaseFactory.ts | 2 + src/types/src/api/requests.ts | 2 + src/webapp/api-client/clients/FeedClient.ts | 2 + 15 files changed, 135 insertions(+), 16 deletions(-) diff --git a/src/modules/cards/tests/test-utils/createTestSchema.ts b/src/modules/cards/tests/test-utils/createTestSchema.ts index 61fee8bb1..b5d8fba6b 100644 --- a/src/modules/cards/tests/test-utils/createTestSchema.ts +++ b/src/modules/cards/tests/test-utils/createTestSchema.ts @@ -79,6 +79,7 @@ export async function createTestSchema(db: PostgresJsDatabase) { actor_id TEXT NOT NULL, type TEXT NOT NULL, metadata JSONB NOT NULL, + url_type TEXT, created_at TIMESTAMP NOT NULL DEFAULT NOW() )`, diff --git a/src/modules/feeds/application/useCases/commands/AddActivityToFeedUseCase.ts b/src/modules/feeds/application/useCases/commands/AddActivityToFeedUseCase.ts index 16f5f2261..3500db41b 100644 --- a/src/modules/feeds/application/useCases/commands/AddActivityToFeedUseCase.ts +++ b/src/modules/feeds/application/useCases/commands/AddActivityToFeedUseCase.ts @@ -7,6 +7,7 @@ import { CardId } from '../../../../cards/domain/value-objects/CardId'; import { CollectionId } from '../../../../cards/domain/value-objects/CollectionId'; import { ActivityTypeEnum } from '../../../domain/value-objects/ActivityType'; import { FeedService } from 'src/modules/feeds/domain/services/FeedService'; +import { ICardRepository } from '../../../../cards/domain/ICardRepository'; export interface AddCardCollectedActivityDTO { type: ActivityTypeEnum.CARD_COLLECTED; @@ -37,7 +38,10 @@ export class AddActivityToFeedUseCase > > { - constructor(private feedService: FeedService) {} + constructor( + private feedService: FeedService, + private cardRepository: ICardRepository, + ) {} async execute( request: AddActivityToFeedDTO, @@ -86,10 +90,25 @@ export class AddActivityToFeedUseCase } } + // Fetch the card to get its URL type + const cardResult = await this.cardRepository.findById(cardId); + if (cardResult.isErr()) { + return err(new ValidationError(`Failed to fetch card: ${cardResult.error.message}`)); + } + + let urlType; + if (cardResult.value && cardResult.value.isUrlCard) { + const urlCardContent = cardResult.value.content as any; + if (urlCardContent.metadata?.type) { + urlType = urlCardContent.metadata.type; + } + } + const activityResult = await this.feedService.addCardCollectedActivity( actorId, cardId, collectionIds, + urlType, ); if (activityResult.isErr()) { diff --git a/src/modules/feeds/application/useCases/queries/GetGemActivityFeedUseCase.ts b/src/modules/feeds/application/useCases/queries/GetGemActivityFeedUseCase.ts index 2a8a9b45e..e0b2c3b6c 100644 --- a/src/modules/feeds/application/useCases/queries/GetGemActivityFeedUseCase.ts +++ b/src/modules/feeds/application/useCases/queries/GetGemActivityFeedUseCase.ts @@ -16,6 +16,7 @@ import { SortOrder, } from 'src/modules/cards/domain/ICollectionQueryRepository'; import { CollectionId } from 'src/modules/cards/domain/value-objects/CollectionId'; +import { UrlType } from '../../../cards/domain/value-objects/UrlType'; import { GetGlobalFeedResponse, FeedItem } from '@semble/types'; export interface GetGemActivityFeedQuery { @@ -23,6 +24,7 @@ export interface GetGemActivityFeedQuery { page?: number; limit?: number; beforeActivityId?: string; // For cursor-based pagination + urlType?: string; // Filter by URL type } // Use the shared API type directly @@ -127,11 +129,18 @@ export class GetGemActivityFeedUseCase beforeActivityId = activityIdResult.value; } + // Parse urlType if provided + let urlType: UrlType | undefined; + if (query.urlType) { + urlType = query.urlType as UrlType; + } + // Fetch activities from repository using gems feed const feedResult = await this.feedRepository.getGemsFeed(collectionIds, { page, limit, beforeActivityId, + urlType, }); if (feedResult.isErr()) { diff --git a/src/modules/feeds/application/useCases/queries/GetGlobalFeedUseCase.ts b/src/modules/feeds/application/useCases/queries/GetGlobalFeedUseCase.ts index 00bb2d8fc..9d2e79057 100644 --- a/src/modules/feeds/application/useCases/queries/GetGlobalFeedUseCase.ts +++ b/src/modules/feeds/application/useCases/queries/GetGlobalFeedUseCase.ts @@ -11,6 +11,7 @@ import { } from '../../../../cards/domain/ICardQueryRepository'; import { ICollectionRepository } from 'src/modules/cards/domain/ICollectionRepository'; import { CollectionId } from 'src/modules/cards/domain/value-objects/CollectionId'; +import { UrlType } from '../../../cards/domain/value-objects/UrlType'; import { GetGlobalFeedResponse, FeedItem } from '@semble/types'; export interface GetGlobalFeedQuery { @@ -18,6 +19,7 @@ export interface GetGlobalFeedQuery { page?: number; limit?: number; beforeActivityId?: string; // For cursor-based pagination + urlType?: string; // Filter by URL type } // Use the shared API type directly @@ -68,11 +70,18 @@ export class GetGlobalFeedUseCase beforeActivityId = activityIdResult.value; } + // Parse urlType if provided + let urlType: UrlType | undefined; + if (query.urlType) { + urlType = query.urlType as UrlType; + } + // Fetch activities from repository const feedResult = await this.feedRepository.getGlobalFeed({ page, limit, beforeActivityId, + urlType, }); if (feedResult.isErr()) { diff --git a/src/modules/feeds/domain/FeedActivity.ts b/src/modules/feeds/domain/FeedActivity.ts index f709a285a..434f7a998 100644 --- a/src/modules/feeds/domain/FeedActivity.ts +++ b/src/modules/feeds/domain/FeedActivity.ts @@ -6,6 +6,7 @@ import { ActivityType, ActivityTypeEnum } from './value-objects/ActivityType'; import { CuratorId } from '../../cards/domain/value-objects/CuratorId'; import { CardId } from '../../cards/domain/value-objects/CardId'; import { CollectionId } from '../../cards/domain/value-objects/CollectionId'; +import { UrlType } from '../../cards/domain/value-objects/UrlType'; export class ActivityValidationError extends Error { constructor(message: string) { @@ -25,6 +26,7 @@ interface ActivityProps { actorId: CuratorId; // The user who performed the activity type: ActivityType; // The type of activity metadata: ActivityMetadata; // Additional metadata specific to the activity type + urlType?: UrlType; // Optional URL type from the card createdAt: Date; } @@ -49,6 +51,10 @@ export class FeedActivity extends Entity { return this.props.createdAt; } + get urlType(): UrlType | undefined { + return this.props.urlType; + } + // Type guards for metadata get cardCollected(): boolean { return this.props.type.value === ActivityTypeEnum.CARD_COLLECTED; @@ -62,6 +68,7 @@ export class FeedActivity extends Entity { actorId: CuratorId, cardId: CardId, collectionIds?: CollectionId[], + urlType?: UrlType, createdAt?: Date, id?: UniqueEntityID, ): Result { @@ -83,6 +90,7 @@ export class FeedActivity extends Entity { actorId, type: typeResult.value, metadata, + urlType, createdAt: createdAt || new Date(), }; diff --git a/src/modules/feeds/domain/IFeedRepository.ts b/src/modules/feeds/domain/IFeedRepository.ts index 297512685..2ec6eed33 100644 --- a/src/modules/feeds/domain/IFeedRepository.ts +++ b/src/modules/feeds/domain/IFeedRepository.ts @@ -2,11 +2,13 @@ import { CollectionId } from 'src/modules/cards/domain/value-objects/CollectionI import { Result } from '../../../shared/core/Result'; import { FeedActivity } from './FeedActivity'; import { ActivityId } from './value-objects/ActivityId'; +import { UrlType } from '../../cards/domain/value-objects/UrlType'; export interface FeedQueryOptions { page: number; limit: number; beforeActivityId?: ActivityId; // For cursor-based pagination + urlType?: UrlType; // Filter by URL type } export interface PaginatedFeedResult { diff --git a/src/modules/feeds/infrastructure/http/controllers/GetGemActivityFeedController.ts b/src/modules/feeds/infrastructure/http/controllers/GetGemActivityFeedController.ts index 2a478aaa5..b018359d8 100644 --- a/src/modules/feeds/infrastructure/http/controllers/GetGemActivityFeedController.ts +++ b/src/modules/feeds/infrastructure/http/controllers/GetGemActivityFeedController.ts @@ -10,6 +10,7 @@ const querySchema = z.object({ page: z.coerce.number().int().positive().optional(), limit: z.coerce.number().int().positive().max(100).optional(), beforeActivityId: z.string().optional(), + urlType: z.string().optional(), }); export class GetGemActivityFeedController extends Controller { @@ -33,6 +34,7 @@ export class GetGemActivityFeedController extends Controller { page: params.page || 1, limit: params.limit || 20, beforeActivityId: params.beforeActivityId, + urlType: params.urlType, }); if (result.isErr()) { diff --git a/src/modules/feeds/infrastructure/http/controllers/GetGlobalFeedController.ts b/src/modules/feeds/infrastructure/http/controllers/GetGlobalFeedController.ts index aa2ae1395..148813373 100644 --- a/src/modules/feeds/infrastructure/http/controllers/GetGlobalFeedController.ts +++ b/src/modules/feeds/infrastructure/http/controllers/GetGlobalFeedController.ts @@ -10,6 +10,7 @@ const querySchema = z.object({ page: z.coerce.number().int().positive().optional(), limit: z.coerce.number().int().positive().max(100).optional(), beforeActivityId: z.string().optional(), + urlType: z.string().optional(), }); export class GetGlobalFeedController extends Controller { @@ -33,6 +34,7 @@ export class GetGlobalFeedController extends Controller { page: params.page || 1, limit: params.limit || 20, beforeActivityId: params.beforeActivityId, + urlType: params.urlType, }); if (result.isErr()) { diff --git a/src/modules/feeds/infrastructure/repositories/DrizzleFeedRepository.ts b/src/modules/feeds/infrastructure/repositories/DrizzleFeedRepository.ts index 3bc0cd23b..f6292ba82 100644 --- a/src/modules/feeds/infrastructure/repositories/DrizzleFeedRepository.ts +++ b/src/modules/feeds/infrastructure/repositories/DrizzleFeedRepository.ts @@ -27,6 +27,7 @@ export class DrizzleFeedRepository implements IFeedRepository { actorId: dto.actorId, type: dto.type, metadata: dto.metadata, + urlType: dto.urlType, createdAt: dto.createdAt, }); @@ -49,9 +50,16 @@ export class DrizzleFeedRepository implements IFeedRepository { actorId: string; type: string; metadata: any; + urlType: string | null; createdAt: Date; }>; + // Build where conditions + const whereConditions = []; + if (options.urlType) { + whereConditions.push(eq(feedActivities.urlType, options.urlType)); + } + if (beforeActivityId) { // Get the timestamp of the beforeActivityId const beforeActivity = await this.db @@ -61,10 +69,11 @@ export class DrizzleFeedRepository implements IFeedRepository { .limit(1); if (beforeActivity.length > 0) { + const conditions = [lt(feedActivities.createdAt, beforeActivity[0]!.createdAt), ...whereConditions]; activitiesResult = await this.db .select() .from(feedActivities) - .where(lt(feedActivities.createdAt, beforeActivity[0]!.createdAt)) + .where(conditions.length > 1 ? and(...conditions) : conditions[0]) .orderBy(desc(feedActivities.createdAt), desc(feedActivities.id)) .limit(limit); } else { @@ -73,18 +82,30 @@ export class DrizzleFeedRepository implements IFeedRepository { } } else { // Regular pagination without cursor - activitiesResult = await this.db + const query = this.db .select() - .from(feedActivities) + .from(feedActivities); + + if (whereConditions.length > 0) { + query.where(whereConditions.length > 1 ? and(...whereConditions) : whereConditions[0]); + } + + activitiesResult = await query .orderBy(desc(feedActivities.createdAt), desc(feedActivities.id)) .limit(limit) .offset(offset); } - // Get total count - const totalCountResult = await this.db + // Get total count with same filters + const countQuery = this.db .select({ count: count() }) .from(feedActivities); + + if (whereConditions.length > 0) { + countQuery.where(whereConditions.length > 1 ? and(...whereConditions) : whereConditions[0]); + } + + const totalCountResult = await countQuery; const totalCount = totalCountResult[0]?.count || 0; @@ -96,6 +117,7 @@ export class DrizzleFeedRepository implements IFeedRepository { actorId: activityData.actorId, type: activityData.type, metadata: activityData.metadata as any, + urlType: activityData.urlType || undefined, createdAt: activityData.createdAt, }; @@ -155,9 +177,16 @@ export class DrizzleFeedRepository implements IFeedRepository { actorId: string; type: string; metadata: any; + urlType: string | null; createdAt: Date; }>; + // Build where conditions for gems feed + const whereConditions = []; + if (options.urlType) { + whereConditions.push(eq(feedActivities.urlType, options.urlType)); + } + // Create the JSON array condition using jsonb_array_elements_text const arrayLiteral = `{${collectionIdStrings.map((id) => `"${id}"`).join(',')}}`; const jsonArrayCondition = sql`EXISTS ( @@ -174,15 +203,15 @@ export class DrizzleFeedRepository implements IFeedRepository { .limit(1); if (beforeActivity.length > 0) { + const conditions = [ + lt(feedActivities.createdAt, beforeActivity[0]!.createdAt), + jsonArrayCondition, + ...whereConditions + ]; activitiesResult = await this.db .select() .from(feedActivities) - .where( - and( - lt(feedActivities.createdAt, beforeActivity[0]!.createdAt), - jsonArrayCondition, - ), - ) + .where(and(...conditions)) .orderBy(desc(feedActivities.createdAt), desc(feedActivities.id)) .limit(limit); } else { @@ -191,20 +220,26 @@ export class DrizzleFeedRepository implements IFeedRepository { } } else { // Regular pagination without cursor + const conditions = [jsonArrayCondition, ...whereConditions]; activitiesResult = await this.db .select() .from(feedActivities) - .where(jsonArrayCondition) + .where(conditions.length > 1 ? and(...conditions) : conditions[0]) .orderBy(desc(feedActivities.createdAt), desc(feedActivities.id)) .limit(limit) .offset(offset); } // Get total count with same filter + const conditions = [jsonArrayCondition]; + if (options.urlType) { + conditions.push(eq(feedActivities.urlType, options.urlType)); + } + const totalCountResult = await this.db .select({ count: count() }) .from(feedActivities) - .where(jsonArrayCondition); + .where(conditions.length > 1 ? and(...conditions) : conditions[0]); const totalCount = totalCountResult[0]?.count || 0; @@ -216,6 +251,7 @@ export class DrizzleFeedRepository implements IFeedRepository { actorId: activityData.actorId, type: activityData.type, metadata: activityData.metadata as any, + urlType: activityData.urlType || undefined, createdAt: activityData.createdAt, }; @@ -266,6 +302,7 @@ export class DrizzleFeedRepository implements IFeedRepository { actorId: activityData.actorId, type: activityData.type, metadata: activityData.metadata as any, + urlType: activityData.urlType || undefined, createdAt: activityData.createdAt, }; diff --git a/src/modules/feeds/infrastructure/repositories/mappers/FeedActivityMapper.ts b/src/modules/feeds/infrastructure/repositories/mappers/FeedActivityMapper.ts index 8f7b08379..d7115d261 100644 --- a/src/modules/feeds/infrastructure/repositories/mappers/FeedActivityMapper.ts +++ b/src/modules/feeds/infrastructure/repositories/mappers/FeedActivityMapper.ts @@ -11,6 +11,7 @@ import { import { CuratorId } from '../../../../cards/domain/value-objects/CuratorId'; import { CardId } from '../../../../cards/domain/value-objects/CardId'; import { CollectionId } from '../../../../cards/domain/value-objects/CollectionId'; +import { UrlType } from '../../../../cards/domain/value-objects/UrlType'; import { err, ok, Result } from '../../../../../shared/core/Result'; // Database representation of a feed activity @@ -19,6 +20,7 @@ export interface FeedActivityDTO { actorId: string; type: string; metadata: ActivityMetadata; + urlType?: string; createdAt: Date; } @@ -53,10 +55,16 @@ export class FeedActivityMapper { collectionIds = collectionIdResults.map((result) => result.unwrap()); } + let urlType: UrlType | undefined; + if (dto.urlType) { + urlType = dto.urlType as UrlType; + } + const activityResult = FeedActivity.createCardCollected( actorIdResult.value, cardIdResult.value, collectionIds, + urlType, dto.createdAt, new UniqueEntityID(dto.id), ); @@ -78,6 +86,7 @@ export class FeedActivityMapper { actorId: activity.actorId.value, type: activity.type.value, metadata: activity.metadata, + urlType: activity.urlType, createdAt: activity.createdAt, }; } diff --git a/src/modules/feeds/infrastructure/repositories/schema/feedActivity.sql.ts b/src/modules/feeds/infrastructure/repositories/schema/feedActivity.sql.ts index 1fa16ab4f..1e41f7240 100644 --- a/src/modules/feeds/infrastructure/repositories/schema/feedActivity.sql.ts +++ b/src/modules/feeds/infrastructure/repositories/schema/feedActivity.sql.ts @@ -5,5 +5,6 @@ export const feedActivities = pgTable('feed_activities', { actorId: text('actor_id').notNull(), // The DID of the user who performed the activity type: text('type').notNull(), // The type of activity (e.g., 'CARD_COLLECTED') metadata: jsonb('metadata').notNull(), // Activity-specific metadata + urlType: text('url_type'), // Optional URL type from the card createdAt: timestamp('created_at').notNull().defaultNow(), }); diff --git a/src/modules/feeds/tests/infrastructure/InMemoryFeedRepository.ts b/src/modules/feeds/tests/infrastructure/InMemoryFeedRepository.ts index 9fecabf73..2ca0d0787 100644 --- a/src/modules/feeds/tests/infrastructure/InMemoryFeedRepository.ts +++ b/src/modules/feeds/tests/infrastructure/InMemoryFeedRepository.ts @@ -42,9 +42,16 @@ export class InMemoryFeedRepository implements IFeedRepository { options: FeedQueryOptions, ): Promise> { try { - const { page, limit, beforeActivityId } = options; + const { page, limit, beforeActivityId, urlType } = options; let filteredActivities = [...this.activities]; + // Filter by URL type if provided + if (urlType) { + filteredActivities = filteredActivities.filter( + (activity) => activity.urlType === urlType, + ); + } + // Filter by cursor if provided if (beforeActivityId) { const beforeIndex = filteredActivities.findIndex((activity) => @@ -87,7 +94,7 @@ export class InMemoryFeedRepository implements IFeedRepository { options: FeedQueryOptions, ): Promise> { try { - const { page, limit, beforeActivityId } = options; + const { page, limit, beforeActivityId, urlType } = options; const collectionIdStrings = collectionIds.map((id) => id.getStringValue(), ); @@ -105,6 +112,13 @@ export class InMemoryFeedRepository implements IFeedRepository { return false; }); + // Filter by URL type if provided + if (urlType) { + filteredActivities = filteredActivities.filter( + (activity) => activity.urlType === urlType, + ); + } + // Apply cursor filtering if needed if (beforeActivityId) { const beforeIndex = filteredActivities.findIndex((activity) => diff --git a/src/shared/infrastructure/http/factories/UseCaseFactory.ts b/src/shared/infrastructure/http/factories/UseCaseFactory.ts index 212a2730b..e7d242d2f 100644 --- a/src/shared/infrastructure/http/factories/UseCaseFactory.ts +++ b/src/shared/infrastructure/http/factories/UseCaseFactory.ts @@ -268,6 +268,7 @@ export class UseCaseFactory { ), addActivityToFeedUseCase: new AddActivityToFeedUseCase( services.feedService, + repositories.cardRepository, ), // Search use cases getSimilarUrlsForUrlUseCase: new GetSimilarUrlsForUrlUseCase( @@ -300,6 +301,7 @@ export class UseCaseFactory { // Feed use cases (only ones needed by workers) addActivityToFeedUseCase: new AddActivityToFeedUseCase( services.feedService, + repositories.cardRepository, ), // Search use cases (only ones needed by workers) indexUrlForSearchUseCase: new IndexUrlForSearchUseCase( diff --git a/src/types/src/api/requests.ts b/src/types/src/api/requests.ts index 904b01e38..f95a00761 100644 --- a/src/types/src/api/requests.ts +++ b/src/types/src/api/requests.ts @@ -112,6 +112,7 @@ export interface GetCollectionPageByAtUriParams extends PaginatedSortedParams { export interface GetGlobalFeedParams extends PaginationParams { beforeActivityId?: string; // For cursor-based pagination + urlType?: UrlType; // Filter by URL type } export interface LoginWithAppPasswordRequest { @@ -164,6 +165,7 @@ export interface GetSimilarUrlsForUrlParams extends PaginatedSortedParams { export interface GetGemActivityFeedParams extends PaginationParams { // Removed beforeActivityId since we're using page-based pagination + urlType?: UrlType; // Filter by URL type } export interface SearchCollectionsParams extends PaginatedSortedParams { diff --git a/src/webapp/api-client/clients/FeedClient.ts b/src/webapp/api-client/clients/FeedClient.ts index 1567c1048..865d2c358 100644 --- a/src/webapp/api-client/clients/FeedClient.ts +++ b/src/webapp/api-client/clients/FeedClient.ts @@ -14,6 +14,7 @@ export class FeedClient extends BaseClient { if (params?.limit) searchParams.set('limit', params.limit.toString()); if (params?.beforeActivityId) searchParams.set('beforeActivityId', params.beforeActivityId); + if (params?.urlType) searchParams.set('urlType', params.urlType); const queryString = searchParams.toString(); const endpoint = queryString @@ -29,6 +30,7 @@ export class FeedClient extends BaseClient { const searchParams = new URLSearchParams(); if (params?.page) searchParams.set('page', params.page.toString()); if (params?.limit) searchParams.set('limit', params.limit.toString()); + if (params?.urlType) searchParams.set('urlType', params.urlType); const queryString = searchParams.toString(); const endpoint = queryString -- knotmirror 0.0.1 From b760d4ea321502d9ac61bb03bb6fa63dbcfe6ae6 Mon Sep 17 00:00:00 2001 From: Wesley Finck Date: Tue, 23 Dec 2025 21:03:24 +0800 Subject: [PATCH 09/18] feat: update FeedService to support urlType parameter Co-authored-by: aider (anthropic/claude-sonnet-4-20250514) --- src/modules/feeds/domain/services/FeedService.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/modules/feeds/domain/services/FeedService.ts b/src/modules/feeds/domain/services/FeedService.ts index 54bf9688b..659834b41 100644 --- a/src/modules/feeds/domain/services/FeedService.ts +++ b/src/modules/feeds/domain/services/FeedService.ts @@ -5,6 +5,7 @@ import { IFeedRepository } from '../IFeedRepository'; import { CuratorId } from '../../../cards/domain/value-objects/CuratorId'; import { CardId } from '../../../cards/domain/value-objects/CardId'; import { CollectionId } from '../../../cards/domain/value-objects/CollectionId'; +import { UrlType } from '../../../cards/domain/value-objects/UrlType'; export class FeedServiceError extends Error { constructor(message: string) { @@ -20,12 +21,14 @@ export class FeedService implements DomainService { actorId: CuratorId, cardId: CardId, collectionIds?: CollectionId[], + urlType?: UrlType, ): Promise> { try { const activityResult = FeedActivity.createCardCollected( actorId, cardId, collectionIds, + urlType, ); if (activityResult.isErr()) { -- knotmirror 0.0.1 From 164059af7c70e492bccfe54fa0abf88161e88165 Mon Sep 17 00:00:00 2001 From: Wesley Finck Date: Tue, 23 Dec 2025 21:06:03 +0800 Subject: [PATCH 10/18] perf: add performance indexes for feed activities query optimization Co-authored-by: aider (anthropic/claude-sonnet-4-20250514) --- .../repositories/schema/feedActivity.sql.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/modules/feeds/infrastructure/repositories/schema/feedActivity.sql.ts b/src/modules/feeds/infrastructure/repositories/schema/feedActivity.sql.ts index 1e41f7240..87221c80a 100644 --- a/src/modules/feeds/infrastructure/repositories/schema/feedActivity.sql.ts +++ b/src/modules/feeds/infrastructure/repositories/schema/feedActivity.sql.ts @@ -1,4 +1,4 @@ -import { pgTable, text, timestamp, jsonb, uuid } from 'drizzle-orm/pg-core'; +import { pgTable, text, timestamp, jsonb, uuid, index } from 'drizzle-orm/pg-core'; export const feedActivities = pgTable('feed_activities', { id: uuid('id').primaryKey(), @@ -7,4 +7,15 @@ export const feedActivities = pgTable('feed_activities', { metadata: jsonb('metadata').notNull(), // Activity-specific metadata urlType: text('url_type'), // Optional URL type from the card createdAt: timestamp('created_at').notNull().defaultNow(), -}); +}, (table) => ({ + // Index for filtering by activity type + typeIdx: index('feed_activities_type_idx').on(table.type), + // Index for filtering by URL type + urlTypeIdx: index('feed_activities_url_type_idx').on(table.urlType), + // Index for sorting by creation date (most recent first) + createdAtIdx: index('feed_activities_created_at_idx').on(table.createdAt.desc()), + // Composite index for common query patterns (type + createdAt) + typeCreatedAtIdx: index('feed_activities_type_created_at_idx').on(table.type, table.createdAt.desc()), + // Composite index for URL type filtering with date sorting + urlTypeCreatedAtIdx: index('feed_activities_url_type_created_at_idx').on(table.urlType, table.createdAt.desc()), +})); -- knotmirror 0.0.1 From e9ff62103ade67aed794633635714b53c28b2a2f Mon Sep 17 00:00:00 2001 From: Wesley Finck Date: Tue, 23 Dec 2025 21:17:59 +0800 Subject: [PATCH 11/18] feat: add composite index for type, url type, and created at in feed activities Co-authored-by: aider (anthropic/claude-sonnet-4-20250514) --- .../tests/test-utils/createTestSchema.ts | 19 +++++++++++++++++-- .../repositories/schema/feedActivity.sql.ts | 2 ++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/modules/cards/tests/test-utils/createTestSchema.ts b/src/modules/cards/tests/test-utils/createTestSchema.ts index b5d8fba6b..0ec0bce9a 100644 --- a/src/modules/cards/tests/test-utils/createTestSchema.ts +++ b/src/modules/cards/tests/test-utils/createTestSchema.ts @@ -153,10 +153,25 @@ export async function createTestSchema(db: PostgresJsDatabase) { CREATE INDEX IF NOT EXISTS idx_collection_cards_card_collection ON collection_cards(card_id) INCLUDE (collection_id) `); + // Feed activities indexes await db.execute(sql` - CREATE INDEX IF NOT EXISTS idx_feed_activities_created_at ON feed_activities(created_at DESC); + CREATE INDEX IF NOT EXISTS feed_activities_type_idx ON feed_activities(type); + `); + await db.execute(sql` + CREATE INDEX IF NOT EXISTS feed_activities_url_type_idx ON feed_activities(url_type); + `); + await db.execute(sql` + CREATE INDEX IF NOT EXISTS feed_activities_created_at_idx ON feed_activities(created_at DESC); + `); + await db.execute(sql` + CREATE INDEX IF NOT EXISTS feed_activities_type_created_at_idx ON feed_activities(type, created_at DESC); + `); + await db.execute(sql` + CREATE INDEX IF NOT EXISTS feed_activities_url_type_created_at_idx ON feed_activities(url_type, created_at DESC); + `); + await db.execute(sql` + CREATE INDEX IF NOT EXISTS feed_activities_type_url_type_created_at_idx ON feed_activities(type, url_type, created_at DESC); `); - await db.execute(sql` CREATE INDEX IF NOT EXISTS idx_feed_activities_actor_id ON feed_activities(actor_id); `); diff --git a/src/modules/feeds/infrastructure/repositories/schema/feedActivity.sql.ts b/src/modules/feeds/infrastructure/repositories/schema/feedActivity.sql.ts index 87221c80a..89bd57464 100644 --- a/src/modules/feeds/infrastructure/repositories/schema/feedActivity.sql.ts +++ b/src/modules/feeds/infrastructure/repositories/schema/feedActivity.sql.ts @@ -18,4 +18,6 @@ export const feedActivities = pgTable('feed_activities', { typeCreatedAtIdx: index('feed_activities_type_created_at_idx').on(table.type, table.createdAt.desc()), // Composite index for URL type filtering with date sorting urlTypeCreatedAtIdx: index('feed_activities_url_type_created_at_idx').on(table.urlType, table.createdAt.desc()), + // Composite index for filtering by both type and URL type with date sorting + typeUrlTypeCreatedAtIdx: index('feed_activities_type_url_type_created_at_idx').on(table.type, table.urlType, table.createdAt.desc()), })); -- knotmirror 0.0.1 From 91fa18a7d24b2567d9879c2130addfb50734d1c3 Mon Sep 17 00:00:00 2001 From: Wesley Finck Date: Tue, 23 Dec 2025 21:27:14 +0800 Subject: [PATCH 12/18] fix: correct import paths and add missing parameter for FeedActivity.createCardCollected Co-authored-by: aider (anthropic/claude-sonnet-4-20250514) --- .../useCases/queries/GetGemActivityFeedUseCase.ts | 2 +- .../useCases/queries/GetGlobalFeedUseCase.ts | 2 +- .../DrizzleFeedRepository.integration.test.ts | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/modules/feeds/application/useCases/queries/GetGemActivityFeedUseCase.ts b/src/modules/feeds/application/useCases/queries/GetGemActivityFeedUseCase.ts index e0b2c3b6c..bc28ac0ac 100644 --- a/src/modules/feeds/application/useCases/queries/GetGemActivityFeedUseCase.ts +++ b/src/modules/feeds/application/useCases/queries/GetGemActivityFeedUseCase.ts @@ -16,7 +16,7 @@ import { SortOrder, } from 'src/modules/cards/domain/ICollectionQueryRepository'; import { CollectionId } from 'src/modules/cards/domain/value-objects/CollectionId'; -import { UrlType } from '../../../cards/domain/value-objects/UrlType'; +import { UrlType } from '../../../../cards/domain/value-objects/UrlType'; import { GetGlobalFeedResponse, FeedItem } from '@semble/types'; export interface GetGemActivityFeedQuery { diff --git a/src/modules/feeds/application/useCases/queries/GetGlobalFeedUseCase.ts b/src/modules/feeds/application/useCases/queries/GetGlobalFeedUseCase.ts index 9d2e79057..0d9004959 100644 --- a/src/modules/feeds/application/useCases/queries/GetGlobalFeedUseCase.ts +++ b/src/modules/feeds/application/useCases/queries/GetGlobalFeedUseCase.ts @@ -11,7 +11,7 @@ import { } from '../../../../cards/domain/ICardQueryRepository'; import { ICollectionRepository } from 'src/modules/cards/domain/ICollectionRepository'; import { CollectionId } from 'src/modules/cards/domain/value-objects/CollectionId'; -import { UrlType } from '../../../cards/domain/value-objects/UrlType'; +import { UrlType } from '../../../../cards/domain/value-objects/UrlType'; import { GetGlobalFeedResponse, FeedItem } from '@semble/types'; export interface GetGlobalFeedQuery { diff --git a/src/modules/feeds/tests/infrastructure/DrizzleFeedRepository.integration.test.ts b/src/modules/feeds/tests/infrastructure/DrizzleFeedRepository.integration.test.ts index 882f296f6..2c28771a7 100644 --- a/src/modules/feeds/tests/infrastructure/DrizzleFeedRepository.integration.test.ts +++ b/src/modules/feeds/tests/infrastructure/DrizzleFeedRepository.integration.test.ts @@ -116,6 +116,7 @@ describe('DrizzleFeedRepository', () => { curatorId, cardId, [collectionId], + undefined, new Date(baseTime.getTime() - 200), // oldest ).unwrap(); @@ -123,6 +124,7 @@ describe('DrizzleFeedRepository', () => { anotherCuratorId, anotherCardId, undefined, + undefined, new Date(baseTime.getTime() - 100), // middle ).unwrap(); @@ -130,6 +132,7 @@ describe('DrizzleFeedRepository', () => { curatorId, anotherCardId, [collectionId], + undefined, new Date(baseTime.getTime()), // newest ).unwrap(); @@ -181,6 +184,7 @@ describe('DrizzleFeedRepository', () => { curatorId, cardId, undefined, + undefined, new Date(baseTime.getTime() - 200), // oldest ).unwrap(); @@ -188,6 +192,7 @@ describe('DrizzleFeedRepository', () => { anotherCuratorId, anotherCardId, undefined, + undefined, new Date(baseTime.getTime() - 100), // middle ).unwrap(); @@ -195,6 +200,7 @@ describe('DrizzleFeedRepository', () => { curatorId, anotherCardId, undefined, + undefined, new Date(baseTime.getTime()), // newest ).unwrap(); @@ -321,6 +327,7 @@ describe('DrizzleFeedRepository', () => { curatorId, cardId, [collection1], + undefined, new Date(baseTime.getTime() - 300), ).unwrap(); @@ -329,6 +336,7 @@ describe('DrizzleFeedRepository', () => { anotherCuratorId, anotherCardId, [collection2], + undefined, new Date(baseTime.getTime() - 200), ).unwrap(); @@ -337,6 +345,7 @@ describe('DrizzleFeedRepository', () => { curatorId, anotherCardId, [collection1, collection3], + undefined, new Date(baseTime.getTime() - 100), ).unwrap(); @@ -345,6 +354,7 @@ describe('DrizzleFeedRepository', () => { anotherCuratorId, cardId, undefined, + undefined, new Date(baseTime.getTime()), ).unwrap(); @@ -419,6 +429,7 @@ describe('DrizzleFeedRepository', () => { curatorId, CardId.createFromString(`card-${i}`).unwrap(), [collection1], + undefined, new Date(baseTime.getTime() - i * 100), ).unwrap(); activities.push(activity); @@ -469,6 +480,7 @@ describe('DrizzleFeedRepository', () => { curatorId, cardId, [collection1], + undefined, new Date(baseTime.getTime() - 300), // oldest ).unwrap(); @@ -476,6 +488,7 @@ describe('DrizzleFeedRepository', () => { anotherCuratorId, anotherCardId, [collection1], + undefined, new Date(baseTime.getTime() - 200), // middle ).unwrap(); @@ -483,6 +496,7 @@ describe('DrizzleFeedRepository', () => { curatorId, CardId.createFromString('card-999').unwrap(), [collection1], + undefined, new Date(baseTime.getTime() - 100), // newest ).unwrap(); -- knotmirror 0.0.1 From d643fbce2a502631a97f67c6b8fce1f097040256 Mon Sep 17 00:00:00 2001 From: Wesley Finck Date: Tue, 23 Dec 2025 21:52:39 +0800 Subject: [PATCH 13/18] properly access card url type --- .../useCases/commands/AddActivityToFeedUseCase.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/modules/feeds/application/useCases/commands/AddActivityToFeedUseCase.ts b/src/modules/feeds/application/useCases/commands/AddActivityToFeedUseCase.ts index 3500db41b..4b5c8edba 100644 --- a/src/modules/feeds/application/useCases/commands/AddActivityToFeedUseCase.ts +++ b/src/modules/feeds/application/useCases/commands/AddActivityToFeedUseCase.ts @@ -93,14 +93,18 @@ export class AddActivityToFeedUseCase // Fetch the card to get its URL type const cardResult = await this.cardRepository.findById(cardId); if (cardResult.isErr()) { - return err(new ValidationError(`Failed to fetch card: ${cardResult.error.message}`)); + return err( + new ValidationError( + `Failed to fetch card: ${cardResult.error.message}`, + ), + ); } let urlType; if (cardResult.value && cardResult.value.isUrlCard) { - const urlCardContent = cardResult.value.content as any; - if (urlCardContent.metadata?.type) { - urlType = urlCardContent.metadata.type; + const urlCardContent = cardResult.value.content; + if (urlCardContent.urlContent?.metadata?.type) { + urlType = urlCardContent.urlContent.metadata.type; } } -- knotmirror 0.0.1 From a948eb1854858cf2f2df0601a062a3cf1e681cec Mon Sep 17 00:00:00 2001 From: Wesley Finck Date: Tue, 23 Dec 2025 21:52:54 +0800 Subject: [PATCH 14/18] update the db migration files --- .../migrations/0011_quick_reavers.sql | 7 + .../migrations/meta/0011_snapshot.json | 1295 +++++++++++++++++ .../database/migrations/meta/_journal.json | 7 + 3 files changed, 1309 insertions(+) create mode 100644 src/shared/infrastructure/database/migrations/0011_quick_reavers.sql create mode 100644 src/shared/infrastructure/database/migrations/meta/0011_snapshot.json diff --git a/src/shared/infrastructure/database/migrations/0011_quick_reavers.sql b/src/shared/infrastructure/database/migrations/0011_quick_reavers.sql new file mode 100644 index 000000000..4f448ab15 --- /dev/null +++ b/src/shared/infrastructure/database/migrations/0011_quick_reavers.sql @@ -0,0 +1,7 @@ +ALTER TABLE "feed_activities" ADD COLUMN "url_type" text;--> statement-breakpoint +CREATE INDEX "feed_activities_type_idx" ON "feed_activities" USING btree ("type");--> statement-breakpoint +CREATE INDEX "feed_activities_url_type_idx" ON "feed_activities" USING btree ("url_type");--> statement-breakpoint +CREATE INDEX "feed_activities_created_at_idx" ON "feed_activities" USING btree ("created_at" DESC NULLS LAST);--> statement-breakpoint +CREATE INDEX "feed_activities_type_created_at_idx" ON "feed_activities" USING btree ("type","created_at" DESC NULLS LAST);--> statement-breakpoint +CREATE INDEX "feed_activities_url_type_created_at_idx" ON "feed_activities" USING btree ("url_type","created_at" DESC NULLS LAST);--> statement-breakpoint +CREATE INDEX "feed_activities_type_url_type_created_at_idx" ON "feed_activities" USING btree ("type","url_type","created_at" DESC NULLS LAST); \ No newline at end of file diff --git a/src/shared/infrastructure/database/migrations/meta/0011_snapshot.json b/src/shared/infrastructure/database/migrations/meta/0011_snapshot.json new file mode 100644 index 000000000..8faf164cd --- /dev/null +++ b/src/shared/infrastructure/database/migrations/meta/0011_snapshot.json @@ -0,0 +1,1295 @@ +{ + "id": "c81b5c59-8f8c-40d6-9538-48ba1e4701e6", + "prevId": "b6b80ebe-71aa-4189-bedd-1ee7cb6d9e46", + "version": "7", + "dialect": "postgresql", + "tables": { + "public.app_password_sessions": { + "name": "app_password_sessions", + "schema": "", + "columns": { + "did": { + "name": "did", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "session_data": { + "name": "session_data", + "type": "jsonb", + "primaryKey": false, + "notNull": true + }, + "app_password": { + "name": "app_password", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.cards": { + "name": "cards", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true + }, + "author_id": { + "name": "author_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "content_data": { + "name": "content_data", + "type": "jsonb", + "primaryKey": false, + "notNull": true + }, + "url": { + "name": "url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "url_type": { + "name": "url_type", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "parent_card_id": { + "name": "parent_card_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "via_card_id": { + "name": "via_card_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "published_record_id": { + "name": "published_record_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "library_count": { + "name": "library_count", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "cards_author_url_idx": { + "name": "cards_author_url_idx", + "columns": [ + { + "expression": "author_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "url", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "cards_author_id_idx": { + "name": "cards_author_id_idx", + "columns": [ + { + "expression": "author_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_cards_type_updated_at": { + "name": "idx_cards_type_updated_at", + "columns": [ + { + "expression": "type", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "updated_at", + "isExpression": false, + "asc": false, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_cards_url_type": { + "name": "idx_cards_url_type", + "columns": [ + { + "expression": "url", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "type", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_cards_url_type_filter": { + "name": "idx_cards_url_type_filter", + "columns": [ + { + "expression": "url_type", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_cards_parent_type": { + "name": "idx_cards_parent_type", + "columns": [ + { + "expression": "parent_card_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "type", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "where": "type = 'NOTE'", + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "cards_parent_card_id_cards_id_fk": { + "name": "cards_parent_card_id_cards_id_fk", + "tableFrom": "cards", + "tableTo": "cards", + "columnsFrom": [ + "parent_card_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "cards_via_card_id_cards_id_fk": { + "name": "cards_via_card_id_cards_id_fk", + "tableFrom": "cards", + "tableTo": "cards", + "columnsFrom": [ + "via_card_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "cards_published_record_id_published_records_id_fk": { + "name": "cards_published_record_id_published_records_id_fk", + "tableFrom": "cards", + "tableTo": "published_records", + "columnsFrom": [ + "published_record_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.collection_cards": { + "name": "collection_cards", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true + }, + "collection_id": { + "name": "collection_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "card_id": { + "name": "card_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "added_by": { + "name": "added_by", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "added_at": { + "name": "added_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "via_card_id": { + "name": "via_card_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "published_record_id": { + "name": "published_record_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "collection_cards_card_id_idx": { + "name": "collection_cards_card_id_idx", + "columns": [ + { + "expression": "card_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "collection_cards_collection_id_idx": { + "name": "collection_cards_collection_id_idx", + "columns": [ + { + "expression": "collection_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_collection_cards_collection_added": { + "name": "idx_collection_cards_collection_added", + "columns": [ + { + "expression": "collection_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "added_at", + "isExpression": false, + "asc": false, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_collection_cards_card_collection": { + "name": "idx_collection_cards_card_collection", + "columns": [ + { + "expression": "card_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "collection_cards_collection_id_collections_id_fk": { + "name": "collection_cards_collection_id_collections_id_fk", + "tableFrom": "collection_cards", + "tableTo": "collections", + "columnsFrom": [ + "collection_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "collection_cards_card_id_cards_id_fk": { + "name": "collection_cards_card_id_cards_id_fk", + "tableFrom": "collection_cards", + "tableTo": "cards", + "columnsFrom": [ + "card_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "collection_cards_via_card_id_cards_id_fk": { + "name": "collection_cards_via_card_id_cards_id_fk", + "tableFrom": "collection_cards", + "tableTo": "cards", + "columnsFrom": [ + "via_card_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "collection_cards_published_record_id_published_records_id_fk": { + "name": "collection_cards_published_record_id_published_records_id_fk", + "tableFrom": "collection_cards", + "tableTo": "published_records", + "columnsFrom": [ + "published_record_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.collection_collaborators": { + "name": "collection_collaborators", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true + }, + "collection_id": { + "name": "collection_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "collaborator_id": { + "name": "collaborator_id", + "type": "text", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": { + "collection_collaborators_collection_id_collections_id_fk": { + "name": "collection_collaborators_collection_id_collections_id_fk", + "tableFrom": "collection_collaborators", + "tableTo": "collections", + "columnsFrom": [ + "collection_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.collections": { + "name": "collections", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true + }, + "author_id": { + "name": "author_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "access_type": { + "name": "access_type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "card_count": { + "name": "card_count", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "published_record_id": { + "name": "published_record_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "collections_author_id_idx": { + "name": "collections_author_id_idx", + "columns": [ + { + "expression": "author_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "collections_author_updated_at_idx": { + "name": "collections_author_updated_at_idx", + "columns": [ + { + "expression": "author_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "updated_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "collections_published_record_id_published_records_id_fk": { + "name": "collections_published_record_id_published_records_id_fk", + "tableFrom": "collections", + "tableTo": "published_records", + "columnsFrom": [ + "published_record_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.library_memberships": { + "name": "library_memberships", + "schema": "", + "columns": { + "card_id": { + "name": "card_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "added_at": { + "name": "added_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "published_record_id": { + "name": "published_record_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "idx_user_cards": { + "name": "idx_user_cards", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_card_users": { + "name": "idx_card_users", + "columns": [ + { + "expression": "card_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_library_memberships_user_type_covering": { + "name": "idx_library_memberships_user_type_covering", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "added_at", + "isExpression": false, + "asc": false, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "library_memberships_card_id_cards_id_fk": { + "name": "library_memberships_card_id_cards_id_fk", + "tableFrom": "library_memberships", + "tableTo": "cards", + "columnsFrom": [ + "card_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "library_memberships_published_record_id_published_records_id_fk": { + "name": "library_memberships_published_record_id_published_records_id_fk", + "tableFrom": "library_memberships", + "tableTo": "published_records", + "columnsFrom": [ + "published_record_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "library_memberships_card_id_user_id_pk": { + "name": "library_memberships_card_id_user_id_pk", + "columns": [ + "card_id", + "user_id" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.published_records": { + "name": "published_records", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true + }, + "uri": { + "name": "uri", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "cid": { + "name": "cid", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "recorded_at": { + "name": "recorded_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "uri_cid_unique_idx": { + "name": "uri_cid_unique_idx", + "columns": [ + { + "expression": "uri", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "cid", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "published_records_uri_idx": { + "name": "published_records_uri_idx", + "columns": [ + { + "expression": "uri", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.feed_activities": { + "name": "feed_activities", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true + }, + "actor_id": { + "name": "actor_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "primaryKey": false, + "notNull": true + }, + "url_type": { + "name": "url_type", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "feed_activities_type_idx": { + "name": "feed_activities_type_idx", + "columns": [ + { + "expression": "type", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "feed_activities_url_type_idx": { + "name": "feed_activities_url_type_idx", + "columns": [ + { + "expression": "url_type", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "feed_activities_created_at_idx": { + "name": "feed_activities_created_at_idx", + "columns": [ + { + "expression": "created_at", + "isExpression": false, + "asc": false, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "feed_activities_type_created_at_idx": { + "name": "feed_activities_type_created_at_idx", + "columns": [ + { + "expression": "type", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": false, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "feed_activities_url_type_created_at_idx": { + "name": "feed_activities_url_type_created_at_idx", + "columns": [ + { + "expression": "url_type", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": false, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "feed_activities_type_url_type_created_at_idx": { + "name": "feed_activities_type_url_type_created_at_idx", + "columns": [ + { + "expression": "type", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "url_type", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": false, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.notifications": { + "name": "notifications", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true + }, + "recipient_user_id": { + "name": "recipient_user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "actor_user_id": { + "name": "actor_user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "primaryKey": false, + "notNull": true + }, + "read": { + "name": "read", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "notifications_recipient_idx": { + "name": "notifications_recipient_idx", + "columns": [ + { + "expression": "recipient_user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "notifications_recipient_created_at_idx": { + "name": "notifications_recipient_created_at_idx", + "columns": [ + { + "expression": "recipient_user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": false, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "notifications_recipient_read_idx": { + "name": "notifications_recipient_read_idx", + "columns": [ + { + "expression": "recipient_user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "read", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.auth_session": { + "name": "auth_session", + "schema": "", + "columns": { + "key": { + "name": "key", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "session": { + "name": "session", + "type": "text", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.auth_state": { + "name": "auth_state", + "schema": "", + "columns": { + "key": { + "name": "key", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "state": { + "name": "state", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.auth_refresh_tokens": { + "name": "auth_refresh_tokens", + "schema": "", + "columns": { + "token_id": { + "name": "token_id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "user_did": { + "name": "user_did", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "refresh_token": { + "name": "refresh_token", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "issued_at": { + "name": "issued_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true + }, + "expires_at": { + "name": "expires_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true + }, + "revoked": { + "name": "revoked", + "type": "boolean", + "primaryKey": false, + "notNull": false, + "default": false + } + }, + "indexes": {}, + "foreignKeys": { + "auth_refresh_tokens_user_did_users_id_fk": { + "name": "auth_refresh_tokens_user_did_users_id_fk", + "tableFrom": "auth_refresh_tokens", + "tableTo": "users", + "columnsFrom": [ + "user_did" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.users": { + "name": "users", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "handle": { + "name": "handle", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "linked_at": { + "name": "linked_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true + }, + "last_login_at": { + "name": "last_login_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + } + }, + "enums": {}, + "schemas": {}, + "sequences": {}, + "roles": {}, + "policies": {}, + "views": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/src/shared/infrastructure/database/migrations/meta/_journal.json b/src/shared/infrastructure/database/migrations/meta/_journal.json index 4441f6d0e..991007998 100644 --- a/src/shared/infrastructure/database/migrations/meta/_journal.json +++ b/src/shared/infrastructure/database/migrations/meta/_journal.json @@ -78,6 +78,13 @@ "when": 1766492444234, "tag": "0010_boring_blazing_skull", "breakpoints": true + }, + { + "idx": 11, + "version": "7", + "when": 1766497443674, + "tag": "0011_quick_reavers", + "breakpoints": true } ] } \ No newline at end of file -- knotmirror 0.0.1 From 460f6e12e435daa9ecdeadf789982f59c3f7d2f6 Mon Sep 17 00:00:00 2001 From: Wesley Finck Date: Tue, 23 Dec 2025 21:55:04 +0800 Subject: [PATCH 15/18] formatting --- .../logs/20251223_card_url_type_migration.md | 13 ++- .../repositories/mappers/CardMapper.ts | 5 +- .../CollectionCardQueryService.ts | 2 +- .../query-services/UrlCardQueryService.ts | 2 +- .../utils/InMemoryCardQueryRepository.ts | 13 +-- .../repositories/DrizzleFeedRepository.ts | 33 ++++-- .../repositories/schema/feedActivity.sql.ts | 65 +++++++---- .../migrations/meta/0010_snapshot.json | 103 +++++------------- .../migrations/meta/0011_snapshot.json | 103 +++++------------- .../database/migrations/meta/_journal.json | 2 +- 10 files changed, 136 insertions(+), 205 deletions(-) diff --git a/.agent/logs/20251223_card_url_type_migration.md b/.agent/logs/20251223_card_url_type_migration.md index ec4036377..5f863fa67 100644 --- a/.agent/logs/20251223_card_url_type_migration.md +++ b/.agent/logs/20251223_card_url_type_migration.md @@ -1,7 +1,7 @@ # Card URL Type Migration Plan **Date:** 2025-12-23 -**Status:** Planning Phase +**Status:** Planning Phase ## Overview @@ -10,18 +10,21 @@ Adding a dedicated `urlType` field to the cards table to enable efficient queryi ## Approach: Incremental Migration ### Phase 1: Add Optional Field (Immediate) + - Add `url_type` TEXT column to cards table (nullable) - Update CardMapper to populate urlType for new cards - Add indexes for performance - New cards will be immediately queryable by type ### Phase 2: Hybrid Queries (Temporary) + - Update query services to handle mixed data: - Fast indexed lookup for new cards (urlType field) - Fallback JSON query for old cards (contentData.metadata.type) - Use OR conditions to support both data states ### Phase 3: Backfill Migration (Later) + - Run SQL update to populate urlType for existing cards - Remove fallback JSON queries once migration complete - Simplify query logic to use only indexed field @@ -31,7 +34,7 @@ Adding a dedicated `urlType` field to the cards table to enable efficient queryi ✅ **Zero downtime** - No breaking changes ✅ **Immediate value** - New cards queryable by type right away ✅ **Gradual migration** - Can backfill when convenient -✅ **Performance** - Indexed queries for new data +✅ **Performance** - Indexed queries for new data ## Implementation Notes @@ -42,10 +45,10 @@ Adding a dedicated `urlType` field to the cards table to enable efficient queryi ## Migration SQL (for Phase 3) ```sql -UPDATE cards +UPDATE cards SET url_type = content_data #>> '{metadata,type}' -WHERE type = 'URL' - AND url_type IS NULL +WHERE type = 'URL' + AND url_type IS NULL AND content_data #>> '{metadata,type}' IS NOT NULL; ``` diff --git a/src/modules/cards/infrastructure/repositories/mappers/CardMapper.ts b/src/modules/cards/infrastructure/repositories/mappers/CardMapper.ts index 8bcaa598f..c8d646dab 100644 --- a/src/modules/cards/infrastructure/repositories/mappers/CardMapper.ts +++ b/src/modules/cards/infrastructure/repositories/mappers/CardMapper.ts @@ -346,7 +346,10 @@ export class CardMapper { type: card.type.value, contentData, url: card.url?.value, - urlType: content.type === CardTypeEnum.URL ? content.urlContent?.metadata?.type : undefined, + urlType: + content.type === CardTypeEnum.URL + ? content.urlContent?.metadata?.type + : undefined, parentCardId: card.parentCardId?.getStringValue(), viaCardId: card.viaCardId?.getStringValue(), libraryCount: card.libraryCount, diff --git a/src/modules/cards/infrastructure/repositories/query-services/CollectionCardQueryService.ts b/src/modules/cards/infrastructure/repositories/query-services/CollectionCardQueryService.ts index b4abd3111..c1c8b8fd7 100644 --- a/src/modules/cards/infrastructure/repositories/query-services/CollectionCardQueryService.ts +++ b/src/modules/cards/infrastructure/repositories/query-services/CollectionCardQueryService.ts @@ -54,7 +54,7 @@ export class CollectionCardQueryService { eq(collectionCards.collectionId, collectionId), eq(cards.type, CardTypeEnum.URL), ]; - + if (options.urlType) { whereConditions.push(eq(cards.urlType, options.urlType)); } diff --git a/src/modules/cards/infrastructure/repositories/query-services/UrlCardQueryService.ts b/src/modules/cards/infrastructure/repositories/query-services/UrlCardQueryService.ts index 90fcdb9a1..7ae94bb7c 100644 --- a/src/modules/cards/infrastructure/repositories/query-services/UrlCardQueryService.ts +++ b/src/modules/cards/infrastructure/repositories/query-services/UrlCardQueryService.ts @@ -37,7 +37,7 @@ export class UrlCardQueryService { eq(cards.authorId, userId), eq(cards.type, CardTypeEnum.URL), ]; - + if (options.urlType) { whereConditions.push(eq(cards.urlType, options.urlType)); } diff --git a/src/modules/cards/tests/utils/InMemoryCardQueryRepository.ts b/src/modules/cards/tests/utils/InMemoryCardQueryRepository.ts index 8655c60ce..37222cd43 100644 --- a/src/modules/cards/tests/utils/InMemoryCardQueryRepository.ts +++ b/src/modules/cards/tests/utils/InMemoryCardQueryRepository.ts @@ -32,12 +32,10 @@ export class InMemoryCardQueryRepository implements ICardQueryRepository { try { // Get all cards and filter by user's library membership const allCards = this.cardRepository.getAllCards(); - let userCards = allCards - .filter( - (card) => - card.isUrlCard && - card.isInLibrary(CuratorId.create(userId).unwrap()), - ); + let userCards = allCards.filter( + (card) => + card.isUrlCard && card.isInLibrary(CuratorId.create(userId).unwrap()), + ); // Filter by urlType if specified if (options.urlType) { @@ -244,8 +242,7 @@ export class InMemoryCardQueryRepository implements ICardQueryRepository { ); let collectionCards = allCards.filter( (card) => - collectionCardIds.has(card.cardId.getStringValue()) && - card.isUrlCard, + collectionCardIds.has(card.cardId.getStringValue()) && card.isUrlCard, ); // Filter by urlType if specified diff --git a/src/modules/feeds/infrastructure/repositories/DrizzleFeedRepository.ts b/src/modules/feeds/infrastructure/repositories/DrizzleFeedRepository.ts index f6292ba82..93246def7 100644 --- a/src/modules/feeds/infrastructure/repositories/DrizzleFeedRepository.ts +++ b/src/modules/feeds/infrastructure/repositories/DrizzleFeedRepository.ts @@ -69,7 +69,10 @@ export class DrizzleFeedRepository implements IFeedRepository { .limit(1); if (beforeActivity.length > 0) { - const conditions = [lt(feedActivities.createdAt, beforeActivity[0]!.createdAt), ...whereConditions]; + const conditions = [ + lt(feedActivities.createdAt, beforeActivity[0]!.createdAt), + ...whereConditions, + ]; activitiesResult = await this.db .select() .from(feedActivities) @@ -82,14 +85,16 @@ export class DrizzleFeedRepository implements IFeedRepository { } } else { // Regular pagination without cursor - const query = this.db - .select() - .from(feedActivities); - + const query = this.db.select().from(feedActivities); + if (whereConditions.length > 0) { - query.where(whereConditions.length > 1 ? and(...whereConditions) : whereConditions[0]); + query.where( + whereConditions.length > 1 + ? and(...whereConditions) + : whereConditions[0], + ); } - + activitiesResult = await query .orderBy(desc(feedActivities.createdAt), desc(feedActivities.id)) .limit(limit) @@ -100,11 +105,15 @@ export class DrizzleFeedRepository implements IFeedRepository { const countQuery = this.db .select({ count: count() }) .from(feedActivities); - + if (whereConditions.length > 0) { - countQuery.where(whereConditions.length > 1 ? and(...whereConditions) : whereConditions[0]); + countQuery.where( + whereConditions.length > 1 + ? and(...whereConditions) + : whereConditions[0], + ); } - + const totalCountResult = await countQuery; const totalCount = totalCountResult[0]?.count || 0; @@ -206,7 +215,7 @@ export class DrizzleFeedRepository implements IFeedRepository { const conditions = [ lt(feedActivities.createdAt, beforeActivity[0]!.createdAt), jsonArrayCondition, - ...whereConditions + ...whereConditions, ]; activitiesResult = await this.db .select() @@ -235,7 +244,7 @@ export class DrizzleFeedRepository implements IFeedRepository { if (options.urlType) { conditions.push(eq(feedActivities.urlType, options.urlType)); } - + const totalCountResult = await this.db .select({ count: count() }) .from(feedActivities) diff --git a/src/modules/feeds/infrastructure/repositories/schema/feedActivity.sql.ts b/src/modules/feeds/infrastructure/repositories/schema/feedActivity.sql.ts index 89bd57464..9f1572395 100644 --- a/src/modules/feeds/infrastructure/repositories/schema/feedActivity.sql.ts +++ b/src/modules/feeds/infrastructure/repositories/schema/feedActivity.sql.ts @@ -1,23 +1,44 @@ -import { pgTable, text, timestamp, jsonb, uuid, index } from 'drizzle-orm/pg-core'; +import { + pgTable, + text, + timestamp, + jsonb, + uuid, + index, +} from 'drizzle-orm/pg-core'; -export const feedActivities = pgTable('feed_activities', { - id: uuid('id').primaryKey(), - actorId: text('actor_id').notNull(), // The DID of the user who performed the activity - type: text('type').notNull(), // The type of activity (e.g., 'CARD_COLLECTED') - metadata: jsonb('metadata').notNull(), // Activity-specific metadata - urlType: text('url_type'), // Optional URL type from the card - createdAt: timestamp('created_at').notNull().defaultNow(), -}, (table) => ({ - // Index for filtering by activity type - typeIdx: index('feed_activities_type_idx').on(table.type), - // Index for filtering by URL type - urlTypeIdx: index('feed_activities_url_type_idx').on(table.urlType), - // Index for sorting by creation date (most recent first) - createdAtIdx: index('feed_activities_created_at_idx').on(table.createdAt.desc()), - // Composite index for common query patterns (type + createdAt) - typeCreatedAtIdx: index('feed_activities_type_created_at_idx').on(table.type, table.createdAt.desc()), - // Composite index for URL type filtering with date sorting - urlTypeCreatedAtIdx: index('feed_activities_url_type_created_at_idx').on(table.urlType, table.createdAt.desc()), - // Composite index for filtering by both type and URL type with date sorting - typeUrlTypeCreatedAtIdx: index('feed_activities_type_url_type_created_at_idx').on(table.type, table.urlType, table.createdAt.desc()), -})); +export const feedActivities = pgTable( + 'feed_activities', + { + id: uuid('id').primaryKey(), + actorId: text('actor_id').notNull(), // The DID of the user who performed the activity + type: text('type').notNull(), // The type of activity (e.g., 'CARD_COLLECTED') + metadata: jsonb('metadata').notNull(), // Activity-specific metadata + urlType: text('url_type'), // Optional URL type from the card + createdAt: timestamp('created_at').notNull().defaultNow(), + }, + (table) => ({ + // Index for filtering by activity type + typeIdx: index('feed_activities_type_idx').on(table.type), + // Index for filtering by URL type + urlTypeIdx: index('feed_activities_url_type_idx').on(table.urlType), + // Index for sorting by creation date (most recent first) + createdAtIdx: index('feed_activities_created_at_idx').on( + table.createdAt.desc(), + ), + // Composite index for common query patterns (type + createdAt) + typeCreatedAtIdx: index('feed_activities_type_created_at_idx').on( + table.type, + table.createdAt.desc(), + ), + // Composite index for URL type filtering with date sorting + urlTypeCreatedAtIdx: index('feed_activities_url_type_created_at_idx').on( + table.urlType, + table.createdAt.desc(), + ), + // Composite index for filtering by both type and URL type with date sorting + typeUrlTypeCreatedAtIdx: index( + 'feed_activities_type_url_type_created_at_idx', + ).on(table.type, table.urlType, table.createdAt.desc()), + }), +); diff --git a/src/shared/infrastructure/database/migrations/meta/0010_snapshot.json b/src/shared/infrastructure/database/migrations/meta/0010_snapshot.json index b8ae3341c..ffe1cd556 100644 --- a/src/shared/infrastructure/database/migrations/meta/0010_snapshot.json +++ b/src/shared/infrastructure/database/migrations/meta/0010_snapshot.json @@ -251,12 +251,8 @@ "name": "cards_parent_card_id_cards_id_fk", "tableFrom": "cards", "tableTo": "cards", - "columnsFrom": [ - "parent_card_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["parent_card_id"], + "columnsTo": ["id"], "onDelete": "no action", "onUpdate": "no action" }, @@ -264,12 +260,8 @@ "name": "cards_via_card_id_cards_id_fk", "tableFrom": "cards", "tableTo": "cards", - "columnsFrom": [ - "via_card_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["via_card_id"], + "columnsTo": ["id"], "onDelete": "no action", "onUpdate": "no action" }, @@ -277,12 +269,8 @@ "name": "cards_published_record_id_published_records_id_fk", "tableFrom": "cards", "tableTo": "published_records", - "columnsFrom": [ - "published_record_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["published_record_id"], + "columnsTo": ["id"], "onDelete": "no action", "onUpdate": "no action" } @@ -414,12 +402,8 @@ "name": "collection_cards_collection_id_collections_id_fk", "tableFrom": "collection_cards", "tableTo": "collections", - "columnsFrom": [ - "collection_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["collection_id"], + "columnsTo": ["id"], "onDelete": "cascade", "onUpdate": "no action" }, @@ -427,12 +411,8 @@ "name": "collection_cards_card_id_cards_id_fk", "tableFrom": "collection_cards", "tableTo": "cards", - "columnsFrom": [ - "card_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["card_id"], + "columnsTo": ["id"], "onDelete": "cascade", "onUpdate": "no action" }, @@ -440,12 +420,8 @@ "name": "collection_cards_via_card_id_cards_id_fk", "tableFrom": "collection_cards", "tableTo": "cards", - "columnsFrom": [ - "via_card_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["via_card_id"], + "columnsTo": ["id"], "onDelete": "no action", "onUpdate": "no action" }, @@ -453,12 +429,8 @@ "name": "collection_cards_published_record_id_published_records_id_fk", "tableFrom": "collection_cards", "tableTo": "published_records", - "columnsFrom": [ - "published_record_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["published_record_id"], + "columnsTo": ["id"], "onDelete": "no action", "onUpdate": "no action" } @@ -498,12 +470,8 @@ "name": "collection_collaborators_collection_id_collections_id_fk", "tableFrom": "collection_collaborators", "tableTo": "collections", - "columnsFrom": [ - "collection_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["collection_id"], + "columnsTo": ["id"], "onDelete": "cascade", "onUpdate": "no action" } @@ -619,12 +587,8 @@ "name": "collections_published_record_id_published_records_id_fk", "tableFrom": "collections", "tableTo": "published_records", - "columnsFrom": [ - "published_record_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["published_record_id"], + "columnsTo": ["id"], "onDelete": "no action", "onUpdate": "no action" } @@ -723,12 +687,8 @@ "name": "library_memberships_card_id_cards_id_fk", "tableFrom": "library_memberships", "tableTo": "cards", - "columnsFrom": [ - "card_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["card_id"], + "columnsTo": ["id"], "onDelete": "cascade", "onUpdate": "no action" }, @@ -736,12 +696,8 @@ "name": "library_memberships_published_record_id_published_records_id_fk", "tableFrom": "library_memberships", "tableTo": "published_records", - "columnsFrom": [ - "published_record_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["published_record_id"], + "columnsTo": ["id"], "onDelete": "no action", "onUpdate": "no action" } @@ -749,10 +705,7 @@ "compositePrimaryKeys": { "library_memberships_card_id_user_id_pk": { "name": "library_memberships_card_id_user_id_pk", - "columns": [ - "card_id", - "user_id" - ] + "columns": ["card_id", "user_id"] } }, "uniqueConstraints": {}, @@ -1106,12 +1059,8 @@ "name": "auth_refresh_tokens_user_did_users_id_fk", "tableFrom": "auth_refresh_tokens", "tableTo": "users", - "columnsFrom": [ - "user_did" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["user_did"], + "columnsTo": ["id"], "onDelete": "no action", "onUpdate": "no action" } @@ -1171,4 +1120,4 @@ "schemas": {}, "tables": {} } -} \ No newline at end of file +} diff --git a/src/shared/infrastructure/database/migrations/meta/0011_snapshot.json b/src/shared/infrastructure/database/migrations/meta/0011_snapshot.json index 8faf164cd..0e594703b 100644 --- a/src/shared/infrastructure/database/migrations/meta/0011_snapshot.json +++ b/src/shared/infrastructure/database/migrations/meta/0011_snapshot.json @@ -251,12 +251,8 @@ "name": "cards_parent_card_id_cards_id_fk", "tableFrom": "cards", "tableTo": "cards", - "columnsFrom": [ - "parent_card_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["parent_card_id"], + "columnsTo": ["id"], "onDelete": "no action", "onUpdate": "no action" }, @@ -264,12 +260,8 @@ "name": "cards_via_card_id_cards_id_fk", "tableFrom": "cards", "tableTo": "cards", - "columnsFrom": [ - "via_card_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["via_card_id"], + "columnsTo": ["id"], "onDelete": "no action", "onUpdate": "no action" }, @@ -277,12 +269,8 @@ "name": "cards_published_record_id_published_records_id_fk", "tableFrom": "cards", "tableTo": "published_records", - "columnsFrom": [ - "published_record_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["published_record_id"], + "columnsTo": ["id"], "onDelete": "no action", "onUpdate": "no action" } @@ -414,12 +402,8 @@ "name": "collection_cards_collection_id_collections_id_fk", "tableFrom": "collection_cards", "tableTo": "collections", - "columnsFrom": [ - "collection_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["collection_id"], + "columnsTo": ["id"], "onDelete": "cascade", "onUpdate": "no action" }, @@ -427,12 +411,8 @@ "name": "collection_cards_card_id_cards_id_fk", "tableFrom": "collection_cards", "tableTo": "cards", - "columnsFrom": [ - "card_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["card_id"], + "columnsTo": ["id"], "onDelete": "cascade", "onUpdate": "no action" }, @@ -440,12 +420,8 @@ "name": "collection_cards_via_card_id_cards_id_fk", "tableFrom": "collection_cards", "tableTo": "cards", - "columnsFrom": [ - "via_card_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["via_card_id"], + "columnsTo": ["id"], "onDelete": "no action", "onUpdate": "no action" }, @@ -453,12 +429,8 @@ "name": "collection_cards_published_record_id_published_records_id_fk", "tableFrom": "collection_cards", "tableTo": "published_records", - "columnsFrom": [ - "published_record_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["published_record_id"], + "columnsTo": ["id"], "onDelete": "no action", "onUpdate": "no action" } @@ -498,12 +470,8 @@ "name": "collection_collaborators_collection_id_collections_id_fk", "tableFrom": "collection_collaborators", "tableTo": "collections", - "columnsFrom": [ - "collection_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["collection_id"], + "columnsTo": ["id"], "onDelete": "cascade", "onUpdate": "no action" } @@ -619,12 +587,8 @@ "name": "collections_published_record_id_published_records_id_fk", "tableFrom": "collections", "tableTo": "published_records", - "columnsFrom": [ - "published_record_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["published_record_id"], + "columnsTo": ["id"], "onDelete": "no action", "onUpdate": "no action" } @@ -723,12 +687,8 @@ "name": "library_memberships_card_id_cards_id_fk", "tableFrom": "library_memberships", "tableTo": "cards", - "columnsFrom": [ - "card_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["card_id"], + "columnsTo": ["id"], "onDelete": "cascade", "onUpdate": "no action" }, @@ -736,12 +696,8 @@ "name": "library_memberships_published_record_id_published_records_id_fk", "tableFrom": "library_memberships", "tableTo": "published_records", - "columnsFrom": [ - "published_record_id" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["published_record_id"], + "columnsTo": ["id"], "onDelete": "no action", "onUpdate": "no action" } @@ -749,10 +705,7 @@ "compositePrimaryKeys": { "library_memberships_card_id_user_id_pk": { "name": "library_memberships_card_id_user_id_pk", - "columns": [ - "card_id", - "user_id" - ] + "columns": ["card_id", "user_id"] } }, "uniqueConstraints": {}, @@ -1227,12 +1180,8 @@ "name": "auth_refresh_tokens_user_did_users_id_fk", "tableFrom": "auth_refresh_tokens", "tableTo": "users", - "columnsFrom": [ - "user_did" - ], - "columnsTo": [ - "id" - ], + "columnsFrom": ["user_did"], + "columnsTo": ["id"], "onDelete": "no action", "onUpdate": "no action" } @@ -1292,4 +1241,4 @@ "schemas": {}, "tables": {} } -} \ No newline at end of file +} diff --git a/src/shared/infrastructure/database/migrations/meta/_journal.json b/src/shared/infrastructure/database/migrations/meta/_journal.json index 991007998..dd39edaaf 100644 --- a/src/shared/infrastructure/database/migrations/meta/_journal.json +++ b/src/shared/infrastructure/database/migrations/meta/_journal.json @@ -87,4 +87,4 @@ "breakpoints": true } ] -} \ No newline at end of file +} -- knotmirror 0.0.1 From 848cecb1f067bf8d533b21d374ac984e754ce765 Mon Sep 17 00:00:00 2001 From: Wesley Finck Date: Tue, 23 Dec 2025 22:10:45 +0800 Subject: [PATCH 16/18] optionally filter similar card search by url type --- src/modules/search/domain/IVectorDatabase.ts | 2 ++ src/modules/search/infrastructure/UpstashVectorDatabase.ts | 1 + 2 files changed, 3 insertions(+) diff --git a/src/modules/search/domain/IVectorDatabase.ts b/src/modules/search/domain/IVectorDatabase.ts index acc6ba790..22aba8598 100644 --- a/src/modules/search/domain/IVectorDatabase.ts +++ b/src/modules/search/domain/IVectorDatabase.ts @@ -1,3 +1,4 @@ +import { UrlType } from 'src/modules/cards/domain/value-objects/UrlType'; import { Result } from '../../../shared/core/Result'; import { UrlMetadataProps } from '../../cards/domain/value-objects/UrlMetadata'; @@ -7,6 +8,7 @@ export interface FindSimilarUrlsParams { url: string; limit: number; threshold?: number; // Similarity threshold (0-1) + urlType?: UrlType; // Optional URL type to filter results } export interface UrlSearchResult { diff --git a/src/modules/search/infrastructure/UpstashVectorDatabase.ts b/src/modules/search/infrastructure/UpstashVectorDatabase.ts index 39cd83d54..95b0680c8 100644 --- a/src/modules/search/infrastructure/UpstashVectorDatabase.ts +++ b/src/modules/search/infrastructure/UpstashVectorDatabase.ts @@ -78,6 +78,7 @@ export class UpstashVectorDatabase implements IVectorDatabase { topK, includeMetadata: true, includeVectors: false, // We don't need the vectors in the response + filter: params.urlType ? `type = '${params.urlType}'` : undefined, }); // Filter out the query URL itself and apply threshold -- knotmirror 0.0.1 From 62da81eeb2bcc40cd35f24d89db7f8892a3d2b13 Mon Sep 17 00:00:00 2001 From: Wesley Finck Date: Tue, 23 Dec 2025 22:17:02 +0800 Subject: [PATCH 17/18] feat: add optional urlType filter for similar URLs search Co-authored-by: aider (anthropic/claude-sonnet-4-20250514) --- .../useCases/queries/GetSimilarUrlsForUrlUseCase.ts | 13 +++++++++++++ src/modules/search/domain/services/SearchService.ts | 3 ++- .../search/infrastructure/InMemoryVectorDatabase.ts | 5 +++++ .../controllers/GetSimilarUrlsForUrlController.ts | 3 ++- src/types/src/api/requests.ts | 1 + src/webapp/api-client/clients/QueryClient.ts | 1 + 6 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/modules/search/application/useCases/queries/GetSimilarUrlsForUrlUseCase.ts b/src/modules/search/application/useCases/queries/GetSimilarUrlsForUrlUseCase.ts index e203361c9..daaa47489 100644 --- a/src/modules/search/application/useCases/queries/GetSimilarUrlsForUrlUseCase.ts +++ b/src/modules/search/application/useCases/queries/GetSimilarUrlsForUrlUseCase.ts @@ -7,6 +7,7 @@ import { SearchService } from '../../../domain/services/SearchService'; import { GetSimilarUrlsForUrlParams } from '@semble/types/api/requests'; import { UrlView } from '@semble/types/api/responses'; import { Pagination } from '@semble/types/api/common'; +import { UrlType } from '../../../../cards/domain/value-objects/UrlType'; export interface GetSimilarUrlsForUrlQuery extends GetSimilarUrlsForUrlParams { callingUserId?: string; @@ -57,6 +58,17 @@ export class GetSimilarUrlsForUrlUseCase ); } + // Parse urlType if provided + let urlType: UrlType | undefined; + if (query.urlType) { + // Validate that the urlType is a valid enum value + if (Object.values(UrlType).includes(query.urlType as UrlType)) { + urlType = query.urlType as UrlType; + } else { + return err(new ValidationError(`Invalid URL type: ${query.urlType}`)); + } + } + // Always get 50 results from vector database const vectorDbLimit = 50; const similarUrlsResult = await this.searchService.findSimilarUrls( @@ -64,6 +76,7 @@ export class GetSimilarUrlsForUrlUseCase { limit: vectorDbLimit, threshold, + urlType, callingUserId: query.callingUserId, }, ); diff --git a/src/modules/search/domain/services/SearchService.ts b/src/modules/search/domain/services/SearchService.ts index 13f3782fb..8c2189005 100644 --- a/src/modules/search/domain/services/SearchService.ts +++ b/src/modules/search/domain/services/SearchService.ts @@ -69,7 +69,7 @@ export class SearchService { async findSimilarUrls( url: URL, - options: { limit: number; threshold?: number; callingUserId?: string }, + options: { limit: number; threshold?: number; urlType?: UrlType; callingUserId?: string }, ): Promise> { try { // 1. Find similar URLs from vector database @@ -77,6 +77,7 @@ export class SearchService { url: url.value, limit: options.limit * 2, // Get more results to account for filtering threshold: options.threshold, + urlType: options.urlType, }; const similarResult = diff --git a/src/modules/search/infrastructure/InMemoryVectorDatabase.ts b/src/modules/search/infrastructure/InMemoryVectorDatabase.ts index 131a26d80..c51bf2ec5 100644 --- a/src/modules/search/infrastructure/InMemoryVectorDatabase.ts +++ b/src/modules/search/infrastructure/InMemoryVectorDatabase.ts @@ -85,6 +85,11 @@ export class InMemoryVectorDatabase implements IVectorDatabase { // Skip the query URL itself if (url === params.url) continue; + // Filter by URL type if specified + if (params.urlType && indexed.metadata.type !== params.urlType) { + continue; + } + const similarity = this.calculateSimilarity( queryContent, indexed.content, diff --git a/src/modules/search/infrastructure/http/controllers/GetSimilarUrlsForUrlController.ts b/src/modules/search/infrastructure/http/controllers/GetSimilarUrlsForUrlController.ts index 16fce359f..137f9c0b7 100644 --- a/src/modules/search/infrastructure/http/controllers/GetSimilarUrlsForUrlController.ts +++ b/src/modules/search/infrastructure/http/controllers/GetSimilarUrlsForUrlController.ts @@ -12,7 +12,7 @@ export class GetSimilarUrlsForUrlController extends Controller { async executeImpl(req: AuthenticatedRequest, res: Response): Promise { try { - const { url, page, limit, threshold } = req.query; + const { url, page, limit, threshold, urlType } = req.query; if (!url || typeof url !== 'string') { return this.fail(res, 'URL parameter is required'); @@ -24,6 +24,7 @@ export class GetSimilarUrlsForUrlController extends Controller { page: page ? parseInt(page as string) : undefined, limit: limit ? parseInt(limit as string) : undefined, threshold: threshold ? parseFloat(threshold as string) : undefined, + urlType: urlType as string | undefined, }); if (result.isErr()) { diff --git a/src/types/src/api/requests.ts b/src/types/src/api/requests.ts index f95a00761..6a85cdd21 100644 --- a/src/types/src/api/requests.ts +++ b/src/types/src/api/requests.ts @@ -161,6 +161,7 @@ export interface GetCollectionsForUrlParams extends PaginatedSortedParams { export interface GetSimilarUrlsForUrlParams extends PaginatedSortedParams { url: string; threshold?: number; + urlType?: string; } export interface GetGemActivityFeedParams extends PaginationParams { diff --git a/src/webapp/api-client/clients/QueryClient.ts b/src/webapp/api-client/clients/QueryClient.ts index 08ccd894f..f6987b0ac 100644 --- a/src/webapp/api-client/clients/QueryClient.ts +++ b/src/webapp/api-client/clients/QueryClient.ts @@ -237,6 +237,7 @@ export class QueryClient extends BaseClient { if (params.sortOrder) searchParams.set('sortOrder', params.sortOrder); if (params.threshold) searchParams.set('threshold', params.threshold.toString()); + if (params.urlType) searchParams.set('urlType', params.urlType); return this.request( 'GET', -- knotmirror 0.0.1 From 55d914763f5e87877c3d6e1efb7c9e61aeac97ef Mon Sep 17 00:00:00 2001 From: Wesley Finck Date: Tue, 23 Dec 2025 22:26:16 +0800 Subject: [PATCH 18/18] formatting --- src/modules/search/domain/services/SearchService.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/modules/search/domain/services/SearchService.ts b/src/modules/search/domain/services/SearchService.ts index 8c2189005..e8cb12030 100644 --- a/src/modules/search/domain/services/SearchService.ts +++ b/src/modules/search/domain/services/SearchService.ts @@ -10,6 +10,7 @@ import { UrlMetadataProps, } from 'src/modules/cards/domain/value-objects/UrlMetadata'; import { Chunk } from '../value-objects/Chunk'; +import { UrlType } from 'src/modules/cards/domain/value-objects/UrlType'; export class SearchService { constructor( @@ -69,7 +70,12 @@ export class SearchService { async findSimilarUrls( url: URL, - options: { limit: number; threshold?: number; urlType?: UrlType; callingUserId?: string }, + options: { + limit: number; + threshold?: number; + urlType?: UrlType; + callingUserId?: string; + }, ): Promise> { try { // 1. Find similar URLs from vector database