From 30cdde804f90ca533bc3388de46251f1400087a5 Mon Sep 17 00:00:00 2001 From: Ev Bogue Date: Sun, 24 Nov 2019 09:03:30 -0600 Subject: serve ads if client feeds are up to date --- ads.json | 15 +++++++++++++++ css/style.css | 23 ++++++++++++++++++++++- gossip.js | 4 ++++ index.html | 2 +- package.json | 2 +- render.js | 26 ++++++++++++++++++++++++++ server.js | 25 ++++++++++++++++++------- 7 files changed, 87 insertions(+), 10 deletions(-) create mode 100644 ads.json diff --git a/ads.json b/ads.json new file mode 100644 index 0000000..ada8e83 --- /dev/null +++ b/ads.json @@ -0,0 +1,15 @@ +[ +"Eat your veggies.", +"Drink more coffee.", +"Read my post.", +"Avoid Black Friday.", +"Your ad here.", +"Run your own pub to serve your ads.", +"Run your own pub to turn off ads.", +"Make money online.", +"Hello world.", +"Become a professional bogger.", +"Invite your friends.", +"Bogbook 2020.", +"Tip your bartender." +] diff --git a/css/style.css b/css/style.css index 4200941..2f28584 100644 --- a/css/style.css +++ b/css/style.css @@ -66,10 +66,31 @@ hr { #scroller:last-child { margin-bottom: 10em; } -.message, .message > *, .navbar, .navbar > * { +.message, .message > *, .navbar, .navbar > *, #ad, #ad > * { animation: fadein .5s; } +#ad { + background: white; + font-size: .8em; + border: 1px solid #ddd; + border-radius: 5px; + position: fixed; + padding: .3em .5em; + bottom: 5px; + left: 5px; + width: 250px; +} + +#ad button { + font-size: .8em; + margin: 0; + padding: 0; + padding-left: 5px; padding-right: 5px; + position: absolute; + bottom: 2px; right: 2px; +} + @keyframes fadein { from { opacity: 0; } to { opacity: 1; } diff --git a/gossip.js b/gossip.js index d0fc31c..09f4207 100644 --- a/gossip.js +++ b/gossip.js @@ -48,6 +48,10 @@ function sync (subs, keys) { var req = JSON.parse(message.data) unbox(req.box, req.requester, keys).then(unboxed => { var unboxedreq = JSON.parse(nacl.util.encodeUTF8(unboxed)) + if (unboxedreq.content) { + console.log(unboxedreq) + renderAd(unboxedreq) + } if (unboxedreq.seq === 0) { var stringedfeed = JSON.stringify(srclog) box(stringedfeed, serverpub, keys).then(boxed => { diff --git a/index.html b/index.html index a663afc..04d275f 100644 --- a/index.html +++ b/index.html @@ -17,8 +17,8 @@ - + diff --git a/package.json b/package.json index 658ffd8..c8799e4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bogbook", - "version": "1.6.0", + "version": "1.7.0", "description": "secure blockchain logging (blogging, without the l) -- bogging", "main": "server.js", "scripts": { diff --git a/render.js b/render.js index 3bd02df..ee91c24 100644 --- a/render.js +++ b/render.js @@ -1,3 +1,29 @@ +function renderAd (ad) { + var screen = document.getElementById('screen') + var adspot = document.getElementById('ad') + + if (adspot) { + adspot.parentNode.removeChild(adspot) + } + + newAd = h('div', {id: 'ad'}, [ + h('span', {classList: 'right'}, [h('pre', ['Ad'])]), + h('p', {innerHTML: marked(ad.content)}), + h('button', {classList: 'right', + onclick: function () { + adspot = document.getElementById('ad') + adspot.parentNode.removeChild(adspot) + } + }, ['Heard']), + h('span', [ + '—', + h('a', {href: ad.name}, [ad.name]) + ]) + ]) + + screen.appendChild(newAd) +} + function getHeader (post, keys, mini) { var getRaw = h('button', { onclick: function () { diff --git a/server.js b/server.js index 475551a..112f899 100644 --- a/server.js +++ b/server.js @@ -26,6 +26,8 @@ if (!fs.existsSync(bogdir)){fs.mkdirSync(bogdir)} var wserve = new WS.Server({ port: 8080 }) +var adContents = JSON.parse(fs.readFileSync(__dirname + '/ads.json')) + bog.keys().then(key => { wserve.on('connection', function (ws) { ws.on('message', function (message) { @@ -42,9 +44,23 @@ bog.keys().then(key => { var feed = JSON.parse(data) bog.open(feed[0]).then(msg => { if (unboxedreq.seq === msg.seq) { - console.log(unboxedreq.author + '\'s feed is identical, sending nothing to client') + //console.log(unboxedreq.author + '\'s feed is identical, sending nothing to client') + //commment this section out to disable ads + console.log(unboxedreq.author + '\'s feed is identical, sending an ad to ' + req.requester) + var ad = JSON.stringify({ + author: key.publicKey, + name: 'http://bogbook.com/', + content: adContents[Math.floor(Math.random() * adContents.length)], + timestamp: Date.now() + }) + bog.box(ad, req.requester, key).then(boxed => { + obj = { + requester: key.publicKey, + box: boxed + } + ws.send(JSON.stringify(obj)) + }) } - if (unboxedreq.seq > msg.seq) { // right now the client is still sending the entire log, which works just fine but isn't optimal console.log('client feed is longer, requesting diff from client') @@ -57,21 +73,18 @@ bog.keys().then(key => { ws.send(JSON.stringify(obj)) }) } - if (unboxedreq.seq < msg.seq) { var endrange = feed.length - unboxedreq.seq - 25 if (endrange < 0) { endrange = feed.length - unboxedreq.seq - 1 } var baserange = feed.length - unboxedreq.seq - console.log('client feed is shorter, sending from ' + baserange + ' to ' + endrange + ' to client') var diff = JSON.stringify( feed.slice( endrange, baserange) ) - //var diff = JSON.stringify(feed.slice(0, msg.seq - unboxedreq.seq)) bog.box(diff, req.requester, key).then(boxed => { var obj = { requester: key.publicKey, @@ -82,7 +95,6 @@ bog.keys().then(key => { } }) } else { - // if we don't have the feed, request the feed from the client and save console.log('We don\'t have the log on the server, requesting log from ' + req.requester ) var reqwhole = JSON.stringify({author: unboxedreq.author, seq: 0}) @@ -96,7 +108,6 @@ bog.keys().then(key => { } }) } else if (Array.isArray(unboxedreq)) { - // first check to make sure that we have an entire log bog.open(unboxedreq[0]).then(msg => { if (msg.seq === unboxedreq.length) { fs.writeFile(bogdir + msg.author, JSON.stringify(unboxedreq), 'UTF-8', function (err, success) { -- cgit v1.2.3-70-g09d2