git ssb

16+

Dominic / patchbay



Tree: 1d93c63544059ee8195e84d178b24ed4b2c39a2f

Files: 1d93c63544059ee8195e84d178b24ed4b2c39a2f / modules_basic / avatar-image.js

2096 bytesRaw
1
2var getAvatar = require('ssb-avatar')
3var h = require('hyperscript')
4var ref = require('ssb-ref')
5var path = require('path')
6var visualize = require('visualize-buffer')
7
8var plugs = require('../plugs')
9var sbot_query = plugs.first(exports.sbot_query = [])
10var blob_url = require('../plugs').first(exports.blob_url = [])
11
12var pull = require('pull-stream')
13
14var id = require('../keys').id
15
16var avatars = AVATARS = {}
17
18function isFunction (f) {
19 return 'function' === typeof f
20}
21
22var self_id = require('../keys').id
23
24var ready = false
25var waiting = []
26
27var last = 0
28
29//blah blah
30exports.connection_status = function (err) {
31 if (err) return
32pull(
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
77exports.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