git ssb

7+

dinoworm ๐Ÿ› / patchcore



Tree: 375a86a96a5e996629eeda68996cbb4b8293ece0

Files: 375a86a96a5e996629eeda68996cbb4b8293ece0 / message / async / name.js

1981 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.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 (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 console.log(id, resolve(name), resolve(title))
47 cb(null, resolve(name) || resolve(title) || id.substring(0, 10) + '...')
48 })
49 }
50}
51
52function titleFromMarkdown (text, max) {
53 text = text.trim().split('\n', 3).join('\n')
54 text = text.replace(/_|`|\*|\#|^\[@.*?\]|\[|\]|\(\S*?\)/g, '').trim()
55 text = text.replace(/\:$/, '')
56 text = text.trim().split('\n', 1)[0].trim()
57 if (text.length > max) {
58 text = text.substring(0, max - 2) + '...'
59 }
60 return text
61}
62

Built with git-ssb-web