git ssb

0+

mixmix / ssb-meme



Commit 85187f4ebcda55555e5b7b2093c9129315c64e0b

initial working plugin

mix irving committed on 5/27/2018, 5:48:28 AM

Files changed

.gitignoreadded
index.jsadded
package.jsonadded
test/config.jsadded
test/index.jsadded
.gitignoreView
@@ -1,0 +1,1 @@
1 +node_modules
index.jsView
@@ -1,0 +1,68 @@
1 +const FlumeView = require('flumeview-search')
2 +const pull = require('pull-stream')
3 +const { isBlobId } = require('ssb-ref')
4 +const Validator = require('is-my-json-valid')
5 +
6 +const hasBlobMention = Validator(require('../schema/mentions'))
7 +
8 +const INDEX_VERSION = 2
9 +
10 +module.exports = {
11 + name: 'meme',
12 + version: require('./package.json').version,
13 + manifest: {
14 + query: 'source',
15 + search: 'async'
16 + },
17 + init: (sbot) => {
18 + const view = sbot._flumeUse('meme', FlumeView(INDEX_VERSION, 3, map))
19 +
20 + return {
21 + query: view.query,
22 + search: (opts, cb) => {
23 + if (typeof opts === 'string') opts = { query: opts }
24 +
25 + pull(
26 + view.query(opts),
27 + pull.map(m => m.value.content.mentions),
28 + pull.collect((err, data) => {
29 + if (err) return cb(err)
30 +
31 + const result = data.reduce((soFar, mentions) => {
32 + mentions
33 + .filter(m => isBlobId(m.link))
34 + .filter(m => m.name.indexOf(opts.query) > -1)
35 + .forEach(({ link, name }) => {
36 + if (!soFar[link]) soFar[link] = [name]
37 + else soFar[link].push(name)
38 + })
39 +
40 + return soFar
41 + }, {})
42 +
43 + cb(null, result)
44 + })
45 + )
46 + }
47 + }
48 + }
49 +}
50 +
51 +const imgExtRegEx = /\.(jpg|jpeg|png|gif|bmp|svg)$/i
52 +const spaceCharRegex = /(-|\.|_|\/|~|\s)/g
53 +
54 +function map (msg) {
55 + var mentions = msg.value.content.mentions || []
56 + if (!Array.isArray(mentions)) mentions = [mentions]
57 +
58 + // if (!hasBlobMention(mentions)) return
59 +
60 + return mentions
61 + // .filter(m => typeof m === 'object' && m.type)
62 + // .filter(m => m.type.indexOf('image') === 0) // some mentions don't have a file type!
63 + .filter(m => isBlobId(m.link))
64 + .map(m => m.name)
65 + .filter(Boolean)
66 + .map(m => m.replace(imgExtRegEx, '').replace(spaceCharRegex, ' '))
67 + .join(' ')
68 +}
package.jsonView
@@ -1,0 +1,28 @@
1 +{
2 + "name": "ssb-meme",
3 + "version": "1.0.0",
4 + "description": "",
5 + "main": "index.js",
6 + "directories": {
7 + "test": "test"
8 + },
9 + "dependencies": {
10 + "file-type": "^8.0.0",
11 + "flumeview-search": "^1.0.3",
12 + "is-my-json-valid": "^2.17.2",
13 + "ssb-ref": "^2.11.1"
14 + },
15 + "devDependencies": {
16 + "pull-stream": "^3.6.8",
17 + "scuttlebot": "^11.3.0",
18 + "ssb-blobs": "^1.1.5",
19 + "ssb-config": "^2.2.0",
20 + "ssb-keys": "^7.0.16"
21 + },
22 + "scripts": {
23 + "test": "echo \"Error: no test specified\" && exit 1"
24 + },
25 + "keywords": [],
26 + "author": "",
27 + "license": "ISC"
28 +}
test/config.jsView
@@ -1,0 +1,15 @@
1 +const Config = require('ssb-config/inject')
2 +// const Config = require('ssb-config')
3 +const ssbKeys = require('ssb-keys')
4 +const Path = require('path')
5 +
6 +const appName = 'ssb' // <<< NOTE THIS IS YOUR DEFAULT IDENTITY
7 +const opts = null // can set things in here
8 +
9 +const config = Config(appName, opts)
10 +Object.assign(config, {
11 + appName,
12 + keys: ssbKeys.loadOrCreateSync(Path.join(config.path, 'secret'))
13 +})
14 +
15 +module.exports = config
test/index.jsView
@@ -1,0 +1,60 @@
1 +const pull = require('pull-stream')
2 +const Server = require('scuttlebot')
3 +const { isBlobId } = require('ssb-ref')
4 +const fileType = require('file-type')
5 +
6 +const config = require('./config')
7 +// console.log('config:', config)
8 +
9 +console.log('*** installing ssb-server plugins ***')
10 +Server
11 + .use(require('scuttlebot/plugins/master'))
12 + .use(require('ssb-blobs'))
13 + .use(require('../index.js'))
14 +
15 +console.log('*** starting ssb-server ***')
16 +const server = Server(config)
17 +
18 +const opts = {
19 + query: 'herm',
20 + // limit: 10
21 +}
22 +
23 +pull(
24 + server.meme.query(opts),
25 + pull.map(m => m.value.content.mentions),
26 + pull.collect((err, data) => {
27 + var result = data.reduce((soFar, mentions) => {
28 + mentions
29 + .filter(m => isBlobId(m.link))
30 + .filter(m => m.name.indexOf(opts.query) > -1)
31 + .forEach(({ link, name }) => {
32 + if (!soFar[link]) soFar[link] = [name]
33 + else soFar[link].push(name)
34 + })
35 +
36 + return soFar
37 + }, {})
38 +
39 + console.log(result)
40 +
41 + pull(
42 + server.blobs.get('&pwDBdb1KWoLVtaYjIA8p1PUXYcgflIWhsig6ESIIz80=.sha256'),
43 + pull.drain((buff) => {
44 + console.log('buff', buff)
45 + console.log(fileType(buff))
46 + })
47 + )
48 +
49 + server.close()
50 + })
51 + // pull.drain(
52 + // (m) => console.log('!', m),
53 + // () => {
54 + // console.log('done')
55 + // server.close()
56 + // }
57 + // )
58 +)
59 +
60 +

Built with git-ssb-web