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