git ssb

0+

ev / microbay



forked from Dominic / patchbay

Tree: 73f72d0a95a4ea7cf876a36e77fa8fef0fd601d8

Files: 73f72d0a95a4ea7cf876a36e77fa8fef0fd601d8 / modules / avatar-image.js

2186 bytesRaw
1
2var getAvatar = require('ssb-avatar')
3var h = require('hyperscript')
4var ref = require('ssb-ref')
5
6var plugs = require('../plugs')
7var sbot_query = plugs.first(exports.sbot_query = [])
8var blob_url = require('../plugs').first(exports.blob_url = [])
9
10var pull = require('pull-stream')
11
12var id = require('../keys').id
13
14var default_avatar = '&qjeAs8+uMXLlyovT4JnEpMwTNDx/QXHfOl2nv2u0VCM=.sha256'
15
16var avatars = AVATARS = {}
17
18function isFunction (f) {
19 return 'function' === typeof f
20}
21
22var self_id = require('../keys').id
23
24var ready = false
25var waiting = []
26
27var last = 0
28
29//blah blah
30exports.connection_status = function (err) {
31 if (err) return
32pull(
33 sbot_query({
34 query: [{
35 $filter: {
36 timestamp: {$gt: last || 0 },
37 value: { content: {
38 type: "about",
39 about: {$prefix: "@"},
40 image: {link: {$prefix: "&"}}
41 }}
42 }},
43 {
44 $map: {
45 id: ["value", "content", "about"],
46 image: ["value", "content", "image", "link"],
47 by: ["value", "author"],
48 ts: 'timestamp'
49 }}],
50 live: true
51 }),
52 pull.drain(function (a) {
53 if(a.sync) {
54 ready = true
55 while(waiting.length) waiting.shift()()
56 return
57 }
58 last = a.ts
59 //set image for avatar.
60 //overwrite another avatar
61 //you picked.
62 if(
63 //if there is no avatar
64 (!avatars[a.id]) ||
65 //if i chose this avatar
66 (a.by == self_id) ||
67 //they chose their own avatar,
68 //and current avatar was not chosen by me
69 (a.by === a.id && avatars[a.id].by != self_id)
70 )
71 avatars[a.id] = a
72
73 })
74)
75}
76
77exports.avatar_image = function (author, classes) {
78 classes = classes || ''
79 if(classes && 'string' === typeof classes) classes = '.avatar--'+classes
80
81 var img = h('img'+classes, {src: blob_url(default_avatar)})
82// getAvatar({links: sbot_links}, id, author, function (err, avatar) {
83// if (err) return console.error(err)
84// if(ref.isBlob(avatar.image))
85// img.src = blob_url(avatar.image)
86// })
87
88 function go () {
89 if(avatars[author]) img.src = blob_url(avatars[author].image)
90 }
91
92 if(!ready)
93 waiting.push(go)
94 else go()
95
96 return img
97}
98
99
100
101
102
103
104
105
106

Built with git-ssb-web