Files: cdcccc55e2a0881ced884e834c3ebda3fd1e9ceb / modules / thread.js
2920 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 | } |
52 | |
53 | exports.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