Files: 1fd3c2d9148ba9ea22b04eabc592afe3ff16dabe / modules / thread.js
2030 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.firstPlug(exports.message_unbox, msg) |
40 | } |
41 | |
42 | exports.screen_view = function (id, sbot) { |
43 | if(ref.isMsg(id)) { |
44 | var div = h('div.column', {style: {'overflow-y': 'auto'}}) |
45 | var render = ui.createRenderers(exports.message_render, sbot) |
46 | |
47 | getThread(id, sbot, function (err, thread) { |
48 | thread = thread.map(function (msg) { |
49 | return 'string' === typeof msg.value.content ? unbox(msg) : msg |
50 | }) |
51 | |
52 | if(err) return div.appendChild(h('pre', err.stack)) |
53 | sort(thread).map(render).forEach(function (el) { |
54 | div.appendChild(el) |
55 | }) |
56 | |
57 | var branches = sort.heads(thread) |
58 | var meta = { |
59 | type: 'post', |
60 | root: id, |
61 | branch: branches.length > 1 ? branches : branches[0] |
62 | } |
63 | var recps = thread[0].value.content.recps |
64 | if(recps && thread[0].value.private) |
65 | meta.recps = recps |
66 | |
67 | div.appendChild( |
68 | h('div', u.firstPlug(exports.message_compose, meta, sbot)) |
69 | ) |
70 | }) |
71 | |
72 | return div |
73 | } |
74 | |
75 | } |
76 | |
77 | exports.message_render = [] |
78 | exports.message_compose = [] |
79 | exports.message_unbox = [] |
80 | |
81 | |
82 |
Built with git-ssb-web