git ssb

16+

Dominic / patchbay



Tree: 8645df8d5c4edf3b8fe98b8e3b257e80ab48d09d

Files: 8645df8d5c4edf3b8fe98b8e3b257e80ab48d09d / modules_basic / avatar / image.js

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

Built with git-ssb-web