Files: f2075a6678ad096ea4b796c5d54979f7d08ab2cf / render.js
6945 bytesRaw
1 | var h = require('hyperscript') |
2 | var pull = require('pull-stream') |
3 | var human = require('human-time') |
4 | |
5 | var sbot = require('./scuttlebot') |
6 | var composer = require('./compose') |
7 | var tools = require('./tools') |
8 | |
9 | var config = require('./config')() |
10 | var id = require('./keys').id |
11 | var avatar = require('./avatar') |
12 | |
13 | function hash () { |
14 | return window.location.hash.substring(1) |
15 | } |
16 | |
17 | module.exports = function (msg) { |
18 | var message = h('div.message#' + msg.key.substring(0, 44)) |
19 | |
20 | if (!localStorage[msg.value.author]) |
21 | var cache = {mute: false} |
22 | else |
23 | var cache = JSON.parse(localStorage[msg.value.author]) |
24 | |
25 | if (cache.mute == true) { |
26 | var muted = h('span', ' muted') |
27 | message.appendChild(tools.mini(msg, muted)) |
28 | return message |
29 | } |
30 | else if (msg.value.content.type == 'scat_message') { |
31 | var src = hash() |
32 | if (src != 'backchannel') { |
33 | message.appendChild(h('button.btn.right', h('a', {href: '#backchannel'}, 'Chat'))) |
34 | } |
35 | message.appendChild(tools.mini(msg, ' ' + msg.value.content.text)) |
36 | return message |
37 | } |
38 | else if (msg.value.content.type == 'contact') { |
39 | var contact = h('a', {href: '#' + msg.value.content.contact}, avatar.name(msg.value.content.contact)) |
40 | if (msg.value.content.following == true) { |
41 | var following = h('span', ' follows ', contact) |
42 | message.appendChild(tools.mini(msg, following)) |
43 | } |
44 | if (msg.value.content.following == false) { |
45 | var unfollowing = h('span', ' unfollows ', contact) |
46 | message.appendChild(tools.mini(msg, unfollowing)) |
47 | } |
48 | if (msg.value.content.blocking == true) { |
49 | var blocking = h('span', ' blocks ', contact) |
50 | message.appendChild(tools.mini(msg, blocking)) |
51 | } |
52 | if (msg.value.content.blocking == false) { |
53 | var unblocking = h('span', ' unblocks ', contact) |
54 | message.appendChild(tools.mini(msg, unblocking)) |
55 | } |
56 | return message |
57 | } |
58 | |
59 | else if (msg.value.content.type == 'git-update') { |
60 | |
61 | message.appendChild(tools.header(msg)) |
62 | |
63 | var reponame = h('p', 'pushed to ', h('a', {href: '#' + msg.value.content.repo}, msg.value.content.repo)) |
64 | |
65 | var cloneurl = h('pre', 'git clone ssb://' + msg.value.content.repo) |
66 | |
67 | message.appendChild(reponame) |
68 | |
69 | pull( |
70 | sbot.get(msg.value.content.repo, function (err, data) { |
71 | if (err) throw err |
72 | if (data.content.name) { |
73 | actualname = h('p', 'pushed to ', h('a', {href: '#' + msg.value.content.repo}, '%' + data.content.name)) |
74 | reponame.parentNode.replaceChild(actualname, reponame) |
75 | } |
76 | }) |
77 | ) |
78 | message.appendChild(cloneurl) |
79 | //message.appendChild(h('pre', tools.rawJSON(msg.value))) |
80 | return message |
81 | |
82 | } |
83 | else if (msg.value.content.type == 'git-repo') { |
84 | message.appendChild(tools.header(msg)) |
85 | if (msg.value.content.name) { |
86 | message.appendChild(h('p', h('a', {href: msg.link}, '%' + msg.value.content.name))) |
87 | } else { |
88 | message.appendChild(h('p', h('a', {href: msg.link}, msg.link))) |
89 | } |
90 | var cloneurl = h('pre', 'git clone ssb://' + msg.key) |
91 | message.appendChild(cloneurl) |
92 | //message.appendChild(h('pre', tools.rawJSON(msg.value.content))) |
93 | return message |
94 | } |
95 | |
96 | else if (msg.value.content.type == 'post') { |
97 | var opts = { |
98 | type: 'post', |
99 | branch: msg.key |
100 | } |
101 | var fallback = {} |
102 | |
103 | |
104 | if (msg.value.content.root) |
105 | opts.root = msg.value.content.root |
106 | else |
107 | opts.root = msg.key |
108 | |
109 | message.appendChild(tools.header(msg)) |
110 | |
111 | if (msg.value.content.root) |
112 | message.appendChild(h('span', 're: ', tools.messageLink(msg.value.content.root))) |
113 | |
114 | message.appendChild(h('div.message__body', tools.markdown(msg.value.content.text))) |
115 | |
116 | pull( |
117 | sbot.query({query: [{$filter: {value: {content: {type: 'edit', original: msg.key}}}}], limit: 100, live: true}), |
118 | pull.drain(function (update) { |
119 | if (update.sync) { |
120 | } else { |
121 | var newMessage = h('div', tools.markdown(update.value.content.text)) |
122 | var latest = h('div.message__body', |
123 | tools.timestamp(update, {edited: true}), |
124 | newMessage |
125 | ) |
126 | message.replaceChild(latest, message.childNodes[message.childNodes.length - 2]) |
127 | fallback.messageText = update.value.content.text |
128 | opts.updated = update.key |
129 | opts.original = msg.key |
130 | } |
131 | }) |
132 | ) |
133 | |
134 | var name = avatar.name(msg.value.author) |
135 | |
136 | var buttons = h('div.buttons') |
137 | buttons.appendChild(h('button.btn', 'Reply', { |
138 | onclick: function () { |
139 | opts.type = 'post' |
140 | opts.mentions = '[' + name.textContent + '](' + msg.value.author + ')' |
141 | if (msg.value.content.recps) { |
142 | opts.recps = msg.value.content.recps |
143 | } |
144 | var r = message.childNodes.length - 1 |
145 | delete opts.updated |
146 | delete opts.original |
147 | delete fallback.messageText |
148 | fallback.buttons = message.childNodes[r] |
149 | var compose = h('div.message#re:' + msg.key.substring(0, 44), composer(opts, fallback)) |
150 | message.removeChild(message.childNodes[r]) |
151 | message.parentNode.insertBefore(compose, message.nextSibling) |
152 | } |
153 | })) |
154 | |
155 | if (msg.value.author == id) |
156 | buttons.appendChild(h('button.btn', 'Edit', { |
157 | onclick: function () { |
158 | opts.type = 'edit' |
159 | if (!fallback.messageText) |
160 | fallback.messageText = msg.value.content.text |
161 | |
162 | if (!opts.updated) |
163 | opts.updated = msg.key |
164 | opts.original = msg.key |
165 | |
166 | var r = message.childNodes.length - 1 |
167 | fallback.buttons = message.childNodes[r] |
168 | message.removeChild(message.childNodes[r]) |
169 | var compose = h('div#edit:' + msg.key.substring(0, 44), composer(opts, fallback)) |
170 | message.replaceChild(compose, message.lastElementChild) |
171 | } |
172 | })) |
173 | |
174 | buttons.appendChild(tools.star(msg)) |
175 | message.appendChild(buttons) |
176 | return message |
177 | |
178 | } else if (msg.value.content.type == 'vote') { |
179 | if (msg.value.content.vote.value == 1) |
180 | var link = h('span', ' ', h('img.emoji', {src: config.emojiUrl + 'star.png'}), ' ', h('a', {href: '#' + msg.value.content.vote.link}, tools.messageLink(msg.value.content.vote.link))) |
181 | else if (msg.value.content.vote.value == -1) |
182 | var link = h('span', ' ', h('img.emoji', {src: config.emojiUrl + 'stars.png'}), ' ', h('a', {href: '#' + msg.value.content.vote.link}, tools.messageLink(msg.value.content.vote.link))) |
183 | message.appendChild(tools.mini(msg, link)) |
184 | return message |
185 | } else if (typeof msg.value.content === 'string') { |
186 | var privateMsg = h('span', ' sent a private message.') |
187 | message.appendChild(tools.mini(msg, privateMsg)) |
188 | return message |
189 | } else { |
190 | |
191 | //FULL FALLBACK |
192 | message.appendChild(tools.header(msg)) |
193 | message.appendChild(h('pre', tools.rawJSON(msg.value.content))) |
194 | |
195 | //MINI FALLBACK |
196 | //var fallback = h('span', ' ' + msg.value.content.type) |
197 | //message.appendChild(tools.mini(msg, fallback)) |
198 | return message |
199 | } |
200 | } |
201 |
Built with git-ssb-web