Skip to content

Commit

Permalink
refactor: intermodule, use direct function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
laminne committed Jul 23, 2024
1 parent 61cb9cb commit 14bd412
Show file tree
Hide file tree
Showing 18 changed files with 242 additions and 170 deletions.
112 changes: 0 additions & 112 deletions pkg/intermodule/account.ts

This file was deleted.

76 changes: 76 additions & 0 deletions pkg/intermodule/adaptor/account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Result } from '@mikuroxina/mini-fn';
import type { Account, AccountID } from '../../accounts/model/account.js';
import type { FetchService } from '../../accounts/service/fetch.js';
import type { FetchFollowService } from '../../accounts/service/fetchFollow.js';
import type {
AccountModuleInterface,
PartialAccount,
} from '../interfaces/account.js';

export class AccountModule implements AccountModuleInterface {
constructor(
private readonly fetchService: FetchService,
private readonly fetchFollowService: FetchFollowService,
) {}

async fetchAccount(id: AccountID): Promise<Result.Result<Error, Account>> {
const res = await this.fetchService.fetchAccountByID(id);
if (Result.isErr(res)) {
return res;
}

Check warning on line 20 in pkg/intermodule/adaptor/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/intermodule/adaptor/account.ts#L17-L20

Added lines #L17 - L20 were not covered by tests

return res;
}

Check warning on line 23 in pkg/intermodule/adaptor/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/intermodule/adaptor/account.ts#L22-L23

Added lines #L22 - L23 were not covered by tests

async fetchFollowings(
id: AccountID,
): Promise<Result.Result<Error, PartialAccount[]>> {
const res = await this.fetchFollowService.fetchFollowingsByID(id);
if (Result.isErr(res)) {
return res;
}

Check warning on line 31 in pkg/intermodule/adaptor/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/intermodule/adaptor/account.ts#L26-L31

Added lines #L26 - L31 were not covered by tests

const accounts = await Promise.all(
res[1].map((v) => this.fetchService.fetchAccountByID(v.getTargetID())),
);

Check warning on line 35 in pkg/intermodule/adaptor/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/intermodule/adaptor/account.ts#L33-L35

Added lines #L33 - L35 were not covered by tests

return Result.ok(
accounts
.filter((v) => Result.isOk(v))
.map((v): PartialAccount => {
return {
id: v[1].getID(),
name: v[1].getName(),
nickname: v[1].getNickname(),
bio: v[1].getBio(),
};
}),
);
}

Check warning on line 49 in pkg/intermodule/adaptor/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/intermodule/adaptor/account.ts#L37-L49

Added lines #L37 - L49 were not covered by tests

async fetchFollowers(
id: AccountID,
): Promise<Result.Result<Error, PartialAccount[]>> {
const res = await this.fetchFollowService.fetchFollowersByID(id);
if (Result.isErr(res)) {
return res;
}

Check warning on line 57 in pkg/intermodule/adaptor/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/intermodule/adaptor/account.ts#L52-L57

Added lines #L52 - L57 were not covered by tests

const accounts = await Promise.all(
res[1].map((v) => this.fetchService.fetchAccountByID(v.getFromID())),
);

Check warning on line 61 in pkg/intermodule/adaptor/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/intermodule/adaptor/account.ts#L59-L61

Added lines #L59 - L61 were not covered by tests

return Result.ok(
accounts
.filter((v) => Result.isOk(v))
.map((v): PartialAccount => {
return {
id: v[1].getID(),
name: v[1].getName(),
nickname: v[1].getNickname(),
bio: v[1].getBio(),
};
}),
);
}

Check warning on line 75 in pkg/intermodule/adaptor/account.ts

View check run for this annotation

Codecov / codecov/patch

pkg/intermodule/adaptor/account.ts#L63-L75

Added lines #L63 - L75 were not covered by tests
}
20 changes: 20 additions & 0 deletions pkg/intermodule/adaptor/timeline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Result } from '@mikuroxina/mini-fn';

Check warning on line 1 in pkg/intermodule/adaptor/timeline.ts

View check run for this annotation

Codecov / codecov/patch

pkg/intermodule/adaptor/timeline.ts#L1

Added line #L1 was not covered by tests
import type { Note } from '../../notes/model/note.js';
import type { PushTimelineService } from '../../timeline/service/push.js';

