git ssb

7+

dinoworm ๐Ÿ› / patchcore



Tree: 7d58542a9f0765a6a828a016717dc9924232228c

Files: 7d58542a9f0765a6a828a016717dc9924232228c / message / async / name.js

1942 bytesRaw
1const nest = require('depnest')
2const ref = require('ssb-ref')
3const {resolve, onceTrue} = require('mutant')
4
5exports.needs = nest({
6 'sbot.async.get': 'first',
7 'sbot.pull.links': 'first',
8 'message.sync.unbox': 'first',
9 'about.obs.socialValue': 'first',
10 'keys.sync.id': 'first'
11})
12exports.gives = nest('message.async.name')
13
14// needs an async version
15
16exports.create = function (api) {
17 return nest('message.async.name', function (id, cb) {
18 if (!ref.isLink(id)) throw new Error('an id must be specified')
19 var fallbackName = id.substring(0, 10) + '...'
20 api.sbot.async.get(id, function (err, value) {
21 if (value && typeof value.content === 'string') {
22 value = api.message.sync.unbox(value)
23 }
24
25 if (err && err.name === 'NotFoundError') {
26 return cb(null, fallbackName + '...(missing)')
27 } else if (value && value.content.type === 'post' && typeof value.content.text === 'string') {
28 if (value.content.text.trim()) {
29 return cb(null, titleFromMarkdown(value.content.text, 40) || fallbackName)
30 }
31 } else if (value && typeof value.content.text === 'string') {
32 return cb(null, value.content.type + ': ' + titleFromMarkdown(value.content.text, 30))
33 } else {
34 return getAboutName(id, cb)
35 }
36
37 return cb(null, fallbackName)
38 })
39 })
40
41 function getAboutName (id, cb) {
42 var name = api.about.obs.socialValue(id, 'name')
43 var title = api.about.obs.socialValue(id, 'title')
44
45 onceTrue(name.sync, () => {
46 cb(null, resolve(name) || resolve(title) || id.substring(0, 10) + '...')
47 })
48 }
49}
50
51function titleFromMarkdown (text, max) {
52 text = text.trim().split('\n', 3).join('\n')
53 text = text.replace(/_|`|\*|#|^\[@.*?]|\[|]|\(\S*?\)/g, '').trim()
54 text = text.replace(/:$/, '')
55 text = text.trim().split('\n', 1)[0].trim()
56 if (text.length > max) {
57 text = text.substring(0, max - 2) + '...'
58 }
59 return text
60}
61

Built with git-ssb-web