var h = require('hyperscript') var ref = require('ssb-ref') var ssbKeys = require('ssb-keys') var sbot = require('./scuttlebot') var config = require('./config')() var markdown = require('ssb-markdown') var id = require('./keys').id var avatar = require('./avatar') var human = require('human-time') module.exports.timestamp = function (msg, edited) { var timestamp = h('span.right', h('a', {href: '#' + msg.key}, human(new Date(msg.value.timestamp)))) return timestamp } module.exports.header = function (msg) { var header = h('div.header') header.appendChild(h('span.avatar', h('a', {href: '#' + msg.value.author}, h('span.avatar--small', avatar.cachedImage(msg.value.author)), avatar.cachedName(msg.value.author) ) ) ) header.appendChild(exports.timestamp(msg)) return header } module.exports.mini = function (msg, content) { var mini = h('div.mini') mini.appendChild( h('span.avatar', h('a', {href: '#' + msg.value.author}, h('span.avatar--small', avatar.cachedImage(msg.value.author)), avatar.cachedName(msg.value.author) ) ) ) var lock = h('span.right', h('img.emoji', {src: config.emojiUrl + 'lock.png'})) mini.appendChild(h('span', content)) mini.appendChild(exports.timestamp(msg)) if (msg.value.content.recps) { mini.appendChild(lock) } if (typeof msg.value.content === 'string') { mini.appendChild(lock) } return mini } module.exports.box = function (content) { return ssbKeys.box(content, content.recps.map(function (e) { return ref.isFeed(e) ? e : e.link })) } module.exports.publish = function (content, cb) { if(content.recps) content = exports.box(content) sbot.publish(content, function (err, msg) { if(err) throw err console.log('Published!', msg) if(cb) cb(err, msg) }) } module.exports.messageName = function (id, cb) { // gets the first few characters of a message, for message-link function title (s) { var m = /^\n*([^\n]{0,40})/.exec(s) return m && (m[1].length == 40 ? m[1]+'...' : m[1]) } sbot.get(id, function (err, value) { if(err && err.name == 'NotFoundError') return cb(null, id.substring(0, 10)+'...(missing)') if(value.content.type === 'post' && 'string' === typeof value.content.text) return cb(null, title(value.content.text)) else if('string' === typeof value.content.text) return cb(null, value.content.type + ':'+title(value.content.text)) else return cb(null, id.substring(0, 10)+'...') }) } var messageName = exports.messageName module.exports.messageLink = function (id) { if (ref.isMsg(id)) { var link = h('a', {href: '#'+id}, id.substring(0, 10)+'...') messageName(id, function (err, name) { if(err) console.error(err) else link.textContent = name }) } else { var link = id } return link } module.exports.rawJSON = function (obj) { return JSON.stringify(obj, null, 2) .split(/([%@&][a-zA-Z0-9\/\+]{43}=*\.[\w]+)/) .map(function (e) { if(ref.isMsg(e) || ref.isFeed(e) || ref.isBlob(e)) { return h('a', {href: '#' + e}, e) } return e }) } module.exports.markdown = function (msg, md) { return {innerHTML: markdown.block(msg, {toUrl: function (url, image) { if(url[0] == '%' || url[0] == '@' || url[0] == '#') return '#' + url if(url[0] !== '&') return url //if(url[0] == '&') return config.blobsUrl + url //if(!image) return url return config.blobsUrl + url }})} }