Commit 5435933800608d5e5da0286230aaa90de27686f7
add avatars and follow
Dominic Tarr committed on 6/24/2016, 9:55:40 PMParent: e41be9be2c5756cd0ca92256d7d7122944415cf2
Files changed
index.js | changed |
modules/avatar-image.js | changed |
modules/avatar.js | changed |
modules/feed.js | changed |
modules/follow.js | changed |
modules/like.js | changed |
modules/names.js | changed |
sbot-api.js | changed |
modules/avatar-image.js | ||
---|---|---|
@@ -6,9 +6,9 @@ | ||
6 | 6 | var plugs = require('../plugs') |
7 | 7 | var sbot_whoami = plugs.first(exports.sbot_whoami = []) |
8 | 8 | var sbot_links = plugs.first(exports.sbot_links = []) |
9 | 9 | |
10 | -exports.avatar_image = function (author, sbot) { | |
10 | +exports.avatar_image = function (author) { | |
11 | 11 | var img = h('img', {src: 'http://localhost:7777/img/fallback.png'}) |
12 | 12 | sbot_whoami(function (err, me) { |
13 | 13 | getAvatar({links: sbot_links}, me.id, author, function (err, avatar) { |
14 | 14 | if(ref.isBlob(avatar.image)) |
modules/avatar.js | ||
---|---|---|
@@ -1,5 +1,4 @@ | ||
1 | - | |
2 | 1 | var h = require('hyperscript') |
3 | 2 | var u = require('../util') |
4 | 3 | |
5 | 4 |
modules/feed.js | ||
---|---|---|
@@ -1,20 +1,50 @@ | ||
1 | 1 | var ref = require('ssb-ref') |
2 | 2 | var ui = require('../ui') |
3 | 3 | var Scroller = require('pull-scroll') |
4 | +var h = require('hyperscript') | |
5 | +var pull = require('pull-stream') | |
6 | +var u = require('../util') | |
4 | 7 | |
5 | 8 | var plugs = require('../plugs') |
6 | 9 | var sbot_user_feed = plugs.first(exports.sbot_user_feed = []) |
7 | 10 | var message_render = plugs.first(exports.message_render = []) |
11 | +var avatar_profile = plugs.first(exports.avatar_profile = []) | |
8 | 12 | |
9 | 13 | exports.screen_view = function (id, sbot) { |
10 | 14 | //TODO: header of user info, avatars, names, follows. |
11 | 15 | |
12 | 16 | if(ref.isFeed(id)) { |
13 | - return ui.createStream( | |
14 | - sbot_user_feed({id: id, reverse: true}), | |
15 | - message_render | |
17 | + | |
18 | + var content = h('div.column') | |
19 | + var div = h('div.column', | |
20 | + {style: {'overflow':'auto'}}, | |
21 | + h('div', avatar_profile(id)), | |
22 | + content | |
16 | 23 | ) |
24 | + | |
25 | + pull( | |
26 | + sbot_user_feed({id: id, old: false, live: true}), | |
27 | + Scroller(div, content, message_render, true, false) | |
28 | + ) | |
29 | + | |
30 | + //how to handle when have scrolled past the start??? | |
31 | + | |
32 | + pull( | |
33 | + u.next(sbot_user_feed, { | |
34 | + id: id, reverse: true, | |
35 | + limit: 50, live: false | |
36 | + }, ['value', 'sequence']), | |
37 | + Scroller(div, content, message_render, false, false) | |
38 | + ) | |
39 | + | |
40 | + return div | |
41 | + | |
17 | 42 | } |
18 | 43 | } |
19 | 44 | |
20 | 45 | |
46 | + | |
47 | + | |
48 | + | |
49 | + | |
50 | + |
modules/follow.js | ||
---|---|---|
@@ -1,11 +1,13 @@ | ||
1 | 1 | var h = require('hyperscript') |
2 | 2 | var u = require('../util') |
3 | 3 | var avatar = require('../plugs').first(exports.avatar = []) |
4 | +var pull = require('pull-stream') | |
5 | +var plugs = require('../plugs') | |
4 | 6 | |
5 | 7 | //render a message when someone follows someone, |
6 | 8 | //so you see new users |
7 | -exports.message_content = function (msg, sbot) { | |
9 | +exports.message_content = function (msg) { | |
8 | 10 | |
9 | 11 | if(msg.value.content.type == 'contact') { |
10 | 12 | return h('div.contact', |
11 | 13 | 'follows', |
@@ -13,11 +15,74 @@ | ||
13 | 15 | ) |
14 | 16 | } |
15 | 17 | } |
16 | 18 | |
19 | +var sbot_links2 = plugs.first(exports.sbot_links2 = []) | |
20 | +var sbot_whoami = plugs.first(exports.sbot_whoami = []) | |
21 | +var message_confirm = plugs.first(exports.message_confirm = []) | |
17 | 22 | |
23 | +function follows (source, dest, cb) { | |
24 | + pull( | |
25 | + sbot_links2({query:[ | |
26 | + {$filter: { | |
27 | + source: source, | |
28 | + dest: dest, | |
29 | + rel: ['contact', {$gt: null}] | |
30 | + }}, | |
31 | + {$map: { | |
32 | + timestamp: 'timestamp', follows: ["rel", 1] | |
33 | + }} | |
34 | + ]}), | |
35 | + pull.collect(function (err, ary) { | |
36 | + if(err) return cb(err) | |
37 | + cb(null, | |
38 | + ary.length ? ary.sort(function (a, b) { | |
39 | + return a.timestamp - b.timestamp | |
40 | + }).pop().follows : false | |
41 | + ) | |
42 | + }) | |
43 | + ) | |
44 | +} | |
18 | 45 | |
46 | +exports.avatar_action = function (id) { | |
47 | + var follows_you, you_follow | |
19 | 48 | |
49 | + sbot_whoami(function (err, me) { | |
50 | + follows(me.id, id, function (err, f) { | |
51 | + you_follow = f | |
52 | + update() | |
53 | + }) | |
54 | + follows(id, me.id, function (err, f) { | |
55 | + follows_you = f | |
56 | + update() | |
57 | + }) | |
58 | + }) | |
20 | 59 | |
60 | + var state = h('label') | |
61 | + var label = h('label') | |
21 | 62 | |
63 | + function update () { | |
64 | + state.textContent = ( | |
65 | + follows_you && you_follow ? 'friend' | |
66 | + : follows_you ? 'follows you' | |
67 | + : you_follow ? 'you follow' | |
68 | + : '' | |
69 | + ) | |
22 | 70 | |
71 | + label.textContent = you_follow ? 'unfollow' : 'follow' | |
72 | + } | |
23 | 73 | |
74 | + return h('div', state, | |
75 | + h('a', {href:'#', onclick: function () { | |
76 | + message_confirm({ | |
77 | + type: 'contact', | |
78 | + contact: id, | |
79 | + following: !you_follow | |
80 | + }, function (err) { | |
81 | + //TODO: update after following. | |
82 | + }) | |
83 | + }}, label) | |
84 | + ) | |
85 | +} | |
86 | + | |
87 | + | |
88 | + |
modules/names.js | ||
---|---|---|
@@ -5,13 +5,15 @@ | ||
5 | 5 | pull(stream, pull.collect(cb)) |
6 | 6 | } |
7 | 7 | |
8 | 8 | var plugs = require('../plugs') |
9 | - | |
9 | +var getAvatar = require('ssb-avatar') | |
10 | 10 | var sbot_links2 = plugs.first(exports.sbot_links2 = []) |
11 | +var sbot_links = plugs.first(exports.sbot_links = []) | |
12 | +var sbot_whoami = plugs.first(exports.sbot_whoami = []) | |
11 | 13 | |
12 | -exports.avatar_name = | |
13 | -function name (id, sbot) { | |
14 | +exports.avatar_name = | |
15 | +function name (id) { | |
14 | 16 | var n = h('span', id.substring(0, 10)) |
15 | 17 | |
16 | 18 | //choose the most popular name for this person. |
17 | 19 | //for anything like this you'll see I have used sbot.links2 |
@@ -32,8 +34,19 @@ | ||
32 | 34 | }} |
33 | 35 | ]}), |
34 | 36 | function (err, names) { |
35 | 37 | if(err) throw err |
38 | + //if they have not been mentioned, fallback | |
39 | + //to patchwork style naming (i.e. self id) | |
40 | + if(!names.length) | |
41 | + return sbot_whoami(function (err, me) { | |
42 | + getAvatar({links: sbot_links}, me.id, id, | |
43 | + function (err, avatar) { | |
44 | + console.log(avatar) | |
45 | + n.textContent = avatar.name | |
46 | + }) | |
47 | + }) | |
48 | + | |
36 | 49 | n.textContent = names.reduce(function (max, item) { |
37 | 50 | return max.count > item.count ? max : item |
38 | 51 | }, {name: id.substring(0, 10), count: 0}).name |
39 | 52 | }) |
sbot-api.js | ||
---|---|---|
@@ -14,10 +14,8 @@ | ||
14 | 14 | if(err && !onHash) throw err |
15 | 15 | onHash && onHash(err, '&'+hash.digest('base64')+'.sha256') |
16 | 16 | }) |
17 | 17 | } |
18 | - | |
19 | - | |
20 | 18 | var createClient = require('ssb-client') |
21 | 19 | |
22 | 20 | module.exports = function () { |
23 | 21 | var sbot = null |
@@ -25,8 +23,9 @@ | ||
25 | 23 | createClient(function (err, _sbot) { |
26 | 24 | if(err) return isConn(err) |
27 | 25 | sbot = _sbot |
28 | 26 | sbot.on('closed', function () { |
27 | + sbot = null | |
29 | 28 | isConn(new Error('closed')) |
30 | 29 | }) |
31 | 30 | isConn() |
32 | 31 | }) |
@@ -62,6 +61,4 @@ | ||
62 | 61 | }) |
63 | 62 | } |
64 | 63 | } |
65 | 64 | |
66 | - | |
67 | - |
Built with git-ssb-web