git ssb

16+

Dominic / patchbay



Tree: 0e8b5960a55bb51280320035b8d4cb1ecd883988

Files: 0e8b5960a55bb51280320035b8d4cb1ecd883988 / modules / thread.js

3393 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')
9var self_id = require('../keys').id
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('../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 = [])
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 content = h('div.column.scroller__content')
64 var div = h('div.column.scroller',
65 {style: {'overflow-y': 'auto'}},
66 h('div.scroller__wrapper',
67 content,
68 message_compose(meta, {shrink: false, placeholder: 'Write a reply'})
69 )
70 )
71
72 message_name(id, function (err, name) {
73 div.id = name
74 })
75
76 pull(
77 sbot_links({
78 rel: 'root', dest: id, keys: true, old: false
79 }),
80 pull.drain(function (msg) {
81 loadThread() //redraw thread
82 }, function () {} )
83 )
84
85
86 function loadThread () {
87 getThread(id, function (err, thread) {
88 //would probably be better keep an id for each message element
89 //(i.e. message key) and then update it if necessary.
90 //also, it may have moved (say, if you received a missing message)
91 content.innerHTML = ''
92 //decrypt
93 thread = thread.map(function (msg) {
94 return 'string' === typeof msg.value.content ? message_unbox(msg) : msg
95 })
96
97 if(err) return content.appendChild(h('pre', err.stack))
98 sort(thread).map(message_render).filter(Boolean).forEach(function (el) {
99 content.appendChild(el)
100 })
101
102 var branches = sort.heads(thread)
103 meta.branch = branches.length > 1 ? branches : branches[0]
104 meta.root = thread[0].value.content.root || thread[0].key
105 meta.channel = thread[0].value.content.channel
106
107 var recps = thread[0].value.content.recps
108 var private = thread[0].value.private
109 if(private) {
110 if(recps)
111 meta.recps = recps
112 else
113 meta.recps = [thread[0].value.author, self_id]
114 }
115 })
116 }
117
118 loadThread()
119 return div
120 }
121}
122
123
124
125
126
127

Built with git-ssb-web