aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEv Bogue <ev@evbogue.com>2019-11-26 11:43:26 -0600
committerEv Bogue <ev@evbogue.com>2019-11-26 11:43:26 -0600
commitb0d71e6dcbef913c05070a9b7d0a7efedd0024ac (patch)
tree5ea1317f7548eba7da84a874fa9550904a1edd36
parent4c1b955f44aa4882967cacf8151f08b40ef3840a (diff)
add an advertisement generator and send views to the client
-rw-r--r--ads.js32
-rw-r--r--render.js2
-rw-r--r--server.js39
3 files changed, 59 insertions, 14 deletions
diff --git a/ads.js b/ads.js
new file mode 100644
index 0000000..efa1bc8
--- /dev/null
+++ b/ads.js
@@ -0,0 +1,32 @@
+var fs = require('fs')
+var nacl = require('tweetnacl')
+ nacl.util = require('tweetnacl-util')
+
+var homedir = require('os').homedir()
+var addir = homedir + '/.bogbook/ads/'
+
+if (!fs.existsSync(homedir + '/.bogbook/')) {fs.mkdirSync(homedir + '/.bogbook/')}
+if (!fs.existsSync(addir)){fs.mkdirSync(addir)}
+
+function makeAd (ad) {
+ console.log(ad)
+
+ var hex = Buffer.from(nacl.hash(nacl.util.decodeUTF8(ad))).toString('hex')
+
+ var obj = {
+ hash: hex,
+ ad: ad,
+ views: 0
+ }
+
+ fs.writeFile(addir + hex, JSON.stringify(obj), 'UTF-8', function () {
+ console.log('Saved as ' + hex)
+ })
+}
+
+if (process.argv[2]) {
+ var ad = process.argv[2]
+ makeAd(ad)
+} else {
+ console.log('Please write an ad. Ex: `node ads.js "Hello World"')
+}
diff --git a/render.js b/render.js
index ee91c24..90bb399 100644
--- a/render.js
+++ b/render.js
@@ -7,7 +7,7 @@ function renderAd (ad) {
}
newAd = h('div', {id: 'ad'}, [
- h('span', {classList: 'right'}, [h('pre', ['Ad'])]),
+ h('span', {classList: 'right'}, [h('pre', [ad.views + ' views'])]),
h('p', {innerHTML: marked(ad.content)}),
h('button', {classList: 'right',
onclick: function () {
diff --git a/server.js b/server.js
index 07697f3..6d22135 100644
--- a/server.js
+++ b/server.js
@@ -91,6 +91,7 @@ var nacl = require('tweetnacl')
var homedir = require('os').homedir()
var bogdir = homedir + '/.bogbook/bogs/'
+var addir = homedir + '/.bogbook/ads/'
if (!fs.existsSync(homedir + '/.bogbook/')) {fs.mkdirSync(homedir + '/.bogbook/')}
if (!fs.existsSync(bogdir)){fs.mkdirSync(bogdir)}
@@ -118,19 +119,31 @@ bog.keys().then(key => {
printFeedIdentical(msg, req)
if (ADVERTISEMENTS) {
if (Math.floor(Math.random() * 4) == 2) {
- var ad = {
- author: key.publicKey,
- name: fullURL,
- content: adContents[Math.floor(Math.random() * adContents.length)],
- timestamp: Date.now()
- }
- printSendAd(ad, req)
- bog.box(JSON.stringify(ad), req.requester, key).then(boxed => {
- obj = {
- requester: key.publicKey,
- box: boxed
- }
- ws.send(JSON.stringify(obj))
+ fs.readdir(addir, function (err, adfiles) {
+ var num = Math.floor(Math.random() * (adfiles.length))
+ console.log(num)
+ fs.readFile(addir + adfiles[num], 'UTF-8', function (err, adFile) {
+ console.log(adFile)
+ var obj = JSON.parse(adFile)
+ console.log(obj)
+ var ad = {
+ author: key.publicKey,
+ name: fullURL,
+ content: obj.ad,
+ timestamp: Date.now(),
+ views: obj.views
+ }
+ obj.views++
+ fs.writeFileSync(addir + obj.hash, JSON.stringify(obj), 'UTF-8')
+ printSendAd(ad, req)
+ bog.box(JSON.stringify(ad), req.requester, key).then(boxed => {
+ sendobj = {
+ requester: key.publicKey,
+ box: boxed
+ }
+ ws.send(JSON.stringify(sendobj))
+ })
+ })
})
}
}