git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: 996cb9ffec97abd3bf8391f3a15e462d01fd663d

Files: 996cb9ffec97abd3bf8391f3a15e462d01fd663d / modules / thread.js

3580 bytesRaw
1var ui = require('patchbay/ui')
2var pull = require('pull-stream')
3var Cat = require('pull-cat')
4var sort = require('ssb-sort')
5var ref = require('ssb-ref')
6var h = require('hyperscript')
7var u = require('patchbay/util')
8var Scroller = require('pull-scroll')
9
10
11function 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
25var plugs = require('patchbay/plugs')
26
27var message_render = plugs.first(exports.message_render = [])
28var message_name = plugs.first(exports.message_name = [])
29var message_compose = plugs.first(exports.message_compose = [])
30var message_unbox = plugs.first(exports.message_unbox = [])
31
32var sbot_get = plugs.first(exports.sbot_get = [])
33var sbot_links = plugs.first(exports.sbot_links = [])
34var get_id = plugs.first(exports.get_id = [])
35
36function 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
56exports.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 previousId = 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, {inContext: true, previousId})
102 previousId = 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