git ssb

2+

ev / mvd



Tree: 6d20a46e7abaa94845f87c72e3d6c8a135b098a2

Files: 6d20a46e7abaa94845f87c72e3d6c8a135b098a2 / render.js

4219 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
11
12
13module.exports = function (msg) {
14 var message = h('div.message#' + msg.key.substring(0, 44))
15
16 if (localStorage['mute:' + msg.value.author]) {
17 var muted = h('span', ' muted')
18 message.appendChild(tools.mini(msg, muted))
19 return message
20 }
21
22 if (msg.value.content.type == 'post') {
23 var opts = {
24 type: 'post',
25 branch: msg.key
26 }
27 var fallback = {}
28
29
30 if (msg.value.content.root)
31 opts.root = msg.value.content.root
32 else
33 opts.root = msg.key
34
35 message.appendChild(tools.header(msg))
36
37 if (msg.value.content.root)
38 message.appendChild(h('span', 're: ', tools.messageLink(msg.value.content.root)))
39
40 message.appendChild(h('div.message__body', tools.markdown(msg.value.content.text)))
41
42
43 pull(
44 sbot.query({query: [{$filter: {value: {content: {type: 'edit', original: msg.key}}}}], limit: 100, live: true}),
45 pull.drain(function (update) {
46 if (update.sync) {
47 //console.log('Waiting for new edits.')
48 } else {
49 var newMessage = h('div', tools.markdown(update.value.content.text))
50 var latest = h('div.message__body',
51 tools.timestamp(update, {edited: true}),
52 newMessage
53 )
54 message.replaceChild(latest, message.childNodes[message.childNodes.length - 2])
55 fallback.messageText = update.value.content.text
56 opts.updated = update.key
57 opts.original = msg.key
58 }
59 })
60 )
61
62 var buttons = h('div.buttons')
63 buttons.appendChild(h('button.btn', 'Reply', {
64 onclick: function () {
65 opts.type = 'post'
66 var r = message.childNodes.length - 1
67 delete opts.updated
68 delete opts.original
69 delete fallback.messageText
70 fallback.buttons = message.childNodes[r]
71 var compose = h('div.message#re:' + msg.key.substring(0, 44), composer(opts, fallback))
72 message.removeChild(message.childNodes[r])
73 message.parentNode.insertBefore(compose, message.nextSibling)
74 }
75 }))
76
77 if (msg.value.author == id)
78 buttons.appendChild(h('button.btn', 'Edit', {
79 onclick: function () {
80 opts.type = 'edit'
81 if (!fallback.messageText)
82 fallback.messageText = msg.value.content.text
83
84 if (!opts.updated)
85 opts.updated = msg.key
86 opts.original = msg.key
87
88 var r = message.childNodes.length - 1
89 fallback.buttons = message.childNodes[r]
90 message.removeChild(message.childNodes[r])
91 var compose = h('div#edit:' + msg.key.substring(0, 44), composer(opts, fallback))
92 message.replaceChild(compose, message.lastElementChild)
93 }
94 }))
95
96 buttons.appendChild(tools.star(msg))
97 message.appendChild(buttons)
98 return message
99
100 } else if (msg.value.content.type == 'vote') {
101 if (msg.value.content.vote.value == 1)
102 var link = h('span', ' ', h('img.emoji', {src: config.emojiUrl + 'star.png'}), ' ', h('a', {href: '#' + msg.value.content.vote.link}, msg.value.content.vote.link.substring(0,16) + '...'))
103 else if (msg.value.content.vote.value == -1)
104 var link = h('span', ' ', h('img.emoji', {src: config.emojiUrl + 'stars.png'}), ' ', h('a', {href: '#' + msg.value.content.vote.link}, msg.value.content.vote.link.substring(0,16) + '...'))
105 message.appendChild(tools.mini(msg, link))
106 return message
107 } else if (typeof msg.value.content === 'string') {
108 var privateMsg = h('span', ' ', h('img.emoji', {src: config.emojiUrl + 'lock.png'}))
109 message.appendChild(tools.mini(msg, privateMsg))
110 return message
111 } else {
112
113 //FULL FALLBACK
114 //message.appendChild(tools.header(msg))
115 //message.appendChild(h('pre', tools.rawJSON(msg.value.content)))
116 //return message
117
118 //MINI FALLBACK
119 var fallback = h('span', ' ' + msg.value.content.type)
120 message.appendChild(tools.mini(msg, fallback))
121 return message
122 }
123}
124

Built with git-ssb-web