var pull = require('pull-stream') var sort = require('ssb-sort') var ref = require('ssb-ref') var h = require('hyperscript') var u = require('../util') var Scroller = require('pull-scroll') var self_id = require('../keys').id var messageName = require('./helpers').message_name var get = require('./scuttlebot').get var links = require('./scuttlebot').links var Unbox = require('./helpers-private').unbox function once (cont) { var ended = false return function (abort, cb) { if(abort) return cb(abort) else if (ended) return cb(ended) else cont(function (err, data) { if(err) return cb(ended = err) ended = true cb(null, data) }) } } exports.needs = { message_render: 'first', message_compose: 'first' } exports.gives = 'screen_view' exports.create = function (api) { function getThread (root, cb) { //in this case, it's inconvienent that panel only takes //a stream. maybe it would be better to accept an array? get(root, function (err, value) { if (err) return cb(err) var msg = {key: root, value: value} pull( links({rel: 'root', dest: root, values: true, keys: true}), pull.collect(function (err, ary) { if(err) return cb(err) ary.unshift(msg) cb(null, ary) }) ) }) } return function (id) { if(ref.isMsg(id)) { var meta = { type: 'post', root: id, branch: id //mutated when thread is loaded. } var content = h('div.column.scroller__content') var div = h('div.column.scroller', {style: {'overflow-y': 'auto'}}, h('div.scroller__wrapper', content, api.message_compose(meta, {placeholder: 'Write a reply'}) ) ) messageName(id, function (err, name) { div.title = name }) pull( links({ rel: 'root', dest: id, keys: true, old: false }), pull.drain(function (msg) { loadThread() //redraw thread }, function () {} ) ) function loadThread () { getThread(id, function (err, thread) { //would probably be better keep an id for each message element //(i.e. message key) and then update it if necessary. //also, it may have moved (say, if you received a missing message) content.innerHTML = '' if(err) return content.appendChild(h('pre', err.stack)) //decrypt thread = thread.map(function (msg) { return 'string' === typeof msg.value.content ? Unbox(msg) : msg }) if(err) return content.appendChild(h('pre', err.stack)) sort(thread).map(api.message_render).filter(Boolean).forEach(function (el) { content.appendChild(el) }) var branches = sort.heads(thread) meta.branch = branches.length > 1 ? branches : branches[0] meta.root = thread[0].value.content.root || thread[0].key meta.channel = thread[0].value.content.channel var recps = thread[0].value.content.recps var private = thread[0].value.private if(private) { if(recps) meta.recps = recps else meta.recps = [thread[0].value.author, self_id] } }) } loadThread() return div } } }