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