Files: 9de4a12dc04dd73d6845202b57055758655aba3a / render.js
3541 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 | //fallback = message.lastElementChild |
62 | //console.log(fallback) |
63 | var compose = h('div.message#' + reply.branch.substring(0, 10), composer(reply)) |
64 | message.parentNode.appendChild(compose) |
65 | //message.replaceChild(compose, message.lastElementChild) |
66 | } |
67 | })) |
68 | if (msg.value.author == id) |
69 | buttons.appendChild(h('button.btn', 'Edit', { |
70 | onclick: function () { |
71 | var r = message.childNodes.length - 1 |
72 | edit.buttons = message.childNodes[r] |
73 | message.removeChild(message.childNodes[r]) |
74 | var compose = h('div#' + edit.updated.substring(0, 10), composer(edit)) |
75 | message.replaceChild(compose, message.lastElementChild) |
76 | } |
77 | })) |
78 | |
79 | message.appendChild(buttons) |
80 | return message |
81 | |
82 | } else if (msg.value.content.type == 'vote') { |
83 | message.appendChild(tools.header(msg)) |
84 | message.appendChild(h('span', 'Starred:')) |
85 | var embed = msg.value.content.vote.link |
86 | |
87 | var embedded = h('div.embedded') |
88 | sbot.get(embed, function (err, msg) { |
89 | if (err) {console.log('could not find message locally, try ooo?') } |
90 | msg.value = msg |
91 | msg.key = embed |
92 | if (msg.value.content.text) { |
93 | message.appendChild(embedded) |
94 | embedded.appendChild(tools.header(msg)) |
95 | embedded.appendChild( |
96 | h('div.message__body', |
97 | tools.markdown(msg.value.content.text.substring(0, 256) + '...'), |
98 | h('span', '[', h('a', {href: '#' + msg.key}, 'Full Post'), ']') |
99 | ) |
100 | ) |
101 | } |
102 | }) |
103 | return message |
104 | } else { |
105 | //message.appendChild(tools.header(msg)) |
106 | //message.appendChild(h('pre', tools.rawJSON(msg.value.content))) |
107 | //return message |
108 | return |
109 | } |
110 | } |
111 |
Built with git-ssb-web