Files: c18b233c6d62741496978cd8dc1f34a093946f8b / about / obs.js
2041 bytesRaw
1 | var {Value, Struct, computed} = require('mutant') |
2 | var Abortable = require('pull-abortable') |
3 | var pull = require('pull-stream') |
4 | var msgs = require('ssb-msgs') |
5 | var nest = require('depnest') |
6 | |
7 | exports.needs = nest({ |
8 | 'sbot.pull.userFeed': 'first', |
9 | 'blob.sync.url': 'first' |
10 | }) |
11 | exports.gives = nest({ |
12 | 'about.obs': [ |
13 | 'name', |
14 | 'image', |
15 | 'imageUrl' |
16 | ] |
17 | }) |
18 | |
19 | exports.create = function (api) { |
20 | var cache = {} |
21 | |
22 | return nest({ |
23 | 'about.obs': { |
24 | name: (id) => get(id).displayName, |
25 | image: (id) => get(id).image, |
26 | imageUrl: (id) => { |
27 | return computed(get(id).image, (image) => { |
28 | var obj = msgs.link(image, 'blob') |
29 | if (obj) { |
30 | return api.blob_url(obj.link) |
31 | } |
32 | }) |
33 | } |
34 | } |
35 | }) |
36 | |
37 | } |
38 | |
39 | function get (id) { |
40 | if (!cache[id]) { |
41 | cache[id] = About(api, id) |
42 | } |
43 | return cache[id] |
44 | } |
45 | } |
46 | |
47 | function About (api, id) { |
48 | // naive about that only looks at what a feed asserts about itself |
49 | |
50 | var obs = Struct({ |
51 | displayName: Value(id.slice(1, 10)), |
52 | image: Value() |
53 | }) |
54 | |
55 | var hasName = false |
56 | var hasImage = false |
57 | |
58 | var abortable = Abortable() |
59 | |
60 | // search history |
61 | pull( |
62 | api.sbot.pull.userFeed({reverse: true, id}), |
63 | abortable, |
64 | pull.drain(function (item) { |
65 | update(item) |
66 | if (hasName && obs.image()) { |
67 | abortable.abort() |
68 | } |
69 | }) |
70 | ) |
71 | |
72 | // get live changes |
73 | pull( |
74 | api.sbot.pull.userFeed({old: false, id}), |
75 | pull.drain(update) |
76 | ) |
77 | |
78 | return obs |
79 | |
80 | // scoped |
81 | |
82 | function update (item) { |
83 | if (item.value && item.value.content.type === 'about' && item.value.content.about === id) { |
84 | if (item.value.content.name) { |
85 | if (!hasName || hasName < item.value.timestamp) { |
86 | hasName = item.value.timestamp |
87 | obs.displayName.set(item.value.content.name) |
88 | } |
89 | } |
90 | if (item.value.content.image) { |
91 | if (!hasImage || hasImage < item.value.timestamp) { |
92 | hasImage = item.value.timestamp |
93 | obs.image.set(item.value.content.image) |
94 | } |
95 | } |
96 | } |
97 | } |
98 | } |
99 |
Built with git-ssb-web