diff options
| author | Ev Bogue <ev@evbogue.com> | 2019-11-24 09:03:30 -0600 | 
|---|---|---|
| committer | Ev Bogue <ev@evbogue.com> | 2019-11-24 09:03:30 -0600 | 
| commit | 30cdde804f90ca533bc3388de46251f1400087a5 (patch) | |
| tree | fc60f3c4ef03bc6b4c66c680d100edc8d02b8e5a | |
| parent | 06a7740326eebda9076a1b4538defbf0a351a209 (diff) | |
serve ads if client feeds are up to date
| -rw-r--r-- | ads.json | 15 | ||||
| -rw-r--r-- | css/style.css | 23 | ||||
| -rw-r--r-- | gossip.js | 4 | ||||
| -rw-r--r-- | index.html | 2 | ||||
| -rw-r--r-- | package.json | 2 | ||||
| -rw-r--r-- | render.js | 26 | ||||
| -rw-r--r-- | server.js | 25 | 
7 files changed, 87 insertions, 10 deletions
| 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; } @@ -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 => { @@ -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> 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": { @@ -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 () { @@ -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) { | 
