Files: b62ecd6f843b26f37e70c7b6d65c71db35bbf922 / modules / thread.js
3410 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 | var self_id = require('../keys').id |
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 | exports.needs = { |
25 | message_render: 'first', |
26 | message_name: 'first', |
27 | message_compose: 'first', |
28 | message_unbox: 'first', |
29 | sbot_get: 'first', |
30 | sbot_links: 'first' |
31 | } |
32 | |
33 | exports.gives = 'screen_view' |
34 | |
35 | |
36 | exports.create = function (api) { |
37 | |
38 | function getThread (root, cb) { |
39 | //in this case, it's inconvienent that panel only takes |
40 | //a stream. maybe it would be better to accept an array? |
41 | |
42 | api.sbot_get(root, function (err, value) { |
43 | if (err) return cb(err) |
44 | var msg = {key: root, value: value} |
45 | |
46 | pull( |
47 | api.sbot_links({rel: 'root', dest: root, values: true, keys: true}), |
48 | pull.collect(function (err, ary) { |
49 | if(err) return cb(err) |
50 | ary.unshift(msg) |
51 | cb(null, ary) |
52 | }) |
53 | ) |
54 | }) |
55 | |
56 | } |
57 | |
58 | return function (id) { |
59 | if(ref.isMsg(id)) { |
60 | var meta = { |
61 | type: 'post', |
62 | root: id, |
63 | branch: id //mutated when thread is loaded. |
64 | } |
65 | |
66 | var content = h('div.column.scroller__content') |
67 | var div = h('div.column.scroller', |
68 | {style: {'overflow-y': 'auto'}}, |
69 | h('div.scroller__wrapper', |
70 | content, |
71 | api.message_compose(meta, {shrink: false, placeholder: 'Write a reply'}) |
72 | ) |
73 | ) |
74 | |
75 | api.message_name(id, function (err, name) { |
76 | div.title = name |
77 | }) |
78 | |
79 | pull( |
80 | api.sbot_links({ |
81 | rel: 'root', dest: id, keys: true, old: false |
82 | }), |
83 | pull.drain(function (msg) { |
84 | loadThread() //redraw thread |
85 | }, function () {} ) |
86 | ) |
87 | |
88 | |
89 | function loadThread () { |
90 | getThread(id, function (err, thread) { |
91 | //would probably be better keep an id for each message element |
92 | //(i.e. message key) and then update it if necessary. |
93 | //also, it may have moved (say, if you received a missing message) |
94 | content.innerHTML = '' |
95 | if(err) return content.appendChild(h('pre', err.stack)) |
96 | |
97 | //decrypt |
98 | thread = thread.map(function (msg) { |
99 | return 'string' === typeof msg.value.content ? api.message_unbox(msg) : msg |
100 | }) |
101 | |
102 | if(err) return content.appendChild(h('pre', err.stack)) |
103 | sort(thread).map(api.message_render).filter(Boolean).forEach(function (el) { |
104 | content.appendChild(el) |
105 | }) |
106 | |
107 | var branches = sort.heads(thread) |
108 | meta.branch = branches.length > 1 ? branches : branches[0] |
109 | meta.root = thread[0].value.content.root || thread[0].key |
110 | meta.channel = thread[0].value.content.channel |
111 | |
112 | var recps = thread[0].value.content.recps |
113 | var private = thread[0].value.private |
114 | if(private) { |
115 | if(recps) |
116 | meta.recps = recps |
117 | else |
118 | meta.recps = [thread[0].value.author, self_id] |
119 | } |
120 | }) |
121 | } |
122 | |
123 | loadThread() |
124 | return div |
125 | } |
126 | } |
127 | } |
128 |
Built with git-ssb-web