Commit 5d2368fbb63ed026f1ec95ac3ccf520f21aab635
switch to using ssb-friends instead of ssb-contacts
mix irving committed on 9/14/2017, 9:18:37 PMParent: 4e0dae39f1a245880675dff53fc9bfff65262448
Files changed
contact/obs.js | changed |
contact/obs.js | ||
---|---|---|
@@ -1,6 +1,6 @@ | ||
1 | 1 | var nest = require('depnest') |
2 | -var {Value, computed} = require('mutant') | |
2 | +var { Value, Dict, computed } = require('mutant') | |
3 | 3 | var pull = require('pull-stream') |
4 | 4 | var ref = require('ssb-ref') |
5 | 5 | |
6 | 6 | exports.needs = nest({ |
@@ -13,63 +13,98 @@ | ||
13 | 13 | }) |
14 | 14 | |
15 | 15 | exports.create = function (api) { |
16 | 16 | var cacheLoading = false |
17 | - var cache = {} | |
17 | + var cache = Dict() | |
18 | 18 | var sync = Value(false) |
19 | 19 | |
20 | 20 | return nest({ |
21 | 21 | 'contact.obs': { |
22 | - following: (id) => values(get(id), 'following', true), | |
23 | - followers: (id) => values(get(id), 'followers', true), | |
22 | + following: following, | |
23 | + followers: followers, | |
24 | 24 | blocking: (id) => values(get(id), 'blocking', true), |
25 | 25 | blockers: (id) => values(get(id), 'blockers', true), |
26 | 26 | }, |
27 | 27 | '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 | + // } | |
57 | 59 | } |
58 | 60 | }) |
59 | 61 | |
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 | + | |
60 | 95 | function values (state, key, compare) { |
61 | 96 | var obs = computed([state, key, compare], getIds) |
62 | 97 | obs.sync = sync |
63 | 98 | return obs |
64 | 99 | } |
65 | 100 | |
66 | 101 | function loadCache () { |
67 | 102 | pull( |
68 | - api.sbot.pull.stream(sbot => sbot.contacts.stream({live: true})), | |
103 | + api.sbot.pull.stream(sbot => sbot.friends.stream({live: true})), | |
69 | 104 | 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]) | |
72 | 107 | } |
73 | 108 | |
74 | 109 | if (!sync()) { |
75 | 110 | sync.set(true) |
@@ -77,23 +112,34 @@ | ||
77 | 112 | }) |
78 | 113 | ) |
79 | 114 | } |
80 | 115 | |
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) | |
84 | 123 | var lastState = state() |
85 | 124 | 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 | |
94 | 129 | } |
95 | 130 | } |
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 | + // } | |
96 | 142 | if (changed) { |
97 | 143 | state.set(lastState) |
98 | 144 | } |
99 | 145 | } |
@@ -103,12 +149,12 @@ | ||
103 | 149 | if (!cacheLoading) { |
104 | 150 | cacheLoading = true |
105 | 151 | loadCache() |
106 | 152 | } |
107 | - if (!cache[id]) { | |
108 | - cache[id] = Value({}) | |
153 | + if (!cache.has(id)) { | |
154 | + cache.put(id, Value({})) | |
109 | 155 | } |
110 | - return cache[id] | |
156 | + return cache.get(id) | |
111 | 157 | } |
112 | 158 | } |
113 | 159 | |
114 | 160 | function getIds (state, key, compare) { |
Built with git-ssb-web