git ssb

16+

Dominic / patchbay



Tree: 4cda013d06b86a002895df3cbed747f6baf1085c

Files: 4cda013d06b86a002895df3cbed747f6baf1085c / modules_basic / thread.js

3880 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('../util')
7var Scroller = require('pull-scroll')
8var self_id = require('../keys').id
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
24//var plugs = require('../plugs')
25//
26//var message_render = plugs.first(exports.message_render = [])
27//var message_name = plugs.first(exports.message_name = [])
28//var message_compose = plugs.first(exports.message_compose = [])
29//var message_unbox = plugs.first(exports.message_unbox = [])
30//
31//var sbot_get = plugs.first(exports.sbot_get = [])
32//var sbot_links = plugs.first(exports.sbot_links = [])
33
34exports.needs = {
35 message_render: 'first',
36 message_name: 'first',
37 message_compose: 'first',
38 message_unbox: 'first',
39 sbot_get: 'first',
40 sbot_links: 'first'
41}
42
43exports.gives = 'screen_view'
44
45
46exports.create = function (api) {
47
48 function getThread (root, cb) {
49 //in this case, it's inconvienent that panel only takes
50 //a stream. maybe it would be better to accept an array?
51
52 api.sbot_get(root, function (err, value) {
53 if (err) return cb(err)
54 var msg = {key: root, value: value}
55 // if(value.content.root) return getThread(value.content.root, cb)
56
57 pull(
58 api.sbot_links({rel: 'root', dest: root, values: true, keys: true}),
59 pull.collect(function (err, ary) {
60 if(err) return cb(err)
61 ary.unshift(msg)
62 cb(null, ary)
63 })
64 )
65 })
66
67 }
68
69 return function (id) {
70 if(ref.isMsg(id)) {
71 var meta = {
72 type: 'post',
73 root: id,
74 branch: id //mutated when thread is loaded.
75 }
76
77 var content = h('div.column.scroller__content')
78 var div = h('div.column.scroller',
79 {style: {'overflow-y': 'auto'}},
80 h('div.scroller__wrapper',
81 content,
82 api.message_compose(meta, {shrink: false, placeholder: 'Write a reply'})
83 )
84 )
85
86 api.message_name(id, function (err, name) {
87 div.title = name
88 })
89
90 pull(
91 api.sbot_links({
92 rel: 'root', dest: id, keys: true, old: false
93 }),
94 pull.drain(function (msg) {
95 loadThread() //redraw thread
96 }, function () {} )
97 )
98
99
100 function loadThread () {
101 getThread(id, function (err, thread) {
102 //would probably be better keep an id for each message element
103 //(i.e. message key) and then update it if necessary.
104 //also, it may have moved (say, if you received a missing message)
105 content.innerHTML = ''
106 if(err) return content.appendChild(h('pre', err.stack))
107
108 //decrypt
109 thread = thread.map(function (msg) {
110 return 'string' === typeof msg.value.content ? api.message_unbox(msg) : msg
111 })
112
113 if(err) return content.appendChild(h('pre', err.stack))
114 sort(thread).map(api.message_render).filter(Boolean).forEach(function (el) {
115 content.appendChild(el)
116 })
117
118 var branches = sort.heads(thread)
119 meta.branch = branches.length > 1 ? branches : branches[0]
120 meta.root = thread[0].value.content.root || thread[0].key
121 meta.channel = thread[0].value.content.channel
122
123 var recps = thread[0].value.content.recps
124 var private = thread[0].value.private
125 if(private) {
126 if(recps)
127 meta.recps = recps
128 else
129 meta.recps = [thread[0].value.author, self_id]
130 }
131 })
132 }
133
134 loadThread()
135 return div
136 }
137 }
138}
139

Built with git-ssb-web