Skip to content

Commit

Permalink
refactor: extract message parser
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrunton committed Aug 12, 2024
1 parent 210c554 commit 647abac
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 10 deletions.
8 changes: 6 additions & 2 deletions services/api/src/app/messages/messages.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { HttpException, Injectable } from '@nestjs/common';
import { CreateMessageDto } from './dto/create-message.dto';
import { isCommand, parseMessage } from './parse-message';
import { User, systemUser } from '@entities/user.entity';
import { SendMessageUseCase } from '@usecases/messages/send';
import { CommandService } from '@app/messages/command.service';
import { isCommand, parseMessage } from '@usecases/messages/parse-message';

@Injectable()
export class MessagesService {
Expand All @@ -16,7 +16,11 @@ export class MessagesService {
incoming: CreateMessageDto,
authenticatedUser: User,
): Promise<void> {
const message = parseMessage(incoming, authenticatedUser);
const message = parseMessage({
...incoming,
authorId: authenticatedUser.id,
});

try {
if (isCommand(message)) {
await this.command.exec(message, authenticatedUser);
Expand Down
36 changes: 36 additions & 0 deletions services/api/src/domain/usecases/messages/parse-message.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { parseMessage } from './parse-message';

describe('parseMessage', () => {
const roomId = 'room-id';
const authorId = 'user-id';

it('parses commands', () => {
expect(
parseMessage({ content: '/lorem 3 words', roomId, authorId }),
).toEqual({
canonicalInput: '/lorem 3 words',
roomId,
tokens: ['lorem', '3', 'words'],
});
});

it('ignores excess whitespace in commands', () => {
expect(
parseMessage({ content: '/lorem 3 words', roomId, authorId }),
).toEqual({
canonicalInput: '/lorem 3 words',
roomId,
tokens: ['lorem', '3', 'words'],
});
});

it('parses normal messages', () => {
expect(
parseMessage({ content: 'Hello, World!', roomId, authorId }),
).toEqual({
content: 'Hello, World!',
roomId,
authorId,
});
});
});
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import { Command } from '@entities/command.entity';
import { DraftMessage } from '@entities/message.entity';
import { User } from '@entities/user.entity';
import { CreateMessageDto } from 'src/app/messages/dto/create-message.dto';

export type ParsedMessage = DraftMessage | Command;

export const isCommand = (message: ParsedMessage): message is Command => {
return (message as Command).tokens !== undefined;
};

export const parseMessage = (
{ content, roomId }: CreateMessageDto,
authenticatedUser: User,
): ParsedMessage => {
type ParseMessageParams = {
content: string;
roomId: string;
authorId: string;
};

export const parseMessage = ({
content,
roomId,
authorId,
}: ParseMessageParams): ParsedMessage => {
if (content.startsWith('/')) {
const tokens = content.slice(1).split(' ');
const tokens = content
.slice(1)
.split(' ')
.filter((token) => token.length > 0);

const canonicalInput = `/${tokens.join(' ')}`;

const command: Command = {
Expand All @@ -29,7 +38,7 @@ export const parseMessage = (
const message: ParsedMessage = {
content,
roomId,
authorId: authenticatedUser.id,
authorId,
};

return message;
Expand Down

0 comments on commit 647abac

Please sign in to comment.