var pull = require('pull-stream') var human = require('human-time') var sbot = require('./scuttlebot') var hyperscroll = require('hyperscroll') var More = require('pull-more') var stream = require('hyperloadmore/stream') var h = require('hyperscript') var render = require('./render') var ref = require('ssb-ref') var Next = require('pull-next-query') var config = require('./config')() var tools = require('./tools') var avatar = require('./avatar') var id = require('./keys').id var ssbKeys = require('ssb-keys') var keys = require('./keys') var userStream = function (src) { var content = h('div.content') var screen = document.getElementById('screen') screen.appendChild(hyperscroll(content)) function createStream (opts) { return pull( Next(sbot.query, opts, ['value', 'timestamp']), pull.map(function (msg) { if (msg.value) { return render(msg) } }) ) } pull( createStream({ limit: 10, reverse: true, live: false, query: [{$filter: { value: {author: src, content: {type: 'scat_message'}, timestamp: { $gt: 0 }}}}] }), stream.bottom(content) ) pull( createStream({ limit: 10, old: false, live: true, query: [{$filter: { value: {author: src, content: {type:'scat_message'}, timestamp: { $gt: 0 }}}}] }), stream.top(content) ) var profile = h('div.content#profile', h('div.message')) if (screen.firstChild.firstChild) { screen.firstChild.insertBefore(profile, screen.firstChild.firstChild) } else { screen.firstChild.appendChild(profile) } var avatars = h('div.avatars', h('a', {href: '#' + src}, h('span.avatar--medium#profileImage', avatar.image(src)), avatar.name(src) ), ) var buttons = h('div.buttons') profile.firstChild.appendChild(avatars) profile.firstChild.appendChild(buttons) if (src != id) { buttons.appendChild(tools.follow(src)) } profile.firstChild.appendChild(h('hr')) } var msgThread = function (src) { var content = h('div.content') var screen = document.getElementById('screen') screen.appendChild(hyperscroll(content)) pull( sbot.query({query: [{$filter: { value: { content: {root: src}, timestamp: { $gt: 1 }}}}], live: true}), pull.drain(function (msg) { if (msg.value) { content.appendChild(render(msg)) } }) ) sbot.get(src, function (err, data) { if (err) { var message = h('div.message', 'Missing message!') content.appendChild(message) } if (data) { data.value = data data.key = src console.log(data) var rootMsg = render(data) if (content.firstChild) { content.insertBefore(rootMsg, content.firstChild) } else { content.appendChild(rootMsg) } } }) } var keyPage = function () { var screen = document.getElementById('screen') var importKey = h('textarea.import', {placeholder: 'Import a new public/private key', name: 'textarea', style: 'width: 97%; height: 100px;'}) var content = h('div.content', h('div.message#key', h('h1', 'Your Key'), h('p', {innerHTML: 'Your public/private key is:
' + localStorage[config.caps.shs + '/secret'] + '
'}, h('button.btn', {onclick: function (e){ localStorage[config.caps.shs +'/secret'] = '' alert('Your public/private key has been deleted') e.preventDefault() location.hash = "" location.reload() }}, 'Delete Key') ), h('hr'), h('form', importKey, h('button.btn', {onclick: function (e){ if(importKey.value) { localStorage[config.caps.shs + '/secret'] = importKey.value.replace(/\s+/g, ' ') e.preventDefault() alert('Your public/private key has been updated') } location.hash = "" location.reload() }}, 'Import key'), ) ) ) screen.appendChild(hyperscroll(content)) } function scatterStream () { var screen = document.getElementById('screen') var content = h('div.content') screen.appendChild(hyperscroll(content)) var chatbox = h('input.compose', {placeholder: '>'}) var chat = h('div.content') var publish = h('button.btn.right', 'Send', { onclick: function () { if (chatbox.value) { var content = { text: chatbox.value, type: 'scat_message' } sbot.publish(content, function (err, msg) { if (err) throw err chatbox.value = '' console.log('Published!', msg) }) } } }) chat.appendChild(h('div.message', chatbox, publish)) if (screen.firstChild.firstChild) { screen.firstChild.insertBefore(chat, screen.firstChild.firstChild) } else { screen.firstChild.appendChild(chat) } function createStream (opts) { return pull( Next(sbot.query, opts, ['value', 'timestamp']), pull.map(function (msg) { if (msg.value) { return render(msg) } }) ) } pull( createStream({ limit: 10, reverse: true, live: false, query: [{$filter: { value: { content: {type: 'scat_message'}, timestamp: { $gt: 0 }}}}] }), stream.bottom(content) ) pull( createStream({ limit: 10, old: false, live: true, query: [{$filter: { value: { content: {type: 'scat_message'}, timestamp: { $gt: 0 }}}}] }), stream.top(content) ) } function hash () { return window.location.hash.substring(1) } module.exports = function () { var src = hash() if (ref.isFeed(src)) { userStream(src) } else if (ref.isMsg(src)) { msgThread(src) } else if (src == 'key') { keyPage() } else { scatterStream() } }