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