Commit 798d7ea858881504d038705f550581dd75eabaa3
refactor so that sbot apis are a plugin, instead of being passed around
Dominic Tarr committed on 5/29/2016, 12:52:03 PMParent: 51bb7f2c4da0239b8114b4f425ca3da89741c261
Files changed
modules/avatar-image.js | changed |
modules/avatar.js | changed |
modules/compose.js | changed |
modules/crypto.js | changed |
modules/feed.js | changed |
modules/file-input.js | changed |
modules/follow.js | changed |
modules/like.js | changed |
modules/main.js | changed |
modules/message-confirm.js | changed |
modules/message-link.js | changed |
modules/message-name.js | changed |
modules/message.js | changed |
modules/names.js | changed |
modules/post.js | changed |
modules/private.js | changed |
modules/suggest-mentions.js | changed |
modules/tabs.js | changed |
modules/thread.js | changed |
sbot-api.js | changed |
plugs.js | added |
modules/avatar-image.js | ||
---|---|---|
@@ -2,15 +2,20 @@ | ||
2 | 2 | var getAvatar = require('ssb-avatar') |
3 | 3 | var h = require('hyperscript') |
4 | 4 | var ref = require('ssb-ref') |
5 | 5 | |
6 | +var plugs = require('../plugs') | |
7 | +var sbot_whoami = plugs.first(exports.sbot_whoami = []) | |
8 | +var sbot_links = plugs.first(exports.sbot_links = []) | |
9 | + | |
6 | 10 | exports.avatar_image = function (author, sbot) { |
7 | 11 | var img = h('img', {src: 'http://localhost:7777/img/fallback.png'}) |
8 | - sbot.whoami(function (err, me) { | |
9 | - getAvatar(sbot, me.id, author, function (err, avatar) { | |
12 | + sbot_whoami(function (err, me) { | |
13 | + getAvatar({links: sbot_links}, me.id, author, function (err, avatar) { | |
10 | 14 | if(ref.isBlob(avatar.image)) |
11 | 15 | img.src = 'http://localhost:7777/'+encodeURIComponent(avatar.image) |
12 | 16 | }) |
13 | 17 | }) |
14 | 18 | return img |
15 | 19 | } |
16 | 20 | |
21 | + |
modules/avatar.js | ||
---|---|---|
@@ -1,15 +1,17 @@ | ||
1 | 1 | |
2 | 2 | var h = require('hyperscript') |
3 | 3 | var u = require('../util') |
4 | 4 | |
5 | -exports.avatar_name = [] | |
6 | -exports.avatar_image = [] | |
7 | 5 | |
8 | -exports.avatar = function (author, sbot) { | |
6 | +var plugs = require('../plugs') | |
7 | +var avatar_name = plugs.first(exports.avatar_name = []) | |
8 | +var avatar_image = plugs.first(exports.avatar_image = []) | |
9 | + | |
10 | +exports.avatar = function (author) { | |
9 | 11 | return h('a.avatar', |
10 | 12 | {href:'#'+author}, |
11 | - u.firstPlug(exports.avatar_image, author, sbot), | |
12 | - u.firstPlug(exports.avatar_name, author, sbot) | |
13 | + avatar_image(author), | |
14 | + avatar_name(author) | |
13 | 15 | ) |
14 | 16 | } |
15 | 17 |
modules/compose.js | ||
---|---|---|
@@ -4,19 +4,23 @@ | ||
4 | 4 | var cont = require('cont') |
5 | 5 | var mentions = require('ssb-mentions') |
6 | 6 | var lightbox = require('hyperlightbox') |
7 | 7 | |
8 | +var plugs = require('../plugs') | |
9 | + | |
10 | +//var suggest = plugs.map(exports.suggest = []) | |
11 | +var publish = plugs.first(exports.publish = []) | |
12 | +var message_content = plugs.first(exports.message_content = []) | |
13 | +var message_confirm = plugs.first(exports.message_confirm = []) | |
14 | +var file_input = plugs.first(exports.file_input = []) | |
15 | + | |
8 | 16 | exports.suggest = [] |
9 | -exports.publish = [] | |
10 | -exports.message_content = [] | |
11 | -exports.message_confirm = [] | |
12 | -exports.file_input = [] | |
13 | 17 | |
14 | 18 | //this decorator expects to be the first |
15 | 19 | |
16 | 20 | function id (e) { return e } |
17 | 21 | |
18 | -exports.message_compose = function (meta, prepublish, sbot) { | |
22 | +exports.message_compose = function (meta, prepublish) { | |
19 | 23 | if('function' !== typeof prepublish) |
20 | 24 | sbot = prepublish, prepublish = id |
21 | 25 | meta = meta || {} |
22 | 26 | if(!meta.type) throw new Error('message must have type') |
@@ -49,16 +53,16 @@ | ||
49 | 53 | meta = prepublish(meta) |
50 | 54 | } catch (err) { |
51 | 55 | return alert(err.message) |
52 | 56 | } |
53 | - u.firstPlug(exports.message_confirm, meta, sbot) | |
57 | + message_confirm(meta) | |
54 | 58 | } |
55 | 59 | |
56 | 60 | |
57 | 61 | var composer = |
58 | 62 | h('div', h('div.column', ta, |
59 | 63 | h('div.row', |
60 | - u.firstPlug(exports.file_input, function (file) { | |
64 | + file_input(function (file) { | |
61 | 65 | files.push(file) |
62 | 66 | |
63 | 67 | var embed = file.type.indexOf('image/') === 0 ? '!' : '' |
64 | 68 | ta.value += embed + '['+file.name+']('+file.link+')' |
@@ -69,9 +73,9 @@ | ||
69 | 73 | ) |
70 | 74 | |
71 | 75 | suggest(ta, function (word, cb) { |
72 | 76 | cont.para(exports.suggest.map(function (fn) { |
73 | - return function (cb) { fn(word, sbot, cb) } | |
77 | + return function (cb) { fn(word, cb) } | |
74 | 78 | })) |
75 | 79 | (function (err, results) { |
76 | 80 | if(err) console.error(err) |
77 | 81 | results = results.reduce(function (ary, item) { |
@@ -87,4 +91,6 @@ | ||
87 | 91 | return composer |
88 | 92 | |
89 | 93 | } |
90 | 94 | |
95 | + | |
96 | + |
modules/crypto.js | ||
---|---|---|
@@ -18,8 +18,11 @@ | ||
18 | 18 | private: true |
19 | 19 | } |
20 | 20 | } |
21 | 21 | |
22 | + | |
23 | +var sbot_publish = require('../plugs').first(exports.sbot_publish = []) | |
24 | + | |
22 | 25 | exports.message_unbox = function (msg) { |
23 | 26 | if(msg.value) { |
24 | 27 | var value = unbox_value(msg.value) |
25 | 28 | if(value) |
@@ -36,19 +39,15 @@ | ||
36 | 39 | return ref.isFeed(e) ? e : e.link |
37 | 40 | })) |
38 | 41 | } |
39 | 42 | |
40 | -exports.message_meta = function (msg) { | |
41 | - if(msg.value.private) | |
42 | - return "PRIVATE" | |
43 | -} | |
44 | - | |
45 | -exports.publish = function (content, id, sbot) { | |
43 | +exports.publish = function (content, id) { | |
46 | 44 | if(content.recps) |
47 | 45 | content = exports.message_box(content) |
48 | - sbot.publish(content, function (err, msg) { | |
46 | + sbot_publish(content, function (err, msg) { | |
49 | 47 | if(err) throw err |
50 | 48 | console.log('PUBLISHED', msg) |
51 | 49 | }) |
52 | 50 | } |
53 | 51 | |
54 | 52 | |
53 | + |
modules/feed.js | ||
---|---|---|
@@ -1,19 +1,20 @@ | ||
1 | 1 | var ref = require('ssb-ref') |
2 | 2 | var ui = require('../ui') |
3 | 3 | var Scroller = require('pull-scroll') |
4 | + | |
5 | +var plugs = require('../plugs') | |
6 | +var sbot_user_feed = plugs.first(exports.sbot_user_feed = []) | |
7 | +var message_render = plugs.first(exports.message_render = []) | |
8 | + | |
4 | 9 | exports.screen_view = function (id, sbot) { |
10 | + //TODO: header of user info, avatars, names, follows. | |
11 | + | |
5 | 12 | if(ref.isFeed(id)) { |
6 | - | |
7 | 13 | return ui.createStream( |
8 | - sbot.createUserStream({id: id, reverse: true}), | |
9 | - ui.createRenderers(exports.message_render, sbot) | |
14 | + sbot_user_feed({id: id, reverse: true}), | |
15 | + message_render | |
10 | 16 | ) |
11 | 17 | } |
12 | 18 | } |
13 | 19 | |
14 | -exports.message_render = [] | |
15 | 20 | |
16 | - | |
17 | - | |
18 | - | |
19 | - |
modules/file-input.js | ||
---|---|---|
@@ -3,17 +3,11 @@ | ||
3 | 3 | var pull = require('pull-stream') |
4 | 4 | var mime = require('mime-types') |
5 | 5 | var split = require('split-buffer') |
6 | 6 | |
7 | -function first(plug) { | |
8 | - return function () { | |
9 | - var args = [].slice.call(arguments) | |
10 | - args.unshift(plug) | |
11 | - return u.firstPlug.apply(null, args) | |
12 | - } | |
13 | -} | |
7 | +var plugs = require('../plugs') | |
14 | 8 | |
15 | -var add = first(exports.sbot_blobs_add = []) | |
9 | +var add = plugs.first(exports.sbot_blobs_add = []) | |
16 | 10 | |
17 | 11 | exports.file_input = function FileInput(onAdded) { |
18 | 12 | |
19 | 13 | return h('input', { type: 'file', |
@@ -42,4 +36,5 @@ | ||
42 | 36 | |
43 | 37 | |
44 | 38 | |
45 | 39 | |
40 | + |
modules/follow.js | ||
---|---|---|
@@ -1,16 +1,16 @@ | ||
1 | 1 | var h = require('hyperscript') |
2 | 2 | var u = require('../util') |
3 | -exports.avatar = [] | |
3 | +var avatar = require('../plugs').first(exports.avatar = []) | |
4 | 4 | |
5 | 5 | //render a message when someone follows someone, |
6 | 6 | //so you see new users |
7 | 7 | exports.message_content = function (msg, sbot) { |
8 | 8 | |
9 | 9 | if(msg.value.content.type == 'contact') { |
10 | 10 | return h('div.contact', |
11 | 11 | 'follows', |
12 | - u.firstPlug(exports.avatar, msg.value.content.contact, sbot) | |
12 | + avatar(msg.value.content.contact) | |
13 | 13 | ) |
14 | 14 | } |
15 | 15 | } |
16 | 16 |
modules/like.js | ||
---|---|---|
@@ -2,25 +2,29 @@ | ||
2 | 2 | var h = require('hyperscript') |
3 | 3 | var u = require('../util') |
4 | 4 | var pull = require('pull-stream') |
5 | 5 | |
6 | -exports.message_confirm = [] | |
7 | -exports.message_link = [] | |
6 | +var plugs = require('../plugs') | |
8 | 7 | |
8 | +var message_confirm = plugs.first(exports.message_confirm = []) | |
9 | +var message_link = plugs.first(exports.message_link = []) | |
10 | +var sbot_links = plugs.first(exports.sbot_links = []) | |
11 | + | |
9 | 12 | exports.message_content = function (msg, sbot) { |
10 | 13 | if(msg.value.content.type !== 'vote') return |
11 | 14 | var link = msg.value.content.vote.link |
12 | - return h('div', msg.value.content.vote.value > 0 ? 'yup' : 'nah', | |
13 | - u.decorate(exports.message_link, link, function (d, e, v) { return d(e, v, sbot) }) | |
15 | + return h('div', | |
16 | + msg.value.content.vote.value > 0 ? 'yup' : 'nah', | |
17 | + ' ', message_link(link) | |
14 | 18 | ) |
15 | 19 | } |
16 | 20 | |
17 | 21 | exports.message_meta = function (msg, sbot) { |
18 | 22 | |
19 | 23 | var yupps = h('a') |
20 | 24 | |
21 | 25 | pull( |
22 | - sbot.links({dest: msg.key, rel: 'vote'}), | |
26 | + sbot_links({dest: msg.key, rel: 'vote'}), | |
23 | 27 | pull.collect(function (err, votes) { |
24 | 28 | if(votes.length === 1) |
25 | 29 | yupps.textContent = ' 1 yup' |
26 | 30 | if(votes.length) |
@@ -45,10 +49,14 @@ | ||
45 | 49 | yup.private = true |
46 | 50 | } |
47 | 51 | //TODO: actually publish... |
48 | 52 | |
49 | - u.firstPlug(exports.message_confirm, yup, sbot) | |
53 | + message_confirm(yup) | |
50 | 54 | }}, 'yup') |
51 | 55 | |
52 | 56 | } |
53 | 57 | |
54 | 58 | |
59 | + | |
60 | + | |
61 | + | |
62 | + |
modules/main.js | ||
---|---|---|
@@ -3,37 +3,46 @@ | ||
3 | 3 | var u = require('../util') |
4 | 4 | var pull = require('pull-stream') |
5 | 5 | var Scroller = require('pull-scroll') |
6 | 6 | |
7 | +var plugs = require('../plugs') | |
8 | +var message_render = plugs.first(exports.message_render = []) | |
9 | +var message_compose = plugs.first(exports.message_compose = []) | |
10 | +var sbot_log = plugs.first(exports.sbot_log = []) | |
11 | + | |
7 | 12 | exports.screen_view = function (path, sbot) { |
8 | 13 | if(path === '/') { |
9 | 14 | var content = h('div.column') |
10 | - var div = h('div.column', {style: {'overflow':'auto'}}, | |
11 | - u.firstPlug(exports.message_compose, {type: 'post'}, sbot), | |
12 | - content | |
15 | + var div = h('div.column', | |
16 | + {style: {'overflow':'auto'}}, | |
17 | + message_compose({type: 'post'}), content | |
13 | 18 | ) |
14 | - var render = ui.createRenderers(exports.message_render, sbot) | |
15 | 19 | |
16 | 20 | pull( |
17 | - sbot.createLogStream({old: false}), | |
18 | - Scroller(div, content, render, true, false) | |
21 | + sbot_log({old: false}), | |
22 | + Scroller(div, content, message_render, true, false) | |
19 | 23 | ) |
20 | 24 | |
21 | 25 | pull( |
22 | - u.next(sbot.createLogStream, {reverse: true, limit: 100, live: false}), | |
23 | - Scroller(div, content, render, false, false) | |
26 | + u.next(sbot_log, {reverse: true, limit: 100, live: false}), | |
27 | + Scroller(div, content, message_render, false, false) | |
24 | 28 | ) |
25 | 29 | |
26 | 30 | return div |
27 | 31 | } |
28 | 32 | } |
29 | 33 | |
30 | -exports.message_render = [] | |
31 | -exports.message_compose = [] | |
32 | 34 | |
33 | 35 | |
34 | 36 | |
35 | 37 | |
36 | 38 | |
37 | 39 | |
38 | 40 | |
39 | 41 | |
42 | + | |
43 | + | |
44 | + | |
45 | + | |
46 | + | |
47 | + | |
48 | + |
modules/message-confirm.js | ||
---|---|---|
@@ -2,43 +2,38 @@ | ||
2 | 2 | var h = require('hyperscript') |
3 | 3 | var u = require('../util') |
4 | 4 | //publish or add |
5 | 5 | |
6 | -exports.publish = [] | |
7 | -exports.message_content = [] | |
6 | +var plugs = require('../plugs') | |
8 | 7 | |
8 | +var publish = plugs.first(exports.publish = []) | |
9 | +var message_content = plugs.first(exports.message_content = []) | |
10 | + | |
9 | 11 | exports.message_confirm = function (content, sbot) { |
10 | 12 | |
11 | 13 | var lb = lightbox() |
12 | 14 | document.body.appendChild(lb) |
13 | 15 | |
14 | 16 | var okay = h('button', 'okay', {onclick: function () { |
15 | - u.firstPlug(exports.publish, content, null, sbot) | |
16 | - lb.remove() | |
17 | + publish(content); lb.remove() | |
17 | 18 | }}) |
18 | 19 | |
19 | 20 | var cancel = h('button', 'cancel', {onclick: function () { |
20 | 21 | lb.remove() |
21 | 22 | }}) |
22 | 23 | |
23 | - var hidden = h('input', {style: {visible: 'hidden'}}) | |
24 | - | |
25 | - | |
26 | 24 | okay.addEventListener('keydown', function (ev) { |
27 | 25 | if(ev.keyCode === 27) cancel.click() //escape |
28 | 26 | }) |
29 | 27 | |
30 | 28 | lb.show(h('div.column', |
31 | - u.firstPlug(exports.message_content, | |
32 | - {key: "DRAFT", value: {content: content}}, sbot | |
33 | - ) || h('pre', JSON.stringify(content, null, 2)), | |
29 | + message_content({key: "DRAFT", value: {content: content}}) | |
30 | + || h('pre', JSON.stringify(content, null, 2)), | |
34 | 31 | h('div.row', okay, cancel) |
35 | 32 | )) |
36 | 33 | |
37 | 34 | okay.focus() |
38 | 35 | |
39 | -// hidden.focus() | |
40 | - | |
41 | 36 | } |
42 | 37 | |
43 | 38 | |
44 | 39 | |
@@ -52,4 +47,7 @@ | ||
52 | 47 | |
53 | 48 | |
54 | 49 | |
55 | 50 | |
51 | + | |
52 | + | |
53 | + |
modules/message-link.js | ||
---|---|---|
@@ -1,16 +1,24 @@ | ||
1 | 1 | var h = require('hyperscript') |
2 | 2 | |
3 | -exports.message_link = function (el, id, sbot) { | |
4 | - if(el) return el | |
3 | +var sbot_get = require('../plugs').first(exports.sbot_get = []) | |
5 | 4 | |
5 | +exports.message_link = function (id) { | |
6 | + | |
7 | + if('string' !== typeof id) | |
8 | + throw new Error('link must be to message id') | |
9 | + | |
6 | 10 | var link = h('a', {href: '#'+id}, id.substring(0, 10)+'...') |
7 | 11 | |
8 | - sbot.get(id, function (err, value) { | |
9 | - if(err) return | |
12 | + sbot_get(id, function (err, value) { | |
13 | + if(err) return console.error(err) | |
10 | 14 | if(value.content.text) |
11 | 15 | link.textContent = value.content.text.substring(0, 40)+'...' |
12 | 16 | }) |
13 | 17 | |
14 | 18 | return link |
15 | 19 | } |
16 | 20 | |
21 | + | |
22 | + | |
23 | + | |
24 | + |
modules/message-name.js | ||
---|---|---|
@@ -1,7 +1,8 @@ | ||
1 | 1 | |
2 | +var sbot_get = require('../plugs').first(exports.sbot_get = []) | |
2 | 3 | |
3 | -exports.message_name = function (id, sbot) { | |
4 | - sbot.get(id, function (err, value) { | |
4 | +exports.message_name = function (id) { | |
5 | + sbot_get(id, function (err, value) { | |
5 | 6 | |
6 | 7 | }) |
7 | 8 | } |
modules/message.js | ||
---|---|---|
@@ -1,47 +1,49 @@ | ||
1 | 1 | var h = require('hyperscript') |
2 | 2 | var u = require('../util') |
3 | 3 | var pull = require('pull-stream') |
4 | 4 | |
5 | + | |
6 | + | |
7 | +var plugs = require('../plugs') | |
8 | +var message_content = plugs.first(exports.message_content = []) | |
9 | +var avatar = plugs.first(exports.avatar = []) | |
10 | +var message_meta = plugs.map(exports.message_meta = []) | |
11 | +var message_action = plugs.map(exports.message_action = []) | |
12 | +var message_link = plugs.first(exports.message_link = []) | |
13 | + | |
14 | +var sbot_links = plugs.first(exports.sbot_links = []) | |
15 | + | |
5 | 16 | exports.message_render = function (msg, sbot) { |
6 | - var el = u.firstPlug(exports.message_content, msg, sbot) | |
17 | + var el = message_content(msg) | |
18 | + if(!el) return | |
7 | 19 | |
8 | - function map (plugs, value) { | |
9 | - return plugs.map(function (plug) { | |
10 | - return plug(value, sbot) | |
11 | - }).filter(Boolean) | |
12 | - } | |
13 | - | |
14 | 20 | var backlinks = h('div.backlinks') |
15 | 21 | |
16 | 22 | pull( |
17 | - sbot.links({dest: msg.key, rel: 'mentions', keys: true}), | |
23 | + sbot_links({dest: msg.key, rel: 'mentions', keys: true}), | |
18 | 24 | pull.collect(function (err, links) { |
19 | 25 | if(links.length) |
20 | 26 | backlinks.appendChild(h('label', 'backlinks:', |
21 | 27 | h('div', links.map(function (link) { |
22 | - return u.decorate(exports.message_link, link.key, function (d, e, v) { return d(e, v, sbot) }) | |
28 | + return message_link(link.key) | |
23 | 29 | })) |
24 | 30 | )) |
25 | 31 | }) |
26 | 32 | ) |
27 | 33 | |
28 | - if(el) | |
29 | - return h('div.message.column', | |
30 | - h('div.title.row', | |
31 | - h('div.avatar', map(exports.avatar, msg.value.author)), | |
32 | - h('div.message_meta.row', map(exports.message_meta, msg)) | |
33 | - ), | |
34 | - h('div.message_content', el), | |
35 | - h('div.message_actions.row', | |
36 | - h('div.actions', map(exports.message_action, msg)) | |
37 | - ), | |
38 | - backlinks | |
39 | - ) | |
34 | + return h('div.message.column', | |
35 | + h('div.title.row', | |
36 | + h('div.avatar', avatar(msg.value.author)), | |
37 | + h('div.message_meta.row', message_meta(msg)) | |
38 | + ), | |
39 | + h('div.message_content', el), | |
40 | + h('div.message_actions.row', | |
41 | + h('div.actions', message_action(msg)) | |
42 | + ), | |
43 | + backlinks | |
44 | + ) | |
40 | 45 | } |
41 | 46 | |
42 | -exports.message_content = [] | |
43 | -exports.avatar = [] | |
44 | -exports.message_meta = [] | |
45 | -exports.message_action = [] | |
46 | -exports.message_link = [] | |
47 | 47 | |
48 | + | |
49 | + |
modules/names.js | ||
---|---|---|
@@ -4,24 +4,30 @@ | ||
4 | 4 | function all(stream, cb) { |
5 | 5 | pull(stream, pull.collect(cb)) |
6 | 6 | } |
7 | 7 | |
8 | +var plugs = require('../plugs') | |
9 | + | |
10 | +var sbot_links2 = plugs.first(exports.sbot_links2 = []) | |
11 | + | |
8 | 12 | exports.avatar_name = |
9 | 13 | function name (id, sbot) { |
10 | 14 | var n = h('span', id.substring(0, 10)) |
15 | + | |
11 | 16 | //choose the most popular name for this person. |
12 | 17 | //for anything like this you'll see I have used sbot.links2 |
13 | 18 | //which is the ssb-links plugin. as you'll see the query interface |
14 | 19 | //is pretty powerful! |
15 | 20 | //TODO: "most popular" name is easily gameable. |
16 | 21 | //must come up with something better than this. |
22 | + | |
17 | 23 | /* |
18 | 24 | filter(rel: ['mentions', prefix('@')]) |
19 | 25 | .reduce(name: rel[1], value: count()) |
20 | 26 | */ |
21 | 27 | |
22 | 28 | all( |
23 | - sbot.links2.read({query: [ | |
29 | + sbot_links2({query: [ | |
24 | 30 | {$filter: {rel: ['mentions', {$prefix: '@'}], dest: id}}, |
25 | 31 | {$reduce: { name: ['rel', 1], count: {$count: true} |
26 | 32 | }} |
27 | 33 | ]}), |
modules/post.js | ||
---|---|---|
@@ -4,15 +4,16 @@ | ||
4 | 4 | var ref = require('ssb-ref') |
5 | 5 | |
6 | 6 | //render a message |
7 | 7 | |
8 | +var plugs = require('../plugs') | |
9 | +var message_link = plugs.first(exports.message_link = []) | |
10 | + | |
8 | 11 | exports.message_content = function (data, sbot) { |
9 | 12 | if(!data.value.content || !data.value.content.text) return |
10 | 13 | |
11 | 14 | var root = data.value.content.root |
12 | - var re = !root ? null : h('span', 're:', | |
13 | - u.decorate(exports.message_link, root, function (d, e, v) { return d(e, v, sbot) }) | |
14 | - ) | |
15 | + var re = !root ? null : h('span', 're:', message_link(root)) | |
15 | 16 | |
16 | 17 | var content = h('div') |
17 | 18 | var d = h('div', re, content) |
18 | 19 | |
@@ -30,10 +31,11 @@ | ||
30 | 31 | }}) |
31 | 32 | return d |
32 | 33 | } |
33 | 34 | |
34 | -exports.message_link = [] | |
35 | 35 | |
36 | 36 | |
37 | 37 | |
38 | 38 | |
39 | 39 | |
40 | + | |
41 | + |
modules/private.js | ||
---|---|---|
@@ -4,64 +4,68 @@ | ||
4 | 4 | var pull = require('pull-stream') |
5 | 5 | var Scroller = require('pull-scroll') |
6 | 6 | var ref = require('ssb-ref') |
7 | 7 | |
8 | -exports.message_render = [] | |
9 | -exports.message_compose = [] | |
10 | -exports.message_unbox = [] | |
8 | +var plugs = require('../plugs') | |
11 | 9 | |
10 | +var message_render = plugs.first(exports.message_render = []) | |
11 | +var message_compose = plugs.first(exports.message_compose = []) | |
12 | +var message_unbox = plugs.first(exports.message_unbox = []) | |
13 | +var sbot_log = plugs.first(exports.sbot_log = []) | |
14 | +var sbot_whoami = plugs.first(exports.sbot_whoami = []) | |
15 | + | |
12 | 16 | function unbox () { |
13 | 17 | return pull( |
14 | 18 | pull.filter(function (msg) { |
15 | 19 | return 'string' == typeof msg.value.content |
16 | 20 | }), |
17 | 21 | pull.map(function (msg) { |
18 | - return u.firstPlug(exports.message_unbox, msg) | |
22 | + return message_unbox(msg) | |
19 | 23 | }), |
20 | 24 | pull.filter(Boolean) |
21 | 25 | ) |
22 | 26 | } |
23 | 27 | |
24 | -exports.screen_view = function (path, sbot) { | |
28 | +exports.screen_view = function (path) { | |
25 | 29 | if(path === '/private') { |
26 | - SBOT = sbot | |
27 | 30 | var content = h('div.column') |
28 | 31 | var id = null |
29 | - sbot.whoami(function (err, me) { | |
32 | + sbot_whoami(function (err, me) { | |
30 | 33 | id = me.id |
31 | 34 | }) |
32 | 35 | |
33 | 36 | var div = h('div.column', {style: {'overflow':'auto'}}, |
34 | - u.firstPlug(exports.message_compose, {type: 'post', recps: [], private: true}, | |
37 | + message_compose({type: 'post', recps: [], private: true}, | |
35 | 38 | function (msg) { |
36 | 39 | msg.recps = [id].concat(msg.mentions).filter(function (e) { |
37 | 40 | return ref.isFeed('string' === typeof e ? e : e.link) |
38 | 41 | }) |
39 | 42 | if(!msg.recps.length) |
40 | 43 | throw new Error('cannot make private message without recipients - just mention them in the message') |
41 | 44 | return msg |
42 | - }, | |
43 | - sbot), | |
45 | + }), | |
44 | 46 | content) |
45 | - var render = ui.createRenderers(exports.message_render, sbot) | |
46 | 47 | |
47 | 48 | pull( |
48 | - sbot.createLogStream({old: false}), | |
49 | + sbot_log({old: false}), | |
49 | 50 | unbox(), |
50 | - Scroller(div, content, render, true, false) | |
51 | + Scroller(div, content, message_render, true, false) | |
51 | 52 | ) |
52 | 53 | |
53 | 54 | pull( |
54 | - u.next(sbot.createLogStream, {reverse: true, limit: 1000}), | |
55 | + u.next(sbot_log, {reverse: true, limit: 1000}), | |
55 | 56 | unbox(), |
56 | - Scroller(div, content, render, false, false, function (err) { | |
57 | + Scroller(div, content, message_render, false, false, function (err) { | |
57 | 58 | if(err) throw err |
58 | 59 | }) |
59 | 60 | ) |
60 | 61 | |
61 | 62 | return div |
62 | 63 | } |
63 | 64 | } |
64 | 65 | |
66 | +exports.message_meta = function (msg) { | |
67 | + if(msg.value.private) | |
68 | + return "PRIVATE" | |
69 | +} | |
65 | 70 | |
66 | 71 | |
67 | - |
modules/suggest-mentions.js | ||
---|---|---|
@@ -1,12 +1,13 @@ | ||
1 | 1 | var pull = require('pull-stream') |
2 | - | |
2 | +var cont = require('cont') | |
3 | 3 | function isImage (filename) { |
4 | 4 | return /\.(gif|jpg|png|svg)$/i.test(filename) |
5 | 5 | } |
6 | 6 | |
7 | -exports.suggest = function (word, sbot, cb) { | |
7 | +var sbot_links2 = require('../plugs').first(exports.sbot_links2 = []) | |
8 | 8 | |
9 | +exports.suggest = cont.to(function (word, cb) { | |
9 | 10 | if(!/^[@%&!]/.test(word[0])) return cb() |
10 | 11 | if(word.length < 2) return cb() |
11 | 12 | |
12 | 13 | var sigil = word[0] |
@@ -14,9 +15,9 @@ | ||
14 | 15 | if(embed) sigil = '&' |
15 | 16 | if(word[0] !== '@') word = word.substring(1) |
16 | 17 | |
17 | 18 | pull( |
18 | - sbot.links2.read({query: [ | |
19 | + sbot_links2({query: [ | |
19 | 20 | {$filter: {rel: ['mentions', {$prefix: word}], dest: {$prefix: sigil}}}, |
20 | 21 | {$reduce: {id: 'dest', name: ['rel', 1], rank: {$count: true}}} |
21 | 22 | ]}), |
22 | 23 | pull.collect(function (err, ary) { |
@@ -34,11 +35,9 @@ | ||
34 | 35 | rank: e.rank, |
35 | 36 | image: isImage(e.name) ? 'http://localhost:7777/'+e.id : undefined |
36 | 37 | } |
37 | 38 | }) |
38 | - console.log(ary) | |
39 | 39 | cb(null, ary) |
40 | 40 | }) |
41 | 41 | ) |
42 | -} | |
42 | +}) | |
43 | 43 | |
44 | - |
modules/tabs.js | ||
---|---|---|
@@ -9,29 +9,30 @@ | ||
9 | 9 | if(el.tagName !== 'A') return ancestor(el.parentElement) |
10 | 10 | return el |
11 | 11 | } |
12 | 12 | |
13 | +var plugs = require('../plugs') | |
14 | +var screen_view = plugs.first(exports.screen_view = []) | |
15 | + | |
16 | +exports.message_render = [] | |
17 | + | |
18 | + | |
13 | 19 | exports.app = function (_, sbot) { |
14 | - function screen (path) { | |
15 | - return u.firstPlug(exports.screen_view, path, sbot) | |
16 | - } | |
17 | - | |
18 | 20 | var tabs = Tabs() |
19 | 21 | tabs.classList.add('screen') |
20 | - var main = screen('/') | |
22 | + | |
23 | + var main = screen_view('/') | |
21 | 24 | if(main) tabs.add('main', main, true) |
22 | 25 | |
23 | - var private = screen('/private') | |
26 | + var private = screen_view('/private') | |
24 | 27 | if(private) tabs.add('private', private, true) |
25 | 28 | |
26 | 29 | |
27 | 30 | tabs.onclick = function (ev) { |
28 | 31 | var link = ancestor(ev.target) |
29 | 32 | if(!link) return |
30 | 33 | var path = link.hash.substring(1) |
31 | 34 | |
32 | - console.log(link) | |
33 | - | |
34 | 35 | ev.preventDefault() |
35 | 36 | ev.stopPropagation() |
36 | 37 | |
37 | 38 | //open external links. |
@@ -40,18 +41,15 @@ | ||
40 | 41 | return require('shell').openExternal(link.href) |
41 | 42 | |
42 | 43 | if(tabs.has(path)) return tabs.select(path) |
43 | 44 | |
44 | - var el = screen(path) | |
45 | + var el = screen_view(path) | |
45 | 46 | if(el) tabs.add(path, el, !ev.ctrlKey) |
46 | 47 | |
47 | 48 | } |
48 | 49 | |
49 | 50 | return tabs |
50 | 51 | } |
51 | 52 | |
52 | -exports.message_render = [] | |
53 | -exports.screen_view = [] | |
54 | 53 | |
55 | 54 | |
56 | 55 | |
57 | - |
modules/thread.js | ||
---|---|---|
@@ -20,26 +20,31 @@ | ||
20 | 20 | }) |
21 | 21 | } |
22 | 22 | } |
23 | 23 | |
24 | +var plugs = require('../plugs') | |
25 | + | |
26 | +var message_render = plugs.first(exports.message_render = []) | |
27 | +var message_compose = plugs.first(exports.message_compose = []) | |
28 | +var message_unbox = plugs.first(exports.message_unbox = []) | |
29 | + | |
30 | +var sbot_get = plugs.first(exports.sbot_get = []) | |
31 | +var sbot_links = plugs.first(exports.sbot_links = []) | |
32 | + | |
24 | 33 | function getThread (root, sbot, cb) { |
25 | 34 | //in this case, it's inconvienent that panel only takes |
26 | 35 | //a stream. maybe it would be better to accept an array? |
27 | 36 | |
28 | 37 | return pull(Cat([ |
29 | 38 | once(function (cb) { |
30 | - sbot.get(root, function (err, value) { | |
39 | + sbot_get(root, function (err, value) { | |
31 | 40 | cb(err, {key: root, value: value}) |
32 | 41 | }) |
33 | 42 | }), |
34 | - sbot.links({rel: 'root', dest: root, values: true, keys: true}) | |
43 | + sbot_links({rel: 'root', dest: root, values: true, keys: true}) | |
35 | 44 | ]), pull.collect(cb)) |
36 | 45 | } |
37 | 46 | |
38 | -function unbox(msg) { | |
39 | - return u.firstPlug(exports.message_unbox, msg) | |
40 | -} | |
41 | - | |
42 | 47 | exports.screen_view = function (id, sbot) { |
43 | 48 | if(ref.isMsg(id)) { |
44 | 49 | var meta = { |
45 | 50 | type: 'post', |
@@ -50,38 +55,33 @@ | ||
50 | 55 | var content = h('div') |
51 | 56 | var div = h('div.column', |
52 | 57 | {style: {'overflow-y': 'auto'}}, |
53 | 58 | content, |
54 | - h('div.editor', u.firstPlug(exports.message_compose, meta, sbot)) | |
59 | + h('div.editor', message_compose(meta)) | |
55 | 60 | ) |
56 | 61 | |
57 | - var render = ui.createRenderers(exports.message_render, sbot) | |
58 | - | |
59 | 62 | pull( |
60 | - sbot.links({ | |
63 | + sbot_links({ | |
61 | 64 | rel: 'root', dest: id, keys: true, old: false |
62 | 65 | }), |
63 | 66 | pull.drain(function (msg) { |
64 | - console.log('new message in thread', msg) | |
65 | - //redraw thread | |
66 | - loadThread() | |
67 | + loadThread() //redraw thread | |
67 | 68 | }, function () {} ) |
68 | 69 | ) |
69 | 70 | |
70 | 71 | |
71 | 72 | function loadThread () { |
72 | - console.log("LOAD THREAD", id) | |
73 | 73 | getThread(id, sbot, function (err, thread) { |
74 | 74 | //would probably be better keep an id for each message element |
75 | 75 | //(i.e. message key) and then update it if necessary. |
76 | 76 | //also, it may have moved (say, if you received a missing message) |
77 | 77 | content.innerHTML = '' |
78 | 78 | thread = thread.map(function (msg) { |
79 | - return 'string' === typeof msg.value.content ? unbox(msg) : msg | |
79 | + return 'string' === typeof msg.value.content ? message_unbox(msg) : msg | |
80 | 80 | }) |
81 | 81 | |
82 | 82 | if(err) return content.appendChild(h('pre', err.stack)) |
83 | - sort(thread).map(render).forEach(function (el) { | |
83 | + sort(thread).map(message_render).forEach(function (el) { | |
84 | 84 | content.appendChild(el) |
85 | 85 | }) |
86 | 86 | |
87 | 87 | var branches = sort.heads(thread) |
@@ -90,35 +90,12 @@ | ||
90 | 90 | var recps = thread[0].value.content.recps |
91 | 91 | if(recps && thread[0].value.private) |
92 | 92 | meta.recps = recps |
93 | 93 | }) |
94 | - | |
95 | 94 | } |
96 | 95 | |
97 | 96 | loadThread() |
98 | - | |
99 | 97 | return div |
100 | 98 | } |
101 | - | |
102 | 99 | } |
103 | 100 | |
104 | -exports.message_render = [] | |
105 | -exports.message_compose = [] | |
106 | -exports.message_unbox = [] | |
107 | 101 | |
108 | - | |
109 | - | |
110 | - | |
111 | - | |
112 | - | |
113 | - | |
114 | - | |
115 | - | |
116 | - | |
117 | - | |
118 | - | |
119 | - | |
120 | - | |
121 | - | |
122 | - | |
123 | - | |
124 | - |
sbot-api.js | ||
---|---|---|
@@ -27,13 +27,27 @@ | ||
27 | 27 | }, |
28 | 28 | sbot_links: function (query) { |
29 | 29 | return sbot.links(query) |
30 | 30 | }, |
31 | + sbot_links2: function (query) { | |
32 | + return sbot.links2.read(query) | |
33 | + }, | |
34 | + sbot_log: function (opts) { | |
35 | + return sbot.createLogStream(opts) | |
36 | + }, | |
37 | + sbot_user_feed: function (opts) { | |
38 | + return sbot.createUserStream(opts) | |
39 | + }, | |
31 | 40 | sbot_get: function (key, cb) { |
32 | 41 | sbot.get(key, cb) |
33 | 42 | }, |
34 | 43 | sbot_publish: function (msg, cb) { |
35 | 44 | sbot.publish(msg, cb) |
45 | + }, | |
46 | + sbot_whoami: function (cb) { | |
47 | + sbot.whoami(cb) | |
36 | 48 | } |
37 | 49 | } |
38 | 50 | } |
39 | 51 | |
52 | + | |
53 | + |
plugs.js | ||
---|---|---|
@@ -1,0 +1,20 @@ | ||
1 | +var u = require('./util') | |
2 | + | |
3 | +exports.first = function first(plug) { | |
4 | + return function () { | |
5 | + var args = [].slice.call(arguments) | |
6 | + args.unshift(plug) | |
7 | + return u.firstPlug.apply(null, args) | |
8 | + } | |
9 | +} | |
10 | + | |
11 | +exports.map = function (plug) { | |
12 | + return function () { | |
13 | + var args = [].slice.call(arguments) | |
14 | + return plug.map(function (fn) { | |
15 | + if(fn) return fn.apply(null, args) | |
16 | + }).filter(Boolean) | |
17 | + } | |
18 | +} | |
19 | + | |
20 | + |
Built with git-ssb-web