git ssb

16+

Dominic / patchbay



Tree: ceab09c9e34bbabf034b57309315733a28ec5e7a

Files: ceab09c9e34bbabf034b57309315733a28ec5e7a / modules / thread.js

2097 bytesRaw
1var ui = require('../ui')
2var pull = require('pull-stream')
3var Cat = require('pull-cat')
4var sort = require('ssb-sort')
5var ref = require('ssb-ref')
6var h = require('hyperscript')
7var u = require('../util')
8var Scroller = require('pull-scroll')
9
10function once (cont) {
11 var ended = false
12 return function (abort, cb) {
13 if(abort) return cb(abort)
14 else if (ended) return cb(ended)
15 else
16 cont(function (err, data) {
17 if(err) return cb(ended = err)
18 ended = true
19 cb(null, data)
20 })
21 }
22}
23
24function getThread (root, sbot, cb) {
25 //in this case, it's inconvienent that panel only takes
26 //a stream. maybe it would be better to accept an array?
27
28 return pull(Cat([
29 once(function (cb) {
30 sbot.get(root, function (err, value) {
31 cb(err, {key: root, value: value})
32 })
33 }),
34 sbot.links({rel: 'root', dest: root, values: true, keys: true})
35 ]), pull.collect(cb))
36}
37
38function unbox(msg) {
39 return u.firstPlug(exports.message_unbox, msg)
40}
41
42exports.screen_view = function (id, sbot) {
43 if(ref.isMsg(id)) {
44 var div = h('div.column', {style: {'overflow-y': 'auto'}})
45 var render = ui.createRenderers(exports.message_render, sbot)
46
47 getThread(id, sbot, function (err, thread) {
48 thread = thread.map(function (msg) {
49 return 'string' === typeof msg.value.content ? unbox(msg) : msg
50 })
51
52 if(err) return div.appendChild(h('pre', err.stack))
53 sort(thread).map(render).forEach(function (el) {
54 div.appendChild(el)
55 })
56
57 var branches = sort.heads(thread)
58 var meta = {
59 type: 'post',
60 root: id,
61 branch: branches.length > 1 ? branches : branches[0]
62 }
63 var recps = thread[0].value.content.recps
64 if(recps && thread[0].value.private)
65 meta.recps = recps
66
67 div.appendChild(
68 h('div',
69 u.decorate(exports.message_compose, meta, function (d, e, v) {
70 return d(e, v, sbot)
71 }))
72 )
73 })
74
75 return div
76 }
77
78}
79
80exports.message_render = []
81exports.message_compose = []
82exports.message_unbox = []
83
84
85
86
87
88

Built with git-ssb-web