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