Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: impl fetch list members #591

Merged
merged 5 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions pkg/accounts/testData/testData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { PartialAccount } from '../../intermodule/account.js';
import { Account, type AccountID } from '../model/account.js';

export const dummyAccount1 = Account.new({
id: '101' as AccountID,
bio: 'this is test user',
mail: 'john@example.com',
name: '@john@example.com',
nickname: 'John Doe',
passphraseHash: '',
role: 'normal',
silenced: 'normal',
status: 'active',
frozen: 'normal',
createdAt: new Date('2023-09-10T00:00:00Z'),
});
export const dummyAccount2 = Account.new({
id: '102' as AccountID,
bio: 'Hello world ✨',
mail: 'john@example.com',
name: '@johndoe@example.com',
nickname: '🌤 John',
passphraseHash: '',
role: 'normal',
silenced: 'normal',
status: 'active',
frozen: 'normal',
createdAt: new Date('2023-09-11T00:00:00Z'),
});
export const dummyAccounts = [dummyAccount1, dummyAccount2];

export const partialAccount1: PartialAccount = {
id: dummyAccount1.getID(),
name: dummyAccount1.getName(),
nickname: dummyAccount1.getNickname(),
bio: dummyAccount1.getBio(),
};
export const partialAccount2: PartialAccount = {
id: dummyAccount2.getID(),
name: dummyAccount2.getName(),
nickname: dummyAccount2.getNickname(),
bio: dummyAccount2.getBio(),
};
export const partialAccounts = [partialAccount1, partialAccount2];
29 changes: 29 additions & 0 deletions pkg/timeline/adaptor/controller/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,32 @@
import type { AccountTimelineService } from '../../service/account.js';
import type { CreateListService } from '../../service/createList.js';
import type { DeleteListService } from '../../service/deleteList.js';
import type { FetchListMemberService } from '../../service/fetchMember.js';
import type {
CreateListResponseSchema,
GetAccountTimelineResponseSchema,
GetListMemberResponseSchema,
} from '../validator/timeline.js';

export class TimelineController {
private readonly accountTimelineService: AccountTimelineService;
private readonly accountModule: AccountModuleFacade;
private readonly createListService: CreateListService;
private readonly deleteListService: DeleteListService;
private readonly fetchMemberService: FetchListMemberService;

Check warning on line 23 in pkg/timeline/adaptor/controller/timeline.ts

View check run for this annotation

Codecov / codecov/patch

pkg/timeline/adaptor/controller/timeline.ts#L23

Added line #L23 was not covered by tests

constructor(args: {
accountTimelineService: AccountTimelineService;
accountModule: AccountModuleFacade;
createListService: CreateListService;
deleteListService: DeleteListService;
fetchMemberService: FetchListMemberService;
}) {
this.accountTimelineService = args.accountTimelineService;
this.accountModule = args.accountModule;
this.createListService = args.createListService;
this.deleteListService = args.deleteListService;
this.fetchMemberService = args.fetchMemberService;

Check warning on line 36 in pkg/timeline/adaptor/controller/timeline.ts

View check run for this annotation

Codecov / codecov/patch

pkg/timeline/adaptor/controller/timeline.ts#L36

Added line #L36 was not covered by tests
}

async getAccountTimeline(
Expand Down Expand Up @@ -122,4 +128,27 @@
const res = await this.deleteListService.handle(id as ListID);
return res;
}

async getListMembers(
id: string,

Check warning on line 133 in pkg/timeline/adaptor/controller/timeline.ts

View check run for this annotation

Codecov / codecov/patch

pkg/timeline/adaptor/controller/timeline.ts#L132-L133

Added lines #L132 - L133 were not covered by tests
): Promise<
Result.Result<Error, z.infer<typeof GetListMemberResponseSchema>>
> {
const accounts = await this.fetchMemberService.handle(id as ListID);
if (Result.isErr(accounts)) {
return accounts;
}

Check warning on line 140 in pkg/timeline/adaptor/controller/timeline.ts

View check run for this annotation

Codecov / codecov/patch

pkg/timeline/adaptor/controller/timeline.ts#L136-L140

Added lines #L136 - L140 were not covered by tests

const unwrapped = Result.unwrap(accounts);
const res = unwrapped.map((v) => {
return {
id: v.getID(),
name: v.getName(),
nickname: v.getNickname(),
avatar: '',
laminne marked this conversation as resolved.
Show resolved Hide resolved
};
});

Check warning on line 150 in pkg/timeline/adaptor/controller/timeline.ts

View check run for this annotation

Codecov / codecov/patch

pkg/timeline/adaptor/controller/timeline.ts#L142-L150

Added lines #L142 - L150 were not covered by tests

return Result.ok({ assignees: res });
}

Check warning on line 153 in pkg/timeline/adaptor/controller/timeline.ts

View check run for this annotation

Codecov / codecov/patch

pkg/timeline/adaptor/controller/timeline.ts#L152-L153

Added lines #L152 - L153 were not covered by tests
}
25 changes: 25 additions & 0 deletions pkg/timeline/adaptor/validator/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,28 @@
}),
})
.openapi('CreateListResponse');

