Files: b6d7ee34ab7b57861dd88fc633ba98bfb721b099 / lib / depject / message / async / name.js
1831 bytesRaw
1 | const nest = require('depnest') |
2 | const ref = require('ssb-ref') |
3 | const { titleFromMarkdown, truncate } = require('../../../markdownSummary') |
4 | const { resolve, onceTrue } = require('mutant') |
5 | |
6 | exports.needs = nest({ |
7 | 'sbot.async.get': 'first', |
8 | 'sbot.pull.links': 'first', |
9 | 'message.sync.unbox': 'first', |
10 | 'about.obs.socialValue': 'first', |
11 | 'keys.sync.id': 'first' |
12 | }) |
13 | exports.gives = nest('message.async.name') |
14 | |
15 | // needs an async version |
16 | |
17 | exports.create = function (api) { |
18 | return nest('message.async.name', function (id, cb) { |
19 | if (!ref.isLink(id)) throw new Error('an id must be specified') |
20 | const fallbackName = id.substring(0, 10) + '...' |
21 | api.sbot.async.get(id, function (err, value) { |
22 | if (value && typeof value.content === 'string') { |
23 | value = api.message.sync.unbox(value) |
24 | } |
25 | |
26 | if (err && err.name === 'NotFoundError') { |
27 | return cb(null, fallbackName + '...(missing)') |
28 | } else if (value && typeof value.content.title === 'string') { |
29 | return cb(null, truncate(value.content.title, 40)) |
30 | } else if (value && value.content.type === 'post' && typeof value.content.text === 'string') { |
31 | if (value.content.text.trim()) { |
32 | return cb(null, titleFromMarkdown(value.content.text, 40, 3) || fallbackName) |
33 | } |
34 | } else if (value && typeof value.content.text === 'string') { |
35 | return cb(null, value.content.type + ': ' + titleFromMarkdown(value.content.text, 30, 3)) |
36 | } else { |
37 | return getAboutName(id, cb) |
38 | } |
39 | |
40 | return cb(null, fallbackName) |
41 | }) |
42 | }) |
43 | |
44 | function getAboutName (id, cb) { |
45 | const name = api.about.obs.socialValue(id, 'name') |
46 | const title = api.about.obs.socialValue(id, 'title') |
47 | |
48 | onceTrue(name.sync, () => { |
49 | cb(null, resolve(name) || resolve(title) || id.substring(0, 10) + '...') |
50 | }) |
51 | } |
52 | } |
53 |
Built with git-ssb-web