git ssb

0+

mixmix / ssb-meme



Commit 8ea0672560e531ebb37c8697fcd37dad9a96986e

good enough for v1

mix irving committed on 5/27/2018, 11:40:37 AM
Parent: 85187f4ebcda55555e5b7b2093c9129315c64e0b

Files changed

index.jschanged
schema/mention.jsadded
index.jsView
@@ -1,12 +1,11 @@
11 const FlumeView = require('flumeview-search')
22 const pull = require('pull-stream')
3-const { isBlobId } = require('ssb-ref')
43 const Validator = require('is-my-json-valid')
54
6-const hasBlobMention = Validator(require('../schema/mentions'))
5 +const isBlobMention = Validator(require('./schema/mention'))
76
8-const INDEX_VERSION = 2
7 +const INDEX_VERSION = 1
98
109 module.exports = {
1110 name: 'meme',
1211 version: require('./package.json').version,
@@ -18,51 +17,55 @@
1817 const view = sbot._flumeUse('meme', FlumeView(INDEX_VERSION, 3, map))
1918
2019 return {
2120 query: view.query,
22- search: (opts, cb) => {
23- if (typeof opts === 'string') opts = { query: opts }
21 + search
22 + }
2423
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)
24 + function search (opts, cb) {
25 + if (typeof opts === 'string') opts = { query: opts }
3026
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- })
27 + pull(
28 + view.query(opts),
29 + pull.collect((err, data) => {
30 + if (err) return cb(err)
3931
40- return soFar
41- }, {})
32 + const result = data.reduce((soFar, msg) => {
33 + getMentions(msg)
34 + .filter(isBlobMention)
35 + .filter(m => m.name.indexOf(opts.query) > -1) // only mentions relevant to the query
36 + .forEach(({ link, name }) => {
37 + if (!soFar[link]) soFar[link] = []
4238
43- cb(null, result)
44- })
45- )
46- }
39 + soFar[link].push({ name, author: getAuthor(msg), msg: msg.key })
40 + })
41 +
42 + return soFar
43 + }, {})
44 +
45 + cb(null, result)
46 + })
47 + )
4748 }
4849 }
4950 }
5051
5152 const imgExtRegEx = /\.(jpg|jpeg|png|gif|bmp|svg)$/i
52-const spaceCharRegex = /(-|\.|_|\/|~|\s)/g
53 +const spaceCharRegex = /(-|\.|_|\/|~)/g
5354
5455 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, ' '))
56 + return getMentions(msg)
57 + .filter(isBlobMention)
58 + // .map(m => m.name)
59 + .map(n => n.replace(imgExtRegEx, '').replace(spaceCharRegex, ' '))
6760 .join(' ')
6861 }
62 +
63 +function getMentions (msg) {
64 + if (!msg.value.content.mentions) return []
65 + else if (!Array.isArray(msg.value.content.mentions)) return [msg.value.content.mentions]
66 + else return msg.value.content.mentions
67 +}
68 +
69 +function getAuthor (msg) {
70 + return msg.value.author
71 +}
schema/mention.jsView
@@ -1,0 +1,25 @@
1 +const { blobIdRegex } = require('ssb-ref')
2 +
3 +const mentionSchema = {
4 + $schema: 'http://json-schema.org/schema#',
5 + type: 'object',
6 + required: ['link', 'name'],
7 + properties: {
8 + link: { type: 'string', pattern: blobIdRegex },
9 + name: { type: 'string', minLength: 3 }
10 + }
11 +}
12 +
13 +module.exports = mentionSchema
14 +
15 +// const mentionsSchema = {
16 +// type: 'array',
17 +// items: {
18 +// anyOf: [
19 +// { $ref: '#/definitions/mention' }
20 +// ]
21 +// },
22 +// definitions: {
23 +// mention: mentionSchema
24 +// }
25 +// }

Built with git-ssb-web