Browse Source

serve ads if client feeds are up to date

master
Ev Bogue 3 years ago
parent
commit
30cdde804f
  1. 15
      ads.json
  2. 23
      css/style.css
  3. 4
      gossip.js
  4. 2
      index.html
  5. 2
      package.json
  6. 26
      render.js
  7. 25
      server.js

15
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."
]

23
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; }

4
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 => {

2
index.html

@ -17,8 +17,8 @@
<script src="bog.js"></script>
<script src="composer.js"></script>
<script src="identify.js"></script>
<script src="gossip.js"></script>
<script src="render.js"></script>
<script src="gossip.js"></script>
<script src="views.js"></script>
<script src="app.js"></script>
</body>

2
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": {

26
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 () {

25
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) {

Loading…
Cancel
Save