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