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

[CON-96] feat: add conversation caching and some tests #22

Merged
merged 11 commits into from
Jan 2, 2024
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
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const reaction = await conversation.messages.addReaction(msgId, {
Delete reaction:

```ts
await conversation.messages.removeReaction(msgId, type)
await conversation.messages.removeReaction(reactionId)
```

### Reaction object
Expand Down Expand Up @@ -169,7 +169,7 @@ conversation.messages.subscribe(({ type, message }) => {

```ts
// Subscribe to all reactions
conversation.reactions.subscribe(({ type, reaction }) => {
conversation.messages.subscribeReactions(({ type, reaction }) => {
switch (type) {
case 'reaction.added':
console.log(reaction);
Expand Down Expand Up @@ -292,8 +292,7 @@ conversation.reactions.add(reactionType)
Remove reaction

```ts
conversation.reactions.delete(reaction)
conversation.reactions.delete(reactionType)
conversation.reactions.delete(reactionId)
```

## Typing indicator
Expand Down
80 changes: 80 additions & 0 deletions __mocks__/ably/promises/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { Types } from 'ably/promises';

const MOCK_CLIENT_ID = 'MOCK_CLIENT_ID';

const mockPromisify = <T>(expectedReturnValue): Promise<T> => new Promise((resolve) => resolve(expectedReturnValue));
const methodReturningVoidPromise = () => mockPromisify<void>((() => {})());

function createMockPresence() {
return {
get: () => mockPromisify<Types.PresenceMessage[]>([]),
update: () => mockPromisify<void>(undefined),
enter: methodReturningVoidPromise,
leave: methodReturningVoidPromise,
subscriptions: {
once: (_: unknown, fn: Function) => {
fn();
},
},
subscribe: () => {},
unsubscribe: () => {},
};
}

function createMockEmitter() {
return {
any: [],
events: {},
anyOnce: [],
eventsOnce: {},
};
}

function createMockChannel() {
return {
presence: createMockPresence(),
subscribe: () => {},
unsubscribe: () => {},
on: () => {},
off: () => {},
publish: () => {},
subscriptions: createMockEmitter(),
};
}

class MockRealtime {
public channels: {
get: () => ReturnType<typeof createMockChannel>;
};
public auth: {
clientId: string;
requestToken(): void;
};
public connection: {
id?: string;
state: string;
};

public time() {}

constructor() {
this.channels = {
get: (() => {
const mockChannel = createMockChannel();
return () => mockChannel;
})(),
};
this.auth = {
clientId: MOCK_CLIENT_ID,
requestToken: () => {},
};
this.connection = {
id: '1',
state: 'connected',
};

this['options'] = {};
}
}

export { MockRealtime as Realtime };
1 change: 0 additions & 1 deletion demo/api/conversations/.env.example

This file was deleted.

12 changes: 12 additions & 0 deletions demo/api/conversations/controllers/conversationsController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Request, Response } from 'express';
import { createConversation, getConversation } from '../inMemoryDb';

export const handleCreateConversation = (req: Request, res: Response) => {
const conversationId = req.params.conversationId;
res.json(createConversation(conversationId));
};

export const handleGetConversation = (req: Request, res: Response) => {
const conversationId = req.params.conversationId;
res.json(getConversation(conversationId));
};
62 changes: 62 additions & 0 deletions demo/api/conversations/controllers/messagesController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import * as Ably from 'ably/promises';
import { Request, Response } from 'express';
import { createMessage, deleteMessage, editMessage, findMessages } from '../inMemoryDb';

export const handleCreateMessage = (req: Request, res: Response) => {
const conversationId = req.params.conversationId;
const ablyToken = req.headers.authorization.split(' ')[1];

const message = createMessage({
...JSON.parse(req.body),
client_id: req.headers['ably-clientid'] as string,
conversation_id: conversationId,
});

const client = new Ably.Rest(ablyToken);

client.channels.get(`conversations:${conversationId}`).publish('message.created', message);

res.json({ id: message.id });

res.status(201).end();
};

export const handleQueryMessages = (req: Request, res: Response) => {
const conversationId = req.params.conversationId;
res.json(findMessages(conversationId, req.headers['ably-clientid'] as string));
};

export const handleEditMessages = (req: Request, res: Response) => {
const conversationId = req.params.conversationId;
const ablyToken = req.headers.authorization.split(' ')[1];

const message = editMessage({
id: req.params.messageId,
conversation_id: conversationId,
...JSON.parse(req.body),
});

const client = new Ably.Rest(ablyToken);

client.channels.get(`conversations:${conversationId}`).publish('message.updated', message);

res.json({ id: message.id });

res.status(201).end();
};

export const handleDeleteMessages = (req: Request, res: Response) => {
const conversationId = req.params.conversationId;
const ablyToken = req.headers.authorization.split(' ')[1];

const message = deleteMessage({
id: req.params.messageId,
conversation_id: conversationId,
});

const client = new Ably.Rest(ablyToken);

client.channels.get(`conversations:${conversationId}`).publish('message.deleted', message);

res.status(201).end();
};
34 changes: 34 additions & 0 deletions demo/api/conversations/controllers/reactionsController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Request, Response } from 'express';
import * as Ably from 'ably/promises';
import { addReaction, deleteReaction } from '../inMemoryDb';

export const handleAddReaction = (req: Request, res: Response) => {
const conversationId = req.params.conversationId;
const ablyToken = req.headers.authorization.split(' ')[1];

const reaction = addReaction({
message_id: req.params.messageId,
conversation_id: conversationId,
client_id: req.headers['ably-clientid'] as string,
...JSON.parse(req.body),
});

const client = new Ably.Rest(ablyToken);

client.channels.get(`conversations:${conversationId}`).publish('reaction.added', reaction);

res.status(201).end();
};

export const handleDeleteReaction = (req: Request, res: Response) => {
const reactionId = req.params.reactionId;
const ablyToken = req.headers.authorization.split(' ')[1];

const reaction = deleteReaction(reactionId);

const client = new Ably.Rest(ablyToken);

client.channels.get(`conversations:${reaction.conversation_id}`).publish('reaction.deleted', reaction);

res.status(201).end();
};
138 changes: 138 additions & 0 deletions demo/api/conversations/inMemoryDb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { ulid } from 'ulidx';

export interface Conversation {
id: string;
application_id: string;
ttl: number | null;
created_at: number;
}

export interface Message {
id: string;
client_id: string;
conversation_id: string;
content: string;
reactions: {
counts: Record<string, number>;
latest: Reaction[];
mine: Reaction[];
};
created_at: number;
updated_at: number | null;
deleted_at: number | null;
}

export interface Reaction {
id: string;
message_id: string;
conversation_id: string;
type: string;
client_id: string;
updated_at: number | null;
deleted_at: number | null;
}

const conversations: Conversation[] = [];
const conversationIdToMessages: Record<string, Message[]> = {};
const reactions: Reaction[] = [];

export const createConversation = (id: string): Conversation => {
const existing = conversations.find((conv) => conv.id === id);
if (existing) return existing;
const conversation = {
id,
application_id: 'demo',
ttl: null,
created_at: Date.now(),
};
conversationIdToMessages[id] = [];
conversations.push(conversation);
return conversation;
};

createConversation('conversation1');

export const getConversation = (id: string): Conversation => {
return conversations.find((conv) => conv.id === id);
};

export const findMessages = (conversationId: string, clientId: string) =>
enrichMessagesWithReactions(conversationIdToMessages[conversationId], clientId);

export const createMessage = (message: Pick<Message, 'client_id' | 'conversation_id' | 'content'>) => {
const created: Message = {
...message,
id: ulid(),
reactions: {
counts: {},
latest: [],
mine: [],
},
created_at: Date.now(),
updated_at: null,
deleted_at: null,
};
conversationIdToMessages[created.conversation_id].push(created);
return created;
};

export const editMessage = (message: Pick<Message, 'id' | 'conversation_id' | 'content'>) => {
const edited = conversationIdToMessages[message.conversation_id].find(({ id }) => message.id === id);
edited.content = message.content;
return edited;
};

export const deleteMessage = (message: Pick<Message, 'id' | 'conversation_id'>) => {
const deletedIndex = conversationIdToMessages[message.conversation_id].findIndex(({ id }) => message.id === id);
const deleted = conversationIdToMessages[message.conversation_id][deletedIndex];
conversationIdToMessages[message.conversation_id].splice(deletedIndex, 1);
return deleted;
};

export const addReaction = (
reaction: Pick<Reaction, 'id' | 'message_id' | 'type' | 'client_id' | 'conversation_id'>,
) => {
const created: Reaction = {
...reaction,
id: ulid(),
updated_at: null,
deleted_at: null,
};
reactions.push(created);
return created;
};

export const deleteReaction = (reactionId: string) => {
const deletedIndex = reactions.findIndex((reaction) => reaction.id === reactionId);
const deleted = reactions[deletedIndex];
reactions.splice(deletedIndex, 1);
return deleted;
};

const enrichMessageWithReactions = (message: Message, clientId: string): Message => {
const messageReactions = reactions.filter((reaction) => reaction.message_id === message.id);
const mine = messageReactions.filter((reaction) => reaction.client_id === clientId);
const counts = messageReactions.reduce(
(acc, reaction) => {
if (acc[reaction.type]) {
acc[reaction.type]++;
} else {
acc[reaction.type] = 1;
}
return acc;
},
{} as Record<string, number>,
);
return {
...message,
reactions: {
counts,
latest: messageReactions,
mine,
},
};
};

const enrichMessagesWithReactions = (messages: Message[], clientId: string) => {
return messages.map((message) => enrichMessageWithReactions(message, clientId));
};
Loading
Loading