git ssb

0+

ev / 0qc



Tree: 423e77eca80692c36556b549a46bcb6c572c3dbb

Files: 423e77eca80692c36556b549a46bcb6c572c3dbb / tools.js

6055 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
11
12module.exports.timestamp = function (msg, edited) {
13 var timestamp = h('span.right', h('a', {href: '#' + msg.key}, human(new Date(msg.value.timestamp))))
14 return timestamp
15}
16
17
18module.exports.star = function (msg) {
19 var votebutton = h('span#votebutton:' + msg.key.substring(0, 44))
20
21 var vote = {
22 type: 'vote',
23 vote: {link: msg.key, expression: 'Star'}
24 }
25
26 var star = h('button.btn', 'Star ',
27 h('img.emoji', {src: config.emojiUrl + 'star.png'}), {
28 onclick: function () {
29 vote.vote.value = 1
30 if (vote.recps) {
31 vote = exports.box(vote)
32 }
33 sbot.publish(vote, function (err, voted) {
34 if(err) throw err
35 })
36 }
37 }
38 )
39
40 votebutton.appendChild(star)
41
42 return votebutton
43}
44
45module.exports.unstar = function (msg) {
46 var votebutton = h('span#votebutton:' + msg.key.substring(0, 44))
47
48 var vote = {
49 type: 'vote',
50 vote: {link: msg.key, expression: 'Star'}
51 }
52
53 var unstar = h('button.btn ', 'Unstar ',
54 h('img.emoji', {src: config.emojiUrl + 'stars.png'}), {
55 onclick: function () {
56 vote.vote.value = -1
57 sbot.publish(vote, function (err, voted) {
58 if(err) throw err
59 })
60 }
61 }
62 )
63
64 votebutton.appendChild(unstar)
65
66 return votebutton
67}
68
69module.exports.header = function (msg) {
70 var header = h('div.header')
71
72 header.appendChild(h('span.avatar',
73 h('a', {href: '#' + msg.value.author},
74 h('span.avatar--small', avatar.cachedImage(msg.value.author)),
75 avatar.cachedName(msg.value.author)
76 )
77 )
78 )
79
80 header.appendChild(exports.timestamp(msg))
81
82 var lock = h('span.right', h('img.emoji', {src: config.emojiUrl + 'lock.png'}))
83
84 if (msg.value.content.recps) {
85 header.appendChild(lock)
86 for (var i = 0; i < msg.value.content.recps.length; i++) {
87 console.log(msg.value.content.recps[i])
88 if (ref.isFeed(msg.value.content.recps[i].link)) {
89 header.appendChild(h('a', {href: '#' + msg.value.content.recps[i].link}, h('span.avatar--small.right', avatar.cachedImage(msg.value.content.recps[i].link))))
90 }
91 if (ref.isFeed(msg.value.content.recps[i])) {
92 header.appendChild(h('a', {href: '#' + msg.value.content.recps[i]}, h('span.avatar--small.right', avatar.cachedImage(msg.value.content.recps[i]))))
93 }
94 }
95 } else if (msg.value.content.mentions) {
96 for (var i = 0; i < msg.value.content.mentions.length; i++) {
97 console.log(msg.value.content.mentions[i])
98 if (ref.isFeed(msg.value.content.mentions[i].link)) {
99 header.appendChild(h('a', {href: '#' + msg.value.content.mentions[i].link}, h('span.avatar--small.right', avatar.cachedImage(msg.value.content.mentions[i].link))))
100 }
101 if (ref.isFeed(msg.value.content.mentions[i])) {
102 header.appendChild(h('a', {href: '#' + msg.value.content.mentions[i]}, h('span.avatar--small.right', avatar.cachedImage(msg.value.content.mentions[i]))))
103 }
104 }
105 }
106
107
108
109 return header
110}
111
112module.exports.mini = function (msg, content) {
113 var mini = h('div.mini')
114
115 mini.appendChild(
116 h('span.avatar',
117 h('a', {href: '#' + msg.value.author},
118 h('span.avatar--small', avatar.cachedImage(msg.value.author)),
119 avatar.cachedName(msg.value.author)
120 )
121 )
122 )
123
124 mini.appendChild(h('span', content))
125 mini.appendChild(exports.timestamp(msg))
126
127 var lock = h('span.right', h('img.emoji', {src: config.emojiUrl + 'lock.png'}))
128 if (msg.value.content.recps) {
129 mini.appendChild(lock)
130 }
131
132 if (typeof msg.value.content === 'string') {
133 mini.appendChild(lock)
134 }
135
136 return mini
137}
138
139
140
141module.exports.box = function (content) {
142 return ssbKeys.box(content, content.recps.map(function (e) {
143 return ref.isFeed(e) ? e : e.link
144 }))
145}
146
147module.exports.publish = function (content, cb) {
148 if(content.recps)
149 content = exports.box(content)
150 sbot.publish(content, function (err, msg) {
151 if(err) throw err
152 console.log('Published!', msg)
153 if(cb) cb(err, msg)
154 })
155}
156
157module.exports.messageName = function (id, cb) {
158 // gets the first few characters of a message, for message-link
159 function title (s) {
160 var m = /^\n*([^\n]{0,40})/.exec(s)
161 return m && (m[1].length == 40 ? m[1]+'...' : m[1])
162 }
163
164 sbot.get(id, function (err, value) {
165 if(err && err.name == 'NotFoundError')
166 return cb(null, id.substring(0, 10)+'...(missing)')
167 if(value.content.type === 'post' && 'string' === typeof value.content.text)
168 return cb(null, title(value.content.text))
169 else if('string' === typeof value.content.text)
170 return cb(null, value.content.type + ':'+title(value.content.text))
171 else
172 return cb(null, id.substring(0, 10)+'...')
173 })
174}
175
176var messageName = exports.messageName
177
178module.exports.messageLink = function (id) {
179 if (ref.isMsg(id)) {
180 var link = h('a', {href: '#'+id}, id.substring(0, 10)+'...')
181 messageName(id, function (err, name) {
182 if(err) console.error(err)
183 else link.textContent = name
184 })
185 } else {
186 var link = id
187 }
188 return link
189}
190
191module.exports.rawJSON = function (obj) {
192 return JSON.stringify(obj, null, 2)
193 .split(/([%@&][a-zA-Z0-9\/\+]{43}=*\.[\w]+)/)
194 .map(function (e) {
195 if(ref.isMsg(e) || ref.isFeed(e) || ref.isBlob(e)) {
196 return h('a', {href: '#' + e}, e)
197 }
198 return e
199 })
200}
201
202module.exports.markdown = function (msg, md) {
203 return {innerHTML: markdown.block(msg, {toUrl: function (url, image) {
204 console.log(url)
205 if(url[0] == '%' || url[0] == '@' || url[0] == '#') return '#' + url
206 if(url[0] !== '&') return url
207 if(ref.isBlobLink(url)) return config.blobsUrl + url
208 //if(url[0] == '&') return config.blobsUrl + url
209 //if(!image) return url
210 //return config.blobsUrl + url
211 }})}
212}
213

Built with git-ssb-web