Files: b65df85068d01bbc7fd6b0ba8686cccbae6928ee / modules / avatar-image.js
2227 bytesRaw
1 | |
2 | |
3 | var getAvatar = require('ssb-avatar') |
4 | var h = require('hyperscript') |
5 | var ref = require('ssb-ref') |
6 | var path = require('path') |
7 | var visualize = require('visualize-buffer') |
8 | var pull = require('pull-stream') |
9 | var self_id = require('../keys').id |
10 | |
11 | exports.needs = { |
12 | sbot_query: 'first', |
13 | blob_url: 'first' |
14 | } |
15 | |
16 | exports.gives = { |
17 | connection_status: true, |
18 | avatar_image: true |
19 | } |
20 | |
21 | function isFunction (f) { |
22 | return 'function' === typeof f |
23 | } |
24 | |
25 | var ready = false |
26 | var waiting = [] |
27 | |
28 | var last = 0 |
29 | |
30 | var cache = {} |
31 | |
32 | exports.create = function (api) { |
33 | var avatars = {} |
34 | return { |
35 | connection_status: function (err) { |
36 | if (err) return |
37 | pull( |
38 | api.sbot_query({ |
39 | query: [{ |
40 | $filter: { |
41 | timestamp: {$gt: last || 0 }, |
42 | value: { content: { |
43 | type: "about", |
44 | about: {$prefix: "@"}, |
45 | image: {$truthy: true} |
46 | }} |
47 | }}, |
48 | { |
49 | $map: { |
50 | id: ["value", "content", "about"], |
51 | image: ["value", "content", "image"], |
52 | by: ["value", "author"], |
53 | ts: 'timestamp' |
54 | }}], |
55 | live: true |
56 | }), |
57 | pull.drain(function (a) { |
58 | if(a.sync) { |
59 | ready = true |
60 | while(waiting.length) waiting.shift()() |
61 | return |
62 | } |
63 | last = a.ts |
64 | if (a.image && typeof a.image === 'object') a.image = a.image.link |
65 | //set image for avatar. |
66 | //overwrite another avatar |
67 | //you picked. |
68 | if( |
69 | //if there is no avatar |
70 | (!avatars[a.id]) || |
71 | //if i chose this avatar |
72 | (a.by == self_id) || |
73 | //they chose their own avatar, |
74 | //and current avatar was not chosen by me |
75 | (a.by === a.id && avatars[a.id].by != self_id) |
76 | ) |
77 | avatars[a.id] = a |
78 | }) |
79 | ) |
80 | }, |
81 | |
82 | |
83 | avatar_image: function (author, classes) { |
84 | var img = ready && avatars[author] ? api.blob_url(avatars[author].image) : "" |
85 | if(!ready) |
86 | waiting.push(function () { |
87 | if(avatars[author]) img = api.blob_url(avatars[author].image) |
88 | }) |
89 | |
90 | return img |
91 | } |
92 | } |
93 | } |
94 |
Built with git-ssb-web