Files: 798d7ea858881504d038705f550581dd75eabaa3 / modules / thread.js
2718 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, 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 | |
47 | exports.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