git ssb

16+

Dominic / patchbay



Tree: 460327c4ff4a7fbb7266be536dba9f071b8edca8

Files: 460327c4ff4a7fbb7266be536dba9f071b8edca8 / modules / thread.js

2718 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
24var plugs = require('../plugs')
25
26var message_render = plugs.first(exports.message_render = [])
27var message_compose = plugs.first(exports.message_compose = [])
28var message_unbox = plugs.first(exports.message_unbox = [])
29
30var sbot_get = plugs.first(exports.sbot_get = [])
31var sbot_links = plugs.first(exports.sbot_links = [])
32
33function getThread (root, sbot, cb) {
34 //in this case, it's inconvienent that panel only takes
35 //a stream. maybe it would be better to accept an array?
36
37 return pull(Cat([
38 once(function (cb) {
39 sbot_get(root, function (err, value) {
40 cb(err, {key: root, value: value})
41 })
42 }),
43 sbot_links({rel: 'root', dest: root, values: true, keys: true})
44 ]), pull.collect(cb))
45}
46
47exports.screen_view = function (id, sbot) {
48 if(ref.isMsg(id)) {
49 var meta = {
50 type: 'post',
51 root: id,
52 branch: id //mutated when thread is loaded.
53 }
54
55 var content = h('div')
56 var div = h('div.column',
57 {style: {'overflow-y': 'auto'}},
58 content,
59 h('div.editor', message_compose(meta))
60 )
61
62 pull(
63 sbot_links({
64 rel: 'root', dest: id, keys: true, old: false
65 }),
66 pull.drain(function (msg) {
67 loadThread() //redraw thread
68 }, function () {} )
69 )
70
71
72 function loadThread () {
73 getThread(id, sbot, function (err, thread) {
74 //would probably be better keep an id for each message element
75 //(i.e. message key) and then update it if necessary.
76 //also, it may have moved (say, if you received a missing message)
77 content.innerHTML = ''
78 thread = thread.map(function (msg) {
79 return 'string' === typeof msg.value.content ? message_unbox(msg) : msg
80 })
81
82 if(err) return content.appendChild(h('pre', err.stack))
83 sort(thread).map(message_render).forEach(function (el) {
84 content.appendChild(el)
85 })
86
87 var branches = sort.heads(thread)
88 meta.branch = branches.length > 1 ? branches : branches[0]
89
90 var recps = thread[0].value.content.recps
91 if(recps && thread[0].value.private)
92 meta.recps = recps
93 })
94 }
95
96 loadThread()
97 return div
98 }
99}
100
101
102

Built with git-ssb-web