git ssb

2+

ev / mvd



Tree: 288090456f797459713cd94143110c26816cf1a3

Files: 288090456f797459713cd94143110c26816cf1a3 / tools.js

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

Built with git-ssb-web