git ssb

7+

dinoworm ๐Ÿ› / patchcore



Commit 5d2368fbb63ed026f1ec95ac3ccf520f21aab635

switch to using ssb-friends instead of ssb-contacts

mix irving committed on 9/14/2017, 9:18:37 PM
Parent: 4e0dae39f1a245880675dff53fc9bfff65262448

Files changed

contact/obs.jschanged
contact/obs.jsView
@@ -1,6 +1,6 @@
11 var nest = require('depnest')
2-var {Value, computed} = require('mutant')
2+var { Value, Dict, computed } = require('mutant')
33 var pull = require('pull-stream')
44 var ref = require('ssb-ref')
55
66 exports.needs = nest({
@@ -13,63 +13,98 @@
1313 })
1414
1515 exports.create = function (api) {
1616 var cacheLoading = false
17- var cache = {}
17+ var cache = Dict()
1818 var sync = Value(false)
1919
2020 return nest({
2121 'contact.obs': {
22- following: (id) => values(get(id), 'following', true),
23- followers: (id) => values(get(id), 'followers', true),
22+ following: following,
23+ followers: followers,
2424 blocking: (id) => values(get(id), 'blocking', true),
2525 blockers: (id) => values(get(id), 'blockers', true),
2626 },
2727 'sbot.hook.publish': function (msg) {
28- if (isContact(msg)) {
29- // HACK: make interface more responsive when sbot is busy
30- var source = msg.value.author
31- var dest = msg.value.content.contact
32- if (typeof msg.value.content.following === 'boolean') {
33- update(source, {
34- following: {
35- [dest]: [msg.value.content]
36- }
37- })
38- update(dest, {
39- followers: {
40- [source]: [msg.value.content]
41- }
42- })
43- }
44- if (typeof msg.value.content.blocking === 'boolean') {
45- update(source, {
46- blocking: {
47- [dest]: [msg.value.content]
48- }
49- })
50- update(dest, {
51- blockers: {
52- [source]: [msg.value.content]
53- }
54- })
55- }
56- }
28+ // TODO ???
29+ // if (isContact(msg)) {
30+ // // HACK: make interface more responsive when sbot is busy
31+ // var source = msg.value.author
32+ // var dest = msg.value.content.contact
33+
34+ // if (typeof msg.value.content.following === 'boolean') {
35+ // update(source, {
36+ // following: {
37+ // [dest]: [msg.value.content]
38+ // }
39+ // })
40+ // update(dest, {
41+ // followers: {
42+ // [source]: [msg.value.content]
43+ // }
44+ // })
45+ // }
46+ // if (typeof msg.value.content.blocking === 'boolean') {
47+ // update(source, {
48+ // blocking: {
49+ // [dest]: [msg.value.content]
50+ // }
51+ // })
52+ // update(dest, {
53+ // blockers: {
54+ // [source]: [msg.value.content]
55+ // }
56+ // })
57+ // }
58+ // }
5759 }
5860 })
5961
62+ function following (key) {
63+ var obs = computed(get(key), state => {
64+ return Object.keys(state)
65+ .reduce((sofar, next) => {
66+ if (state[next]) return [...sofar, next]
67+ else return sofar
68+ }, [])
69+ })
70+
71+ obs.sync = sync
72+ return obs
73+ }
74+
75+ function followers (key) {
76+ var obs = computed(cache, cache => {
77+ return Object.keys(cache)
78+ .reduce((sofar, next) => {
79+ if (cache[next][key]) return [...sofar, next]
80+ else return sofar
81+ }, [])
82+ })
83+ // var obs = computed(cache.keys, keys => {
84+ // return keys
85+ // .reduce((sofar, next) => {
86+ // if (get(next)()[key]) return [...sofar, next]
87+ // else return sofar
88+ // }, [])
89+ // })
90+
91+ obs.sync = sync
92+ return obs
93+ }
94+
6095 function values (state, key, compare) {
6196 var obs = computed([state, key, compare], getIds)
6297 obs.sync = sync
6398 return obs
6499 }
65100
66101 function loadCache () {
67102 pull(
68- api.sbot.pull.stream(sbot => sbot.contacts.stream({live: true})),
103+ api.sbot.pull.stream(sbot => sbot.friends.stream({live: true})),
69104 pull.drain(item => {
70- for (var target in item) {
71- if (ref.isFeed(target)) update(target, item[target])
105+ for (var source in item) {
106+ if (ref.isFeed(source)) update(source, item[source])
72107 }
73108
74109 if (!sync()) {
75110 sync.set(true)
@@ -77,23 +112,34 @@
77112 })
78113 )
79114 }
80115
81- function update (id, values) {
82- // values = { following, followers, blocking, blockedBy, ... }
83- var state = get(id)
116+ function update (sourceId, values) {
117+ // ssb-contacts: values = { following, followers, blocking, blockers, ... }
118+ // ssb-friends: values = {
119+ // keyA: true|null|false (friend, neutral, block)
120+ // keyB: true|null|false (friend, neutral, block)
121+ // }
122+ var state = get(sourceId)
84123 var lastState = state()
85124 var changed = false
86- for (var key in values) {
87- var valuesForKey = lastState[key] = lastState[key] || {}
88- for (var dest in values[key]) {
89- var value = values[key][dest]
90- if (!valuesForKey[dest] || value[1] > valuesForKey[dest][1] || !values[1] || !valuesForKey[dest[1]]) {
91- valuesForKey[dest] = value
92- changed = true
93- }
125+ for (var targetId in values) {
126+ if (values[targetId] != lastState[targetId]) {
127+ lastState[targetId] = values[targetId]
128+ changed = true
94129 }
95130 }
131+ // for (var key in values) {
132+
133+ // var valuesForKey = lastState[key] = lastState[key] || {}
134+ // for (var dest in values[key]) {
135+ // var value = values[key][dest]
136+ // if (!valuesForKey[dest] || value[1] > valuesForKey[dest][1] || !values[1] || !valuesForKey[dest[1]]) {
137+ // valuesForKey[dest] = value
138+ // changed = true
139+ // }
140+ // }
141+ // }
96142 if (changed) {
97143 state.set(lastState)
98144 }
99145 }
@@ -103,12 +149,12 @@
103149 if (!cacheLoading) {
104150 cacheLoading = true
105151 loadCache()
106152 }
107- if (!cache[id]) {
108- cache[id] = Value({})
153+ if (!cache.has(id)) {
154+ cache.put(id, Value({}))
109155 }
110- return cache[id]
156+ return cache.get(id)
111157 }
112158 }
113159
114160 function getIds (state, key, compare) {

Built with git-ssb-web