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