Files: 3ee0398629a78832c2fbe78831ce756faeaeca74 / modules / thread.js
3404 bytesRaw
1 | var pull = require('pull-stream') |
2 | var sort = require('ssb-sort') |
3 | var ref = require('ssb-ref') |
4 | var h = require('hyperscript') |
5 | var u = require('../util') |
6 | var Scroller = require('pull-scroll') |
7 | var self_id = require('../keys').id |
8 | var messageName = require('./helpers').message_name |
9 | |
10 | var get = require('./scuttlebot').get |
11 | var links = require('./scuttlebot').links |
12 | var Unbox = require('./helpers-private').unbox |
13 | |
14 | function once (cont) { |
15 | var ended = false |
16 | return function (abort, cb) { |
17 | if(abort) return cb(abort) |
18 | else if (ended) return cb(ended) |
19 | else |
20 | cont(function (err, data) { |
21 | if(err) return cb(ended = err) |
22 | ended = true |
23 | cb(null, data) |
24 | }) |
25 | } |
26 | } |
27 | |
28 | exports.needs = { |
29 | message_render: 'first', |
30 | message_compose: '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 | get(root, function (err, value) { |
43 | if (err) return cb(err) |
44 | var msg = {key: root, value: value} |
45 | |
46 | pull( |
47 | 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, {placeholder: 'Write a reply'}) |
72 | ) |
73 | ) |
74 | |
75 | messageName(id, function (err, name) { |
76 | div.title = name |
77 | }) |
78 | |
79 | pull( |
80 | 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 ? 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