Files: 7d6683ba58e6e89f69b2908cd003f825f4d05dd9 / tools.js
10524 bytesRaw
1 | var h = require('hyperscript') |
2 | var human = require('human-time') |
3 | var avatar = require('./avatar') |
4 | var ref = require('ssb-ref') |
5 | |
6 | var ssbKeys = require('ssb-keys') |
7 | |
8 | var pull = require('pull-stream') |
9 | |
10 | var sbot = require('./scuttlebot') |
11 | |
12 | var config = require('./config')() |
13 | |
14 | var id = require('./keys').id |
15 | |
16 | |
17 | module.exports.getBlocks = function (src) { |
18 | var blocks = h('div.blocks', 'Blocking: ') |
19 | |
20 | pull( |
21 | sbot.query({query: [{$filter: { value: { author: src, content: {type: 'contact'}}}}], live: true}), |
22 | pull.drain(function (msg) { |
23 | if (msg.value) { |
24 | if (msg.value.content.blocking == true) { |
25 | console.log(msg.value) |
26 | var gotIt = document.getElementById('blocks:' + msg.value.content.contact.substring(0, 44)) |
27 | if (gotIt == null) { |
28 | blocks.appendChild(h('a#blocks:'+ msg.value.content.contact.substring(0, 44), {title: avatar.cachedName(msg.value.content.contact).textContent, href: '#' + msg.value.content.contact}, h('span.avatar--small', avatar.cachedImage(msg.value.content.contact)))) |
29 | } |
30 | } |
31 | if (msg.value.content.blocking == false) { |
32 | var gotIt = document.getElementById('blocks:' + msg.value.content.contact.substring(0, 44)) |
33 | if (gotIt != null) { |
34 | gotIt.outerHTML = '' |
35 | } |
36 | } |
37 | } |
38 | }) |
39 | ) |
40 | |
41 | return blocks |
42 | |
43 | } |
44 | |
45 | module.exports.getBlocked = function (src) { |
46 | var blocked = h('div.blocked', 'Blocked by: ') |
47 | |
48 | pull( |
49 | sbot.query({query: [{$filter: { value: { content: {type: 'contact', contact: src}}}}], live: true}), |
50 | pull.drain(function (msg) { |
51 | if (msg.value) { |
52 | if (msg.value.content.blocking == true) { |
53 | console.log(msg.value) |
54 | var gotIt = document.getElementById('blocked:' + msg.value.content.contact.substring(0, 44)) |
55 | if (gotIt == null) { |
56 | blocked.appendChild(h('a#blocked:'+ msg.value.author.substring(0, 44), {title: avatar.cachedName(msg.value.author).textContent, href: '#' + msg.value.author}, h('span.avatar--small', avatar.cachedImage(msg.value.author)))) |
57 | } |
58 | } |
59 | if (msg.value.content.blocking == false) { |
60 | var gotIt = document.getElementById('blocked:' + msg.value.author.substring(0, 44)) |
61 | if (gotIt != null) { |
62 | gotIt.outerHTML = '' |
63 | } |
64 | } |
65 | } |
66 | }) |
67 | ) |
68 | |
69 | return blocked |
70 | |
71 | } |
72 | |
73 | module.exports.getFollowing = function (src) { |
74 | var followingCount = 0 |
75 | |
76 | var following = h('div.following', 'Following: ') |
77 | |
78 | following.appendChild(h('span#followingcount', '0')) |
79 | following.appendChild(h('br')) |
80 | |
81 | pull( |
82 | sbot.query({query: [{$filter: { value: { author: src, content: {type: 'contact'}}}}], live: true}), |
83 | pull.drain(function (msg) { |
84 | if (msg.value) { |
85 | if (msg.value.content.following == true) { |
86 | followingcount = document.getElementById('followingcount') |
87 | followingCount++ |
88 | followingcount.textContent = followingCount |
89 | var gotIt = document.getElementById('following:' + msg.value.content.contact.substring(0, 44)) |
90 | if (gotIt == null) { |
91 | following.appendChild(h('a#following:'+ msg.value.content.contact.substring(0, 44), {title: avatar.cachedName(msg.value.content.contact).textContent, href: '#' + msg.value.content.contact}, h('span.avatar--small', avatar.cachedImage(msg.value.content.contact)))) |
92 | } |
93 | } |
94 | if (msg.value.content.following == false) { |
95 | followingcount = document.getElementById('followingcount') |
96 | followingCount-- |
97 | followingcount.textContent = followingCount |
98 | var gotIt = document.getElementById('following:' + msg.value.content.contact.substring(0, 44)) |
99 | if (gotIt != null) { |
100 | gotIt.outerHTML = '' |
101 | } |
102 | } |
103 | } |
104 | }) |
105 | ) |
106 | return following |
107 | } |
108 | |
109 | module.exports.getFollowers = function (src) { |
110 | var followerCount = 0 |
111 | |
112 | var followers = h('div.followers', 'Followers: ') |
113 | |
114 | followers.appendChild(h('span#followercount', '0')) |
115 | followers.appendChild(h('br')) |
116 | |
117 | pull( |
118 | sbot.query({query: [{$filter: { value: { content: {type: 'contact', contact: src}}}}], live: true}), |
119 | pull.drain(function (msg) { |
120 | if (msg.value) { |
121 | if (msg.value.content.following == true) { |
122 | followcount = document.getElementById('followercount') |
123 | followerCount++ |
124 | followcount.textContent = followerCount |
125 | var gotIt = document.getElementById('followers:' + msg.value.author.substring(0, 44)) |
126 | if (gotIt == null) { |
127 | followers.appendChild(h('a#followers:'+ msg.value.author.substring(0, 44), {title: avatar.cachedName(msg.value.author).textContent, href: '#' + msg.value.author}, h('span.avatar--small', avatar.cachedImage(msg.value.author)))) |
128 | } |
129 | } |
130 | if (msg.value.content.following == false) { |
131 | followcount = document.getElementById('followercount') |
132 | followerCount-- |
133 | followcount.textContent = followerCount |
134 | var gotIt = document.getElementById('followers:' + msg.value.author.substring(0, 44)) |
135 | if (gotIt != null) { |
136 | gotIt.outerHTML = '' |
137 | } |
138 | } |
139 | } |
140 | }) |
141 | ) |
142 | |
143 | return followers |
144 | } |
145 | |
146 | module.exports.queueButton = function (src) { |
147 | var queueButton = h('span.queue:' + src.key.substring(0,44)) |
148 | |
149 | var addToQueue = h('button.btn.right', 'Queue', { |
150 | onclick: function () { |
151 | var content = { |
152 | type: 'queue', |
153 | message: src.key, |
154 | queue: true |
155 | } |
156 | sbot.publish(content, function (err, publish) { |
157 | if (err) throw err |
158 | console.log(publish) |
159 | }) |
160 | } |
161 | }) |
162 | |
163 | var removeFromQueue = h('button.btn.right#', 'Done', { |
164 | onclick: function () { |
165 | var content = { |
166 | type: 'queue', |
167 | message: src.key, |
168 | queue: false |
169 | } |
170 | sbot.publish(content, function (err, publish) { |
171 | if (err) throw err |
172 | console.log(publish) |
173 | if (window.location.hash.substring(1) == 'queue') { |
174 | setTimeout(function () { |
175 | var gotIt = document.getElementById(src.key.substring(0,44)) |
176 | if (gotIt != null) { |
177 | gotIt.outerHTML = '' |
178 | } |
179 | }, 100) |
180 | |
181 | } |
182 | }) |
183 | } |
184 | }) |
185 | |
186 | pull( |
187 | sbot.query({query: [{$filter: { value: { author: id, content: {type: 'queue', message: src.key}}}}], live: true}), |
188 | pull.drain(function (msg) { |
189 | if (msg.value) { |
190 | if (msg.value.content.queue == true) { |
191 | queueButton.replaceChild(removeFromQueue, addToQueue) |
192 | } |
193 | if (msg.value.content.queue == false) { |
194 | queueButton.replaceChild(addToQueue, removeFromQueue) |
195 | } |
196 | } |
197 | }) |
198 | ) |
199 | |
200 | queueButton.appendChild(addToQueue) |
201 | |
202 | return queueButton |
203 | } |
204 | |
205 | module.exports.follow = function (src) { |
206 | var button = h('span.button') |
207 | |
208 | var followButton = h('button.btn', 'Follow ', avatar.name(src), { |
209 | onclick: function () { |
210 | var content = { |
211 | type: 'contact', |
212 | contact: src, |
213 | following: true |
214 | } |
215 | sbot.publish(content, function (err, publish) { |
216 | if (err) throw err |
217 | console.log(publish) |
218 | }) |
219 | } |
220 | }) |
221 | |
222 | var unfollowButton = h('button.btn', 'Unfollow ', avatar.name(src), { |
223 | onclick: function () { |
224 | var content = { |
225 | type: 'contact', |
226 | contact: src, |
227 | following: false |
228 | } |
229 | sbot.publish(content, function (err, publish) { |
230 | if (err) throw err |
231 | console.log(publish) |
232 | }) |
233 | } |
234 | }) |
235 | |
236 | pull( |
237 | sbot.query({query: [{$filter: { value: { author: id, content: {type: 'contact', contact: src}}}}], live: true}), |
238 | pull.drain(function (msg) { |
239 | if (msg.value) { |
240 | if (msg.value.content.following == true) { |
241 | button.removeChild(button.firstChild) |
242 | button.appendChild(unfollowButton) |
243 | } |
244 | if (msg.value.content.following == false) { |
245 | button.removeChild(button.firstChild) |
246 | button.appendChild(followButton) |
247 | } |
248 | } |
249 | }) |
250 | ) |
251 | |
252 | button.appendChild(followButton) |
253 | |
254 | return button |
255 | } |
256 | |
257 | module.exports.box = function (content) { |
258 | return ssbKeys.box(content, content.recps.map(function (e) { |
259 | return ref.isFeed(e) ? e : e.link |
260 | })) |
261 | } |
262 | |
263 | module.exports.publish = function (content, cb) { |
264 | if(content.recps) |
265 | content = exports.box(content) |
266 | sbot.publish(content, function (err, msg) { |
267 | if(err) throw err |
268 | console.log('Published!', msg) |
269 | if(cb) cb(err, msg) |
270 | }) |
271 | } |
272 | |
273 | module.exports.timestamp = function (msg, edited) { |
274 | return h('span.timestamp', h('a', {href: '#' + msg.key}, human(new Date(msg.value.timestamp)))) |
275 | } |
276 | |
277 | |
278 | module.exports.mini = function (msg, content) { |
279 | var mini = h('div.mini') |
280 | |
281 | mini.appendChild( |
282 | h('span.avatar', |
283 | h('a', {href: '#' + msg.value.author}, |
284 | h('span.avatar--small', avatar.cachedImage(msg.value.author)), |
285 | avatar.cachedName(msg.value.author) |
286 | ) |
287 | ) |
288 | ) |
289 | var lock = h('span.right', h('img.emoji', {src: config.emojiUrl + 'lock.png'})) |
290 | |
291 | |
292 | mini.appendChild(exports.timestamp(msg)) |
293 | mini.appendChild(h('span', content)) |
294 | |
295 | if (msg.value.content.recps) { |
296 | mini.appendChild(lock) |
297 | } |
298 | |
299 | if (typeof msg.value.content === 'string') { |
300 | mini.appendChild(lock) |
301 | } |
302 | |
303 | return mini |
304 | } |
305 | |
306 | module.exports.messageName = function (id, cb) { |
307 | // gets the first few characters of a message, for message-link |
308 | function title (s) { |
309 | var m = /^\n*([^\n]{0,40})/.exec(s) |
310 | return m && (m[1].length == 40 ? m[1]+'...' : m[1]) |
311 | } |
312 | |
313 | sbot.get(id, function (err, value) { |
314 | if(err && err.name == 'NotFoundError') |
315 | return cb(null, id.substring(0, 10)+'...(missing)') |
316 | if(value.content.type === 'post' && 'string' === typeof value.content.text) |
317 | return cb(null, title(value.content.text)) |
318 | else if('string' === typeof value.content.text) |
319 | return cb(null, value.content.type + ':'+title(value.content.text)) |
320 | else |
321 | return cb(null, id.substring(0, 10)+'...') |
322 | }) |
323 | } |
324 | |
325 | var messageName = exports.messageName |
326 | |
327 | module.exports.messageLink = function (id) { |
328 | if (ref.isMsg(id)) { |
329 | var link = h('a', {href: '#'+id}, id.substring(0, 10)+'...') |
330 | messageName(id, function (err, name) { |
331 | if(err) console.error(err) |
332 | else link.textContent = name |
333 | }) |
334 | } else { |
335 | var link = id |
336 | } |
337 | return link |
338 | } |
339 | |
340 | module.exports.rawJSON = function (obj) { |
341 | return JSON.stringify(obj, null, 2) |
342 | .split(/([%@&][a-zA-Z0-9\/\+]{43}=*\.[\w]+)/) |
343 | .map(function (e) { |
344 | if(ref.isMsg(e) || ref.isFeed(e) || ref.isBlob(e)) { |
345 | return h('a', {href: '#' + e}, e) |
346 | } |
347 | return e |
348 | }) |
349 | } |
350 | |
351 |
Built with git-ssb-web