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
12 changes: 10 additions & 2 deletions pkg/timeline/adaptor/repository/dummy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
ListNotFoundError,
TimelineInvalidFilterRangeError,
} from '../../model/errors.ts';
import type { List, ListID } from '../../model/list.ts';
import { List, type ListID } from '../../model/list.ts';
import {
type BookmarkTimelineFilter,
type BookmarkTimelineRepository,
Expand Down Expand Up @@ -338,7 +338,15 @@ export class InMemoryListRepository implements ListRepository {
);
}

list.removeMember(accountID);
const updatedList = List.reconstruct({
id: list.getId(),
title: list.getTitle(),
publicity: list.isPublic() ? 'PUBLIC' : 'PRIVATE',
ownerId: list.getOwnerId(),
memberIds: list.getMemberIds().filter((v) => v !== accountID),
createdAt: list.getCreatedAt(),
});
this.#listData.set(listID, updatedList);
return Result.ok(undefined);
}

Expand Down
117 changes: 109 additions & 8 deletions pkg/timeline/model/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('List', () => {

describe('new', () => {
it('should create a new list', () => {
const res = List.new(args);
const res = List.new(args, args.ownerId);

expect(Result.isOk(res)).toBe(true);
const list = Result.unwrap(res);
Expand All @@ -34,14 +34,14 @@ describe('List', () => {
});

it('should return ListTitleLengthInvalidError when title is empty', () => {
const res = List.new({ ...args, title: '' });
const res = List.new({ ...args, title: '' }, args.ownerId);

expect(Result.isErr(res)).toBe(true);
expect(Result.unwrapErr(res)).toBeInstanceOf(ListTitleLengthInvalidError);
});

it('should return ListTitleLengthInvalidError when title exceeds 100 chars', () => {
const res = List.new({ ...args, title: 'a'.repeat(101) });
const res = List.new({ ...args, title: 'a'.repeat(101) }, args.ownerId);

expect(Result.isErr(res)).toBe(true);
expect(Result.unwrapErr(res)).toBeInstanceOf(ListTitleLengthInvalidError);
Expand All @@ -52,7 +52,7 @@ describe('List', () => {
{ length: 251 },
(_, i) => `${i + 1}` as AccountID,
);
const res = List.new({ ...args, memberIds });
const res = List.new({ ...args, memberIds }, args.ownerId);

expect(Result.isErr(res)).toBe(true);
expect(Result.unwrapErr(res)).toBeInstanceOf(ListTooManyMembersError);
Expand All @@ -63,7 +63,7 @@ describe('List', () => {
const list = List.reconstruct(args);
const memberId = '4' as AccountID;

const res = list.addMember(memberId);
const res = list.addMember(memberId, args.ownerId);

expect(Result.isOk(res)).toBe(true);
expect(list.getMemberIds()).toStrictEqual([
Expand All @@ -79,7 +79,7 @@ describe('List', () => {
});
const memberId = '3' as AccountID;

const res = list.addMember(memberId);
const res = list.addMember(memberId, args.ownerId);

expect(Result.isErr(res)).toBe(true);
expect(Result.unwrapErr(res)).toBeInstanceOf(ListMemberAlreadyExistsError);
Expand All @@ -94,7 +94,7 @@ describe('List', () => {
const list = List.reconstruct({ ...args, memberIds });
const newMemberId = '251' as AccountID;

const res = list.addMember(newMemberId);
const res = list.addMember(newMemberId, args.ownerId);

expect(Result.isErr(res)).toBe(true);
expect(Result.unwrapErr(res)).toBeInstanceOf(ListTooManyMembersError);
Expand All @@ -108,8 +108,9 @@ describe('List', () => {
});
const memberId = '3' as AccountID;

list.removeMember(memberId);
const res = list.removeMember(memberId, args.ownerId);

expect(Result.isOk(res)).toBe(true);
expect(list.getMemberIds()).toStrictEqual([]);
});

Expand Down Expand Up @@ -172,4 +173,104 @@ describe('List', () => {
expect(list.isPublic()).toBe(true);
});
});

describe('domain events', () => {
it('should not push any event on reconstruct', () => {
const list = List.reconstruct(args);

expect(list.pullEvents()).toStrictEqual([]);
});

describe('list.created', () => {
it('should push exactly one list.created event on success', () => {
const res = List.new(args, args.ownerId);
const list = Result.unwrap(res);

const events = list.pullEvents();

expect(events).toHaveLength(1);
const [event] = events;
expect(event?.eventName).toBe('list.created');
expect(event?.target).toBe(args.id);
expect(event?.actor).toBe(args.ownerId);
expect(event?.payload).toStrictEqual({
ownerID: args.ownerId,
title: args.title,
});
});

it('should not push any event when validation fails', () => {
const res = List.new({ ...args, title: '' }, args.ownerId);

expect(Result.isErr(res)).toBe(true);
});
});

describe('list.member.appended', () => {
it('should push exactly one list.member.appended event on success', () => {
const list = List.reconstruct(args);
const memberId = '4' as AccountID;

const res = list.addMember(memberId, args.ownerId);
expect(Result.isOk(res)).toBe(true);

const events = list.pullEvents();

expect(events).toHaveLength(1);
const [event] = events;
expect(event?.eventName).toBe('list.member.appended');
expect(event?.target).toBe(args.id);
expect(event?.actor).toBe(args.ownerId);
expect(event?.payload).toStrictEqual({ memberID: memberId });
});

it('should not push any event when member already exists', () => {
const list = List.reconstruct(args);

const res = list.addMember('3' as AccountID, args.ownerId);

expect(Result.isErr(res)).toBe(true);
expect(list.pullEvents()).toStrictEqual([]);
});

it('should return an empty array on the second pullEvents call', () => {
const list = List.reconstruct(args);
const res = list.addMember('4' as AccountID, args.ownerId);
expect(Result.isOk(res)).toBe(true);

list.pullEvents();

expect(list.pullEvents()).toStrictEqual([]);
});
});

describe('list.member.removed', () => {
it('should push exactly one list.member.removed event on success', () => {
const list = List.reconstruct(args);
const memberId = '3' as AccountID;

const res = list.removeMember(memberId, args.ownerId);
expect(Result.isOk(res)).toBe(true);

const events = list.pullEvents();

expect(events).toHaveLength(1);
const [event] = events;
expect(event?.eventName).toBe('list.member.removed');
expect(event?.target).toBe(args.id);
expect(event?.actor).toBe(args.ownerId);
expect(event?.payload).toStrictEqual({ memberID: memberId });
});

it('should return an empty array on the second pullEvents call', () => {
const list = List.reconstruct(args);
const res = list.removeMember('3' as AccountID, args.ownerId);
expect(Result.isOk(res)).toBe(true);

list.pullEvents();

expect(list.pullEvents()).toStrictEqual([]);
});
});
});
});
62 changes: 58 additions & 4 deletions pkg/timeline/model/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ListTitleLengthInvalidError,
ListTooManyMembersError,
} from './errors.ts';
import { type ListEvent, listEventFactory } from './event/listEvents.ts';

export type ListID = ID<List>;
export type CreateListArgs = Readonly<{
Expand Down Expand Up @@ -35,6 +36,7 @@ export class List {
readonly #ownerId: AccountID;
readonly #memberIds: Set<AccountID>;
readonly #createdAt: Date;
#events: ListEvent[] = [];

private constructor(args: CreateListArgs) {
this.#id = args.id;
Expand All @@ -47,9 +49,41 @@ export class List {

static new(
args: CreateListArgs,
actor: AccountID,
): Result.Result<
ListTitleLengthInvalidError | ListTooManyMembersError,
List
> {
const validationErr = List.#checkArgs(args);
if (Result.isErr(validationErr)) {
return validationErr;
}

const list = new List(args);
list.#events.push(
listEventFactory.created({
target: args.id,
actor,
ownerID: args.ownerId,
title: args.title,
}),
);
return Result.ok(list);
}

static reconstruct(args: CreateListArgs): List {
const validationErr = List.#checkArgs(args);
if (Result.isErr(validationErr)) {
throw Result.unwrapErr(validationErr);
}
return new List(args);
}

static #checkArgs(
args: CreateListArgs,
): Result.Result<
ListTitleLengthInvalidError | ListTooManyMembersError,
void
> {
const parsed = v.safeParse(listTitleSchema, args.title);
if (!parsed.success) {
Expand All @@ -64,11 +98,11 @@ export class List {
new ListTooManyMembersError('too many members', { cause: null }),
);
}
return Result.ok(new List(args));
return Result.ok(undefined);
}

static reconstruct(args: CreateListArgs): List {
return Result.unwrap(List.new(args));
pullEvents(): ListEvent[] {
return this.#events.splice(0);
}

getId(): ListID {
Expand Down Expand Up @@ -120,6 +154,7 @@ export class List {

addMember(
memberId: AccountID,
actor: AccountID,
): Result.Result<
ListTooManyMembersError | ListMemberAlreadyExistsError,
void
Expand All @@ -136,11 +171,30 @@ export class List {
new ListTooManyMembersError('too many members', { cause: null }),
);
}

this.#memberIds.add(memberId);
this.#events.push(
listEventFactory.memberAppended({
target: this.#id,
actor,
memberID: memberId,
}),
);
return Result.ok(undefined);
}

removeMember(memberId: AccountID): void {
removeMember(
memberId: AccountID,
actor: AccountID,
): Result.Result<never, void> {
this.#memberIds.delete(memberId);
this.#events.push(
listEventFactory.memberRemoved({
target: this.#id,
actor,
memberID: memberId,
}),
);
return Result.ok(undefined);
}
}
8 changes: 6 additions & 2 deletions pkg/timeline/service/appendMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ export class AppendListMemberService {
),
)
.runWith(({ list }) =>
monad.map(() => [])(Promise.resolve(list.addMember(accountID))),
monad.map(() => [])(
Promise.resolve(list.addMember(accountID, actorID)),
),
)
.runWith(({ list }) =>
monad.map(() => [])(this.#listRepository.appendListMember(list)),
Expand All @@ -69,5 +71,7 @@ export const appendListMemberSymbol =
export const appendListMember = Ether.newEther(
appendListMemberSymbol,
({ listRepository }) => new AppendListMemberService(listRepository),
{ listRepository: listRepoSymbol },
{
listRepository: listRepoSymbol,
},
);
17 changes: 10 additions & 7 deletions pkg/timeline/service/createList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ export class CreateListService {
.addM('id', Promise.resolve(this.#idGenerator.generate<List>()))
.addMWith('list', ({ id }) =>
Promise.resolve(
List.new({
id,
title,
publicity: isPublic ? 'PUBLIC' : 'PRIVATE',
List.new(
{
id,
title,
publicity: isPublic ? 'PUBLIC' : 'PRIVATE',
ownerId,
memberIds: [] as const,
createdAt: new Date(Number(this.#clock.now())),
},
ownerId,
memberIds: [] as const,
createdAt: new Date(Number(this.#clock.now())),
}),
),
),
)
.runWith(({ list }) =>
Expand Down
Loading