Files: 29297eb81e25e8f6bae7fa6b62b38f6c07b7d6fe / render.js
8738 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 | //message.appendChild(h('pre', tools.rawJSON(msg.value))) |
80 | return message |
81 | |
82 | } |
83 | else if (msg.value.content.type == 'git-repo') { |
84 | message.appendChild(tools.header(msg)) |
85 | if (msg.value.content.name) { |
86 | message.appendChild(h('p', h('a', {href: msg.link}, '%' + msg.value.content.name))) |
87 | } else { |
88 | message.appendChild(h('p', h('a', {href: msg.link}, msg.link))) |
89 | } |
90 | var cloneurl = h('pre', 'git clone ssb://' + msg.key) |
91 | message.appendChild(cloneurl) |
92 | //message.appendChild(h('pre', tools.rawJSON(msg.value.content))) |
93 | return message |
94 | } |
95 | |
96 | else if (msg.value.content.type == 'wiki') { |
97 | var fallback = {} |
98 | |
99 | var opts = { |
100 | type: 'wiki', |
101 | branch: msg.key |
102 | } |
103 | |
104 | if (msg.value.content.root) |
105 | opts.root = msg.value.content.root |
106 | else |
107 | opts.root = msg.key |
108 | |
109 | message.appendChild(tools.header(msg)) |
110 | |
111 | message.appendChild(h('div.message__body', tools.markdown(msg.value.content.text))) |
112 | |
113 | pull( |
114 | sbot.query({query: [{$filter: {value: {content: {type: 'edit', original: msg.key}}}}], limit: 100, live: true}), |
115 | pull.drain(function (update) { |
116 | if (update.sync) { |
117 | } else { |
118 | var newMessage = h('div', tools.markdown(update.value.content.text)) |
119 | var latest = h('div.message__body', |
120 | tools.timestamp(update, {edited: true}), |
121 | newMessage |
122 | ) |
123 | message.replaceChild(latest, message.childNodes[message.childNodes.length - 2]) |
124 | fallback.messageText = update.value.content.text |
125 | opts.updated = update.key |
126 | opts.original = msg.key |
127 | } |
128 | }) |
129 | ) |
130 | |
131 | var buttons = h('div.buttons') |
132 | |
133 | buttons.appendChild(h('button.btn', 'Edit wiki', { |
134 | onclick: function () { |
135 | opts.type = 'edit' |
136 | if (!fallback.messageText) |
137 | fallback.messageText = msg.value.content.text |
138 | |
139 | if (!opts.updated) |
140 | opts.updated = msg.key |
141 | opts.original = msg.key |
142 | |
143 | var r = message.childNodes.length - 1 |
144 | fallback.buttons = message.childNodes[r] |
145 | message.removeChild(message.childNodes[r]) |
146 | var compose = h('div#edit:' + msg.key.substring(0, 44), composer(opts, fallback)) |
147 | message.replaceChild(compose, message.lastElementChild) |
148 | } |
149 | })) |
150 | |
151 | buttons.appendChild(tools.star(msg)) |
152 | message.appendChild(buttons) |
153 | return message |
154 | |
155 | } else if (msg.value.content.type == 'post') { |
156 | var opts = { |
157 | type: 'post', |
158 | branch: msg.key |
159 | } |
160 | var fallback = {} |
161 | |
162 | |
163 | if (msg.value.content.root) |
164 | opts.root = msg.value.content.root |
165 | else |
166 | opts.root = msg.key |
167 | |
168 | message.appendChild(tools.header(msg)) |
169 | |
170 | if (msg.value.content.root) |
171 | message.appendChild(h('span', 're: ', tools.messageLink(msg.value.content.root))) |
172 | |
173 | message.appendChild(h('div.message__body', tools.markdown(msg.value.content.text))) |
174 | |
175 | pull( |
176 | sbot.query({query: [{$filter: {value: {content: {type: 'edit', original: msg.key}}}}], limit: 100, live: true}), |
177 | pull.drain(function (update) { |
178 | if (update.sync) { |
179 | } else { |
180 | var newMessage = h('div', tools.markdown(update.value.content.text)) |
181 | var latest = h('div.message__body', |
182 | tools.timestamp(update, {edited: true}), |
183 | newMessage |
184 | ) |
185 | message.replaceChild(latest, message.childNodes[message.childNodes.length - 2]) |
186 | fallback.messageText = update.value.content.text |
187 | opts.updated = update.key |
188 | opts.original = msg.key |
189 | } |
190 | }) |
191 | ) |
192 | |
193 | var name = avatar.name(msg.value.author) |
194 | |
195 | var buttons = h('div.buttons') |
196 | buttons.appendChild(h('button.btn', 'Reply', { |
197 | onclick: function () { |
198 | opts.type = 'post' |
199 | opts.mentions = '[' + name.textContent + '](' + msg.value.author + ')' |
200 | if (msg.value.content.recps) { |
201 | opts.recps = msg.value.content.recps |
202 | } |
203 | var r = message.childNodes.length - 1 |
204 | delete opts.updated |
205 | delete opts.original |
206 | delete fallback.messageText |
207 | fallback.buttons = message.childNodes[r] |
208 | var compose = h('div.message#re:' + msg.key.substring(0, 44), composer(opts, fallback)) |
209 | message.removeChild(message.childNodes[r]) |
210 | message.parentNode.insertBefore(compose, message.nextSibling) |
211 | } |
212 | })) |
213 | |
214 | if (msg.value.author == id) |
215 | buttons.appendChild(h('button.btn', 'Edit', { |
216 | onclick: function () { |
217 | opts.type = 'edit' |
218 | if (!fallback.messageText) |
219 | fallback.messageText = msg.value.content.text |
220 | |
221 | if (!opts.updated) |
222 | opts.updated = msg.key |
223 | opts.original = msg.key |
224 | |
225 | var r = message.childNodes.length - 1 |
226 | fallback.buttons = message.childNodes[r] |
227 | message.removeChild(message.childNodes[r]) |
228 | var compose = h('div#edit:' + msg.key.substring(0, 44), composer(opts, fallback)) |
229 | message.replaceChild(compose, message.lastElementChild) |
230 | } |
231 | })) |
232 | |
233 | buttons.appendChild(tools.star(msg)) |
234 | message.appendChild(buttons) |
235 | return message |
236 | |
237 | } else if (msg.value.content.type == 'vote') { |
238 | if (msg.value.content.vote.value == 1) |
239 | 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))) |
240 | else if (msg.value.content.vote.value == -1) |
241 | 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))) |
242 | message.appendChild(tools.mini(msg, link)) |
243 | return message |
244 | } else if (typeof msg.value.content === 'string') { |
245 | var privateMsg = h('span', ' sent a private message.') |
246 | message.appendChild(tools.mini(msg, privateMsg)) |
247 | return message |
248 | } else { |
249 | |
250 | //FULL FALLBACK |
251 | message.appendChild(tools.header(msg)) |
252 | message.appendChild(h('pre', tools.rawJSON(msg.value.content))) |
253 | |
254 | //MINI FALLBACK |
255 | //var fallback = h('span', ' ' + msg.value.content.type) |
256 | //message.appendChild(tools.mini(msg, fallback)) |
257 | return message |
258 | } |
259 | } |
260 |
Built with git-ssb-web