git ssb

16+

Dominic / patchbay



Tree: 80807add560dfbbd3fe40aa17eac62b859624063

Files: 80807add560dfbbd3fe40aa17eac62b859624063 / modules_basic / thread.js

3381 bytesRaw
1var pull = require('pull-stream')
2var sort = require('ssb-sort')
3var ref = require('ssb-ref')
4var h = require('hyperscript')
5var self_id = require('../keys').id
6
7function once (cont) {
8 var ended = false
9 return function (abort, cb) {
10 if(abort) return cb(abort)
11 else if (ended) return cb(ended)
12 else
13 cont(function (err, data) {
14 if(err) return cb(ended = err)
15 ended = true
16 cb(null, data)
17 })
18 }
19}
20
21exports.needs = {
22 message_render: 'first',
23 message_name: 'first',
24 message_compose: 'first',
25 message_unbox: 'first',
26 sbot_get: 'first',
27 sbot_links: 'first'
28}
29
30exports.gives = 'screen_view'
31
32
33exports.create = function (api) {
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 api.sbot_get(root, function (err, value) {
40 if (err) return cb(err)
41 var msg = {key: root, value: value}
42 // if(value.content.root) return getThread(value.content.root, cb)
43
44 pull(
45 api.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 return 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 content = h('div.column.scroller__content')
65 var div = h('div.column.scroller',
66 {style: {'overflow-y': 'auto'}},
67 h('div.scroller__wrapper',
68 content,
69 api.message_compose(meta, {shrink: false, placeholder: 'Write a reply'})
70 )
71 )
72
73 api.message_name(id, function (err, name) {
74 div.title = name
75 })
76
77 pull(
78 api.sbot_links({
79 rel: 'root', dest: id, keys: true, old: false
80 }),
81 pull.drain(function (msg) {
82 loadThread() //redraw thread
83 }, function () {} )
84 )
85
86
87 function loadThread () {
88 getThread(id, function (err, thread) {
89 //would probably be better keep an id for each message element
90 //(i.e. message key) and then update it if necessary.
91 //also, it may have moved (say, if you received a missing message)
92 content.innerHTML = ''
93 if(err) return content.appendChild(h('pre', err.stack))
94
95 //decrypt
96 thread = thread.map(function (msg) {
97 return 'string' === typeof msg.value.content ? api.message_unbox(msg) : msg
98 })
99
100 if(err) return content.appendChild(h('pre', err.stack))
101 sort(thread).map(api.message_render).filter(Boolean).forEach(function (el) {
102 content.appendChild(el)
103 })
104
105 var branches = sort.heads(thread)
106 meta.branch = branches.length > 1 ? branches : branches[0]
107 meta.root = thread[0].value.content.root || thread[0].key
108 meta.channel = thread[0].value.content.channel
109
110 var recps = thread[0].value.content.recps
111 var priv = thread[0].value['private']
112 if(priv) {
113 if(recps)
114 meta.recps = recps
115 else
116 meta.recps = [thread[0].value.author, self_id]
117 }
118 })
119 }
120
121 loadThread()
122 return div
123 }
124 }
125}
126

Built with git-ssb-web