git ssb

2+

ev / mvd



Tree: 82667629e00c7d73e813e77adc04302f468562dc

Files: 82667629e00c7d73e813e77adc04302f468562dc / tools.js

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

Built with git-ssb-web