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