git ssb

7+

dinoworm 🐛 / patchcore



Commit ad18e9a2ed4609a915cfc7801e7d6f1fed76da84

add profile.obs followers and following

Matt McKegg committed on 2/15/2017, 2:32:45 PM
Parent: 901b36d8a35b01b7600c4573cf1e27c671ff6e6f

Files changed

index.jschanged
package.jsonchanged
profile/obs.jsadded
index.jsView
@@ -1,5 +1,5 @@
11 const bulk = require('bulk-require')
22 module.exports = bulk(__dirname, [
33 './+(config|keys|sbot|blob|emoji).js',
4- './+(lib|about|feed|message)/**/*.js'
4 + './+(lib|about|feed|message|profile)/**/*.js'
55 ])
package.jsonView
@@ -30,8 +30,9 @@
3030 "emoji-named-characters": "^1.0.2",
3131 "es2040": "^1.2.4",
3232 "human-time": "0.0.1",
3333 "mutant": "^3.14.2",
34 + "mutant-pull-reduce": "^1.0.0",
3435 "pull-abortable": "^4.1.0",
3536 "pull-cat": "^1.1.11",
3637 "pull-reconnect": "0.0.3",
3738 "pull-stream": "^3.5.0",
profile/obs.jsView
@@ -1,0 +1,67 @@
1 +var nest = require('depnest')
2 +var MutantPullReduce = require('mutant-pull-reduce')
3 +
4 +exports.needs = nest({
5 + 'sbot.pull.query': 'first'
6 +})
7 +
8 +exports.gives = nest({
9 + 'profile.obs': ['following', 'followers']
10 +})
11 +
12 +exports.create = function (api) {
13 + var followingCache = {}
14 + var followerCache = {}
15 +
16 + return nest({
17 + 'profile.obs': { following, followers }
18 + })
19 +
20 + function following (id) {
21 + if (!followingCache[id]) {
22 + followingCache[id] = reduce(api.sbot.pull.query({
23 + query: [
24 + makeQuery(id, { $prefix: '@' }),
25 + {'$map': ['value', 'content', 'contact']}
26 + ],
27 + live: true
28 + }))
29 + }
30 + return followingCache[id]
31 + }
32 +
33 + function followers (id) {
34 + if (!followerCache[id]) {
35 + followerCache[id] = reduce(api.sbot.pull.query({
36 + query: [
37 + makeQuery({ $prefix: '@' }, id),
38 + {'$map': ['value', 'author']}
39 + ],
40 + live: true
41 + }))
42 + }
43 + return followerCache[id]
44 + }
45 +}
46 +
47 +function reduce (stream) {
48 + return MutantPullReduce(stream, (result, item) => {
49 + result.add(item)
50 + return result
51 + }, {
52 + startValue: new Set()
53 + })
54 +}
55 +
56 +function makeQuery (a, b) {
57 + return {'$filter': {
58 + value: {
59 + author: a,
60 + content: {
61 + type: 'contact',
62 + contact: b,
63 + following: true
64 + }
65 + }
66 + }}
67 +}

Built with git-ssb-web