git ssb

2+

ev / mvd



Tree: d7d4e4991854c117c786361a707e0ae793c73562

Files: d7d4e4991854c117c786361a707e0ae793c73562 / tools.js

4918 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
14/*function votes (msg) {
15 var votes = h('div.votes')
16 if (msg.key) {
17 pull(
18 sbot.links({dest: msg.key, rel: 'vote'}),
19 pull.drain(function (data) {
20 if (data) {
21 votes.appendChild(h('a', {href:'#' + data.key}, h('img.emoji', {src: config.emojiUrl + 'star.png'})))
22 } else {console.log(data)}
23 })
24 )
25 }
26 return votes
27}*/
28
29function votes (msg) {
30 var votes = h('div.votes')
31 if (msg.key) {
32 console.log('Looking for votes')
33 pull(
34 sbot.backlinks({query: [{$filter: {value: {content: {type: 'vote', vote: {link: msg.key}}}}}], live: true}),
35 pull.drain(function (data) {
36 if (data.sync) { console.log('waiting for new votes')}
37 else if (data.value.content.vote.value == 1) {
38 votes.appendChild(h('a#vote:' + data.value.author.substring(0, 44), {href:'#' + data.value.author}, h('img.emoji', {src: config.emojiUrl + 'star.png'})))
39 }
40 else if (data.value.content.vote.value == -1) {
41 var lookFor = 'vote:' + data.value.author.substring(0, 44)
42 var remove = document.getElementById(lookFor)
43 remove.parentNode.removeChild(remove)
44 }
45 })
46 )
47 }
48 return votes
49}
50
51module.exports.star = function (msg) {
52 var votebutton = h('span#star:' + msg.key.substring(0, 44))
53 var vote = {
54 type: 'vote',
55 vote: { link: msg.key, expression: 'Star' }
56 }
57
58 var star = h('button.btn.right',
59 h('img.emoji', {src: config.emojiUrl + 'star.png'}), {
60 onclick: function () {
61 console.log(vote)
62 vote.vote.value = 1
63 sbot.publish(vote, function (err, voted) {
64 if(err) throw err
65 console.log('Starred!', voted)
66 votebutton.replaceChild(unstar, star)
67 })
68 }
69 }
70 )
71
72 var unstar = h('button.btn.right',
73 h('img.emoji', {src: config.emojiUrl + 'stars.png'}), {
74 onclick: function () {
75 vote.vote.value = -1
76 sbot.publish(vote, function (err, voted) {
77 if(err) throw err
78 console.log('Unstarred!', voted)
79 votebutton.replaceChild(star, unstar)
80 })
81 }
82 }
83 )
84
85 pull(
86 sbot.backlinks({query: [{$filter: {value: {content: {type: 'vote', vote: {link: msg.key}}}}}]}),
87 pull.drain(function (data) {
88 if (data.sync) { }
89 else if (data.value.content.vote.value == 1) {
90 votebutton.replaceChild(unstar, star)
91 }
92 else if (data.value.content.vote.value == -1) {
93 votebutton.replaceChild(star, unstar)
94 }
95 })
96 )
97 /*pull(
98 sbot.links({dest: msg.key, rel: 'vote', author: id}),
99 pull.drain(function (data) {
100 if (data)
101 console.log(data)
102 if (data.value.vote.value == 1)
103 votebutton.replaceChild(unstar, star)
104 else if (data.value.vote.value == -1)
105 votebutton.replaceChild(star, unstar)
106 })
107 )*/
108
109 votebutton.appendChild(star)
110 return votebutton
111}
112
113module.exports.timestamp = function (msg, edited) {
114 var timestamp
115 if (edited)
116 timestamp = h('span.timestamp', 'Edited: ', h('a', {href: '#' + msg.key}, human(new Date(msg.value.timestamp))))
117 else
118 timestamp = h('span.timestamp', h('a', {href: '#' + msg.key}, human(new Date(msg.value.timestamp))))
119 return timestamp
120}
121
122
123module.exports.mini = function (msg, content) {
124 return h('div.mini',
125 h('span.avatar',
126 h('a', {href: '#' + msg.value.author},
127 h('span.avatar--small', avatar.image(msg.value.author)),
128 avatar.name(msg.value.author)
129 )
130 ),
131 exports.timestamp(msg),
132 content
133 )
134}
135
136
137module.exports.header = function (msg) {
138 return h('div.header',
139 h('span.avatar',
140 h('a', {href: '#' + msg.value.author},
141 h('span.avatar--small', avatar.image(msg.value.author)),
142 avatar.name(msg.value.author)
143 )
144 ),
145 exports.timestamp(msg),
146 votes(msg)
147 )
148}
149
150module.exports.messageLink = function (msglink) {
151 var link = h('span', h('a', {href: '#' + msglink}, msglink.substring(0, 8) + '...'))
152 return link
153}
154
155module.exports.rawJSON = function (obj) {
156 return JSON.stringify(obj, null, 2)
157 .split(/([%@&][a-zA-Z0-9\/\+]{43}=*\.[\w]+)/)
158 .map(function (e) {
159 if(ref.isMsg(e) || ref.isFeed(e) || ref.isBlob(e)) {
160 return h('a', {href: '#' + e}, e)
161 }
162 return e
163 })
164}
165
166var markdown = require('ssb-markdown')
167var config = require('./config')()
168
169module.exports.markdown = function (msg, md) {
170 return {innerHTML: markdown.block(msg, {toUrl: function (url, image) {
171 if(url[0] == '%' || url[0] == '@') return '#' + url
172 if(!image) return url
173 if(url[0] !== '&') return url
174 return config.blobsUrl + url
175 }})}
176}
177

Built with git-ssb-web