Files: 423e77eca80692c36556b549a46bcb6c572c3dbb / render.js
9624 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 | var ssbAvatar = require('ssb-avatar') |
13 | |
14 | var ssbKeys = require('ssb-keys') |
15 | var keys = require('./keys') |
16 | |
17 | var diff = require('diff') |
18 | |
19 | function hash () { |
20 | return window.location.hash.substring(1) |
21 | } |
22 | |
23 | module.exports.render = function (msg) { |
24 | |
25 | |
26 | var messageExists = (document.getElementById(msg.key.substring(0, 44)) !== null); |
27 | |
28 | if (!messageExists) { |
29 | var message = h('div.message#' + msg.key.substring(0, 44)) |
30 | //console.log(msg) |
31 | var messageContent = h('div.messageContent') |
32 | message.appendChild(messageContent) |
33 | |
34 | pull( |
35 | sbot.backlinks({ |
36 | query: [{$filter: {dest: msg.key}}], |
37 | live: true, |
38 | reverse: false, |
39 | index: 'DTA' |
40 | }), |
41 | pull.drain(function (data) { |
42 | if (data.sync) { |
43 | } else { |
44 | var subMessageExists = (document.getElementById(data.key.substring(0, 44)) !== null) |
45 | |
46 | if (!subMessageExists) { |
47 | //console.log(data) |
48 | if (data.value.content.type == 'vote') { |
49 | if (msg.key == data.value.content.vote.link) { |
50 | console.log(data) |
51 | if (data.value.content.vote.value == 1) |
52 | var link = h('span', ' ', h('img.emoji', {src: config.emojiUrl + 'star.png'}), ' ', h('a', {href: '#' + data.value.content.vote.link}, tools.messageLink(data.value.content.vote.link))) |
53 | if (data.value.author == id) { |
54 | var gotIt = document.getElementById('votebutton:' + msg.key.substring(0,44)) |
55 | if (gotIt != null) { |
56 | gotIt.parentNode.appendChild(tools.unstar(msg)) |
57 | gotIt.removeChild(gotIt.firstChild) |
58 | //gotIt.outerHTML = '' |
59 | } |
60 | } |
61 | else if (data.value.content.vote.value == -1) |
62 | var link = h('span', ' ', h('img.emoji', {src: config.emojiUrl + 'stars.png'}), ' ', h('a', {href: '#' + data.value.content.vote.link}, tools.messageLink(data.value.content.vote.link))) |
63 | message.appendChild(h('div.submessage', h('div.messageContent', tools.mini(data, link)))) |
64 | } else { |
65 | //console.log('did not render vote') |
66 | } |
67 | } else if (data.value.content.type == 'post') { |
68 | if ( |
69 | (msg.key == data.value.content.root) || |
70 | (msg.value.content.root == data.value.content.root) |
71 | ) { |
72 | message.appendChild(h('div.submessage', (exports.render(data)))) |
73 | } |
74 | } else { |
75 | var link = h('span', ' backlinked from ', tools.messageLink(data.key)) |
76 | message.appendChild(h('div.submessage', h('div.messageContent', tools.mini(data, link)))) |
77 | } |
78 | } else { //console.log('message already rendered') |
79 | } |
80 | } |
81 | }) |
82 | ) |
83 | |
84 | if (msg.value.content.type == 'post') { |
85 | |
86 | messageContent.appendChild(tools.header(msg)) |
87 | |
88 | if (msg.value.content.root) { |
89 | messageContent.appendChild(h('span', 're: ', tools.messageLink(msg.value.content.root))) |
90 | } |
91 | |
92 | messageContent.appendChild(h('div.message__body', tools.markdown(msg.value.content.text))) |
93 | |
94 | var buttons = h('div.buttons') |
95 | |
96 | buttons.appendChild(tools.star(msg)) |
97 | |
98 | messageContent.appendChild(buttons) |
99 | return message |
100 | } |
101 | |
102 | if (msg.value.content.type == 'vote') { |
103 | if (msg.value.content.vote.value == 1) |
104 | 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))) |
105 | else if (msg.value.content.vote.value == -1) |
106 | 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))) |
107 | messageContent.appendChild(tools.mini(msg, link)) |
108 | return message |
109 | } |
110 | |
111 | if (msg.value.content.type == 'about') { |
112 | if (msg.value.content.image) { |
113 | var image = h('span.avatar--small', |
114 | ' identified ', |
115 | h('a', {href: '#' + msg.value.content.about}, avatar.cachedName(msg.value.content.about)), |
116 | ' as ', |
117 | h('img', {src: config.blobsUrl + msg.value.content.image.link}) |
118 | ) |
119 | messageContent.appendChild(tools.mini(msg, image)) |
120 | return message |
121 | } |
122 | if (msg.value.content.name) { |
123 | var name = h('span', |
124 | ' identified ', |
125 | h('a', {href: '#' + msg.value.content.about}, avatar.cachedName(msg.value.content.about)), |
126 | ' as ', msg.value.content.name |
127 | ) |
128 | messageContent.appendChild(tools.mini(msg, name)) |
129 | return message |
130 | } |
131 | } |
132 | |
133 | if (msg.value.content.type == 'label'){ |
134 | var content = h('span', ' labeled ', tools.messageLink(msg.value.content.link), ' as ', h('mark', h('a', {href: '#label/' + msg.value.content.label}, msg.value.content.label))) |
135 | messageContent.appendChild(tools.mini(msg, content)) |
136 | return message |
137 | } |
138 | |
139 | if (msg.value.content.type == 'queue') { |
140 | if (msg.value.content.queue == true) { |
141 | var content = h('span', ' added ', tools.messageLink(msg.value.content.message), ' to their ', h('a', {href: '#queue'}, 'queue')) |
142 | messageContent.appendChild(tools.mini(msg, content)) |
143 | } |
144 | if (msg.value.content.queue == false) { |
145 | var content = h('span', ' removed ', tools.messageLink(msg.value.content.message), ' from their ', h('a', {href: '#queue'}, 'queue')) |
146 | messageContent.appendChild(tools.mini(msg, content)) |
147 | |
148 | } |
149 | return message |
150 | } |
151 | |
152 | if (msg.value.content.type == 'scat_message') { |
153 | var src = hash() |
154 | if (src != 'backchannel') { |
155 | messageContent.appendChild(h('button.btn.right', h('a', {href: '#backchannel'}, 'Chat'))) |
156 | } |
157 | messageContent.appendChild(tools.mini(msg, ' ' + msg.value.content.text)) |
158 | return message |
159 | } |
160 | |
161 | else if (msg.value.content.type == 'contact') { |
162 | if (msg.value.content.contact) { |
163 | var contact = h('a', {href: '#' + msg.value.content.contact}, avatar.name(msg.value.content.contact)) |
164 | } else { var contact = h('p', 'no contact named')} |
165 | |
166 | if (msg.value.content.following == true) { |
167 | var following = h('span', ' follows ', contact) |
168 | messageContent.appendChild(tools.mini(msg, following)) |
169 | } |
170 | if (msg.value.content.following == false) { |
171 | var unfollowing = h('span', ' unfollows ', contact) |
172 | messageContent.appendChild(tools.mini(msg, unfollowing)) |
173 | } |
174 | if (msg.value.content.blocking == true) { |
175 | var blocking = h('span', ' blocks ', contact) |
176 | messageContent.appendChild(tools.mini(msg, blocking)) |
177 | } |
178 | if (msg.value.content.blocking == false) { |
179 | var unblocking = h('span', ' unblocks ', contact) |
180 | messageContent.appendChild(tools.mini(msg, unblocking)) |
181 | } |
182 | return message |
183 | |
184 | } |
185 | |
186 | |
187 | if (msg.value.content.type == 'git-update') { |
188 | |
189 | messageContent.appendChild(tools.header(msg)) |
190 | |
191 | var reponame = h('p', 'pushed to ', h('a', {href: '#' + msg.value.content.repo}, msg.value.content.repo)) |
192 | |
193 | var cloneurl = h('pre', 'git clone ssb://' + msg.value.content.repo) |
194 | |
195 | messageContent.appendChild(reponame) |
196 | |
197 | ssbAvatar(sbot, id, msg.value.content.repo, function (err, data) { |
198 | if (data) { |
199 | var actualname = h('p', 'pushed to ', h('a', {href: '#' + msg.value.content.repo}, '%' + data.name)) |
200 | reponame.parentNode.replaceChild(actualname, reponame) |
201 | } |
202 | }) |
203 | |
204 | messageContent.appendChild(cloneurl) |
205 | |
206 | var commits = h('ul') |
207 | if (msg.value.content.commits) { |
208 | msg.value.content.commits.map(function (commit) { |
209 | commits.appendChild(h('li', h('code', commit.sha1), ' - ', commit.title)) |
210 | }) |
211 | } |
212 | |
213 | messageContent.appendChild(commits) |
214 | |
215 | return message |
216 | } |
217 | |
218 | if (msg.value.content.type == 'git-repo') { |
219 | messageContent.appendChild(tools.header(msg)) |
220 | |
221 | var reponame = h('p', 'git-ssb repo ', h('a', {href: '#' + msg.key}, msg.key)) |
222 | |
223 | messageContent.appendChild(reponame) |
224 | |
225 | ssbAvatar(sbot, id, msg.key, function (err, data) { |
226 | if (data) |
227 | var actualname = h('p', 'git-ssb repo ', h('a', {href: '#' + msg.key}, '%' + data.name)) |
228 | reponame.parentNode.replaceChild(actualname, reponame) |
229 | }) |
230 | |
231 | var cloneurl = h('pre', 'git clone ssb://' + msg.key) |
232 | messageContent.appendChild(cloneurl) |
233 | return message |
234 | } |
235 | |
236 | if (typeof msg.value.content === 'string') { |
237 | var unboxed = ssbKeys.unbox(msg.value.content, keys) |
238 | if (unboxed) { |
239 | msg.value.content = unboxed |
240 | msg.value.private = true |
241 | return exports.render(msg) |
242 | } else { |
243 | var privateMsg = h('span', ' sent a private message.') |
244 | messageContent.appendChild(tools.mini(msg, privateMsg)) |
245 | return message |
246 | } |
247 | } |
248 | |
249 | else { |
250 | //FULL FALLBACK |
251 | //message.appendChild(tools.header(msg)) |
252 | messageContent.appendChild(tools.header(msg)) |
253 | messageContent.appendChild(h('pre', tools.rawJSON(msg.value))) |
254 | |
255 | //MINI FALLBACK |
256 | //var fallback = h('span', ' ' + msg.value.content.type) |
257 | //message.appendChild(tools.mini(msg, fallback)) |
258 | return message |
259 | } |
260 | } |
261 | } |
262 |
Built with git-ssb-web