git ssb

7+

dinoworm ๐Ÿ› / patchcore



Tree: 8261134eb7daa7f16ad7f83ebb1549e60b37b70d

Files: 8261134eb7daa7f16ad7f83ebb1549e60b37b70d / observables / about.js

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

Built with git-ssb-web