git ssb

1+

ev / minbay



Tree: afe7de9580840925af61711b5ab0d6c57d1131a4

Files: afe7de9580840925af61711b5ab0d6c57d1131a4 / modules / avatar-image.js

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

Built with git-ssb-web