Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 1 addition & 12 deletions pkg/accounts/adaptor/repository/dummy/follow.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Ether, Result } from '@mikuroxina/mini-fn';
import type { AccountID } from '../../../model/account.ts';
import { AccountNotFoundError } from '../../../model/errors.ts';
import type { AccountFollow } from '../../../model/follow.ts';
import {
type AccountFollowCount,
Expand Down Expand Up @@ -36,17 +35,7 @@ export class InMemoryAccountFollowRepository
return Result.ok(undefined);
}

async unfollow(
accountID: AccountID,
targetID: AccountID,
): Promise<Result.Result<Error, void>> {
const follow = [...this.#data].find(
(f) => f.getFromID() === accountID && f.getTargetID() === targetID,
);
if (!follow) {
return Result.err(new AccountNotFoundError('not found', { cause: null }));
}

async unfollow(follow: AccountFollow): Promise<Result.Result<Error, void>> {
this.#data.delete(follow);
return Result.ok(undefined);
}
Expand Down
13 changes: 6 additions & 7 deletions pkg/accounts/adaptor/repository/prisma/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,21 +306,20 @@ export class PrismaAccountFollowRepository implements AccountFollowRepository {
}
}

async unfollow(
fromID: AccountID,
targetID: AccountID,
): Promise<Result.Result<Error, void>> {
async unfollow(follow: AccountFollow): Promise<Result.Result<Error, void>> {
try {
// ToDo: Should replace with a hard delete. It can't follow it back again due to a composite primary key.
await this.#prisma.following.update({
where: {
fromId_toId: {
fromId: fromID,
toId: targetID,
fromId: follow.getFromID(),
toId: follow.getTargetID(),
},
},
data: {
deletedAt: new Date(),
deletedAt: Option.isNone(follow.getDeletedAt())
? undefined
: Option.unwrap(follow.getDeletedAt()),
},
});
return Result.ok(undefined);
Expand Down
3 changes: 2 additions & 1 deletion pkg/accounts/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ export const controller = new AccountController({
unFollowService: Ether.runEther(
Cat.cat(unfollow)
.feed(Ether.compose(accountFollowRepository))
.feed(Ether.compose(accountRepository)).value,
.feed(Ether.compose(accountRepository))
.feed(Ether.compose(clock)).value,
),
resendTokenService: Ether.runEther(
Cat.cat(resendToken)
Expand Down
5 changes: 1 addition & 4 deletions pkg/accounts/model/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ export interface AccountFollowCount {
}
export interface AccountFollowRepository {
follow(follow: AccountFollow): Promise<Result.Result<Error, void>>;
unfollow(
fromID: AccountID,
targetID: AccountID,
): Promise<Result.Result<Error, void>>;
unfollow(follow: AccountFollow): Promise<Result.Result<Error, void>>;
fetchAllFollowers(
accountID: AccountID,
): Promise<Result.Result<Error, AccountFollow[]>>;
Expand Down
29 changes: 19 additions & 10 deletions pkg/accounts/service/unfollow.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Option, Result } from '@mikuroxina/mini-fn';
import { describe, expect, it } from 'vitest';

import { MockClock } from '../../internal/id/mod.ts';
import { InMemoryAccountRepository } from '../adaptor/repository/dummy/account.ts';
import { InMemoryAccountFollowRepository } from '../adaptor/repository/dummy/follow.ts';
import { Account, type AccountID } from '../model/account.ts';
Expand Down Expand Up @@ -42,16 +43,20 @@ await accountRepository.create(
deletedAt: undefined,
}),
);
const repository = new InMemoryAccountFollowRepository([
Result.unwrap(
AccountFollow.new({
fromID: '1' as AccountID,
targetID: '2' as AccountID,
createdAt: new Date(),
}),
),
]);
const service = new UnfollowService(repository, accountRepository);
const follow = Result.unwrap(
AccountFollow.new({
fromID: '1' as AccountID,
targetID: '2' as AccountID,
createdAt: new Date('2023-09-10T00:00:00Z'),
}),
);
follow.pullEvents();
const repository = new InMemoryAccountFollowRepository([follow]);
const service = new UnfollowService(
repository,
accountRepository,
new MockClock(new Date('2023-09-11T00:00:00Z')),
);

describe('UnfollowService', () => {
it('should unfollow', async () => {
Expand All @@ -61,5 +66,9 @@ describe('UnfollowService', () => {
);

expect(Option.isSome(res)).toBe(false);
expect(follow.getDeletedAt()).toStrictEqual(
Option.some(new Date('2023-09-11T00:00:00Z')),
);
expect(follow.pullEvents()).toHaveLength(1);
});
});
39 changes: 31 additions & 8 deletions pkg/accounts/service/unfollow.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Cat, Ether, Option, Promise, Result } from '@mikuroxina/mini-fn';

import { type Clock, clockSymbol } from '../../internal/id/mod.ts';
import type { AccountName } from '../model/account.ts';
import { AccountNotFoundError } from '../model/errors.ts';
import {
Expand All @@ -12,12 +13,15 @@ import {
export class UnfollowService {
readonly #followRepository: AccountFollowRepository;
readonly #accountRepository: AccountRepository;
readonly #clock: Clock;
constructor(
followRepository: AccountFollowRepository,
accountRepository: AccountRepository,
clock: Clock,
) {
this.#followRepository = followRepository;
this.#accountRepository = accountRepository;
this.#clock = clock;
}

async handle(
Expand Down Expand Up @@ -47,12 +51,30 @@ export class UnfollowService {
),
),
)
.finishM(({ fromAccount, targetAccount }) =>
this.#followRepository.unfollow(
fromAccount.getID(),
targetAccount.getID(),
),
);
.addMWith('allFollows', ({ fromAccount }) =>
this.#followRepository.fetchAllFollowing(fromAccount.getID()),
)
.addMWith('follow', async ({ allFollows, targetAccount }) => {
const follow = allFollows.find(
(item) => item.getTargetID() === targetAccount.getID(),
);
return follow
? Result.ok(follow)
: Result.err(
new AccountNotFoundError('follow not found', {
cause: null,
}),
);
})
.runWith(({ follow }) =>
Promise.resolve(
follow.delete(new Date(Number(this.#clock.now()))),
).then(Result.map(() => [])),
)
.runWith(({ follow }) =>
monad.map(() => [])(this.#followRepository.unfollow(follow)),
)
.finish(() => []);

return Result.optionErr(res);
}
Expand All @@ -61,10 +83,11 @@ export class UnfollowService {
export const unfollowSymbol = Ether.newEtherSymbol<UnfollowService>();
export const unfollow = Ether.newEther(
unfollowSymbol,
({ accountFollowRepository, accountRepository }) =>
new UnfollowService(accountFollowRepository, accountRepository),
({ accountFollowRepository, accountRepository, clock }) =>
new UnfollowService(accountFollowRepository, accountRepository, clock),
{
accountFollowRepository: followRepoSymbol,
accountRepository: accountRepoSymbol,
clock: clockSymbol,
},
);
Loading