Something went wrong. Try again.
A social knowledge tool for research trails, built on ATProto semble.so
research-tool social-network tools-for-thought atproto
Something went wrong. Try again.
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197import { IConnectionQueryRepository, ConnectionQueryOptions, PaginatedConnectionQueryResult, ConnectionForUserDTO, ConnectionSortField, SortOrder,} from '../../domain/IConnectionQueryRepository';import { InMemoryConnectionRepository } from './InMemoryConnectionRepository';import { UrlOrCardIdType } from '../../domain/value-objects/UrlOrCardId';import { ConnectionTypeEnum } from '../../domain/value-objects/ConnectionType';
export class InMemoryConnectionQueryRepository implements IConnectionQueryRepository { constructor(private connectionRepository: InMemoryConnectionRepository) {}
async getConnectionsForUrl( url: string, direction: 'forward' | 'backward' | 'both', options: ConnectionQueryOptions, ): Promise<PaginatedConnectionQueryResult<ConnectionForUserDTO>> { const { page, limit, sortBy, sortOrder, connectionTypes } = options;
// Get all connections from the repository const allConnections = this.connectionRepository.getAllConnections();
// Filter connections based on direction and URL type let filteredConnections = allConnections.filter((connection) => { const isSourceMatch = connection.source.type === UrlOrCardIdType.URL && connection.source.stringValue === url; const isTargetMatch = connection.target.type === UrlOrCardIdType.URL && connection.target.stringValue === url; const bothUrlToUrl = connection.source.type === UrlOrCardIdType.URL && connection.target.type === UrlOrCardIdType.URL;
if (direction === 'forward') { return isSourceMatch && bothUrlToUrl; } else if (direction === 'backward') { return isTargetMatch && bothUrlToUrl; } else { // both return (isSourceMatch || isTargetMatch) && bothUrlToUrl; } });
// Apply connection type filter if provided if (connectionTypes && connectionTypes.length > 0) { filteredConnections = filteredConnections.filter((connection) => connection.type ? connectionTypes.includes(connection.type.value) : false, ); }
// Sort connections filteredConnections.sort((a, b) => { const dateA = sortBy === ConnectionSortField.UPDATED_AT ? a.updatedAt : a.createdAt; const dateB = sortBy === ConnectionSortField.UPDATED_AT ? b.updatedAt : b.createdAt;
const comparison = dateA.getTime() - dateB.getTime(); return sortOrder === SortOrder.ASC ? comparison : -comparison; });
// Apply pagination const offset = (page - 1) * limit; const paginatedConnections = filteredConnections.slice( offset, offset + limit, );
// Map to DTOs with both source and target const items: ConnectionForUserDTO[] = paginatedConnections.map( (connection) => ({ connection: { id: connection.connectionId.getStringValue(), type: connection.type?.value, note: connection.note?.value, createdAt: connection.createdAt, updatedAt: connection.updatedAt, curatorId: connection.curatorId.value, }, sourceUrl: connection.source.stringValue, targetUrl: connection.target.stringValue, }), );
return { items, totalCount: filteredConnections.length, hasMore: offset + paginatedConnections.length < filteredConnections.length, }; }
async getConnectionsForUser( curatorId: string, options: ConnectionQueryOptions, ): Promise<PaginatedConnectionQueryResult<ConnectionForUserDTO>> { const { page, limit, sortBy, sortOrder, connectionTypes } = options;
// Get all connections from the repository const allConnections = this.connectionRepository.getAllConnections();
// Filter connections by curator and URL-to-URL type let filteredConnections = allConnections.filter((connection) => { return ( connection.curatorId.value === curatorId && connection.source.type === UrlOrCardIdType.URL && connection.target.type === UrlOrCardIdType.URL ); });
// Apply connection type filter if provided if (connectionTypes && connectionTypes.length > 0) { filteredConnections = filteredConnections.filter((connection) => connection.type ? connectionTypes.includes(connection.type.value) : false, ); }
// Sort connections filteredConnections.sort((a, b) => { const dateA = sortBy === ConnectionSortField.UPDATED_AT ? a.updatedAt : a.createdAt; const dateB = sortBy === ConnectionSortField.UPDATED_AT ? b.updatedAt : b.createdAt;
const comparison = dateA.getTime() - dateB.getTime(); return sortOrder === SortOrder.ASC ? comparison : -comparison; });
// Apply pagination const offset = (page - 1) * limit; const paginatedConnections = filteredConnections.slice( offset, offset + limit, );
// Map to DTOs const items: ConnectionForUserDTO[] = paginatedConnections.map( (connection) => ({ connection: { id: connection.connectionId.getStringValue(), type: connection.type?.value, note: connection.note?.value, createdAt: connection.createdAt, updatedAt: connection.updatedAt, curatorId: connection.curatorId.value, }, sourceUrl: connection.source.stringValue, targetUrl: connection.target.stringValue, }), );
return { items, totalCount: filteredConnections.length, hasMore: offset + paginatedConnections.length < filteredConnections.length, }; }
async getConnectionStatsForCurator(curatorId: string): Promise<{ total: number; byType: Map<ConnectionTypeEnum, number>; }> { const allConnections = this.connectionRepository.getAllConnections();
// Get all URL-to-URL connections created by this curator const curatorConnections = allConnections.filter( (connection) => connection.curatorId.value === curatorId && connection.source.type === UrlOrCardIdType.URL && connection.target.type === UrlOrCardIdType.URL, );
// Count by type const byType = new Map<ConnectionTypeEnum, number>(); curatorConnections.forEach((connection) => { if (connection.type) { const current = byType.get(connection.type.value) || 0; byType.set(connection.type.value, current + 1); } });
return { total: curatorConnections.length, byType, }; }}