git ssb

0+

ev / 0qc



Tree: 231a81f9e0adf80fa3566be93221cf1f5158fcb6

Files: 231a81f9e0adf80fa3566be93221cf1f5158fcb6 / tools.js

3529 bytesRaw
1var h = require('hyperscript')
2var ref = require('ssb-ref')
3var ssbKeys = require('ssb-keys')
4var sbot = require('./scuttlebot')
5var config = require('./config')()
6var markdown = require('ssb-markdown')
7var id = require('./keys').id
8var avatar = require('./avatar')
9var human = require('human-time')
10
11module.exports.timestamp = function (msg, edited) {
12 var timestamp = h('span.right', h('a', {href: '#' + msg.key}, human(new Date(msg.value.timestamp))))
13 return timestamp
14}
15
16
17module.exports.header = function (msg) {
18 var header = h('div.header')
19
20 header.appendChild(h('span.avatar',
21 h('a', {href: '#' + msg.value.author},
22 h('span.avatar--small', avatar.cachedImage(msg.value.author)),
23 avatar.cachedName(msg.value.author)
24 )
25 )
26 )
27
28 header.appendChild(exports.timestamp(msg))
29
30 return header
31}
32
33module.exports.mini = function (msg, content) {
34 var mini = h('div.mini')
35
36 mini.appendChild(
37 h('span.avatar',
38 h('a', {href: '#' + msg.value.author},
39 h('span.avatar--small', avatar.cachedImage(msg.value.author)),
40 avatar.cachedName(msg.value.author)
41 )
42 )
43 )
44 var lock = h('span.right', h('img.emoji', {src: config.emojiUrl + 'lock.png'}))
45
46
47 mini.appendChild(h('span', content))
48 mini.appendChild(exports.timestamp(msg))
49
50 if (msg.value.content.recps) {
51 mini.appendChild(lock)
52 }
53
54 if (typeof msg.value.content === 'string') {
55 mini.appendChild(lock)
56 }
57
58 return mini
59}
60
61
62
63module.exports.box = function (content) {
64 return ssbKeys.box(content, content.recps.map(function (e) {
65 return ref.isFeed(e) ? e : e.link
66 }))
67}
68
69module.exports.publish = function (content, cb) {
70 if(content.recps)
71 content = exports.box(content)
72 sbot.publish(content, function (err, msg) {
73 if(err) throw err
74 console.log('Published!', msg)
75 if(cb) cb(err, msg)
76 })
77}
78
79module.exports.messageName = function (id, cb) {
80 // gets the first few characters of a message, for message-link
81 function title (s) {
82 var m = /^\n*([^\n]{0,40})/.exec(s)
83 return m && (m[1].length == 40 ? m[1]+'...' : m[1])
84 }
85
86 sbot.get(id, function (err, value) {
87 if(err && err.name == 'NotFoundError')
88 return cb(null, id.substring(0, 10)+'...(missing)')
89 if(value.content.type === 'post' && 'string' === typeof value.content.text)
90 return cb(null, title(value.content.text))
91 else if('string' === typeof value.content.text)
92 return cb(null, value.content.type + ':'+title(value.content.text))
93 else
94 return cb(null, id.substring(0, 10)+'...')
95 })
96}
97
98var messageName = exports.messageName
99
100module.exports.messageLink = function (id) {
101 if (ref.isMsg(id)) {
102 var link = h('a', {href: '#'+id}, id.substring(0, 10)+'...')
103 messageName(id, function (err, name) {
104 if(err) console.error(err)
105 else link.textContent = name
106 })
107 } else {
108 var link = id
109 }
110 return link
111}
112
113module.exports.rawJSON = function (obj) {
114 return JSON.stringify(obj, null, 2)
115 .split(/([%@&][a-zA-Z0-9\/\+]{43}=*\.[\w]+)/)
116 .map(function (e) {
117 if(ref.isMsg(e) || ref.isFeed(e) || ref.isBlob(e)) {
118 return h('a', {href: '#' + e}, e)
119 }
120 return e
121 })
122}
123
124module.exports.markdown = function (msg, md) {
125 return {innerHTML: markdown.block(msg, {toUrl: function (url, image) {
126 if(url[0] == '%' || url[0] == '@' || url[0] == '#') return '#' + url
127 if(url[0] !== '&') return url
128 //if(url[0] == '&') return config.blobsUrl + url
129 //if(!image) return url
130 return config.blobsUrl + url
131 }})}
132}
133

Built with git-ssb-web