Files: 9334dd2b798b099cd5e9bd7e007f444359f466bb / modules / avatar-image.js
2096 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 | var visualize = require('visualize-buffer') |
7 | |
8 | var plugs = require('../plugs') |
9 | var sbot_query = plugs.first(exports.sbot_query = []) |
10 | var blob_url = require('../plugs').first(exports.blob_url = []) |
11 | |
12 | var pull = require('pull-stream') |
13 | |
14 | var id = require('../keys').id |
15 | |
16 | var avatars = AVATARS = {} |
17 | |
18 | function isFunction (f) { |
19 | return 'function' === typeof f |
20 | } |
21 | |
22 | var self_id = require('../keys').id |
23 | |
24 | var ready = false |
25 | var waiting = [] |
26 | |
27 | var last = 0 |
28 | |
29 | //blah blah |
30 | exports.connection_status = function (err) { |
31 | if (err) return |
32 | pull( |
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 | |
77 | exports.avatar_image = function (author, classes) { |
78 | classes = classes || '' |
79 | if(classes && 'string' === typeof classes) classes = '.avatar--'+classes |
80 | |
81 | var img = visualize(new Buffer(author.substring(1), 'base64'), 256) |
82 | ;(classes || '').split('.').filter(Boolean).forEach(function (c) { |
83 | img.classList.add(c) |
84 | }) |
85 | |
86 | function go () { |
87 | if(avatars[author]) img.src = blob_url(avatars[author].image) |
88 | } |
89 | |
90 | if(!ready) |
91 | waiting.push(go) |
92 | else go() |
93 | |
94 | return img |
95 | } |
96 | |
97 | |
98 | |
99 | |
100 | |
101 | |
102 | |
103 | |
104 | |
105 | |
106 | |
107 | |
108 | |
109 |
Built with git-ssb-web