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