git ssb

16+

Dominic / patchbay



Tree: 8962d847a1ccdc99a10a5d9b54d5f6c4abf9ce8d

Files: 8962d847a1ccdc99a10a5d9b54d5f6c4abf9ce8d / modules / thread.js

2920 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, 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 sbot_get(root, function (err, value) {
38 var msg = {key: root, value: value}
39// if(value.content.root) return getThread(value.content.root, cb)
40
41 pull(
42 sbot_links({rel: 'root', dest: root, values: true, keys: true}),
43 pull.collect(function (err, ary) {
44 if(err) return cb(err)
45 ary.unshift(msg)
46 cb(null, ary)
47 })
48 )
49 })
50
51}
52
53exports.screen_view = function (id, sbot) {
54 if(ref.isMsg(id)) {
55 var meta = {
56 type: 'post',
57 root: id,
58 branch: id //mutated when thread is loaded.
59 }
60
61 var content = h('div')
62 var div = h('div.column',
63 {style: {'overflow-y': 'auto'}},
64 content,
65 h('div.editor', message_compose(meta))
66 )
67
68 pull(
69 sbot_links({
70 rel: 'root', dest: id, keys: true, old: false
71 }),
72 pull.drain(function (msg) {
73 loadThread() //redraw thread
74 }, function () {} )
75 )
76
77
78 function loadThread () {
79 getThread(id, function (err, thread) {
80 //would probably be better keep an id for each message element
81 //(i.e. message key) and then update it if necessary.
82 //also, it may have moved (say, if you received a missing message)
83 content.innerHTML = ''
84 //decrypt
85 thread = thread.map(function (msg) {
86 return 'string' === typeof msg.value.content ? message_unbox(msg) : msg
87 })
88
89 if(err) return content.appendChild(h('pre', err.stack))
90 sort(thread).map(message_render).forEach(function (el) {
91 content.appendChild(el)
92 })
93
94 var branches = sort.heads(thread)
95 meta.branch = branches.length > 1 ? branches : branches[0]
96 meta.root = thread[0].value.content.root || thread[0].key
97
98 var recps = thread[0].value.content.recps
99 if(recps && thread[0].value.private)
100 meta.recps = recps
101 })
102 }
103
104 loadThread()
105 return div
106 }
107}
108
109

Built with git-ssb-web