Commit 134d423c26820a78c6bbfbc506df577e0673b6b2
update everything
Dominic Tarr committed on 5/12/2016, 9:16:22 AMParent: 1b75f8ffcfadbde6d14a55155a3f436a7e485d94
Files changed
index.js | changed |
modules/avatar.js | changed |
modules/like.js | changed |
modules/message.js | changed |
modules/names.js | changed |
modules/post.js | changed |
modules/timestamp.js | changed |
modules/feed.js | added |
modules/main.js | added |
modules/markdown.js | added |
modules/message-link.js | added |
modules/message-name.js | added |
modules/tabs.js | added |
modules/thread.js | added |
ui.js | added |
index.js | ||
---|---|---|
@@ -1,5 +1,6 @@ | ||
1 | 1 | var h = require('hyperscript') |
2 | +var u = require('./util') | |
2 | 3 | var pull = require('pull-stream') |
3 | 4 | var combine = require('depject') |
4 | 5 | var fs = require('fs') |
5 | 6 | var path = require('path') |
@@ -7,31 +8,17 @@ | ||
7 | 8 | var modules = fs.readdirSync(path.join(__dirname, 'modules')) |
8 | 9 | .map(function (e) { return require('./modules/'+e) }) |
9 | 10 | |
10 | 11 | var renderers = [] |
11 | -modules.unshift({message_render: renderers}) | |
12 | +var app = [] | |
13 | +modules.unshift({app: app}) | |
12 | 14 | |
13 | 15 | combine(modules) |
14 | 16 | |
15 | 17 | var u = require('./util') |
16 | 18 | |
17 | 19 | require('ssb-client')(function (err, sbot) { |
18 | 20 | if(err) throw err |
19 | - pull( | |
20 | - sbot.createLogStream({reverse: true, limit: 100}), | |
21 | - pull.drain(function (data) { | |
22 | - | |
23 | - var el = u.first(renderers, function (render) { | |
24 | - return render(data, sbot) | |
25 | - }) | |
26 | - | |
27 | - if('string' === typeof el) el = document.createTextNode(el) | |
28 | - if(el) document.body.appendChild(el) | |
29 | - }) | |
30 | - ) | |
21 | + document.body.appendChild(u.decorate(app, sbot)) | |
31 | 22 | }) |
32 | 23 | |
33 | 24 | |
34 | - | |
35 | - | |
36 | - | |
37 | - |
modules/avatar.js | ||
---|---|---|
@@ -2,9 +2,9 @@ | ||
2 | 2 | var h = require('hyperscript') |
3 | 3 | var u = require('../util') |
4 | 4 | |
5 | 5 | exports.avatar = function (author, sbot) { |
6 | - return h('a', {href:'#'}, u.first(exports.avatar_name, function (plug) { | |
6 | + return h('a', {href:'#'+author}, u.first(exports.avatar_name, function (plug) { | |
7 | 7 | return plug(author, sbot) |
8 | 8 | })) |
9 | 9 | } |
10 | 10 |
modules/like.js | ||
---|---|---|
@@ -1,9 +1,15 @@ | ||
1 | 1 | |
2 | 2 | var h = require('hyperscript') |
3 | +var u = require('../util') | |
3 | 4 | |
4 | -exports.message_content = function (msg) { | |
5 | - if(msg.value.content && msg.value.content.type === 'vote') | |
5 | +exports.message_content = function (msg, sbot) { | |
6 | + if(msg.value.content.type !== 'vote') return | |
7 | + var link = msg.value.content.vote.link | |
6 | 8 | return h('div', msg.value.content.vote.value > 0 ? 'yup' : 'nah', |
7 | - h('a', {href: '#/msg/'+msg.value.content.vote.link}, msg.key) | |
9 | + u.decorate(exports.message_link, link, function (d, e, v) { return d(e, v, sbot) }) | |
8 | 10 | ) |
9 | 11 | } |
12 | + | |
13 | +exports.message_link = [] | |
14 | + | |
15 | + |
modules/message.js | ||
---|---|---|
@@ -1,10 +1,11 @@ | ||
1 | 1 | var h = require('hyperscript') |
2 | 2 | var u = require('../util') |
3 | +var pull = require('pull-stream') | |
3 | 4 | |
4 | 5 | exports.message_render = function (msg, sbot) { |
5 | 6 | var el = u.first(exports.message_content, function (fn) { |
6 | - return fn(msg) | |
7 | + return fn(msg, sbot) | |
7 | 8 | }) |
8 | 9 | |
9 | 10 | if(el) console.log(el) |
10 | 11 | |
@@ -13,22 +14,37 @@ | ||
13 | 14 | return plug(value, sbot) |
14 | 15 | }).filter(Boolean) |
15 | 16 | } |
16 | 17 | |
18 | + var backlinks = h('div.backlinks') | |
19 | + | |
20 | + pull( | |
21 | + sbot.links({dest: msg.key, rel: 'mentions', keys: true}), | |
22 | + pull.collect(function (err, links) { | |
23 | + if(links.length) | |
24 | + backlinks.appendChild(h('label', 'backlinks:', | |
25 | + h('div', links.map(function (link) { | |
26 | + return u.decorate(exports.message_link, link.key, function (d, e, v) { return d(e, v, sbot) }) | |
27 | + })) | |
28 | + )) | |
29 | + }) | |
30 | + ) | |
31 | + | |
17 | 32 | if(el) |
18 | 33 | return h('div.message', |
19 | 34 | h('div.title', |
20 | 35 | h('div.avatar', map(exports.avatar, msg.value.author)), |
21 | 36 | h('div.metadata', map(exports.message_meta, msg)) |
22 | 37 | ), |
23 | 38 | h('div.content', el), |
24 | 39 | h('div.footer', |
25 | - h('div.actions', map(exports.message_actions)) | |
26 | - ) | |
40 | + h('div.actions', map(exports.message_action)) | |
41 | + ), | |
42 | + backlinks | |
27 | 43 | ) |
28 | 44 | } |
29 | 45 | |
30 | 46 | exports.message_content = [] |
31 | 47 | exports.avatar = [] |
32 | 48 | exports.message_meta = [] |
33 | 49 | exports.message_action = [] |
34 | - | |
50 | +exports.message_link = [] |
modules/names.js | ||
---|---|---|
@@ -4,9 +4,9 @@ | ||
4 | 4 | function all(stream, cb) { |
5 | 5 | pull(stream, pull.collect(cb)) |
6 | 6 | } |
7 | 7 | |
8 | -exports.avatar = | |
8 | +exports.avatar_name = | |
9 | 9 | function name (id, sbot) { |
10 | 10 | var n = h('span', id.substring(0, 10)) |
11 | 11 | //choose the most popular name for this person. |
12 | 12 | //for anything like this you'll see I have used sbot.links2 |
modules/post.js | ||
---|---|---|
@@ -1,17 +1,35 @@ | ||
1 | 1 | var markdown = require('ssb-markdown') |
2 | 2 | var h = require('hyperscript') |
3 | - | |
3 | +var u = require('../util') | |
4 | 4 | //render a message |
5 | 5 | |
6 | 6 | exports.message_content = function (data, sbot) { |
7 | - if(data.value.content && data.value.content.text) { | |
8 | - var d = h('div'/*, data.value.root ? */) | |
9 | - d.innerHTML = | |
10 | - markdown.block(data.value.content.text, data.value.content.mentions) | |
11 | - return d | |
12 | - } | |
7 | + if(!data.value.content || !data.value.content.text) return | |
8 | + | |
9 | + var root = data.value.content.root | |
10 | + var re = !root ? null : h('span', 're:', | |
11 | + u.decorate(exports.message_link, root, function (d, e, v) { return d(e, v, sbot) }) | |
12 | + ) | |
13 | + | |
14 | + var content = h('div') | |
15 | + var d = h('div', re, content) | |
16 | + | |
17 | + var mentions = {} | |
18 | + if(Array.isArray(data.value.content.mentions)) | |
19 | + data.value.content.mentions.forEach(function (link) { | |
20 | + if(link.name) mentions["@"+link.name] = link.link | |
21 | + }) | |
22 | + | |
23 | + content.innerHTML = | |
24 | + markdown.block(data.value.content.text, {toUrl: function (id) { | |
25 | + return '#'+(mentions[id]?mentions[id]:id) | |
26 | + }}) | |
27 | + return d | |
13 | 28 | } |
14 | 29 | |
30 | +exports.message_link = [] | |
15 | 31 | |
16 | 32 | |
17 | 33 | |
34 | + | |
35 | + |
modules/timestamp.js | ||
---|---|---|
@@ -1,6 +1,6 @@ | ||
1 | 1 | var h = require('hyperscript') |
2 | 2 | var moment = require('moment') |
3 | 3 | |
4 | 4 | exports.message_meta = function (msg) { |
5 | - return h('a', {href: '#/'+msg.key}, moment(msg.value.timestamp).fromNow()) | |
5 | + return h('a', {href: '#'+msg.key}, moment(msg.value.timestamp).fromNow()) | |
6 | 6 | } |
modules/feed.js | ||
---|---|---|
@@ -1,0 +1,17 @@ | ||
1 | +var ref = require('ssb-ref') | |
2 | +var ui = require('../ui') | |
3 | + | |
4 | +exports.screen_view = function (id, sbot) { | |
5 | + if(ref.isFeed(id)) | |
6 | + return ui.createStream( | |
7 | + sbot.createUserStream({id: id, limit: 100, reverse: true}), | |
8 | + ui.createRenderers(exports.message_render, sbot) | |
9 | + ) | |
10 | +} | |
11 | + | |
12 | +exports.message_render = [] | |
13 | + | |
14 | + | |
15 | + | |
16 | + | |
17 | + |
modules/main.js | ||
---|---|---|
@@ -1,0 +1,18 @@ | ||
1 | + | |
2 | +var ui = require('../ui') | |
3 | + | |
4 | +exports.screen_view = function (path, sbot) { | |
5 | + if(path === '/') | |
6 | + return ui.createStream( | |
7 | + sbot.createLogStream({limit: 100, reverse: true}), | |
8 | + ui.createRenderers(exports.message_render, sbot) | |
9 | + ) | |
10 | +} | |
11 | + | |
12 | +exports.message_render = [] | |
13 | + | |
14 | + | |
15 | + | |
16 | + | |
17 | + | |
18 | + |
modules/markdown.js | ||
---|---|---|
@@ -1,0 +1,7 @@ | ||
1 | + | |
2 | +// | |
3 | +//exports.markdown = function (body, msg | |
4 | +// | |
5 | +//function (text, ref, title) { | |
6 | +// return <h | |
7 | +//} |
modules/message-link.js | ||
---|---|---|
@@ -1,0 +1,19 @@ | ||
1 | +var h = require('hyperscript') | |
2 | + | |
3 | +exports.message_link = function (el, id, sbot) { | |
4 | + console.log('MESSAGE_LINK', el, id) | |
5 | + if(el) return el | |
6 | + | |
7 | + var link = h('a', {href: '#'+id}, id.substring(0, 10)+'...') | |
8 | + | |
9 | + sbot.get(id, function (err, value) { | |
10 | + if(err) return | |
11 | + console.log('LINKED', value) | |
12 | + if(value.content.text) | |
13 | + link.textContent = value.content.text.substring(0, 40)+'...' | |
14 | + }) | |
15 | + | |
16 | + return link | |
17 | +} | |
18 | + | |
19 | + |
modules/message-name.js | ||
---|---|---|
@@ -1,0 +1,7 @@ | ||
1 | + | |
2 | + | |
3 | +exports.message_name = function (id, sbot) { | |
4 | + sbot.get(id, function (err, value) { | |
5 | + | |
6 | + }) | |
7 | +} |
modules/tabs.js | ||
---|---|---|
@@ -1,0 +1,45 @@ | ||
1 | +var tabs = require('tabs') | |
2 | +var h = require('hyperscript') | |
3 | +var pull = require('pull-stream') | |
4 | +var u = require('../util') | |
5 | + | |
6 | + | |
7 | +function ancestor (el) { | |
8 | + if(!el) return | |
9 | + if(el.tagName !== 'A') return ancestor(el.parentElement) | |
10 | + return el | |
11 | +} | |
12 | +exports.app = function (_, sbot) { | |
13 | + | |
14 | + function screen (path) { | |
15 | + return u.first(exports.screen_view, function (fn) { | |
16 | + return fn(path, sbot) | |
17 | + }) | |
18 | + } | |
19 | + | |
20 | + var t = tabs() | |
21 | + var main = screen('/') | |
22 | + if(main) t.add('main', main, true) | |
23 | + | |
24 | + t.onclick = function (ev) { | |
25 | + var link = ancestor(ev.target) | |
26 | + EV = ev | |
27 | + var path = link.hash.substring(1) | |
28 | + var el = screen(path) | |
29 | + if(el) t.add(path, el, !ev.ctrlKey) | |
30 | + | |
31 | + ev.preventDefault() | |
32 | + ev.stopPropagation() | |
33 | + } | |
34 | + | |
35 | + return t | |
36 | +} | |
37 | + | |
38 | +exports.message_render = [] | |
39 | +exports.screen_view = [] | |
40 | + | |
41 | + | |
42 | + | |
43 | + | |
44 | + | |
45 | + |
modules/thread.js | ||
---|---|---|
@@ -1,0 +1,53 @@ | ||
1 | +var ui = require('../ui') | |
2 | +var pull = require('pull-stream') | |
3 | +var Cat = require('pull-cat') | |
4 | +var Sort = require('pull-sort') | |
5 | +var ref = require('ssb-ref') | |
6 | + | |
7 | +function once (cont) { | |
8 | + var ended = false | |
9 | + return function (abort, cb) { | |
10 | + if(abort) return cb(abort) | |
11 | + else if (ended) return cb(ended) | |
12 | + else | |
13 | + cont(function (err, data) { | |
14 | + if(err) return cb(ended = err) | |
15 | + ended = true | |
16 | + cb(null, data) | |
17 | + }) | |
18 | + } | |
19 | +} | |
20 | + | |
21 | +function threadStream (root, sbot ) { | |
22 | + //in this case, it's inconvienent that panel only takes | |
23 | + //a stream. maybe it would be better to accept an array? | |
24 | + | |
25 | + return pull( | |
26 | + Cat([ | |
27 | + once(function (cb) { | |
28 | + sbot.get(root, function (err, value) { | |
29 | + cb(err, {key: root, value: value}) | |
30 | + }) | |
31 | + }), | |
32 | + sbot.links({rel: 'root', dest: root, values: true, keys: true}) | |
33 | + ]), | |
34 | + Sort(function (a, b) { | |
35 | + //THIS IS WRONG AND HAS KNOWN BUGS!!! | |
36 | + //TODO: sort by cryptographic causal ordering. | |
37 | + return a.value.timestamp - b.value.timestamp | |
38 | + }) | |
39 | + ) | |
40 | +} | |
41 | + | |
42 | +exports.screen_view = function (id, sbot) { | |
43 | + if(ref.isMsg(id)) | |
44 | + return ui.createStream( | |
45 | + threadStream(id, sbot), | |
46 | + ui.createRenderers(exports.message_render, sbot) | |
47 | + ) | |
48 | +} | |
49 | + | |
50 | +exports.message_render = [] | |
51 | + | |
52 | + | |
53 | + |
ui.js | ||
---|---|---|
@@ -1,0 +1,31 @@ | ||
1 | +var h = require('hyperscript') | |
2 | +var pull = require('pull-stream') | |
3 | +var u = require('./util') | |
4 | + | |
5 | +exports.createStream = function createStream (stream, render) { | |
6 | + var div = h('div.content') | |
7 | + | |
8 | + pull( | |
9 | + stream, | |
10 | + pull.drain(function (data) { | |
11 | + var el = render(data) | |
12 | + if('string' === typeof el) el = document.createTextNode(el) | |
13 | + if(el) { | |
14 | + if(div.children.length) | |
15 | + div.appendChild(h('hr')) | |
16 | + div.appendChild(el) | |
17 | + } | |
18 | + | |
19 | + }) | |
20 | + ) | |
21 | + | |
22 | + return div | |
23 | +} | |
24 | + | |
25 | +exports.createRenderers = function (renderers, sbot) { | |
26 | + return function (data) { | |
27 | + return u.first(renderers, function (fn) { | |
28 | + return fn(data, sbot) | |
29 | + }) | |
30 | + } | |
31 | +} |
Built with git-ssb-web