git ssb

16+

Dominic / patchbay



Tree: cb94bd0d83fae7b007a0e33061df18c620bd6cb1

Files: cb94bd0d83fae7b007a0e33061df18c620bd6cb1 / modules / thread.js

2744 bytesRaw
1var ui = require('../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('../util')
8var Scroller = require('pull-scroll')
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
24function getThread (root, sbot, cb) {
25 //in this case, it's inconvienent that panel only takes
26 //a stream. maybe it would be better to accept an array?
27
28 return pull(Cat([
29 once(function (cb) {
30 sbot.get(root, function (err, value) {
31 cb(err, {key: root, value: value})
32 })
33 }),
34 sbot.links({rel: 'root', dest: root, values: true, keys: true})
35 ]), pull.collect(cb))
36}
37
38function unbox(msg) {
39 return u.firstPlug(exports.message_unbox, msg)
40}
41
42exports.screen_view = function (id, sbot) {
43 if(ref.isMsg(id)) {
44 var meta = {
45 type: 'post',
46 root: id,
47 branch: id //mutated when thread is loaded.
48 }
49
50 var content = h('div')
51 var div = h('div.column',
52 {style: {'overflow-y': 'auto'}},
53 content,
54 h('div.editor', u.firstPlug(exports.message_compose, meta, sbot))
55 )
56
57 var render = ui.createRenderers(exports.message_render, sbot)
58
59 pull(
60 sbot.links({
61 rel: 'root', dest: id, keys: true, old: false
62 }),
63 pull.drain(function (msg) {
64 console.log('new message in thread', msg)
65 //redraw thread
66 loadThread()
67 }, function () {} )
68 )
69
70
71 function loadThread () {
72 console.log("LOAD THREAD", id)
73 getThread(id, sbot, function (err, thread) {
74 //would probably be better keep an id for each message element
75 //(i.e. message key) and then update it if necessary.
76 //also, it may have moved (say, if you received a missing message)
77 content.innerHTML = ''
78 thread = thread.map(function (msg) {
79 return 'string' === typeof msg.value.content ? unbox(msg) : msg
80 })
81
82 if(err) return content.appendChild(h('pre', err.stack))
83 sort(thread).map(render).forEach(function (el) {
84 content.appendChild(el)
85 })
86
87 var branches = sort.heads(thread)
88 meta.branch = branches.length > 1 ? branches : branches[0]
89
90 var recps = thread[0].value.content.recps
91 if(recps && thread[0].value.private)
92 meta.recps = recps
93 })
94
95 }
96
97 loadThread()
98
99 return div
100 }
101
102}
103
104exports.message_render = []
105exports.message_compose = []
106exports.message_unbox = []
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

Built with git-ssb-web