git ssb

16+

Dominic / patchbay



Tree: 010c28ba45ecc8a029b813a3cd9e7edc8e098059

Files: 010c28ba45ecc8a029b813a3cd9e7edc8e098059 / modules_basic / avatar-image.js

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

Built with git-ssb-web