Files: 74fe7cba24a1be16ab96edeb828f80c1740bef44 / modules_basic / avatar-image.js
2493 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 pull = require('pull-stream') |
9 | |
10 | var self_id = require('../keys').id |
11 | |
12 | //var plugs = require('../plugs') |
13 | //var sbot_query = plugs.first(exports.sbot_query = []) |
14 | //var blob_url = require('../plugs').first(exports.blob_url = []) |
15 | // |
16 | |
17 | exports.needs = { |
18 | sbot_query: 'first', |
19 | blob_url: 'first' |
20 | } |
21 | |
22 | exports.gives = { |
23 | connection_status: true, avatar_image: true |
24 | } |
25 | |
26 | |
27 | function isFunction (f) { |
28 | return 'function' === typeof f |
29 | } |
30 | |
31 | |
32 | var ready = false |
33 | var waiting = [] |
34 | |
35 | var last = 0 |
36 | |
37 | exports.create = function (api) { |
38 | var avatars = {} |
39 | |
40 | //blah blah |
41 | return { |
42 | connection_status: function (err) { |
43 | if (err) return |
44 | pull( |
45 | api.sbot_query({ |
46 | query: [{ |
47 | $filter: { |
48 | timestamp: {$gt: last || 0 }, |
49 | value: { content: { |
50 | type: "about", |
51 | about: {$prefix: "@"}, |
52 | image: {link: {$prefix: "&"}} |
53 | }} |
54 | }}, |
55 | { |
56 | $map: { |
57 | id: ["value", "content", "about"], |
58 | image: ["value", "content", "image", "link"], |
59 | by: ["value", "author"], |
60 | ts: 'timestamp' |
61 | }}], |
62 | live: true |
63 | }), |
64 | pull.drain(function (a) { |
65 | if(a.sync) { |
66 | ready = true |
67 | while(waiting.length) waiting.shift()() |
68 | return |
69 | } |
70 | last = a.ts |
71 | //set image for avatar. |
72 | //overwrite another avatar |
73 | //you picked. |
74 | if( |
75 | //if there is no avatar |
76 | (!avatars[a.id]) || |
77 | //if i chose this avatar |
78 | (a.by == self_id) || |
79 | //they chose their own avatar, |
80 | //and current avatar was not chosen by me |
81 | (a.by === a.id && avatars[a.id].by != self_id) |
82 | ) |
83 | avatars[a.id] = a |
84 | |
85 | }) |
86 | ) |
87 | }, |
88 | |
89 | avatar_image: function (author, classes) { |
90 | classes = classes || '' |
91 | |
92 | var img = visualize(new Buffer(author.substring(1), 'base64'), 256) |
93 | ;(classes || '').split('.').filter(Boolean).forEach(function (c) { |
94 | img.classList.add(c) |
95 | }) |
96 | |
97 | function go () { |
98 | if(avatars[author]) img.src = api.blob_url(avatars[author].image) |
99 | } |
100 | |
101 | if(!ready) |
102 | waiting.push(go) |
103 | else go() |
104 | |
105 | return img |
106 | } |
107 | } |
108 | } |
109 |
Built with git-ssb-web