Files: e011bd5b98dd02ca18961e374977a17784ad659b / modules_basic / thread.js
3069 bytesRaw
1 | var pull = require('pull-stream') |
2 | var Cat = require('pull-cat') |
3 | var sort = require('ssb-sort') |
4 | var ref = require('ssb-ref') |
5 | var h = require('hyperscript') |
6 | var u = require('../util') |
7 | var Scroller = require('pull-scroll') |
8 | |
9 | function once (cont) { |
10 | var ended = false |
11 | return function (abort, cb) { |
12 | if(abort) return cb(abort) |
13 | else if (ended) return cb(ended) |
14 | else |
15 | cont(function (err, data) { |
16 | if(err) return cb(ended = err) |
17 | ended = true |
18 | cb(null, data) |
19 | }) |
20 | } |
21 | } |
22 | |
23 | var plugs = require('../plugs') |
24 | |
25 | var message_render = plugs.first(exports.message_render = []) |
26 | var message_compose = plugs.first(exports.message_compose = []) |
27 | var message_unbox = plugs.first(exports.message_unbox = []) |
28 | |
29 | var sbot_get = plugs.first(exports.sbot_get = []) |
30 | var sbot_links = plugs.first(exports.sbot_links = []) |
31 | |
32 | function getThread (root, cb) { |
33 | //in this case, it's inconvienent that panel only takes |
34 | //a stream. maybe it would be better to accept an array? |
35 | |
36 | sbot_get(root, function (err, value) { |
37 | var msg = {key: root, value: value} |
38 | // if(value.content.root) return getThread(value.content.root, cb) |
39 | |
40 | pull( |
41 | sbot_links({rel: 'root', dest: root, values: true, keys: true}), |
42 | pull.collect(function (err, ary) { |
43 | if(err) return cb(err) |
44 | ary.unshift(msg) |
45 | cb(null, ary) |
46 | }) |
47 | ) |
48 | }) |
49 | |
50 | } |
51 | |
52 | exports.screen_view = function (id) { |
53 | if(ref.isMsg(id)) { |
54 | var meta = { |
55 | type: 'post', |
56 | root: id, |
57 | branch: id //mutated when thread is loaded. |
58 | } |
59 | |
60 | var content = h('div.column.scroller__content') |
61 | var div = h('div.column.scroller', |
62 | {style: {'overflow-y': 'auto'}}, |
63 | h('div.scroller__wrapper', |
64 | content, |
65 | message_compose(meta, {shrink: false, placeholder: 'Write a reply'}) |
66 | ) |
67 | ) |
68 | |
69 | pull( |
70 | sbot_links({ |
71 | rel: 'root', dest: id, keys: true, old: false |
72 | }), |
73 | pull.drain(function (msg) { |
74 | loadThread() //redraw thread |
75 | }, function () {} ) |
76 | ) |
77 | |
78 | |
79 | function loadThread () { |
80 | getThread(id, function (err, thread) { |
81 | //would probably be better keep an id for each message element |
82 | //(i.e. message key) and then update it if necessary. |
83 | //also, it may have moved (say, if you received a missing message) |
84 | content.innerHTML = '' |
85 | //decrypt |
86 | thread = thread.map(function (msg) { |
87 | return 'string' === typeof msg.value.content ? message_unbox(msg) : msg |
88 | }) |
89 | |
90 | if(err) return content.appendChild(h('pre', err.stack)) |
91 | sort(thread).map(message_render).filter(Boolean).forEach(function (el) { |
92 | content.appendChild(el) |
93 | }) |
94 | |
95 | var branches = sort.heads(thread) |
96 | meta.branch = branches.length > 1 ? branches : branches[0] |
97 | meta.root = thread[0].value.content.root || thread[0].key |
98 | meta.channel = thread[0].value.content.channel |
99 | |
100 | var recps = thread[0].value.content.recps |
101 | if(recps && thread[0].value.private) |
102 | meta.recps = recps |
103 | }) |
104 | } |
105 | |
106 | loadThread() |
107 | return div |
108 | } |
109 | } |
110 | |
111 | |
112 |
Built with git-ssb-web