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