Files: 9916a3c063184f892f6b76d2865ecd6b8b3af8b8 / modules / thread.js
3262 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_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 | pull( |
72 | sbot_links({ |
73 | rel: 'root', dest: id, keys: true, old: false |
74 | }), |
75 | pull.drain(function (msg) { |
76 | loadThread() //redraw thread |
77 | }, function () {} ) |
78 | ) |
79 | |
80 | |
81 | function loadThread () { |
82 | getThread(id, function (err, thread) { |
83 | //would probably be better keep an id for each message element |
84 | //(i.e. message key) and then update it if necessary. |
85 | //also, it may have moved (say, if you received a missing message) |
86 | content.innerHTML = '' |
87 | //decrypt |
88 | thread = thread.map(function (msg) { |
89 | return 'string' === typeof msg.value.content ? message_unbox(msg) : msg |
90 | }) |
91 | |
92 | if(err) return content.appendChild(h('pre', err.stack)) |
93 | sort(thread).map(message_render).filter(Boolean).forEach(function (el) { |
94 | content.appendChild(el) |
95 | }) |
96 | |
97 | var branches = sort.heads(thread) |
98 | meta.branch = branches.length > 1 ? branches : branches[0] |
99 | meta.root = thread[0].value.content.root || thread[0].key |
100 | meta.channel = thread[0].value.content.channel |
101 | |
102 | var recps = thread[0].value.content.recps |
103 | var private = thread[0].value.private |
104 | if(private) { |
105 | if(recps) |
106 | meta.recps = recps |
107 | else |
108 | meta.recps = [thread[0].value.author, self_id] |
109 | } |
110 | }) |
111 | } |
112 | |
113 | loadThread() |
114 | return div |
115 | } |
116 | } |
117 | |
118 | |
119 | |
120 | |
121 |
Built with git-ssb-web