export class TimelineModule {
constructor(private readonly pushTimelineService: PushTimelineService) {}

Check warning on line 6 in pkg/intermodule/adaptor/timeline.ts

View check run for this annotation

Codecov / codecov/patch

pkg/intermodule/adaptor/timeline.ts#L5-L6

Added lines #L5 - L6 were not covered by tests

/*
* @description Push note to timeline
* @param note to be pushed
* */
async pushNoteToTimeline(note: Note): Promise<Result.Result<Error, void>> {
const res = await this.pushTimelineService.handle(note);
if (Result.isErr(res)) {
return res;
}

Check warning on line 16 in pkg/intermodule/adaptor/timeline.ts

View check run for this annotation

Codecov / codecov/patch

pkg/intermodule/adaptor/timeline.ts#L12-L16

Added lines #L12 - L16 were not covered by tests

return Result.ok(undefined);
}
}

Check warning on line 20 in pkg/intermodule/adaptor/timeline.ts

View check run for this annotation

Codecov / codecov/patch

pkg/intermodule/adaptor/timeline.ts#L18-L20

Added lines #L18 - L20 were not covered by tests
23 changes: 23 additions & 0 deletions pkg/intermodule/interfaces/account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Result } from '@mikuroxina/mini-fn';
import type {
Account,
AccountID,
AccountName,
} from '../../accounts/model/account.js';

export interface PartialAccount {
id: AccountID;
name: AccountName;
nickname: string;
bio: string;
}

export interface AccountModuleInterface {
fetchAccount(id: AccountID): Promise<Result.Result<Error, Account>>;
fetchFollowings(
id: AccountID,
): Promise<Result.Result<Error, PartialAccount[]>>;
fetchFollowers(
id: AccountID,
): Promise<Result.Result<Error, PartialAccount[]>>;
}
10 changes: 10 additions & 0 deletions pkg/intermodule/interfaces/timeline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Result } from '@mikuroxina/mini-fn';
import type { Note } from '../../notes/model/note.js';

export interface TimelineModuleInterface {
/*
* @description Push note to timeline
* @param note to be pushed
* */
pushNoteToTimeline(note: Note): Promise<Result.Result<Error, void>>;
}
29 changes: 0 additions & 29 deletions pkg/intermodule/timeline.ts

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/notes/adaptor/controller/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Option, Result } from '@mikuroxina/mini-fn';

import type { AccountID } from '../../../accounts/model/account.js';
import type { MediumID } from '../../../drive/model/medium.js';
import type { AccountModule } from '../../../intermodule/account.js';
import type { AccountModuleInterface } from '../../../intermodule/interfaces/account.js';
import type { NoteID, NoteVisibility } from '../../model/note.js';
import type { CreateService } from '../../service/create.js';
import type { FetchService } from '../../service/fetch.js';
Expand All @@ -19,7 +19,7 @@ export class NoteController {
private readonly createService: CreateService,
private readonly fetchService: FetchService,
private readonly renoteService: RenoteService,
private readonly accountModule: AccountModule,
private readonly accountModule: AccountModuleInterface,

Check warning on line 22 in pkg/notes/adaptor/controller/note.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/adaptor/controller/note.ts#L22

Added line #L22 was not covered by tests
) {}

