git ssb

1+

Daan Patchwork / patchwork



Tree: 3451510316992d414ec76ba5b29681fe359b7428

Files: 3451510316992d414ec76ba5b29681fe359b7428 / lib / depject / about / obs.js

3856 bytesRaw
1const shortFeedId = require('./sync.js')
2const { computed } = require('mutant')
3const nest = require('depnest')
4const ref = require('ssb-ref')
5const colorHash = new (require('color-hash'))()
6const fallbackImageUrl = 'data:image/gif;base64,R0lGODlhAQABAPAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='
7const MutantPullValue = require('../../mutant-pull-value')
8const MutantPullDict = require('../../mutant-pull-dict')
9
10exports.needs = nest({
11 'sbot.pull.stream': 'first',
12 'blob.sync.url': 'first'
13})
14
15exports.gives = nest({
16 'about.obs': [
17 'name',
18 'description',
19 'image',
20 'imageUrl',
21 'names',
22 'images',
23 'color',
24 'latestValue',
25 'valueFrom',
26 'socialValue',
27 'groupedValues',
28 'socialValues'
29 ]
30})
31
32exports.create = function (api) {
33 const socialValueCache = {}
34 return nest({
35 'about.obs': {
36 // quick helpers, probably should deprecate!
37 name: (id) => socialValue(id, 'name', shortFeedId(id)),
38 description: (id) => socialValue(id, 'description'),
39 image: (id) => socialValue(id, 'image'),
40 names: (id) => groupedValues(id, 'name'),
41 images: (id) => groupedValues(id, 'image'),
42 color: (id) => computed(id, (id) => colorHash.hex(id)),
43 imageUrl: (id) => computed(socialValue(id, 'image'), (blobId) => {
44 return blobId ? api.blob.sync.url(blobId) : fallbackImageUrl
45 }),
46
47 // custom abouts (the future!)
48 valueFrom,
49 latestValue,
50 socialValue,
51 socialValues,
52 groupedValues
53 }
54 })
55
56 function valueFrom (id, key, authorId) {
57 if (!ref.isLink(id)) throw new Error('About requires an ssb ref!')
58 return MutantPullValue(() => {
59 return api.sbot.pull.stream((sbot) => sbot.about.latestValueStream({ dest: id, key, authorId }))
60 })
61 }
62
63 function latestValue (id, key) {
64 if (!ref.isLink(id)) throw new Error('About requires an ssb ref!')
65 return MutantPullValue(() => {
66 return api.sbot.pull.stream((sbot) => sbot.about.latestValueStream({ dest: id, key }))
67 })
68 }
69
70 function socialValue (id, key, defaultValue) {
71 if (!ref.isLink(id)) throw new Error('About requires an ssb ref!')
72 if (!socialValueCache[id + '/' + key]) {
73 const obs = socialValueCache[id + '/' + key] = MutantPullValue(() => {
74 return api.sbot.pull.stream((sbot) => sbot.about.socialValueStream({ dest: id, key }))
75 }, {
76 onListen: () => { socialValueCache[id + '/' + key] = obs },
77 onUnlisten: () => delete socialValueCache[id + '/' + key]
78 })
79 }
80 return withDefault(socialValueCache[id + '/' + key], defaultValue)
81 }
82
83 function socialValues (id, key) {
84 if (!ref.isLink(id)) throw new Error('About requires an ssb ref!')
85 return MutantPullDict(() => {
86 return api.sbot.pull.stream((sbot) => sbot.about.socialValuesStream({ dest: id, key }))
87 }, { checkDelete })
88 }
89
90 function groupedValues (id, key) {
91 if (!ref.isLink(id)) throw new Error('About requires an ssb ref!')
92 return computed(socialValues(id, key), getGroupedValues)
93 }
94}
95
96function checkDelete (msg) {
97 if (msg && msg.remove) return true
98}
99
100function getGroupedValues (lookup) {
101 const values = {}
102 for (const author in lookup) {
103 const value = getValue(lookup[author])
104 if (value != null) {
105 values[value] = values[value] || []
106 values[value].push(author)
107 }
108 }
109 return values
110}
111
112function getValue (item) {
113 if (typeof item === 'string') {
114 return item
115 } else if (item && item.link && ref.isLink(item.link) && !item.remove) {
116 return item.link
117 }
118}
119
120function withDefault (value, defaultValue) {
121 if (typeof defaultValue === 'undefined') {
122 return value
123 } else {
124 return computed([value, defaultValue], fallback)
125 }
126}
127
128function fallback (value, defaultValue) {
129 if (value == null) {
130 return defaultValue
131 } else {
132 return value
133 }
134}
135

Built with git-ssb-web