git ssb

16+

Dominic / patchbay



Commit 5435933800608d5e5da0286230aaa90de27686f7

add avatars and follow

Dominic Tarr committed on 6/24/2016, 9:55:40 PM
Parent: e41be9be2c5756cd0ca92256d7d7122944415cf2

Files changed

index.jschanged
modules/avatar-image.jschanged
modules/avatar.jschanged
modules/feed.jschanged
modules/follow.jschanged
modules/like.jschanged
modules/names.jschanged
sbot-api.jschanged
index.jsView
@@ -30,4 +30,5 @@
3030
3131
3232
3333
34+
modules/avatar-image.jsView
@@ -6,9 +6,9 @@
66 var plugs = require('../plugs')
77 var sbot_whoami = plugs.first(exports.sbot_whoami = [])
88 var sbot_links = plugs.first(exports.sbot_links = [])
99
10-exports.avatar_image = function (author, sbot) {
10+exports.avatar_image = function (author) {
1111 var img = h('img', {src: 'http://localhost:7777/img/fallback.png'})
1212 sbot_whoami(function (err, me) {
1313 getAvatar({links: sbot_links}, me.id, author, function (err, avatar) {
1414 if(ref.isBlob(avatar.image))
modules/avatar.jsView
@@ -1,5 +1,4 @@
1-
21 var h = require('hyperscript')
32 var u = require('../util')
43
54
modules/feed.jsView
@@ -1,20 +1,50 @@
11 var ref = require('ssb-ref')
22 var ui = require('../ui')
33 var Scroller = require('pull-scroll')
4+var h = require('hyperscript')
5+var pull = require('pull-stream')
6+var u = require('../util')
47
58 var plugs = require('../plugs')
69 var sbot_user_feed = plugs.first(exports.sbot_user_feed = [])
710 var message_render = plugs.first(exports.message_render = [])
11+var avatar_profile = plugs.first(exports.avatar_profile = [])
812
913 exports.screen_view = function (id, sbot) {
1014 //TODO: header of user info, avatars, names, follows.
1115
1216 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
1623 )
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+
1742 }
1843 }
1944
2045
46+
47+
48+
49+
50+
modules/follow.jsView
@@ -1,11 +1,13 @@
11 var h = require('hyperscript')
22 var u = require('../util')
33 var avatar = require('../plugs').first(exports.avatar = [])
4+var pull = require('pull-stream')
5+var plugs = require('../plugs')
46
57 //render a message when someone follows someone,
68 //so you see new users
7-exports.message_content = function (msg, sbot) {
9+exports.message_content = function (msg) {
810
911 if(msg.value.content.type == 'contact') {
1012 return h('div.contact',
1113 'follows',
@@ -13,11 +15,74 @@
1315 )
1416 }
1517 }
1618
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 = [])
1722
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+}
1845
46+exports.avatar_action = function (id) {
47+ var follows_you, you_follow
1948
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+ })
2059
60+ var state = h('label')
61+ var label = h('label')
2162
63+ function update () {
64+ state.textContent = (
65+ follows_you && you_follow ? 'friend'
66+ : follows_you ? 'follows you'
67+ : you_follow ? 'you follow'
68+ : ''
69+ )
2270
71+ label.textContent = you_follow ? 'unfollow' : 'follow'
72+ }
2373
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/like.jsView
@@ -54,9 +54,4 @@
5454 }}, 'yup')
5555
5656 }
5757
58-
59-
60-
61-
62-
modules/names.jsView
@@ -5,13 +5,15 @@
55 pull(stream, pull.collect(cb))
66 }
77
88 var plugs = require('../plugs')
9-
9+var getAvatar = require('ssb-avatar')
1010 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 = [])
1113
12-exports.avatar_name =
13-function name (id, sbot) {
14+exports.avatar_name =
15+function name (id) {
1416 var n = h('span', id.substring(0, 10))
1517
1618 //choose the most popular name for this person.
1719 //for anything like this you'll see I have used sbot.links2
@@ -32,8 +34,19 @@
3234 }}
3335 ]}),
3436 function (err, names) {
3537 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+
3649 n.textContent = names.reduce(function (max, item) {
3750 return max.count > item.count ? max : item
3851 }, {name: id.substring(0, 10), count: 0}).name
3952 })
sbot-api.jsView
@@ -14,10 +14,8 @@
1414 if(err && !onHash) throw err
1515 onHash && onHash(err, '&'+hash.digest('base64')+'.sha256')
1616 })
1717 }
18-
19-
2018 var createClient = require('ssb-client')
2119
2220 module.exports = function () {
2321 var sbot = null
@@ -25,8 +23,9 @@
2523 createClient(function (err, _sbot) {
2624 if(err) return isConn(err)
2725 sbot = _sbot
2826 sbot.on('closed', function () {
27+ sbot = null
2928 isConn(new Error('closed'))
3029 })
3130 isConn()
3231 })
@@ -62,6 +61,4 @@
6261 })
6362 }
6463 }
6564
66-
67-

Built with git-ssb-web