Files: 33dbf75c73b0fb716af94766e08f4dd2fd3443d5 / modules / thread.js
3396 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 | var self_id = require('../keys').id |
10 | |
11 | function once (cont) { |
12 | var ended = false |
13 | return function (abort, cb) { |
14 | if(abort) return cb(abort) |
15 | else if (ended) return cb(ended) |
16 | else |
17 | cont(function (err, data) { |
18 | if(err) return cb(ended = err) |
19 | ended = true |
20 | cb(null, data) |
21 | }) |
22 | } |
23 | } |
24 | |
25 | var plugs = require('../plugs') |
26 | |
27 | var message_render = plugs.first(exports.message_render = []) |
28 | var message_name = plugs.first(exports.message_name = []) |
29 | var message_compose = plugs.first(exports.message_compose = []) |
30 | var message_unbox = plugs.first(exports.message_unbox = []) |
31 | |
32 | var sbot_get = plugs.first(exports.sbot_get = []) |
33 | var sbot_links = plugs.first(exports.sbot_links = []) |
34 | |
35 | function getThread (root, cb) { |
36 | //in this case, it's inconvienent that panel only takes |
37 | //a stream. maybe it would be better to accept an array? |
38 | |
39 | sbot_get(root, function (err, value) { |
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 | //decrypt |
93 | thread = thread.map(function (msg) { |
94 | return 'string' === typeof msg.value.content ? message_unbox(msg) : msg |
95 | }) |
96 | |
97 | if(err) return content.appendChild(h('pre', err.stack)) |
98 | sort(thread).map(message_render).filter(Boolean).forEach(function (el) { |
99 | content.appendChild(el) |
100 | }) |
101 | |
102 | var branches = sort.heads(thread) |
103 | meta.branch = branches.length > 1 ? branches : branches[0] |
104 | meta.root = thread[0].value.content.root || thread[0].key |
105 | meta.channel = thread[0].value.content.channel |
106 | |
107 | var recps = thread[0].value.content.recps |
108 | var private = thread[0].value.private |
109 | if(private) { |
110 | if(recps) |
111 | meta.recps = recps |
112 | else |
113 | meta.recps = [thread[0].value.author, self_id] |
114 | } |
115 | }) |
116 | } |
117 | |
118 | loadThread() |
119 | return div |
120 | } |
121 | } |
122 | |
123 | |
124 | |
125 | |
126 | |
127 |
Built with git-ssb-web