-
Notifications
You must be signed in to change notification settings - Fork 2
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-97] feat: add reaction methods #26
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
28d3294
feat: add message reactions SDK
ttypic 877a13f
feat: add presence
ttypic cc895ad
feat: provide auth header
ttypic 26f4f0d
fix: server token details
ttypic 34ab922
fix: mock reactions calls
ttypic 6adccd2
feat: update demo
ttypic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
demo/api/conversations/controllers/conversationsController.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,12 @@ | ||
import * as dotenv from 'dotenv'; | ||
import * as Ably from 'ably/promises'; | ||
import { HandlerEvent } from '@netlify/functions'; | ||
import { ulid } from 'ulidx'; | ||
import express from 'express'; | ||
import serverless from 'serverless-http'; | ||
import { router } from './routes'; | ||
|
||
dotenv.config(); | ||
|
||
const messages = []; | ||
const api = express(); | ||
|
||
export async function handler(event: HandlerEvent) { | ||
if (!process.env.ABLY_API_KEY) { | ||
console.error(` | ||
Missing ABLY_API_KEY environment variable. | ||
If you're running locally, please ensure you have a ./.env file with a value for ABLY_API_KEY=your-key. | ||
If you're running in Netlify, make sure you've configured env variable ABLY_API_KEY. | ||
api.use('/api/conversations/v1', router); | ||
|
||
Please see README.md for more details on configuring your Ably API Key.`); | ||
|
||
return { | ||
statusCode: 500, | ||
headers: { 'content-type': 'application/json' }, | ||
body: JSON.stringify('ABLY_API_KEY is not set'), | ||
}; | ||
} | ||
|
||
if (/\/api\/conversations\/v1\/conversations\/(\w+)\/messages/.test(event.path) && event.httpMethod === 'POST') { | ||
const conversationId = /\/api\/conversations\/v1\/conversations\/(\w+)\/messages/.exec(event.path)[1]; | ||
const message = { | ||
id: ulid(), | ||
...JSON.parse(event.body), | ||
client_id: event.headers['ably-clientid'], | ||
conversation_id: conversationId, | ||
reactions: { | ||
counts: {}, | ||
latest: [], | ||
mine: [], | ||
}, | ||
created_at: Date.now(), | ||
updated_at: null, | ||
deleted_at: null, | ||
}; | ||
messages.push(message); | ||
|
||
const client = new Ably.Rest(process.env.ABLY_API_KEY); | ||
|
||
client.channels.get(`conversations:${conversationId}`).publish('message.created', message); | ||
|
||
return { | ||
statusCode: 201, | ||
headers: { 'content-type': 'application/json' }, | ||
body: JSON.stringify({ id: message.id }), | ||
}; | ||
} | ||
|
||
const getMessagesRegEx = /\/api\/conversations\/v1\/conversations\/(\w+)\/messages/; | ||
if (getMessagesRegEx.test(event.path) && event.httpMethod === 'GET') { | ||
return { | ||
statusCode: 200, | ||
headers: { 'content-type': 'application/json' }, | ||
body: JSON.stringify(messages), | ||
}; | ||
} | ||
|
||
return { | ||
statusCode: 404, | ||
body: 'Not Found', | ||
}; | ||
} | ||
export const handler = serverless(api); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So just to check my understanding, would this be the "room-level" reactions, whereas the message-level ones would be the example above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's right