Files: 38af2fdb5719a156793f1114d29328b6f0386cd7 / modules / avatar-image.js
2192 bytesRaw
1 | |
2 | var getAvatar = require('ssb-avatar') |
3 | var h = require('hyperscript') |
4 | var ref = require('ssb-ref') |
5 | var path = require('path') |
6 | |
7 | var plugs = require('../plugs') |
8 | var sbot_query = plugs.first(exports.sbot_query = []) |
9 | var blob_url = require('../plugs').first(exports.blob_url = []) |
10 | |
11 | var pull = require('pull-stream') |
12 | |
13 | var id = require('../keys').id |
14 | |
15 | var default_avatar = path.join(__dirname, 'avatar_fallback.png') |
16 | |
17 | var avatars = AVATARS = {} |
18 | |
19 | function isFunction (f) { |
20 | return 'function' === typeof f |
21 | } |
22 | |
23 | var self_id = require('../keys').id |
24 | |
25 | var ready = false |
26 | var waiting = [] |
27 | |
28 | var last = 0 |
29 | |
30 | //blah blah |
31 | exports.connection_status = function (err) { |
32 | if (err) return |
33 | pull( |
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 | |
78 | exports.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