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