git ssb

30+

cel / git-ssb-web



Tree: 0411de2819fd37e1f9396464199f86706819a483

Files: 0411de2819fd37e1f9396464199f86706819a483 / lib / about.js

2852 bytesRaw
1/* ssb-about
2 * factored out of ssb-notifier
3 *
4 * TODO:
5 * - publish as own module
6 * - handle live updates and reconnecting
7 * - deprecate when ssb-names is used in scuttlebot
8 */
9
10var pull = require('pull-stream')
11var cat = require('pull-cat')
12var asyncMemo = require('asyncmemo')
13var u = require('./util')
14
15function getLink(obj) {
16 return typeof obj === 'string' ? obj : obj ? obj.link : null
17}
18
19module.exports = function (sbot, id) {
20 var getAbout = asyncMemo(getAboutFull, sbot, id)
21
22 getAbout.getName = function (id, cb) {
23 getAbout(id, function (err, about) {
24 cb(err, about && about.name)
25 })
26 }
27
28 getAbout.getImage = function (id, cb) {
29 getAbout(id, function (err, about) {
30 cb(err, about && about.image)
31 })
32 }
33
34 return getAbout
35}
36
37// Get About info (name and icon) for a feed.
38function getAboutFull(sbot, source, dest, cb) {
39 var info = {}
40 var target = dest.target || dest
41 var owner = dest.owner || dest
42
43 pull(
44 cat([
45 // First get About info that we gave them.
46 sbot.links({
47 source: source,
48 dest: target,
49 rel: 'about',
50 values: true,
51 reverse: true
52 }),
53 // If that isn't enough, then get About info that they gave themselves.
54 sbot.links({
55 source: owner,
56 dest: target,
57 rel: 'about',
58 values: true,
59 reverse: true
60 }),
61 // If that isn't enough, then get About info from other feeds
62 sbot.links({
63 dest: target,
64 rel: 'about',
65 values: true,
66 reverse: true
67 }),
68 ]),
69 pull.filter(function (msg) {
70 return msg && msg.value && msg.value.content
71 }),
72 pull.drain(function (msg) {
73 if (info.name && info.image) return false
74 var c = msg.value.content
75 if (!info.name && c.name)
76 info.name = c.name
77 if (!info.image && c.image)
78 info.image = getLink(c.image)
79 }, function (err) {
80 if (err && err !== true) return cb(err)
81 if (!info.name) info.name = u.truncate(target, 20)
82 cb(null, info)
83 })
84 )
85
86 // Keep updated as changes are made
87 pull(
88 sbot.links({
89 dest: target,
90 rel: 'about',
91 live: true,
92 old: false,
93 values: true,
94 }),
95 pull.drain(function (msg) {
96 if (!msg.value) return
97 var c = msg.value.content
98 if (!c) return
99 if (msg.value.author == source || msg.value.author == owner) {
100 // TODO: give about from source (self) priority over about from owner
101 if (c.name)
102 info.name = c.name
103 if (c.image)
104 info.image = getLink(c.image)
105 } else {
106 if (c.name && !info.name)
107 info.name = c.name
108 if (c.image && !info.image)
109 info.image = getLink(c.image)
110 }
111 }, function (err) {
112 if (err) console.error('about', err)
113 })
114 )
115}
116

Built with git-ssb-web