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