Files: 3451510316992d414ec76ba5b29681fe359b7428 / lib / depject / message / async / name.js
1774 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 | 'message.sync.unbox': 'first', |
9 | 'about.obs.socialValue': '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 | const fallbackName = id.substring(0, 10) + '...' |
19 | api.sbot.async.get(id, function (err, value) { |
20 | if (value && typeof value.content === 'string') { |
21 | value = api.message.sync.unbox(value) |
22 | } |
23 | |
24 | if (err && err.name === 'NotFoundError') { |
25 | return cb(null, fallbackName + '...(missing)') |
26 | } else if (value && typeof value.content.title === 'string') { |
27 | return cb(null, truncate(value.content.title, 40)) |
28 | } else if (value && value.content.type === 'post' && typeof value.content.text === 'string') { |
29 | if (value.content.text.trim()) { |
30 | return cb(null, titleFromMarkdown(value.content.text, 40, 3) || fallbackName) |
31 | } |
32 | } else if (value && typeof value.content.text === 'string') { |
33 | return cb(null, value.content.type + ': ' + titleFromMarkdown(value.content.text, 30, 3)) |
34 | } else { |
35 | return getAboutName(id, cb) |
36 | } |
37 | |
38 | return cb(null, fallbackName) |
39 | }) |
40 | }) |
41 | |
42 | function getAboutName (id, cb) { |
43 | const name = api.about.obs.socialValue(id, 'name') |
44 | const title = api.about.obs.socialValue(id, 'title') |
45 | |
46 | onceTrue(name.sync, () => { |
47 | cb(null, resolve(name) || resolve(title) || id.substring(0, 10) + '...') |
48 | }) |
49 | } |
50 | } |
51 |
Built with git-ssb-web