git ssb

0+

mixmix / ssb-server-plugin-intro



Tree: 8c71e9c979626faf9b3d2a3f9293a285ba56d033

Files: 8c71e9c979626faf9b3d2a3f9293a285ba56d033 / 2_actual_friends / ssb-server-actual-friends.js

1674 bytesRaw
1const flumeView = require('flumeview-reduce')
2const pull = require('pull-stream')
3const get = require('lodash/get')
4const mergeWith = require('lodash/mergeWith')
5
6module.exports = {
7 name: 'actualFriends',
8 version: '1.0.0',
9 manifest: {
10 get: 'async',
11 stream: 'source'
12 },
13 init: function (ssbServer, config) {
14 console.log('*** loading actual-friends ***')
15
16 const view = ssbServer._flumeUse('actualFriends', flumeView(
17 1.3, // version
18 reduceData,
19 mapToData,
20 null, //codec
21 initialState()
22 ))
23 console.log('init FlumeView', view)
24
25 return {
26 get: view.get,
27 stream: view.stream
28 }
29 }
30}
31
32function reduceData (acc, newData) {
33 // https://lodash.com/docs/4.17.4#mergeWith
34 process.stdout.write('<3')
35 return mergeWith(acc, newData, (accVal, newVal) => {
36 if (typeof accVal === 'number') {
37 return accVal + newVal
38 }
39 })
40}
41
42function mapToData (msg) {
43 // TODO - handle private message
44 // TODO - check mentions are valid user keys
45
46 const { author, content } = msg.value
47 var mentions = get(content, 'mentions', []) //map
48 if (!Array.isArray(mentions)) {
49 console.log('actualFriends - weird mentions:', mentions)
50 // e.g { '0': { link: '@hxGxqPrplLjRG2vtjQL87abX4QKqeLgCwQpS730nNwE=.ed25519', name: 'paul' } }
51 return {}
52 }
53 // catches old mentions types :
54
55 mentions = mentions
56 .map(mention => typeof mention === 'object'
57 ? mention.link
58 : mention
59 )
60 .filter(Boolean)
61 .reduce(
62 (acc, user) => Object.assign(acc, { [user]: 1 }),
63 {}
64 )
65
66 return {
67 [author]: {
68 mentions
69 }
70 }
71}
72
73function initialState () {
74 return {}
75}
76
77

Built with git-ssb-web