git ssb

30+

cel / git-ssb-web



Commit 568953d78d77276b131c221c8c15f5e4af5edbb4

Rewrite About to use a single live stream

cel committed on 10/19/2016, 1:34:32 AM
Parent: c870aa737762e1030c802a7cdddd3a9c494d279b

Files changed

index.jschanged
lib/about.jschanged
index.jsView
@@ -161,13 +161,9 @@
161161
162162 G.getRepoName = function (ownerId, repoId, cb) {
163163 this.about.getName({
164164 owner: ownerId,
165- target: repoId,
166- toString: function () {
167- // hack to fit two parameters into asyncmemo
168- return ownerId + '/' + repoId
169- }
165 + target: repoId
170166 }, cb)
171167 }
172168
173169 G.getRepoFullName = function (author, repoId, cb) {
lib/about.jsView
@@ -1,126 +1,98 @@
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-
101 var pull = require('pull-stream')
112 var cat = require('pull-cat')
12-var asyncMemo = require('asyncmemo')
133 var u = require('./util')
144 var ref = require('ssb-ref')
5 +var asyncMemo = require('asyncmemo')
156
16-function getLink(obj) {
17- return typeof obj === 'string' ? obj : obj ? obj.link : null
7 +function mixin(a, b) {
8 + if (b) for (var k in b) a[k] = b[k]
189 }
1910
20-module.exports = function (sbot, id) {
21- var getAbout = asyncMemo(getAboutFull, sbot, id)
11 +module.exports = function (sbot, source) {
12 + var abouts = {/* link: {feed: name} */}
13 + var msgs = {/* key: content */}
2214
23- getAbout.getName = function (id, cb) {
24- getAbout(id, function (err, about) {
25- cb(err, about && about.name)
15 + pull(
16 + sbot.createLogStream({old: false}),
17 + pull.drain(processMsg, function (err) {
18 + if (err) console.error('about', err)
2619 })
27- }
20 + )
2821
29- getAbout.getImage = function (id, cb) {
30- getAbout(id, function (err, about) {
31- cb(err, about && about.image)
32- })
33- }
34-
35- return getAbout
36-}
37-
38-// Get About info (name and icon) for a feed.
39-function getAboutFull(sbot, source, dest, cb) {
40- var info = {}
41- var target = dest.target || dest
42- var owner = dest.owner || dest
43-
44- pull(
45- cat([
46- // First get About info that we gave them.
22 + var getAboutInfo = asyncMemo(function (id, cb) {
23 + var abt = abouts[id] = {}
24 + var _err
25 + var w = 1
26 + if (ref.isMsg(id)) {
27 + w++
28 + sbot.get(id, function (err, value) {
29 + if (err) console.error('about: missing message', id, err)
30 + msgs[id] = value && value.content
31 + if (!--w) next()
32 + })
33 + }
34 + pull(
4735 sbot.links({
48- source: source,
49- dest: target,
5036 rel: 'about',
5137 values: true,
52- reverse: true
38 + dest: id
5339 }),
54- // If that isn't enough, then get About info that they gave themselves.
55- sbot.links({
56- source: owner,
57- dest: target,
58- rel: 'about',
59- values: true,
60- reverse: true
61- }),
62- // If that isn't enough, then get About info from other feeds
63- sbot.links({
64- dest: target,
65- rel: 'about',
66- values: true,
67- reverse: true
68- }),
69- // Finally, get About info from the thing itself (if possible)
70- u.readOnce(function (cb) {
71- if (ref.isMsg(target)) {
72- sbot.get(target, function (err, value) {
73- cb(null, {key: target, value: value})
74- })
75- } else {
76- cb()
77- }
40 + pull.drain(processMsg, function (err) {
41 + _err = err
42 + if (!--w) next()
7843 })
79- ]),
80- pull.filter(function (msg) {
81- return msg && msg.value && msg.value.content
82- }),
83- pull.drain(function (msg) {
84- if (info.name && info.image) return false
85- var c = msg.value.content
86- if (!info.name && c.name)
87- info.name = c.name
88- if (!info.image && c.image)
89- info.image = getLink(c.image)
90- }, function (err) {
91- if (err && err !== true) return cb(err)
92- if (!info.name) info.name = u.truncate(target, 20)
93- cb(null, info)
94- })
95- )
44 + )
45 + function next() {
46 + cb(_err, abt)
47 + }
48 + })
9649
97- // Keep updated as changes are made
98- pull(
99- sbot.links({
100- dest: target,
101- rel: 'about',
102- live: true,
103- old: false,
104- values: true,
105- }),
106- pull.drain(function (msg) {
107- if (!msg.value) return
108- var c = msg.value.content
109- if (!c) return
110- if (msg.value.author == source || msg.value.author == owner) {
111- // TODO: give about from source (self) priority over about from owner
112- if (c.name)
113- info.name = c.name
114- if (c.image)
115- info.image = getLink(c.image)
116- } else {
117- if (c.name && !info.name)
118- info.name = c.name
119- if (c.image && !info.image)
120- info.image = getLink(c.image)
50 + function processMsg(msg) {
51 + var c = msg.value.content
52 + if (!c) return
53 +
54 + // handle receiving a message after receiving a link to it
55 + if (msg.key in abouts) msgs[msg.key] = c
56 +
57 + var target = c.about
58 + if (!target) return
59 + var abt = abouts[target]
60 + if (!abt) return
61 + var ab = abt[msg.value.author] || (abt[msg.value.author] = {})
62 + for (var key in c) {
63 + if (key === 'about' || key === 'type') continue
64 + var val = c[key]
65 + if (!val) delete ab[key]
66 + else {
67 + if (key === 'image' && typeof val === 'object' && val.link) {
68 + val = val.link
69 + }
70 + ab[key] = val
12171 }
122- }, function (err) {
123- if (err) console.error('about', err)
72 + }
73 + }
74 +
75 + function getAbout(dest, cb) {
76 + if (!dest) return cb(null, {})
77 + var target = dest.target || dest
78 + getAboutInfo(target, function (err, info) {
79 + if (err) return cb(err)
80 + // order of preference: source, owner, any, msg
81 + var ab = {}
82 + mixin(ab, msgs[target])
83 + for (var feed in info) mixin(ab, info[feed])
84 + mixin(ab, info[dest.owner || dest])
85 + mixin(ab, info[source])
86 + if (!ab.name) ab.name = u.truncate(target, 20)
87 + cb(null, ab)
12488 })
125- )
89 + }
90 +
91 + getAbout.getName = function (id, cb) {
92 + getAbout(id, function (err, about) {
93 + cb(err, about && about.name)
94 + })
95 + }
96 +
97 + return getAbout
12698 }

Built with git-ssb-web