git ssb

2+

ev / mvd



Tree: 72ed4d5b4515885d10fdbd05dacc6a3197f2fd72

Files: 72ed4d5b4515885d10fdbd05dacc6a3197f2fd72 / render.js

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

Built with git-ssb-web