async createNote(args: {
Expand Down
37 changes: 35 additions & 2 deletions pkg/notes/mod.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import { OpenAPIHono } from '@hono/zod-openapi';
import { Cat, Ether, Promise, Result } from '@mikuroxina/mini-fn';

import {
InMemoryAccountRepository,
newFollowRepo,
} from '../accounts/adaptor/repository/dummy.js';

Check warning on line 7 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L7

Added line #L7 was not covered by tests
import {
PrismaAccountRepository,
prismaFollowRepo,
} from '../accounts/adaptor/repository/prisma.js';

Check warning on line 11 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L11

Added line #L11 was not covered by tests
import type { AccountID } from '../accounts/model/account.js';
import { accountRepoSymbol } from '../accounts/model/repository.js';

Check warning on line 13 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L13

Added line #L13 was not covered by tests
import { authenticateToken } from '../accounts/service/authenticationTokenService.js';
import { fetch } from '../accounts/service/fetch.js';
import { fetchFollow } from '../accounts/service/fetchFollow.js';

Check warning on line 16 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L15-L16

Added lines #L15 - L16 were not covered by tests
import {
type AuthMiddlewareVariable,
authenticateMiddleware,
} from '../adaptors/authenticateMiddleware.js';
import { prismaClient } from '../adaptors/prisma.js';
import { SnowflakeIDGenerator } from '../id/mod.js';
import type { ID } from '../id/type.js';
import { AccountModule } from '../intermodule/account.js';
import { AccountModule } from '../intermodule/adaptor/account.js';

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

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L24

Added line #L24 was not covered by tests
import { BookmarkController } from './adaptor/controller/bookmark.js';
import { NoteController } from './adaptor/controller/note.js';
import {
Expand Down Expand Up @@ -68,7 +79,29 @@ const AuthMiddleware = await Ether.runEtherT(
);

// Account
const accountModule = new AccountModule();
const accountRepoObject = isProduction
? new PrismaAccountRepository(prismaClient)
: new InMemoryAccountRepository([]);
const accountRepository = Ether.newEther(
accountRepoSymbol,
() => accountRepoObject,
);

Check warning on line 88 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L82-L88

Added lines #L82 - L88 were not covered by tests

const accountFollowRepository = isProduction
? prismaFollowRepo(prismaClient)
: newFollowRepo();

Check warning on line 92 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L90-L92

Added lines #L90 - L92 were not covered by tests

// const accountFetchService = new AccountFetchService(accountRepoObject);
// const accountFetchFollowservice = new FetchFollowService();

const accountModule = new AccountModule(
Ether.runEther(Cat.cat(fetch).feed(Ether.compose(accountRepository)).value),
Ether.runEther(
Cat.cat(fetchFollow)
.feed(Ether.compose(accountFollowRepository))
.feed(Ether.compose(accountRepository)).value,
),
);

Check warning on line 104 in pkg/notes/mod.ts

View check run for this annotation

Codecov / codecov/patch

pkg/notes/mod.ts#L97-L104

Added lines #L97 - L104 were not covered by tests

// Note
const createService = new CreateService(
Expand Down
15 changes: 12 additions & 3 deletions pkg/notes/service/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Option, Result } from '@mikuroxina/mini-fn';
import { afterEach, describe, expect, it, vi } from 'vitest';

import { InMemoryAccountRepository } from '../../accounts/adaptor/repository/dummy.js';
import {
InMemoryAccountFollowRepository,
InMemoryAccountRepository,
} from '../../accounts/adaptor/repository/dummy.js';
import { Account, type AccountID } from '../../accounts/model/account.js';
import { AccountModule } from '../../intermodule/account.js';
import { FetchService as AccountFetchService } from '../../accounts/service/fetch.js';
import { FetchFollowService } from '../../accounts/service/fetchFollow.js';
import { AccountModule } from '../../intermodule/adaptor/account.js';
import { InMemoryNoteRepository } from '../adaptor/repository/dummy.js';
import { Note, type NoteID } from '../model/note.js';
import { FetchService } from './fetch.js';
Expand Down Expand Up @@ -85,7 +90,11 @@ const accountRepository = new InMemoryAccountRepository([
testAccount,
frozenAccount,
]);
const accountModule = new AccountModule();
const accountFollowRepository = new InMemoryAccountFollowRepository();
const accountModule = new AccountModule(
new AccountFetchService(accountRepository),
new FetchFollowService(accountFollowRepository, accountRepository),
);
const service = new FetchService(repository, accountModule);

describe('FetchService', () => {
Expand Down
4 changes: 2 additions & 2 deletions pkg/notes/service/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Option, Result } from '@mikuroxina/mini-fn';

import type { AccountModule } from '../../intermodule/account.js';
import type { AccountModuleInterface } from '../../intermodule/interfaces/account.js';
import type { Note, NoteID } from '../model/note.js';
import type { NoteRepository } from '../model/repository.js';

export class FetchService {
constructor(
private readonly noteRepository: NoteRepository,
private readonly accountModule: AccountModule,
private readonly accountModule: AccountModuleInterface,
) {}

async fetchNoteByID(noteID: NoteID): Promise<Option.Option<Note>> {
Expand Down
Loading

0 comments on commit 14bd412

Please sign in to comment.