git ssb

0+

ev / microbay



forked from Dominic / patchbay

Tree: 7745cee71776d98cebb4b0e9225e82484ffb1792

Files: 7745cee71776d98cebb4b0e9225e82484ffb1792 / modules / avatar-image.js

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

Built with git-ssb-web