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') var pull = require('pull-stream') 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.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.mute = function (src) { if (!localStorage[src]) var cache = {mute: false} else var cache = JSON.parse(localStorage[src]) if (cache.mute == true) { var mute = h('button.btn', 'Unmute', { onclick: function () { cache.mute = false localStorage[src] = JSON.stringify(cache) location.hash = '#' location.hash = src } }) return mute } else { var mute = h('button.btn', 'Mute', { onclick: function () { cache.mute = true localStorage[src] = JSON.stringify(cache) location.hash = '#' location.hash = src } }) return mute } } 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.star = function (msg) { var votebutton = h('span.right#votebutton:' + msg.key.substring(0, 44)) var vote = { type: 'vote', vote: {link: msg.key, expression: 'Star'} } var star = h('button.btn', 'Star ', h('img.emoji', {src: config.emojiUrl + 'star.png'}), { onclick: function () { vote.vote.value = 1 if (vote.recps) { vote = exports.box(vote) } sbot.publish(vote, function (err, voted) { if(err) throw err }) } } ) votebutton.appendChild(star) return votebutton } module.exports.unstar = function (msg) { var votebutton = h('span.right#votebutton:' + msg.key.substring(0, 44)) var vote = { type: 'vote', vote: {link: msg.key, expression: 'Star'} } var unstar = h('button.btn ', 'Unstar ', h('img.emoji', {src: config.emojiUrl + 'stars.png'}), { onclick: function () { vote.vote.value = -1 sbot.publish(vote, function (err, voted) { if(err) throw err }) } } ) votebutton.appendChild(unstar) return votebutton } 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)) var lock = h('span.right', h('img.emoji', {src: config.emojiUrl + 'lock.png'})) if (msg.value.content.recps) { header.appendChild(lock) for (var i = 0; i < msg.value.content.recps.length; i++) { //console.log(msg.value.content.recps[i]) if (ref.isFeed(msg.value.content.recps[i].link)) { header.appendChild(h('a', {href: '#' + msg.value.content.recps[i].link}, h('span.avatar--small.right', avatar.cachedImage(msg.value.content.recps[i].link)))) } if (ref.isFeed(msg.value.content.recps[i])) { header.appendChild(h('a', {href: '#' + msg.value.content.recps[i]}, h('span.avatar--small.right', avatar.cachedImage(msg.value.content.recps[i])))) } } } else if (msg.value.content.mentions) { for (var i = 0; i < msg.value.content.mentions.length; i++) { //console.log(msg.value.content.mentions[i]) if (ref.isFeed(msg.value.content.mentions[i].link)) { header.appendChild(h('a', {href: '#' + msg.value.content.mentions[i].link}, h('span.avatar--small.right', avatar.cachedImage(msg.value.content.mentions[i].link)))) } if (ref.isFeed(msg.value.content.mentions[i])) { header.appendChild(h('a', {href: '#' + msg.value.content.mentions[i]}, h('span.avatar--small.right', avatar.cachedImage(msg.value.content.mentions[i])))) } } } 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) ) ) ) mini.appendChild(h('span', content)) mini.appendChild(exports.timestamp(msg)) var lock = h('span.right', h('img.emoji', {src: config.emojiUrl + 'lock.png'})) 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(ref.isBlob(url)) return config.blobsUrl + url //if(url[0] == '&') return config.blobsUrl + url //if(!image) return url return url }})} }