git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: c89580e473baf5b62c31f30a2bb713f7a7982bc6

Files: c89580e473baf5b62c31f30a2bb713f7a7982bc6 / modules / message-name.js

1436 bytesRaw
1var plugs = require('patchbay/plugs')
2var sbot_links = plugs.first(exports.sbot_links = [])
3var get_id = plugs.first(exports.get_id = [])
4var sbot_get = plugs.first(exports.sbot_get = [])
5var getAvatar = require('ssb-avatar')
6
7exports.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
25function 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
36function 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