Files: fb04a43925cf4be500fe4bb2161b98de71debd15 / render.js
9846 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 | var avatar = require('./avatar') |
12 | |
13 | var diff = require('diff') |
14 | |
15 | function hash () { |
16 | return window.location.hash.substring(1) |
17 | } |
18 | |
19 | module.exports = function (msg) { |
20 | var message = h('div.message#' + msg.key.substring(0, 44)) |
21 | |
22 | |
23 | if (!localStorage[msg.value.author]) |
24 | var cache = {mute: false} |
25 | else |
26 | var cache = JSON.parse(localStorage[msg.value.author]) |
27 | |
28 | if (cache.mute == true) { |
29 | var muted = h('span', ' muted') |
30 | message.appendChild(tools.mini(msg, muted)) |
31 | return message |
32 | } |
33 | else if (msg.value.content.type == 'edit') { |
34 | message.appendChild(tools.header(msg)) |
35 | var current = msg.value.content.text |
36 | sbot.get(msg.value.content.updated, function (err, updated) { |
37 | if (updated) { |
38 | fragment = document.createDocumentFragment() |
39 | var previous = updated.content.text |
40 | var ready = diff.diffWords(previous, current) |
41 | console.log(ready) |
42 | ready.forEach(function (part) { |
43 | if (part.added === true) { |
44 | color = 'cyan' |
45 | } else if (part.removed === true) { |
46 | color = 'gray' |
47 | } else {color = 'white'} |
48 | var span = h('span') |
49 | span.style.color = color |
50 | span.appendChild(document.createTextNode(part.value)) |
51 | fragment.appendChild(span) |
52 | }) |
53 | message.appendChild(h('code', fragment)) |
54 | } |
55 | }) |
56 | return message |
57 | } |
58 | |
59 | |
60 | |
61 | else if (msg.value.content.type == 'scat_message') { |
62 | var src = hash() |
63 | if (src != 'backchannel') { |
64 | message.appendChild(h('button.btn.right', h('a', {href: '#backchannel'}, 'Chat'))) |
65 | } |
66 | message.appendChild(tools.mini(msg, ' ' + msg.value.content.text)) |
67 | return message |
68 | } |
69 | else if (msg.value.content.type == 'contact') { |
70 | var contact = h('a', {href: '#' + msg.value.content.contact}, avatar.name(msg.value.content.contact)) |
71 | if (msg.value.content.following == true) { |
72 | var following = h('span', ' follows ', contact) |
73 | message.appendChild(tools.mini(msg, following)) |
74 | } |
75 | if (msg.value.content.following == false) { |
76 | var unfollowing = h('span', ' unfollows ', contact) |
77 | message.appendChild(tools.mini(msg, unfollowing)) |
78 | } |
79 | if (msg.value.content.blocking == true) { |
80 | var blocking = h('span', ' blocks ', contact) |
81 | message.appendChild(tools.mini(msg, blocking)) |
82 | } |
83 | if (msg.value.content.blocking == false) { |
84 | var unblocking = h('span', ' unblocks ', contact) |
85 | message.appendChild(tools.mini(msg, unblocking)) |
86 | } |
87 | return message |
88 | } |
89 | |
90 | else if (msg.value.content.type == 'git-update') { |
91 | |
92 | message.appendChild(tools.header(msg)) |
93 | |
94 | var reponame = h('p', 'pushed to ', h('a', {href: '#' + msg.value.content.repo}, msg.value.content.repo)) |
95 | |
96 | var cloneurl = h('pre', 'git clone ssb://' + msg.value.content.repo) |
97 | |
98 | message.appendChild(reponame) |
99 | |
100 | pull( |
101 | sbot.get(msg.value.content.repo, function (err, data) { |
102 | if (err) throw err |
103 | if (data.content.name) { |
104 | actualname = h('p', 'pushed to ', h('a', {href: '#' + msg.value.content.repo}, '%' + data.content.name)) |
105 | reponame.parentNode.replaceChild(actualname, reponame) |
106 | } |
107 | }) |
108 | ) |
109 | message.appendChild(cloneurl) |
110 | |
111 | var commits = h('ul') |
112 | if (msg.value.content.commits[0]) { |
113 | msg.value.content.commits.map(function (commit) { |
114 | commits.appendChild(h('li', h('code', commit.sha1), ' - ', commit.title)) |
115 | }) |
116 | |
117 | } |
118 | |
119 | message.appendChild(commits) |
120 | |
121 | return message |
122 | |
123 | } |
124 | else if (msg.value.content.type == 'git-repo') { |
125 | message.appendChild(tools.header(msg)) |
126 | if (msg.value.content.name) { |
127 | message.appendChild(h('p', h('a', {href: '#' + msg.key}, '%' + msg.value.content.name))) |
128 | } else { |
129 | message.appendChild(h('p', h('a', {href: '#' + msg.key}, msg.key))) |
130 | } |
131 | var cloneurl = h('pre', 'git clone ssb://' + msg.key) |
132 | message.appendChild(cloneurl) |
133 | //message.appendChild(h('pre', tools.rawJSON(msg.value.content))) |
134 | return message |
135 | } |
136 | |
137 | else if (msg.value.content.type == 'wiki') { |
138 | var fallback = {} |
139 | |
140 | var opts = { |
141 | type: 'wiki', |
142 | branch: msg.key |
143 | } |
144 | |
145 | if (msg.value.content.root) |
146 | opts.root = msg.value.content.root |
147 | else |
148 | opts.root = msg.key |
149 | |
150 | message.appendChild(tools.header(msg)) |
151 | |
152 | message.appendChild(h('div.message__body', tools.markdown(msg.value.content.text))) |
153 | |
154 | pull( |
155 | sbot.query({query: [{$filter: {value: {content: {type: 'edit', original: msg.key}}}}], limit: 100, live: true}), |
156 | pull.drain(function (update) { |
157 | if (update.sync) { |
158 | } else { |
159 | var newMessage = h('div', tools.markdown(update.value.content.text)) |
160 | var latest = h('div.message__body', |
161 | tools.timestamp(update, {edited: true}), |
162 | newMessage |
163 | ) |
164 | message.replaceChild(latest, message.childNodes[message.childNodes.length - 2]) |
165 | fallback.messageText = update.value.content.text |
166 | opts.updated = update.key |
167 | opts.original = msg.key |
168 | } |
169 | }) |
170 | ) |
171 | |
172 | var buttons = h('div.buttons') |
173 | |
174 | buttons.appendChild(h('button.btn', 'Edit wiki', { |
175 | onclick: function () { |
176 | opts.type = 'edit' |
177 | if (!fallback.messageText) |
178 | fallback.messageText = msg.value.content.text |
179 | |
180 | if (!opts.updated) |
181 | opts.updated = msg.key |
182 | opts.original = msg.key |
183 | |
184 | var r = message.childNodes.length - 1 |
185 | fallback.buttons = message.childNodes[r] |
186 | message.removeChild(message.childNodes[r]) |
187 | var compose = h('div#edit:' + msg.key.substring(0, 44), composer(opts, fallback)) |
188 | message.replaceChild(compose, message.lastElementChild) |
189 | } |
190 | })) |
191 | |
192 | buttons.appendChild(tools.star(msg)) |
193 | message.appendChild(buttons) |
194 | return message |
195 | |
196 | } else if (msg.value.content.type == 'post') { |
197 | var opts = { |
198 | type: 'post', |
199 | branch: msg.key |
200 | } |
201 | var fallback = {} |
202 | |
203 | |
204 | if (msg.value.content.root) |
205 | opts.root = msg.value.content.root |
206 | else |
207 | opts.root = msg.key |
208 | |
209 | message.appendChild(tools.header(msg)) |
210 | |
211 | if (msg.value.content.root) |
212 | message.appendChild(h('span', 're: ', tools.messageLink(msg.value.content.root))) |
213 | |
214 | message.appendChild(h('div.message__body', tools.markdown(msg.value.content.text))) |
215 | |
216 | pull( |
217 | sbot.query({query: [{$filter: {value: {content: {type: 'edit', original: msg.key}}}}], limit: 100, live: true}), |
218 | pull.drain(function (update) { |
219 | if (update.sync) { |
220 | } else { |
221 | var newMessage = h('div', tools.markdown(update.value.content.text)) |
222 | var latest = h('div.message__body', |
223 | tools.timestamp(update, {edited: true}), |
224 | newMessage |
225 | ) |
226 | message.replaceChild(latest, message.childNodes[message.childNodes.length - 2]) |
227 | fallback.messageText = update.value.content.text |
228 | opts.updated = update.key |
229 | opts.original = msg.key |
230 | } |
231 | }) |
232 | ) |
233 | |
234 | var name = avatar.name(msg.value.author) |
235 | |
236 | var buttons = h('div.buttons') |
237 | buttons.appendChild(h('button.btn', 'Reply', { |
238 | onclick: function () { |
239 | opts.type = 'post' |
240 | opts.mentions = '[' + name.textContent + '](' + msg.value.author + ')' |
241 | if (msg.value.content.recps) { |
242 | opts.recps = msg.value.content.recps |
243 | } |
244 | var r = message.childNodes.length - 1 |
245 | delete opts.updated |
246 | delete opts.original |
247 | delete fallback.messageText |
248 | fallback.buttons = message.childNodes[r] |
249 | var compose = h('div.message#re:' + msg.key.substring(0, 44), composer(opts, fallback)) |
250 | message.removeChild(message.childNodes[r]) |
251 | message.parentNode.insertBefore(compose, message.nextSibling) |
252 | } |
253 | })) |
254 | |
255 | if (msg.value.author == id) |
256 | buttons.appendChild(h('button.btn', 'Edit', { |
257 | onclick: function () { |
258 | opts.type = 'edit' |
259 | if (!fallback.messageText) |
260 | fallback.messageText = msg.value.content.text |
261 | |
262 | if (!opts.updated) |
263 | opts.updated = msg.key |
264 | opts.original = msg.key |
265 | |
266 | var r = message.childNodes.length - 1 |
267 | fallback.buttons = message.childNodes[r] |
268 | message.removeChild(message.childNodes[r]) |
269 | var compose = h('div#edit:' + msg.key.substring(0, 44), composer(opts, fallback)) |
270 | message.replaceChild(compose, message.lastElementChild) |
271 | } |
272 | })) |
273 | |
274 | buttons.appendChild(tools.star(msg)) |
275 | message.appendChild(buttons) |
276 | return message |
277 | |
278 | } else if (msg.value.content.type == 'vote') { |
279 | if (msg.value.content.vote.value == 1) |
280 | var link = h('span', ' ', h('img.emoji', {src: config.emojiUrl + 'star.png'}), ' ', h('a', {href: '#' + msg.value.content.vote.link}, tools.messageLink(msg.value.content.vote.link))) |
281 | else if (msg.value.content.vote.value == -1) |
282 | var link = h('span', ' ', h('img.emoji', {src: config.emojiUrl + 'stars.png'}), ' ', h('a', {href: '#' + msg.value.content.vote.link}, tools.messageLink(msg.value.content.vote.link))) |
283 | message.appendChild(tools.mini(msg, link)) |
284 | return message |
285 | } else if (typeof msg.value.content === 'string') { |
286 | var privateMsg = h('span', ' sent a private message.') |
287 | message.appendChild(tools.mini(msg, privateMsg)) |
288 | return message |
289 | } else { |
290 | |
291 | //FULL FALLBACK |
292 | message.appendChild(tools.header(msg)) |
293 | message.appendChild(h('pre', tools.rawJSON(msg.value.content))) |
294 | |
295 | //MINI FALLBACK |
296 | //var fallback = h('span', ' ' + msg.value.content.type) |
297 | //message.appendChild(tools.mini(msg, fallback)) |
298 | return message |
299 | } |
300 | } |
301 |
Built with git-ssb-web