Files: 11ede85b7634bfac97e4ee875592d5f8610f4433 / render.js
3640 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 | module.exports = function (msg) { |
13 | var edit = {} |
14 | var reply = {} |
15 | |
16 | var message = h('div.message') |
17 | |
18 | if (msg.value.content.type == 'post') { |
19 | reply.type = 'post' |
20 | reply.branch = msg.key |
21 | |
22 | if (msg.value.content.root) |
23 | reply.root = msg.value.content.root |
24 | else |
25 | reply.root = msg.key |
26 | |
27 | if (msg.value.author == id) |
28 | edit.original = msg.key |
29 | edit.type = 'update' |
30 | edit.updated = msg.key |
31 | edit.messageText = msg.value.content.text |
32 | |
33 | message.appendChild(tools.header(msg)) |
34 | |
35 | if (msg.value.content.root) { |
36 | message.appendChild(h('span', 're: ', tools.messageLink(msg.value.content.root))) |
37 | } |
38 | |
39 | message.appendChild( |
40 | h('div.message__body', tools.markdown(msg.value.content.text)) |
41 | ) |
42 | |
43 | pull( |
44 | sbot.query({query: [{$filter: {value: {content: {type: 'update', updated: msg.key}}}}]}), |
45 | pull.drain(function (update) { |
46 | var newTimestamp = h('span.timestamp', 'Edited: ', h('a', {href: '#' + update.key}, human(new Date(update.value.timestamp)))) |
47 | var newMessage = h('div', tools.markdown(update.value.content.text)) |
48 | var latest = h('div.message__body', |
49 | newTimestamp, |
50 | newMessage |
51 | ) |
52 | var r = message.childNodes.length - 2 |
53 | message.replaceChild(latest, message.childNodes[r]) |
54 | edit.messageText = update.value.content.text |
55 | edit.original = msg.value.content.original |
56 | }) |
57 | ) |
58 | var buttons = h('div.buttons') |
59 | buttons.appendChild(h('button.btn', 'Reply', { |
60 | onclick: function () { |
61 | var r = message.childNodes.length - 1 |
62 | buttons = message.childNodes[r] |
63 | //fallback = message.lastElementChild |
64 | //console.log(fallback) |
65 | var compose = h('div.message#' + reply.branch.substring(0, 10), composer(reply, buttons)) |
66 | message.parentNode.appendChild(compose) |
67 | //message.replaceChild(compose, message.lastElementChild) |
68 | } |
69 | })) |
70 | if (msg.value.author == id) |
71 | buttons.appendChild(h('button.btn', 'Edit', { |
72 | onclick: function () { |
73 | var r = message.childNodes.length - 1 |
74 | buttons = message.childNodes[r] |
75 | message.removeChild(message.childNodes[r]) |
76 | var compose = h('div#' + edit.updated.substring(0, 10), composer(edit, buttons)) |
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 |
111 | } |
112 | } |
113 |
Built with git-ssb-web