aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEv Bogue <ev@evbogue.com>2019-11-24 09:03:30 -0600
committerEv Bogue <ev@evbogue.com>2019-11-24 09:03:30 -0600
commit30cdde804f90ca533bc3388de46251f1400087a5 (patch)
treefc60f3c4ef03bc6b4c66c680d100edc8d02b8e5a
parent06a7740326eebda9076a1b4538defbf0a351a209 (diff)
serve ads if client feeds are up to date
-rw-r--r--ads.json15
-rw-r--r--css/style.css23
-rw-r--r--gossip.js4
-rw-r--r--index.html2
-rw-r--r--package.json2
-rw-r--r--render.js26
-rw-r--r--server.js25
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; }
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 @@
<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": {
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) {