git ssb

0+

ev / microbay



forked from Dominic / patchbay

Tree: 94d9ca2cbe74997f6ec36751cd631e2bf6305283

Files: 94d9ca2cbe74997f6ec36751cd631e2bf6305283 / modules / avatar-image.js

2149 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
30setTimeout(function () {
31pull(
32 sbot_query({
33 query: [{
34 $filter: {
35 timestamp: {$gt: last || 0 },
36 value: { content: {
37 type: "about",
38 about: {$prefix: "@"},
39 image: {link: {$prefix: "&"}}
40 }}
41 }},
42 {
43 $map: {
44 id: ["value", "content", "about"],
45 image: ["value", "content", "image", "link"],
46 by: ["value", "author"],
47 ts: 'timestamp'
48 }}],
49 live: true
50 }),
51 pull.drain(function (a) {
52 if(a.sync) {
53 ready = true
54 while(waiting.length) waiting.shift()()
55 return
56 }
57 last = a.ts
58 //set image for avatar.
59 //overwrite another avatar
60 //you picked.
61 if(
62 //if there is no avatar
63 (!avatars[a.id]) ||
64 //if i chose this avatar
65 (a.by == self_id) ||
66 //they chose their own avatar,
67 //and current avatar was not chosen by me
68 (a.by === a.id && avatars[a.id].by != self_id)
69 )
70 avatars[a.id] = a
71
72 })
73)
74})
75
76exports.avatar_image = function (author, classes) {
77 classes = classes || ''
78 if(classes && 'string' === typeof classes) classes = '.avatar--'+classes
79
80 var img = h('img'+classes, {src: blob_url(default_avatar)})
81// getAvatar({links: sbot_links}, id, author, function (err, avatar) {
82// if (err) return console.error(err)
83// if(ref.isBlob(avatar.image))
84// img.src = blob_url(avatar.image)
85// })
86
87 function go () {
88 if(avatars[author]) img.src = blob_url(avatars[author].image)
89 }
90
91 if(!ready)
92 waiting.push(go)
93 else go()
94
95 return img
96}
97
98
99
100
101
102
103
104
105

Built with git-ssb-web