-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
1,361 additions
and
126 deletions.
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 was deleted.
Oops, something went wrong.
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,50 @@ | ||
import * as dotenv from 'dotenv'; | ||
import * as Ably from 'ably/promises'; | ||
import { HandlerEvent } from '@netlify/functions'; | ||
import { ulid } from 'ulidx'; | ||
import express, { Router } from 'express'; | ||
import serverless from 'serverless-http'; | ||
|
||
dotenv.config(); | ||
|
||
const messages = []; | ||
|
||
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. | ||
const api = express(); | ||
|
||
const router = Router(); | ||
router.post('/conversations/:conversationId/messages', (req, res) => { | ||
const conversationId = req.params.conversationId; | ||
const ablyToken = req.headers.authorization.split(' ')[1]; | ||
|
||
const message = { | ||
id: ulid(), | ||
...JSON.parse(req.body), | ||
client_id: req.headers['ably-clientid'], | ||
conversation_id: conversationId, | ||
reactions: { | ||
counts: {}, | ||
latest: [], | ||
mine: [], | ||
}, | ||
created_at: Date.now(), | ||
updated_at: null, | ||
deleted_at: null, | ||
}; | ||
|
||
Please see README.md for more details on configuring your Ably API Key.`); | ||
messages.push(message); | ||
|
||
return { | ||
statusCode: 500, | ||
headers: { 'content-type': 'application/json' }, | ||
body: JSON.stringify('ABLY_API_KEY is not set'), | ||
}; | ||
} | ||
const client = new Ably.Rest(ablyToken); | ||
|
||
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); | ||
client.channels.get(`conversations:${conversationId}`).publish('message.created', message); | ||
|
||
const client = new Ably.Rest(process.env.ABLY_API_KEY); | ||
res.json({ id: message.id }); | ||
|
||
client.channels.get(`conversations:${conversationId}`).publish('message.created', message); | ||
res.status(201).end(); | ||
}); | ||
|
||
return { | ||
statusCode: 201, | ||
headers: { 'content-type': 'application/json' }, | ||
body: JSON.stringify({ id: message.id }), | ||
}; | ||
} | ||
router.get('/conversations/:conversationId/messages', (req, res) => { | ||
res.json(messages); | ||
}); | ||
|
||
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), | ||
}; | ||
} | ||
api.use('/api/conversations/v1', router); | ||
|
||
return { | ||
statusCode: 404, | ||
body: 'Not Found', | ||
}; | ||
} | ||
export const handler = serverless(api); |
Oops, something went wrong.