Files: 23576a5dca023c72b5a92bf3c1f5b35bb4f22744 / modules / thread.js
2168 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 | function getThread (root, sbot, cb) { |
25 | //in this case, it's inconvienent that panel only takes |
26 | //a stream. maybe it would be better to accept an array? |
27 | |
28 | return pull(Cat([ |
29 | once(function (cb) { |
30 | sbot.get(root, function (err, value) { |
31 | cb(err, {key: root, value: value}) |
32 | }) |
33 | }), |
34 | sbot.links({rel: 'root', dest: root, values: true, keys: true}) |
35 | ]), pull.collect(cb)) |
36 | } |
37 | |
38 | function unbox(msg) { |
39 | return u.first(exports.message_unbox, function (fn) { |
40 | return fn(msg) |
41 | }) |
42 | } |
43 | |
44 | exports.screen_view = function (id, sbot) { |
45 | if(ref.isMsg(id)) { |
46 | var div = h('div.column', {style: {'overflow-y': 'auto'}}) |
47 | var render = ui.createRenderers(exports.message_render, sbot) |
48 | |
49 | getThread(id, sbot, function (err, thread) { |
50 | thread = thread.map(function (msg) { |
51 | return 'string' === typeof msg.value.content ? unbox(msg) : msg |
52 | }) |
53 | |
54 | if(err) return div.appendChild(h('pre', err.stack)) |
55 | sort(thread).map(render).forEach(function (el) { |
56 | div.appendChild(el) |
57 | }) |
58 | |
59 | var branches = sort.heads(thread) |
60 | var meta = { |
61 | root: id, |
62 | branch: branches.length > 1 ? branches : branches[0] |
63 | } |
64 | var recps = thread[0].value.content.recps |
65 | if(recps && thread[0].value.private) |
66 | meta.recps = recps |
67 | |
68 | console.log('recipients', thread[0].value.content.recps) |
69 | |
70 | div.appendChild( |
71 | h('div', |
72 | u.decorate(exports.message_compose, meta, function (d, e, v) { |
73 | return d(e, v, sbot) |
74 | })) |
75 | ) |
76 | }) |
77 | |
78 | return div |
79 | } |
80 | |
81 | } |
82 | |
83 | exports.message_render = [] |
84 | exports.message_compose = [] |
85 | exports.message_unbox = [] |
86 | |
87 | |
88 | |
89 |
Built with git-ssb-web