Files: 34d99e9e2271da97998a436bbf03e9a3a5b80d16 / modules / thread.js
3130 bytesRaw
1 | var ui = require('../ui') |
2 | var pull = require('pull-stream') |
3 | var Cat = require('pull-cat') |
4 | var sort = require('ssb-sort') |
5 | var ref = require('ssb-ref') |
6 | var h = require('hyperscript') |
7 | var u = require('../util') |
8 | var Scroller = require('pull-scroll') |
9 | |
10 | function 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 | |
24 | var plugs = require('../plugs') |
25 | |
26 | var message_render = plugs.first(exports.message_render = []) |
27 | var message_compose = plugs.first(exports.message_compose = []) |
28 | var message_unbox = plugs.first(exports.message_unbox = []) |
29 | |
30 | var sbot_get = plugs.first(exports.sbot_get = []) |
31 | var sbot_links = plugs.first(exports.sbot_links = []) |
32 | |
33 | function 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 | // return pull(Cat([ |
52 | // once(function (cb) { |
53 | // sbot_get(root, function (err, value) { |
54 | // cb(err, {key: root, value: value}) |
55 | // }) |
56 | // }), |
57 | // sbot_links({rel: 'root', dest: root, values: true, keys: true}) |
58 | // ]), pull.collect(cb)) |
59 | } |
60 | |
61 | exports.screen_view = function (id, sbot) { |
62 | if(ref.isMsg(id)) { |
63 | var meta = { |
64 | type: 'post', |
65 | root: id, |
66 | branch: id //mutated when thread is loaded. |
67 | } |
68 | |
69 | var content = h('div') |
70 | var div = h('div.column', |
71 | {style: {'overflow-y': 'auto'}}, |
72 | content, |
73 | h('div.editor', message_compose(meta)) |
74 | ) |
75 | |
76 | pull( |
77 | sbot_links({ |
78 | rel: 'root', dest: id, keys: true, old: false |
79 | }), |
80 | pull.drain(function (msg) { |
81 | loadThread() //redraw thread |
82 | }, function () {} ) |
83 | ) |
84 | |
85 | |
86 | function loadThread () { |
87 | getThread(id, function (err, thread) { |
88 | //would probably be better keep an id for each message element |
89 | //(i.e. message key) and then update it if necessary. |
90 | //also, it may have moved (say, if you received a missing message) |
91 | content.innerHTML = '' |
92 | thread = thread.map(function (msg) { |
93 | return 'string' === typeof msg.value.content ? message_unbox(msg) : msg |
94 | }) |
95 | |
96 | if(err) return content.appendChild(h('pre', err.stack)) |
97 | sort(thread).map(message_render).forEach(function (el) { |
98 | content.appendChild(el) |
99 | }) |
100 | |
101 | var branches = sort.heads(thread) |
102 | meta.branch = branches.length > 1 ? branches : branches[0] |
103 | meta.root = thread[0].key |
104 | |
105 | var recps = thread[0].value.content.recps |
106 | if(recps && thread[0].value.private) |
107 | meta.recps = recps |
108 | }) |
109 | } |
110 | |
111 | loadThread() |
112 | return div |
113 | } |
114 | } |
115 | |
116 | |
117 | |
118 | |
119 | |
120 |
Built with git-ssb-web