git ssb

2+

ev / mvd



Tree: d2f7cc14c8650f2b3b3c95897783d27e9ff627e0

Files: d2f7cc14c8650f2b3b3c95897783d27e9ff627e0 / render.js

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

Built with git-ssb-web