Files: 934aacc8386594385eab23772a38b11406625294 / render.js
3556 bytesRaw
1 | var h = require('hyperscript') |
2 | var pull = require('pull-stream') |
3 | var human = require('human-time') |
4 | |
5 | var sbot = require('./scuttlebot') |
6 | var composer = require('./compose') |
7 | var tools = require('./tools') |
8 | |
9 | var config = require('./config')() |
10 | var id = require('./keys').id |
11 | |
12 | |
13 | module.exports = function (msg) { |
14 | console.log(msg) |
15 | var message = h('div.message#' + msg.key.substring(0, 44)) |
16 | if (msg.value.content.type == 'post') { |
17 | var opts = {} |
18 | var fallback = {} |
19 | |
20 | opts.type = 'post' |
21 | opts.branch = msg.key |
22 | |
23 | if (msg.value.content.root) |
24 | opts.root = msg.value.content.root |
25 | else |
26 | opts.root = msg.key |
27 | |
28 | message.appendChild(tools.header(msg)) |
29 | |
30 | if (msg.value.content.root) |
31 | message.appendChild(h('span', 're: ', tools.messageLink(msg.value.content.root))) |
32 | |
33 | message.appendChild(h('div.message__body', tools.markdown(msg.value.content.text))) |
34 | |
35 | |
36 | pull( |
37 | sbot.query({query: [{$filter: {value: {content: {type: 'edit', original: msg.key}}}}]}), |
38 | pull.drain(function (update) { |
39 | var newMessage = h('div', tools.markdown(update.value.content.text)) |
40 | var latest = h('div.message__body', |
41 | tools.timestamp(update, {edited: true}), |
42 | newMessage |
43 | ) |
44 | message.replaceChild(latest, message.childNodes[message.childNodes.length - 2]) |
45 | fallback.messageText = update.value.content.text |
46 | opts.updated = update.key |
47 | opts.original = msg.key |
48 | }) |
49 | ) |
50 | |
51 | var buttons = h('div.buttons') |
52 | buttons.appendChild(h('button.btn', 'Reply', { |
53 | onclick: function () { |
54 | var r = message.childNodes.length - 1 |
55 | |
56 | fallback.buttons = message.childNodes[r] |
57 | var compose = h('div.message#re:' + msg.key.substring(0, 44), composer(opts, fallback)) |
58 | message.parentNode.insertBefore(compose, message.nextSibling) |
59 | } |
60 | })) |
61 | |
62 | if (msg.value.author == id) |
63 | buttons.appendChild(h('button.btn', 'Edit', { |
64 | onclick: function () { |
65 | opts.type = 'edit' |
66 | if (!fallback.messageText) |
67 | fallback.messageText = msg.value.content.text |
68 | |
69 | if (!opts.updated) |
70 | opts.updated = msg.key |
71 | opts.original = msg.key |
72 | |
73 | var r = message.childNodes.length - 1 |
74 | fallback.buttons = message.childNodes[r] |
75 | message.removeChild(message.childNodes[r]) |
76 | var compose = h('div#edit:' + msg.key.substring(0, 44), composer(opts, fallback)) |
77 | message.replaceChild(compose, message.lastElementChild) |
78 | } |
79 | })) |
80 | |
81 | message.appendChild(buttons) |
82 | return message |
83 | |
84 | } else if (msg.value.content.type == 'vote') { |
85 | message.appendChild(tools.header(msg)) |
86 | message.appendChild(h('span', 'Starred:')) |
87 | var embed = msg.value.content.vote.link |
88 | |
89 | var embedded = h('div.embedded') |
90 | sbot.get(embed, function (err, msg) { |
91 | if (err) {console.log('could not find message locally, try ooo?') } |
92 | msg.value = msg |
93 | msg.key = embed |
94 | if (msg.value.content.text) { |
95 | message.appendChild(embedded) |
96 | embedded.appendChild(tools.header(msg)) |
97 | embedded.appendChild( |
98 | h('div.message__body', |
99 | tools.markdown(msg.value.content.text.substring(0, 256) + '...'), |
100 | h('span', '[', h('a', {href: '#' + msg.key}, 'Full Post'), ']') |
101 | ) |
102 | ) |
103 | } |
104 | }) |
105 | return message |
106 | } else { |
107 | //message.appendChild(tools.header(msg)) |
108 | //message.appendChild(h('pre', tools.rawJSON(msg.value.content))) |
109 | //return message |
110 | return h('div.invisibleMessage') |
111 | } |
112 | } |
113 |
Built with git-ssb-web