git ssb

0+

Matt McKegg / ssb-contacts



Commit 992ac7e2f51ff50f73c4e2f1ad98aff46c5e114f

initial commit

Matt McKegg committed on 6/12/2017, 1:07:00 PM
Parent: 01e9407b5a97bd81ea7e5fa48b7a3c77292a8df9

Files changed

.gitignoreadded
README.mdadded
index.jsadded
.gitignoreView
@@ -1,0 +1,1 @@
1 +node_modules
README.mdView
@@ -1,0 +1,9 @@
1 +# ssb-contacts
2 +
3 +[scuttlebot](http://scuttlebutt.nz/) plugin for getting reduced "contacts" state.
4 +
5 +Who follows who? Find out fast!
6 +
7 +## License
8 +
9 +MIT
index.jsView
@@ -1,0 +1,55 @@
1 +var FlumeReduce = require('flumeview-reduce')
2 +var ref = require('ssb-ref')
3 +
4 +exports.name = 'contacts'
5 +exports.version = require('./package.json').version
6 +exports.manifest = {
7 + stream: 'source',
8 + get: 'async'
9 +}
10 +
11 +exports.init = function (ssb, config) {
12 + return ssb._flumeUse('contacts', FlumeReduce(0, reduce, map))
13 +}
14 +
15 +function reduce (result, item) {
16 + if (!result) result = {}
17 + if (item) {
18 + for (var source in item) {
19 + var valuesForSource = result[source] = result[source] || {}
20 + for (var key in item[source]) {
21 + var valuesForKey = valuesForSource[key] = valuesForSource[key] || {}
22 + for (var dest in item[source][key]) {
23 + var value = item[source][key][dest]
24 + if (!valuesForKey[dest] || value[1] > valuesForKey[dest][1]) {
25 + valuesForKey[dest] = value
26 + }
27 + }
28 + }
29 + }
30 + }
31 + return result
32 +}
33 +
34 +function map (msg) {
35 + if (msg.value.content && msg.value.content.type === 'contact' && ref.isLink(msg.value.content.contact)) {
36 + var source = msg.value.author
37 + var dest = msg.value.content.contact
38 + var values = {}
39 +
40 + if ('following' in msg.value.content) {
41 + values[source] = {
42 + following: {
43 + [dest]: [msg.value.content.following, msg.value.timestamp]
44 + }
45 + }
46 + values[dest] = {
47 + followers: {
48 + [source]: [msg.value.content.following, msg.value.timestamp]
49 + }
50 + }
51 + }
52 +
53 + return values
54 + }
55 +}

Built with git-ssb-web