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