Files: 565af159eaf7a2f0d880ff7e4286e19e59da7ae7 / render.js
3767 bytesRaw
1 | var h = require('hyperscript') |
2 | var config = require('./config')() |
3 | |
4 | var pull = require('pull-stream') |
5 | |
6 | var human = require('human-time') |
7 | |
8 | var sbot = require('./scuttlebot') |
9 | var composer = require('./compose') |
10 | |
11 | var tools = require('./tools') |
12 | |
13 | var id = require('./keys').id |
14 | |
15 | module.exports = function (msg) { |
16 | var opts = {} |
17 | opts.root = null |
18 | var message = h('div.message') |
19 | |
20 | |
21 | if (msg.value.content.type == 'post') { |
22 | opts.type = 'post' |
23 | opts.branch = msg.key |
24 | |
25 | message.appendChild(tools.header(msg)) |
26 | |
27 | if (msg.value.content.root) { |
28 | message.appendChild(h('span', 're: ', tools.messageLink(msg.value.content.root))) |
29 | } |
30 | |
31 | message.appendChild( |
32 | h('div.message__body', tools.markdown(msg.value.content.text)) |
33 | ) |
34 | |
35 | if (msg.value.author == id) { |
36 | opts.type = 'update' |
37 | opts.updated = msg.key |
38 | opts.messageText = msg.value.content.text |
39 | pull( |
40 | sbot.query({query: [{$filter: {value: {content: {type: 'update', updated: msg.key}}}}]}), |
41 | pull.drain(function (update) { |
42 | var latest = h('div.message__body', |
43 | tools.markdown(update.value.content.text), |
44 | h('span.timestamp', 'Edited: ', h('a', {href: '#' + update.key}, human(new Date(update.value.timestamp)))) |
45 | ) |
46 | var num = message.childNodes.length |
47 | var act = num - 2 |
48 | console.log(act) |
49 | message.replaceChild(latest, message.childNodes[act]) |
50 | opts.messageText = update.value.content.text |
51 | }) |
52 | |
53 | ) |
54 | |
55 | |
56 | if (msg.value.content.original) |
57 | opts.original = msg.value.content.original |
58 | else |
59 | opts.original = msg.key |
60 | message.appendChild(h('button.btn', 'Edit', { |
61 | onclick: function () { |
62 | var compose = h('div.message', composer(opts)) |
63 | message.parentNode.replaceChild(compose, message) |
64 | } |
65 | })) |
66 | } else { |
67 | opts.type = 'post' |
68 | opts.branch = msg.key |
69 | |
70 | if (msg.value.content.root) { |
71 | message.appendChild(h('span', 're: ', tools.messageLink(msg.value.content.root))) |
72 | opts.root = msg.value.content.root |
73 | } else { opts.root = msg.key } |
74 | |
75 | |
76 | pull( |
77 | sbot.query({query: [{$filter: {value: {content: {type: 'update', updated: msg.key}}}}]}), |
78 | pull.drain(function (data) { |
79 | console.log(data) |
80 | var latest = h('div.message__body', tools.markdown(data.value.content.text), h('span.timestamp', 'Edited: ' + human(new Date(data.value.timestamp)))) |
81 | var num = message.childNodes.length |
82 | var act = num - 2 |
83 | |
84 | message.replaceChild(latest, message.childNodes[act]) |
85 | }) |
86 | |
87 | ) |
88 | |
89 | |
90 | message.appendChild(h('button.btn', 'Reply', { |
91 | onclick: function () { |
92 | var compose = composer(opts) |
93 | message.replaceChild(compose, message.lastElementChild) |
94 | } |
95 | })) |
96 | } |
97 | return message |
98 | } else if (msg.value.content.type == 'vote') { |
99 | message.appendChild(tools.header(msg)) |
100 | var embed = msg.value.content.vote.link |
101 | |
102 | var embedded = h('div.embedded') |
103 | sbot.get(embed, function (err, msg) { |
104 | if (err) {console.log('could not find message locally, try ooo?') } |
105 | msg.value = msg |
106 | msg.key = embed |
107 | if (msg.value.content.text) { |
108 | message.appendChild(embedded) |
109 | embedded.appendChild(tools.header(msg)) |
110 | embedded.appendChild( |
111 | h('div.message__body', |
112 | tools.markdown(msg.value.content.text.substring(0, 256) + '...'), |
113 | h('span', '[', h('a', {href: '#' + msg.key}, 'Full Post'), ']') |
114 | ) |
115 | ) |
116 | } |
117 | }) |
118 | return message |
119 | } else { |
120 | //message.appendChild(tools.header(msg)) |
121 | //message.appendChild(h('pre', tools.rawJSON(msg.value.content))) |
122 | //return message |
123 | return |
124 | } |
125 | } |
126 |
Built with git-ssb-web