var h = require('hyperscript') var human = require('human-time') var avatar = require('./avatar') var ref = require('ssb-ref') var ssbKeys = require('ssb-keys') var pull = require('pull-stream') var sbot = require('./scuttlebot') var config = require('./config')() var id = require('./keys').id module.exports.getBlocks = function (src) { var blocks = h('div.blocks', 'Blocking: ') pull( sbot.query({query: [{$filter: { value: { author: src, content: {type: 'contact'}}}}], live: true}), pull.drain(function (msg) { if (msg.value) { if (msg.value.content.blocking == true) { console.log(msg.value) var gotIt = document.getElementById('blocks:' + msg.value.content.contact.substring(0, 44)) if (gotIt == null) { blocks.appendChild(h('a#blocks:'+ msg.value.content.contact.substring(0, 44), {title: avatar.cachedName(msg.value.content.contact).textContent, href: '#' + msg.value.content.contact}, h('span.avatar--small', avatar.cachedImage(msg.value.content.contact)))) } } if (msg.value.content.blocking == false) { var gotIt = document.getElementById('blocks:' + msg.value.content.contact.substring(0, 44)) if (gotIt != null) { gotIt.outerHTML = '' } } } }) ) return blocks } module.exports.getBlocked = function (src) { var blocked = h('div.blocked', 'Blocked by: ') pull( sbot.query({query: [{$filter: { value: { content: {type: 'contact', contact: src}}}}], live: true}), pull.drain(function (msg) { if (msg.value) { if (msg.value.content.blocking == true) { console.log(msg.value) var gotIt = document.getElementById('blocked:' + msg.value.content.contact.substring(0, 44)) if (gotIt == null) { blocked.appendChild(h('a#blocked:'+ msg.value.author.substring(0, 44), {title: avatar.cachedName(msg.value.author).textContent, href: '#' + msg.value.author}, h('span.avatar--small', avatar.cachedImage(msg.value.author)))) } } if (msg.value.content.blocking == false) { var gotIt = document.getElementById('blocked:' + msg.value.author.substring(0, 44)) if (gotIt != null) { gotIt.outerHTML = '' } } } }) ) return blocked } module.exports.getFollowing = function (src) { var followingCount = 0 var following = h('div.following', 'Following: ') following.appendChild(h('span#followingcount', '0')) following.appendChild(h('br')) pull( sbot.query({query: [{$filter: { value: { author: src, content: {type: 'contact'}}}}], live: true}), pull.drain(function (msg) { if (msg.value) { if (msg.value.content.following == true) { followingcount = document.getElementById('followingcount') followingCount++ followingcount.textContent = followingCount var gotIt = document.getElementById('following:' + msg.value.content.contact.substring(0, 44)) if (gotIt == null) { following.appendChild(h('a#following:'+ msg.value.content.contact.substring(0, 44), {title: avatar.cachedName(msg.value.content.contact).textContent, href: '#' + msg.value.content.contact}, h('span.avatar--small', avatar.cachedImage(msg.value.content.contact)))) } } if (msg.value.content.following == false) { followingcount = document.getElementById('followingcount') followingCount-- followingcount.textContent = followingCount var gotIt = document.getElementById('following:' + msg.value.content.contact.substring(0, 44)) if (gotIt != null) { gotIt.outerHTML = '' } } } }) ) return following } module.exports.getFollowers = function (src) { var followerCount = 0 var followers = h('div.followers', 'Followers: ') followers.appendChild(h('span#followercount', '0')) followers.appendChild(h('br')) pull( sbot.query({query: [{$filter: { value: { content: {type: 'contact', contact: src}}}}], live: true}), pull.drain(function (msg) { if (msg.value) { if (msg.value.content.following == true) { followcount = document.getElementById('followercount') followerCount++ followcount.textContent = followerCount var gotIt = document.getElementById('followers:' + msg.value.author.substring(0, 44)) if (gotIt == null) { followers.appendChild(h('a#followers:'+ msg.value.author.substring(0, 44), {title: avatar.cachedName(msg.value.author).textContent, href: '#' + msg.value.author}, h('span.avatar--small', avatar.cachedImage(msg.value.author)))) } } if (msg.value.content.following == false) { followcount = document.getElementById('followercount') followerCount-- followcount.textContent = followerCount var gotIt = document.getElementById('followers:' + msg.value.author.substring(0, 44)) if (gotIt != null) { gotIt.outerHTML = '' } } } }) ) return followers } module.exports.queueButton = function (src) { var queueButton = h('span.queue:' + src.key.substring(0,44)) var addToQueue = h('button.btn.right', 'Queue', { onclick: function () { var content = { type: 'queue', message: src.key, queue: true } sbot.publish(content, function (err, publish) { if (err) throw err console.log(publish) }) } }) var removeFromQueue = h('button.btn.right#', 'Done', { onclick: function () { var content = { type: 'queue', message: src.key, queue: false } sbot.publish(content, function (err, publish) { if (err) throw err console.log(publish) if (window.location.hash.substring(1) == 'queue') { setTimeout(function () { var gotIt = document.getElementById(src.key.substring(0,44)) if (gotIt != null) { gotIt.outerHTML = '' } }, 100) } }) } }) pull( sbot.query({query: [{$filter: { value: { author: id, content: {type: 'queue', message: src.key}}}}], live: true}), pull.drain(function (msg) { if (msg.value) { if (msg.value.content.queue == true) { queueButton.replaceChild(removeFromQueue, addToQueue) } if (msg.value.content.queue == false) { queueButton.replaceChild(addToQueue, removeFromQueue) } } }) ) queueButton.appendChild(addToQueue) return queueButton } module.exports.follow = function (src) { var button = h('span.button') var followButton = h('button.btn', 'Follow ', avatar.name(src), { onclick: function () { var content = { type: 'contact', contact: src, following: true } sbot.publish(content, function (err, publish) { if (err) throw err console.log(publish) }) } }) var unfollowButton = h('button.btn', 'Unfollow ', avatar.name(src), { onclick: function () { var content = { type: 'contact', contact: src, following: false } sbot.publish(content, function (err, publish) { if (err) throw err console.log(publish) }) } }) pull( sbot.query({query: [{$filter: { value: { author: id, content: {type: 'contact', contact: src}}}}], live: true}), pull.drain(function (msg) { if (msg.value) { if (msg.value.content.following == true) { button.removeChild(button.firstChild) button.appendChild(unfollowButton) } if (msg.value.content.following == false) { button.removeChild(button.firstChild) button.appendChild(followButton) } } }) ) button.appendChild(followButton) return button } 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.timestamp = function (msg, edited) { return h('span.timestamp', h('a', {href: '#' + msg.key}, human(new Date(msg.value.timestamp)))) } 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(exports.timestamp(msg)) mini.appendChild(h('span', content)) if (msg.value.content.recps) { mini.appendChild(lock) } if (typeof msg.value.content === 'string') { mini.appendChild(lock) } return mini } 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 }) }