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