Created
January 3, 2020 22:04
-
-
Save anotherjesse/8574637d4af62742738bbe9f775d62b4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| const boardId = 'FIXME' | |
| const { Repo } = require('hypermerge') | |
| const Hyperswarm = require('hyperswarm') | |
| const uuid = require('uuid/v4') | |
| function myRepos(dbName) { | |
| return new Repo({ memory: true }) | |
| } | |
| function getSwarm() { | |
| return Hyperswarm({ | |
| queue: { | |
| multiplex: true, | |
| }, | |
| }) | |
| } | |
| function urlize(url) { | |
| let u = url.split('?')[0] | |
| return u.includes('/') ? u.split('/')[1] : u | |
| } | |
| const db = {} | |
| const repo = myRepos() | |
| repo.addSwarm(getSwarm(), { announce: true }) | |
| repo.watch(boardId, (board) => { | |
| db[boardId] = board | |
| const { cards } = board | |
| const urls = Object.values(cards) | |
| .map((c) => urlize(c.url)) | |
| .filter((url) => !(url in db)) | |
| urls.map((url) => { | |
| db[url] = null | |
| repo.watch(url, (d) => { | |
| db[url] = d | |
| }) | |
| }) | |
| }) | |
| function render_card(card) { | |
| return `<div style="width: ${card.width}px; | |
| background: white; | |
| height: ${card.height}px; | |
| position: fixed; | |
| top: ${card.y}px; | |
| overflow: scroll; | |
| left: ${card.x}px">${render(card.url)}</div>` | |
| } | |
| function render(url) { | |
| const content = db[urlize(url)] | |
| if (!content) { | |
| return 'missing' | |
| } | |
| if (content.cards) { | |
| let body = Object.values(content.cards) | |
| .map((card) => render_card(card)) | |
| .join('') | |
| return `<!DOCTYPE html><html><head><meta charset="utf-8"/><title>${content.title}</title></head><body bgcolor="${content.backgroundColor}">${body}</body></html>` | |
| } | |
| if (content.videoId) { | |
| return `<img src="https://i1.ytimg.com/vi/${content.videoId}/mqdefault.jpg" />` | |
| } | |
| if (content.text) { | |
| return `<pre>${content.text}</pre>` | |
| } | |
| if (content.messages) { | |
| return content.messages.map((m) => m.content).join('<br>') | |
| } | |
| return `content: ${JSON.stringify(content)}` | |
| } | |
| var express = require('express') | |
| var app = express() | |
| app.get('/', function(req, res) { | |
| res.send(render(boardId)) | |
| }) | |
| var server = app.listen(8081, function() { | |
| var host = server.address().address | |
| var port = server.address().port | |
| console.log('Example app listening at http://%s:%s', host, port) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment