Files: d45e0ad160051c97802cf3566af0ab967fcc0c0e / modules_basic / avatar-image.js
2490 bytesRaw
1 | |
2 | var h = require('hyperscript') |
3 | var visualize = require('visualize-buffer') |
4 | |
5 | var pull = require('pull-stream') |
6 | |
7 | var self_id = require('../keys').id |
8 | |
9 | exports.needs = { |
10 | sbot_query: 'first', |
11 | blob_url: 'first' |
12 | } |
13 | |
14 | exports.gives = { |
15 | connection_status: true, avatar_image: true |
16 | } |
17 | |
18 | var ready = false |
19 | var waiting = [] |
20 | |
21 | var last = 0 |
22 | |
23 | var cache = {} |
24 | |
25 | exports.create = function (api) { |
26 | var avatars = {} |
27 | |
28 | //blah blah |
29 | return { |
30 | connection_status: function (err) { |
31 | if (err) return |
32 | pull( |
33 | api.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 | avatar_image: function (author, classes) { |
78 | classes = classes || '' |
79 | if(classes && 'string' === typeof classes) classes = '.avatar--'+classes |
80 | |
81 | function gen (id) { |
82 | if(cache[id]) return h('img', {src: cache[id]}) |
83 | var img = visualize(new Buffer(author.substring(1), 'base64'), 256) |
84 | cache[id] = img.src |
85 | return img |
86 | } |
87 | |
88 | var img = ready && avatars[author] ? h('img', {src: api.blob_url(avatars[author].image)}) : gen(author) |
89 | |
90 | ;(classes || '').split('.').filter(Boolean).forEach(function (c) { |
91 | img.classList.add(c) |
92 | }) |
93 | |
94 | if(!ready) |
95 | waiting.push(function () { |
96 | if(avatars[author]) img.src = api.blob_url(avatars[author].image) |
97 | }) |
98 | |
99 | return img |
100 | } |
101 | } |
102 | } |
103 |
Built with git-ssb-web