Files: bb8598896fc1966615e1f1d1c5b6d90fad9be52e / render.js
6119 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 | |
12 | var avatar = require('./avatar') |
13 | |
14 | module.exports = function (msg) { |
15 | var message = h('div.message#' + msg.key.substring(0, 44)) |
16 | |
17 | if (!localStorage[msg.value.author]) |
18 | var cache = {mute: false} |
19 | else |
20 | var cache = JSON.parse(localStorage[msg.value.author]) |
21 | |
22 | if (cache.mute == true) { |
23 | var muted = h('span', ' muted') |
24 | message.appendChild(tools.mini(msg, muted)) |
25 | return message |
26 | } |
27 | |
28 | /*if (msg.value.private == true) { |
29 | var privateMsg = h('span', ' ', h('img.emoji', {src: config.emojiUrl + 'lock.png'}), ' ', h('button.btn', 'Open')) |
30 | message.appendChild(tools.mini(msg, privateMsg)) |
31 | return message |
32 | }*/ |
33 | else if (msg.value.content.type == 'git-update') { |
34 | |
35 | message.appendChild(tools.header(msg)) |
36 | |
37 | var reponame = h('p', 'pushed to ', h('a', {href: '#' + msg.value.content.repo}, msg.value.content.repo)) |
38 | |
39 | var cloneurl = h('pre', 'git clone ssb://' + msg.value.content.repo) |
40 | |
41 | message.appendChild(reponame) |
42 | |
43 | pull( |
44 | sbot.get(msg.value.content.repo, function (err, data) { |
45 | if (err) throw err |
46 | console.log(data) |
47 | if (data.content.name) { |
48 | actualname = h('p', 'pushed to ', h('a', {href: '#' + msg.value.content.repo}, '%' + data.content.name)) |
49 | reponame.parentNode.replaceChild(actualname, reponame) |
50 | } |
51 | }) |
52 | ) |
53 | message.appendChild(cloneurl) |
54 | //message.appendChild(h('pre', tools.rawJSON(msg.value))) |
55 | return message |
56 | |
57 | } |
58 | else if (msg.value.content.type == 'git-repo') { |
59 | message.appendChild(tools.header(msg)) |
60 | if (msg.value.content.name) { |
61 | message.appendChild(h('p', h('a', {href: msg.link}, '%' + msg.value.content.name))) |
62 | } else { |
63 | message.appendChild(h('p', h('a', {href: msg.link}, msg.link))) |
64 | } |
65 | var cloneurl = h('pre', 'git clone ssb://' + msg.key) |
66 | message.appendChild(cloneurl) |
67 | //message.appendChild(h('pre', tools.rawJSON(msg.value.content))) |
68 | return message |
69 | } |
70 | |
71 | else if (msg.value.content.type == 'post') { |
72 | var opts = { |
73 | type: 'post', |
74 | branch: msg.key |
75 | } |
76 | var fallback = {} |
77 | |
78 | |
79 | if (msg.value.content.root) |
80 | opts.root = msg.value.content.root |
81 | else |
82 | opts.root = msg.key |
83 | |
84 | message.appendChild(tools.header(msg)) |
85 | |
86 | if (msg.value.content.root) |
87 | message.appendChild(h('span', 're: ', tools.messageLink(msg.value.content.root))) |
88 | |
89 | message.appendChild(h('div.message__body', tools.markdown(msg.value.content.text))) |
90 | |
91 | pull( |
92 | sbot.query({query: [{$filter: {value: {content: {type: 'edit', original: msg.key}}}}], limit: 100, live: true}), |
93 | pull.drain(function (update) { |
94 | if (update.sync) { |
95 | //console.log('Waiting for new edits.') |
96 | } else { |
97 | var newMessage = h('div', tools.markdown(update.value.content.text)) |
98 | var latest = h('div.message__body', |
99 | tools.timestamp(update, {edited: true}), |
100 | newMessage |
101 | ) |
102 | message.replaceChild(latest, message.childNodes[message.childNodes.length - 2]) |
103 | fallback.messageText = update.value.content.text |
104 | opts.updated = update.key |
105 | opts.original = msg.key |
106 | } |
107 | }) |
108 | ) |
109 | |
110 | var buttons = h('div.buttons') |
111 | buttons.appendChild(h('button.btn', 'Reply', { |
112 | onclick: function () { |
113 | opts.type = 'post' |
114 | opts.mentions = '[' + avatar.name(msg.value.author).textContent + '](' + msg.value.author + ')' |
115 | if (msg.value.content.recps) { |
116 | opts.recps = msg.value.content.recps |
117 | } |
118 | var r = message.childNodes.length - 1 |
119 | delete opts.updated |
120 | delete opts.original |
121 | delete fallback.messageText |
122 | fallback.buttons = message.childNodes[r] |
123 | var compose = h('div.message#re:' + msg.key.substring(0, 44), composer(opts, fallback)) |
124 | message.removeChild(message.childNodes[r]) |
125 | message.parentNode.insertBefore(compose, message.nextSibling) |
126 | } |
127 | })) |
128 | |
129 | if (msg.value.author == id) |
130 | buttons.appendChild(h('button.btn', 'Edit', { |
131 | onclick: function () { |
132 | opts.type = 'edit' |
133 | if (!fallback.messageText) |
134 | fallback.messageText = msg.value.content.text |
135 | |
136 | if (!opts.updated) |
137 | opts.updated = msg.key |
138 | opts.original = msg.key |
139 | |
140 | var r = message.childNodes.length - 1 |
141 | fallback.buttons = message.childNodes[r] |
142 | message.removeChild(message.childNodes[r]) |
143 | var compose = h('div#edit:' + msg.key.substring(0, 44), composer(opts, fallback)) |
144 | message.replaceChild(compose, message.lastElementChild) |
145 | } |
146 | })) |
147 | |
148 | buttons.appendChild(tools.done(msg.key)) |
149 | |
150 | buttons.appendChild(tools.star(msg)) |
151 | message.appendChild(buttons) |
152 | return message |
153 | |
154 | } else if (msg.value.content.type == 'vote') { |
155 | if (msg.value.content.vote.value == 1) |
156 | 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))) |
157 | else if (msg.value.content.vote.value == -1) |
158 | 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))) |
159 | message.appendChild(tools.mini(msg, link)) |
160 | return message |
161 | } else if (typeof msg.value.content === 'string') { |
162 | var privateMsg = h('span', ' ', h('img.emoji', {src: config.emojiUrl + 'lock.png'})) |
163 | message.appendChild(tools.mini(msg, privateMsg)) |
164 | return message |
165 | } else { |
166 | |
167 | //FULL FALLBACK |
168 | message.appendChild(tools.header(msg)) |
169 | message.appendChild(h('pre', tools.rawJSON(msg.value.content))) |
170 | |
171 | //MINI FALLBACK |
172 | //var fallback = h('span', ' ' + msg.value.content.type) |
173 | //message.appendChild(tools.mini(msg, fallback)) |
174 | return message |
175 | } |
176 | } |
177 |
Built with git-ssb-web