git ssb

10+

Matt McKegg / patchwork



Tree: 99aead0a4c4797532b2986ba64d8c30f3d3c5993

Files: 99aead0a4c4797532b2986ba64d8c30f3d3c5993 / modules / thread.js

3548 bytesRaw
1var pull = require('pull-stream')
2var Cat = require('pull-cat')
3var sort = require('ssb-sort')
4var ref = require('ssb-ref')
5var h = require('hyperscript')
6var u = require('patchbay/util')
7var Scroller = require('pull-scroll')
8
9
10function once (cont) {
11 var ended = false
12 return function (abort, cb) {
13 if(abort) return cb(abort)
14 else if (ended) return cb(ended)
15 else
16 cont(function (err, data) {
17 if(err) return cb(ended = err)
18 ended = true
19 cb(null, data)
20 })
21 }
22}
23
24var plugs = require('patchbay/plugs')
25
26var message_render = plugs.first(exports.message_render = [])
27var message_name = plugs.first(exports.message_name = [])
28var message_compose = plugs.first(exports.message_compose = [])
29var message_unbox = plugs.first(exports.message_unbox = [])
30
31var sbot_get = plugs.first(exports.sbot_get = [])
32var sbot_links = plugs.first(exports.sbot_links = [])
33var get_id = plugs.first(exports.get_id = [])
34
35function 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
55exports.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 previousId = id
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 message_compose(meta, {shrink: false, placeholder: 'Write a reply'})
70 )
71 )
72
73 message_name(id, function (err, name) {
74 div.title = name
75 })
76
77 pull(
78 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 //decrypt
94 thread = thread.map(function (msg) {
95 return 'string' === typeof msg.value.content ? message_unbox(msg) : msg
96 })
97
98 if(err) return content.appendChild(h('pre', err.stack))
99 sort(thread).map((msg) => {
100 var result = message_render(msg, {inContext: true, previousId})
101 previousId = msg.key
102 return result
103 }).filter(Boolean).forEach(function (el) {
104 content.appendChild(el)
105 })
106
107 var branches = sort.heads(thread)
108 meta.branch = branches.length > 1 ? branches : branches[0]
109 meta.root = thread[0].value.content.root || thread[0].key
110 meta.channel = thread[0].value.content.channel
111
112 var recps = thread[0].value.content.recps
113 var private = thread[0].value.private
114 if(private) {
115 if(recps)
116 meta.recps = recps
117 else
118 meta.recps = [thread[0].value.author, get_id()]
119 }
120 })
121 }
122
123 loadThread()
124 return div
125 }
126}
127

Built with git-ssb-web