export const GetListMemberResponseSchema = z
.object({
assignees: z.array(
z.object({
id: z.string().openapi({
example: '30984308495',
description: 'Assignee account ID',
}),
name: z.string().openapi({
example: '@john@example.com',
description: 'Assignee account name',
}),
nickname: z.string().openapi({
example: 'John Doe',
description: 'Assignee nickname',
}),
avatar: z.string().url().openapi({
example: 'https://example.com/avatar.png',
description: 'avatar URL',
}),
}),
),
})
.openapi('GetListMemberResponseSchema');

Check warning on line 94 in pkg/timeline/adaptor/validator/timeline.ts

View check run for this annotation

Codecov / codecov/patch

pkg/timeline/adaptor/validator/timeline.ts#L71-L94

Added lines #L71 - L94 were not covered by tests
16 changes: 16 additions & 0 deletions pkg/timeline/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
CreateListRoute,
DeleteListRoute,
GetAccountTimelineRoute,
GetListMemberRoute,
PushNoteToTimelineRoute,
} from './router.js';
import { AccountTimelineService } from './service/account.js';
import { CreateListService } from './service/createList.js';
import { DeleteListService } from './service/deleteList.js';
import { FetchListMemberService } from './service/fetchMember.js';

Check warning on line 24 in pkg/timeline/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/timeline/mod.ts#L24

Added line #L24 was not covered by tests
import { NoteVisibilityService } from './service/noteVisibility.js';
import { PushTimelineService } from './service/push.js';

Expand All @@ -31,6 +33,7 @@
const listRepository = new InMemoryListRepository();
const timelineNotesCacheRepository = new InMemoryTimelineCacheRepository();
const noteVisibilityService = new NoteVisibilityService(accountModule);

const controller = new TimelineController({
accountTimelineService: new AccountTimelineService({
noteVisibilityService: noteVisibilityService,
Expand All @@ -39,6 +42,7 @@
createListService: new CreateListService(idGenerator, listRepository),
deleteListService: new DeleteListService(listRepository),
accountModule,
fetchMemberService: new FetchListMemberService(listRepository, accountModule),

Check warning on line 45 in pkg/timeline/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/timeline/mod.ts#L45

Added line #L45 was not covered by tests
});
const pushTimelineService = new PushTimelineService(
accountModule,
Expand Down Expand Up @@ -126,3 +130,15 @@

return new Response(undefined, { status: 204 });
});

timeline.openapi(GetListMemberRoute, async (c) => {
const { id } = c.req.param();

Check warning on line 135 in pkg/timeline/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/timeline/mod.ts#L134-L135

Added lines #L134 - L135 were not covered by tests

const res = await controller.getListMembers(id);
if (Result.isErr(res)) {
return c.json({ error: res[1].message }, 404);
}

Check warning on line 140 in pkg/timeline/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/timeline/mod.ts#L137-L140

Added lines #L137 - L140 were not covered by tests

const unwrapped = Result.unwrap(res);
return c.json(unwrapped, 200);
});

Check warning on line 144 in pkg/timeline/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/timeline/mod.ts#L142-L144

Added lines #L142 - L144 were not covered by tests
25 changes: 25 additions & 0 deletions pkg/timeline/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
CreateListRequestSchema,
CreateListResponseSchema,
GetAccountTimelineResponseSchema,
GetListMemberResponseSchema,
} from './adaptor/validator/timeline.js';

export const GetAccountTimelineRoute = createRoute({
Expand Down Expand Up @@ -164,3 +165,27 @@
},
},
});

export const GetListMemberRoute = createRoute({
method: 'get',
tags: ['timeline'],
path: '/lists/:id/members',
responses: {
200: {
description: 'OK',
content: {
'application/json': {
schema: GetListMemberResponseSchema,
},
},
},
404: {
content: {
'application/json': {
schema: CommonErrorResponseSchema,
},
},
description: 'LIST_NOTFOUND',
},
},
});

Check warning on line 191 in pkg/timeline/router.ts

View check run for this annotation

Codecov / codecov/patch

pkg/timeline/router.ts#L169-L191

Added lines #L169 - L191 were not covered by tests
37 changes: 37 additions & 0 deletions pkg/timeline/service/fetchMember.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Result } from '@mikuroxina/mini-fn';
import { describe, expect, it, vi } from 'vitest';
import type { AccountID } from '../../accounts/model/account.js';
import { dummyAccounts } from '../../accounts/testData/testData.js';
import { dummyAccountModuleFacade } from '../../intermodule/account.js';
import { InMemoryListRepository } from '../adaptor/repository/dummy.js';
import { List, type ListID } from '../model/list.js';
import { FetchListMemberService } from './fetchMember.js';

