2022-05-19 10:45:59 +00:00
|
|
|
import { Utils } from '@tldraw/core'
|
2022-08-02 13:56:12 +00:00
|
|
|
import { TDDocument } from '@tldraw/tldraw'
|
|
|
|
import { NextApiRequest, NextApiResponse } from 'next'
|
2022-05-18 15:59:30 +00:00
|
|
|
|
2022-05-19 10:45:59 +00:00
|
|
|
type RequestBody = {
|
|
|
|
pageId: string
|
|
|
|
document: TDDocument
|
|
|
|
}
|
2022-05-18 15:59:30 +00:00
|
|
|
|
2022-05-19 10:45:59 +00:00
|
|
|
export default async function CreateMultiplayerRoom(req: NextApiRequest, res: NextApiResponse) {
|
2022-05-18 15:59:30 +00:00
|
|
|
try {
|
2022-05-19 10:45:59 +00:00
|
|
|
// 1. Get an authentication token from Liveblocks
|
|
|
|
|
|
|
|
const { token } = await fetch('https://liveblocks.io/api/authorize', {
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${process.env.LIVEBLOCKS_SECRET_KEY}`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
2022-07-28 07:19:53 +00:00
|
|
|
}).then((d) => d.json())
|
2022-05-19 10:45:59 +00:00
|
|
|
|
|
|
|
// 2. Create the Liveblocks storage JSON
|
2022-05-18 15:59:30 +00:00
|
|
|
|
2022-05-19 10:45:59 +00:00
|
|
|
const { pageId, document } = JSON.parse(req.body) as RequestBody
|
|
|
|
|
|
|
|
const storageJson = {
|
2022-05-18 15:59:30 +00:00
|
|
|
liveblocksType: 'LiveObject',
|
|
|
|
data: {
|
|
|
|
version: 2.1,
|
|
|
|
shapes: {
|
|
|
|
liveblocksType: 'LiveMap',
|
|
|
|
data: {},
|
|
|
|
},
|
|
|
|
bindings: {
|
|
|
|
liveblocksType: 'LiveMap',
|
|
|
|
data: {},
|
|
|
|
},
|
|
|
|
assets: {
|
|
|
|
liveblocksType: 'LiveMap',
|
|
|
|
data: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-05-19 10:45:59 +00:00
|
|
|
const page = document.pages[pageId]
|
2022-05-18 15:59:30 +00:00
|
|
|
|
2022-05-19 10:45:59 +00:00
|
|
|
storageJson.data.shapes.data = page.shapes ?? {}
|
|
|
|
storageJson.data.bindings.data = page.bindings ?? {}
|
|
|
|
storageJson.data.assets.data = document.assets ?? {}
|
|
|
|
|
|
|
|
// 3. Post the JSON and token to Liveblocks
|
2022-05-18 15:59:30 +00:00
|
|
|
|
2022-05-19 10:45:59 +00:00
|
|
|
const roomId = Utils.uniqueId()
|
2022-05-18 15:59:30 +00:00
|
|
|
|
|
|
|
const result = await fetch(`https://liveblocks.net/api/v1/room/${roomId}/storage`, {
|
|
|
|
method: 'POST',
|
2022-05-19 10:45:59 +00:00
|
|
|
body: JSON.stringify(storageJson),
|
2022-05-18 15:59:30 +00:00
|
|
|
headers: {
|
2022-05-19 10:45:59 +00:00
|
|
|
Authorization: `Bearer ${token}`,
|
2022-05-18 15:59:30 +00:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if (result.status === 200) {
|
2022-05-19 11:03:06 +00:00
|
|
|
// If success, send back the url for the new multiplayer project
|
2022-05-19 10:45:59 +00:00
|
|
|
res.send({ status: 'success', message: result.statusText, url: '/r/' + roomId })
|
2022-05-18 20:46:24 +00:00
|
|
|
} else {
|
2022-05-19 10:45:59 +00:00
|
|
|
throw Error(result.statusText)
|
2022-05-18 15:59:30 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2022-05-18 20:46:24 +00:00
|
|
|
res.send({ status: 'error', message: e.message })
|
2022-05-18 15:59:30 +00:00
|
|
|
}
|
|
|
|
}
|