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