Files: c89580e473baf5b62c31f30a2bb713f7a7982bc6 / modules / message-name.js
1436 bytesRaw
1 | var plugs = require('patchbay/plugs') |
2 | var sbot_links = plugs.first(exports.sbot_links = []) |
3 | var get_id = plugs.first(exports.get_id = []) |
4 | var sbot_get = plugs.first(exports.sbot_get = []) |
5 | var getAvatar = require('ssb-avatar') |
6 | |
7 | exports.message_name = function (id, cb) { |
8 | sbot_get(id, function (err, value) { |
9 | if (err && err.name === 'NotFoundError') { |
10 | return cb(null, id.substring(0, 10) + '...(missing)') |
11 | } else if (value.content.type === 'post' && typeof value.content.text === 'string') { |
12 | if (value.content.text.trim()) { |
13 | return cb(null, titleFromMarkdown(value.content.text, 40)) |
14 | } |
15 | } else if (value.content.type === 'git-repo') { |
16 | return getRepoName(id, cb) |
17 | } else if (typeof value.content.text === 'string') { |
18 | return cb(null, value.content.type + ': ' + titleFromMarkdown(value.content.text, 30)) |
19 | } |
20 | |
21 | return cb(null, id.substring(0, 10) + '...') |
22 | }) |
23 | } |
24 | |
25 | function titleFromMarkdown (text, max) { |
26 | text = text.trim().split('\n', 2).join('\n') |
27 | text = text.replace(/_|`|\*|\#|\[.*?\]|\(\S*?\)/g, '').trim() |
28 | text = text.replace(/\:$/, '') |
29 | text = text.trim().split('\n', 1)[0].trim() |
30 | if (text.length > max) { |
31 | text = text.substring(0, max - 2) + '...' |
32 | } |
33 | return text |
34 | } |
35 | |
36 | function getRepoName (id, cb) { |
37 | getAvatar({ |
38 | links: sbot_links, |
39 | get: sbot_get |
40 | }, get_id(), id, function (err, avatar) { |
41 | if (err) return cb(err) |
42 | cb(null, avatar.name) |
43 | }) |
44 | } |
45 |
Built with git-ssb-web