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