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