var http = require('http')
var fs = require('fs')
var h = require('hyperscript')
var pull = require('pull-stream')
var client = require('ssb-client')
var md = require('ssb-markdown')
var avatar = require('ssb-avatar')
var mo = require('moment')
var ref = require('ssb-ref')
var title = 'sdash'
var me = '@8Qee0I/DwI5DHSCi3p5fsl6FyLGArrnDz3ox9qZr5Qc=.ed25519'
var desc = 'This is sdash, a work-in-progress personal gateway to the scuttlebot distributed social network'
var bloburl = 'http://localhost:8989/blobs/get/'
http.createServer(function (req, res){
if (req.url === '/') {
client(function (err, sbot) {
avatar(sbot, me, me, function (err, avatar){
if (err) throw err
name = avatar.name;
image = avatar.image;
})
pull(
sbot.query.read({query: [{$filter: { value: { author: me, content: {type: 'post'}}}}], limit: 1, reverse: true}),
pull.drain(function (data) {
post = data
})
)
pull(
sbot.query.read({query: [{$filter: { value: { author: me, content: {type: 'pin'}}}}], limit: 1, reverse: true}),
pull.drain(function (data) {
getpin = data;
pull(
sbot.query.read({query: [{$filter: { key: getpin.value.content.link}}]}),
pull.drain(function (data) {
pinned = data;
})
)
})
)
pull(
sbot.query.read({query: [{$filter: { value: { author: me, content: {type: 'vote'}}}}], limit: 1, reverse: true}),
pull.drain(function (data) {
vote = data;
pull(
sbot.query.read({query: [{$filter: { key: data.value.content.vote.link}}]}),
pull.drain(function (data) {
avatar(sbot, me, data.value.author, function (err, avatar){
if (err) {
throw err;
}
reauthor = avatar;
gotPost()
})
redo = data;
})
)
})
)
pull(
sbot.query.read({query: [{$filter: { value: { content: {mentions: [{link: me}]}}}}], limit: 1, reverse: true}),
pull.drain(function (data) {
avatar(sbot, me, data.value.author, function (err, avatar){
if (err) throw err
mentionauthor = avatar
})
mention = data
})
)
pull(
sbot.query.read({query: [{$filter: { value: { author: me, content: {type: 'git-update'}}}}], limit: 1, reverse: true}),
pull.drain(function (data) {
gits = data
})
)
})
function gotPost() {
res.end(
h('html',
h('head',
h('title', title),
h('style', fs.readFileSync('style.css', 'utf8'))
),
h('body',
h('img.profile', {src: bloburl + image}),
h('h1',
h('a', {href: me }, '@' + name)
),
h('p', {innerHTML: desc}),
h('hr'),
h('div.msg',
h('p.small.ri', 'pinned'),
h('img.avatar', {src: bloburl + image}),
h('div', {innerHTML: md.block(pinned.value.content.text)},
h('span.date', mo(pinned.value.timestamp).fromNow())
)
),
h('div.msg',
h('p.small.ri', 'post'),
h('img.avatar', {src: bloburl + image}),
h('div', {innerHTML: md.block(post.value.content.text)},
h('span.date', mo(post.value.timestamp).fromNow())
)
),
h('div.msg',
h('p.small.ri', 'dig'),
h('img.avatar', {src: bloburl + image}),
h('div', vote.value.content.vote.link,
h('p.date', mo(vote.value.timestamp).fromNow()),
h('div.msg',
h('img.avatar', {src: bloburl + reauthor.image}),
h('div', {innerHTML: md.block(redo.value.content.text)},
h('span.date', mo(redo.value.timestamp).fromNow())
)
)
)
),
h('div.msg',
h('p.small.ri', 'last mention'),
h('img.avatar', {src: bloburl + mentionauthor.image}),
h('div', {innerHTML: md.block(mention.value.content.text)},
h('span.date', mo(mention.value.timestamp).fromNow())
)
)
)
).outerHTML)
}
}
}).listen(1337)