Skip to content

Commit

Permalink
feat: move ListRepository.fetchListTimeline to TimelineRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
laminne committed Jul 21, 2024
1 parent 2a3c07a commit 7e5c35e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 39 deletions.
34 changes: 17 additions & 17 deletions pkg/timeline/adaptor/repository/dummy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,23 @@ describe('InMemoryTimelineRepository', () => {
.includes('DIRECT'),
).toBe(false);
});

it('should fetch list timeline', async () => {
const actual = await repository.fetchListTimeline(['1' as NoteID]);
expect(Result.isOk(actual)).toBe(true);
});

it('should not return DIRECT notes', async () => {
const actual = await repository.fetchListTimeline([
'1' as NoteID,
'4' as NoteID,
]);
expect(Result.unwrap(actual)).toStrictEqual([dummyPublicNote]);
expect(Result.unwrap(actual)).not.toStrictEqual([
dummyPublicNote,
dummyDirectNote,
]);
});
});

describe('InMemoryListRepository', () => {
Expand Down Expand Up @@ -183,21 +200,4 @@ describe('InMemoryListRepository', () => {
'101' as AccountID,
]);
});

it('should fetch list timeline', async () => {
const actual = await repository.fetchListTimeline(['10' as NoteID]);
expect(Result.isOk(actual)).toBe(true);
});

it('should not return DIRECT notes', async () => {
const actual = await repository.fetchListTimeline([
'10' as NoteID,
'14' as NoteID,
]);
expect(Result.unwrap(actual)).toStrictEqual([dummyPublicNote]);
expect(Result.unwrap(actual)).not.toStrictEqual([
dummyPublicNote,
dummyDirectNote,
]);
});
});
38 changes: 19 additions & 19 deletions pkg/timeline/adaptor/repository/dummy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ export class InMemoryTimelineRepository implements TimelineRepository {
return Result.ok(filtered.slice(0, beforeIndex));
}

async fetchListTimeline(
noteId: NoteID[],
): Promise<Result.Result<Error, Note[]>> {
const notes: Note[] = [];
for (const noteID of noteId) {
const n = this.data.get(noteID);
if (!n) {
return Result.err(new Error('Not found'));
}
notes.push(n);
}

// NOTE: filter out DIRECT notes
// ToDo: Consider whether to filter out when Visibility is ‘FOLLOWERS’.
const filtered = notes.filter((note) => note.getVisibility() !== 'DIRECT');

return Result.ok(filtered);
}

reset(data: readonly Note[] = []) {
this.data.clear();
this.data = new Map(data.map((v) => [v.getID(), v]));
Expand Down Expand Up @@ -117,25 +136,6 @@ export class InMemoryListRepository implements ListRepository {
return Result.ok(list.getMemberIds() as AccountID[]);
}

async fetchListTimeline(
noteId: NoteID[],
): Promise<Result.Result<Error, Note[]>> {
const notes: Note[] = [];
for (const noteID of noteId) {
const n = this.notes.get(noteID);
if (!n) {
return Result.err(new Error('Not found'));
}
notes.push(n);
}

// NOTE: filter out DIRECT notes
// ToDo: Consider whether to filter out when Visibility is ‘FOLLOWERS’.
const filtered = notes.filter((note) => note.getVisibility() !== 'DIRECT');

return Result.ok(filtered);
}

reset(data: readonly List[] = [], notes: readonly Note[] = []) {
this.listData.clear();
this.notes.clear();
Expand Down
17 changes: 17 additions & 0 deletions pkg/timeline/adaptor/repository/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,21 @@ export class PrismaTimelineRepository implements TimelineRepository {
});
return Result.ok(this.deserialize(homeNotes));
}

async fetchListTimeline(
noteIDs: NoteID[],
): Promise<Result.Result<Error, Note[]>> {
// ToDo: Add filter
const listNotes = await this.prisma.note.findMany({
where: {
id: {
in: noteIDs,
},
},
orderBy: {
createdAt: 'desc',
},
});
return Result.ok(this.deserialize(listNotes));
}
}
10 changes: 7 additions & 3 deletions pkg/timeline/model/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { Ether, type Result } from '@mikuroxina/mini-fn';

import type { AccountID } from '../../accounts/model/account.js';
import type { Note, NoteID } from '../../notes/model/note.js';
import type { ListID } from './list.js';
import type { List } from './list.js';
import type { List, ListID } from './list.js';

export interface FetchAccountTimelineFilter {
id: AccountID;
Expand Down Expand Up @@ -37,6 +36,12 @@ export interface TimelineRepository {
noteIDs: readonly NoteID[],
filter: FetchAccountTimelineFilter,
): Promise<Result.Result<Error, Note[]>>;

/**
* @description Fetch list timeline
* @param noteId IDs of the notes to be fetched
* */
fetchListTimeline(noteId: NoteID[]): Promise<Result.Result<Error, Note[]>>;
}
export const timelineRepoSymbol = Ether.newEtherSymbol<TimelineRepository>();

Expand All @@ -61,5 +66,4 @@ export interface ListRepository {
ownerId: AccountID,
): Promise<Result.Result<Error, List[]>>;
fetchListMembers(listId: ListID): Promise<Result.Result<Error, AccountID[]>>;
fetchListTimeline(noteId: NoteID[]): Promise<Result.Result<Error, Note[]>>;
}

0 comments on commit 7e5c35e

Please sign in to comment.