git ssb

1+

punkmonk.termux / mvd



forked from ev / mvd

Tree: fe195d78c8c4fadba1362945cf2e75864ca3dcad

Files: fe195d78c8c4fadba1362945cf2e75864ca3dcad / render.js

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

Built with git-ssb-web