describe('FetchListMemberService', () => {
const dummyListData = List.new({
id: '1' as ListID,
title: 'List',
publicity: 'PRIVATE',
memberIds: ['11' as AccountID, '12' as AccountID],
ownerId: '1' as AccountID,
createdAt: new Date('2024-01-01T00:00:00.000Z'),
});
const repository = new InMemoryListRepository([dummyListData]);
const service = new FetchListMemberService(
repository,
dummyAccountModuleFacade,
);

it('should fetch list members', async () => {
vi.spyOn(dummyAccountModuleFacade, 'fetchAccounts').mockImplementation(
async () => {
return Result.ok(dummyAccounts);
},
);

const res = await service.handle('1' as ListID);

expect(Result.isOk(res)).toBe(true);
expect(Result.unwrap(res)).toStrictEqual(dummyAccounts);
});
});
29 changes: 29 additions & 0 deletions pkg/timeline/service/fetchMember.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Result } from '@mikuroxina/mini-fn';
import type {
Account,
AccountModuleFacade,
} from '../../intermodule/account.js';
import type { ListID } from '../model/list.js';
import type { ListRepository } from '../model/repository.js';

export class FetchListMemberService {
constructor(
private readonly listRepository: ListRepository,
private readonly accountModule: AccountModuleFacade,
) {}

async handle(listID: ListID): Promise<Result.Result<Error, Account[]>> {
const list = await this.listRepository.fetchListMembers(listID);
if (Result.isErr(list)) {
return list;
}

Check warning on line 19 in pkg/timeline/service/fetchMember.ts

View check run for this annotation

Codecov / codecov/patch

pkg/timeline/service/fetchMember.ts#L18-L19

Added lines #L18 - L19 were not covered by tests
const unwrappedAccountID = Result.unwrap(list);

const accounts = await this.accountModule.fetchAccounts(unwrappedAccountID);
if (Result.isErr(accounts)) {
return accounts;
}

Check warning on line 25 in pkg/timeline/service/fetchMember.ts

View check run for this annotation

Codecov / codecov/patch

pkg/timeline/service/fetchMember.ts#L24-L25

Added lines #L24 - L25 were not covered by tests

return Result.ok(Result.unwrap(accounts));
laminne marked this conversation as resolved.
Show resolved Hide resolved
}
}
2 changes: 1 addition & 1 deletion pkg/timeline/service/noteVisibility.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { Result } from '@mikuroxina/mini-fn';
import { describe, expect, it, vi } from 'vitest';

import type { AccountID } from '../../accounts/model/account.js';
import { partialAccount1 } from '../../accounts/testData/testData.js';
import { dummyAccountModuleFacade } from '../../intermodule/account.js';
import {
dummyDirectNote,
dummyFollowersNote,
dummyHomeNote,
dummyPublicNote,
partialAccount1,
} from '../testData/testData.js';
import { NoteVisibilityService } from './noteVisibility.js';

Expand Down
3 changes: 2 additions & 1 deletion pkg/timeline/service/push.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Result } from '@mikuroxina/mini-fn';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { partialAccount1 } from '../../accounts/testData/testData.js';
import { dummyAccountModuleFacade } from '../../intermodule/account.js';
import { InMemoryTimelineCacheRepository } from '../adaptor/repository/dummyCache.js';
import { dummyPublicNote, partialAccount1 } from '../testData/testData.js';
import { dummyPublicNote } from '../testData/testData.js';
import { NoteVisibilityService } from './noteVisibility.js';
import { PushTimelineService } from './push.js';

Expand Down
22 changes: 1 addition & 21 deletions pkg/timeline/testData/testData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
* */
import { Option } from '@mikuroxina/mini-fn';

import { Account, type AccountID } from '../../accounts/model/account.js';
import type { PartialAccount } from '../../intermodule/account.js';
import type { AccountID } from '../../accounts/model/account.js';
import { Note, type NoteID } from '../../notes/model/note.js';

export const dummyPublicNote = Note.new({
Expand Down Expand Up @@ -51,22 +50,3 @@ export const dummyDirectNote = Note.new({
visibility: 'DIRECT',
createdAt: new Date('2023/10/10 00:00:00'),
});
export const dummyAccount1 = Account.new({
id: '101' as AccountID,
bio: 'this is test user',
mail: 'john@example.com',
name: '@john@example.com',
nickname: 'John Doe',
passphraseHash: '',
role: 'normal',
silenced: 'normal',
status: 'active',
frozen: 'normal',
createdAt: new Date(),
});
export const partialAccount1: PartialAccount = {
id: dummyAccount1.getID(),
name: dummyAccount1.getName(),
nickname: dummyAccount1.getNickname(),
bio: dummyAccount1.getBio(),
};
Loading