-
Notifications
You must be signed in to change notification settings - Fork 2
/
avatar.js
42 lines (38 loc) · 1.36 KB
/
avatar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var ref = require('ssb-ref')
var toUrl = require('yap-util').toUrl
module.exports = function (sbot) {
return function (opts, apply) {
//accept feed directly, so you can do map(api.avatar)
if(ref.isFeed(opts))
opts = {id: opts}
if(ref.isFeed(opts.link))
opts = {id: opts.link}
var _image = opts.image !== false //defaults to true
var _name = opts.name === true //defaults to false
if(!opts.id) throw new Error('missing id, had:'+JSON.stringify(opts))
return function (cb) {
if(!ref.isFeed(opts.id))
return cb(new Error('expected valid feed id as id'))
sbot.names.getImageFor(opts.id, function (err, blobId) {
sbot.names.getSignifier(opts.id, function (err, name) {
cb(null,
['a.Avatar',
Object.assign(
{href: opts.href || toUrl('patch/public', {author:opts.id})},
apply.cacheAttrs(toUrl('avatar', opts), opts.id)
),
_image ? ['img', {
className:'avatar',
src: '/blobs/get/'+blobId,
//getSignifier returns id as name if there isn't a name available.
title: name !== opts.id ? name+'\n'+opts.id : opts.id
}] : '',
_image && _name ? ['br'] : '',
_name ? name : ''
]
)
})
})
}
}
}