git ssb

10+

Matt McKegg / patchwork



Tree: 0839379f2aeeb22bd18fed24094f50e96f13421a

Files: 0839379f2aeeb22bd18fed24094f50e96f13421a / modules / page / html / render / message.js

2441 bytesRaw
1var { h, when, map, Proxy, Struct, Value, computed } = require('mutant')
2var nest = require('depnest')
3var ref = require('ssb-ref')
4
5exports.needs = nest({
6 'keys.sync.id': 'first',
7 'feed.obs.thread': 'first',
8 'message.sync.unbox': 'first',
9 'message.html': {
10 render: 'first',
11 compose: 'first'
12 },
13 'sbot.async.get': 'first'
14})
15
16exports.gives = nest('page.html.render')
17
18exports.create = function (api) {
19 return nest('page.html.render', function (id) {
20 if (!ref.isMsg(id)) return
21 var loader = h('div', {className: 'Loading -large'})
22
23 var result = Proxy(loader)
24
25 var meta = Struct({
26 type: 'post',
27 root: Proxy(id),
28 branch: Proxy(id),
29 channel: Value(undefined),
30 recps: Value(undefined)
31 })
32
33 var compose = api.message.html.compose({
34 meta,
35 shrink: false,
36 placeholder: when(meta.recps, 'Write a private reply', 'Write a public reply')
37 })
38
39 api.sbot.async.get(id, (err, value) => {
40 if (err) {
41 return result.set(h('PageHeading', [
42 h('h1', 'Cannot load thread. Root message missing.')
43 ]))
44 }
45
46 if (typeof value.content === 'string') {
47 value = api.message.sync.unbox(value)
48 }
49
50 if (!value) {
51 return result.set(h('PageHeading', [
52 h('h1', 'Cannot display message.')
53 ]))
54 }
55
56 // what happens in private stays in private!
57 meta.recps.set(value.content.recps)
58
59 var isReply = !!value.content.root
60 var thread = api.feed.obs.thread(id, {branch: isReply})
61
62 meta.channel.set(value.content.channel)
63 meta.root.set(value.content.root || thread.rootId)
64
65 // if root thread, reply to last post
66 meta.branch.set(isReply ? thread.branchId : thread.lastId)
67
68 var container = h('Thread', [
69 h('div.messages', [
70 when(thread.branchId, h('a.full', {href: thread.rootId}, ['View full thread'])),
71 map(thread.messages, (msg) => {
72 return computed([msg, thread.previousKey(msg)], (msg, previousId) => {
73 return api.message.html.render(msg, {pageId: id, previousId, includeReferences: true})
74 })
75 }, {
76 maxTime: 5,
77 idle: true
78 })
79 ]),
80 compose
81 ])
82 result.set(when(thread.sync, container, loader))
83 })
84
85 return h('div', {className: 'SplitView'}, [
86 h('div.main', [
87 result
88 ])
89 ])
90 })
91}
92

Built with git-ssb-web