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