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