git ssb

2+

ev / mvd



Tree: 51c13efa0a9e4c1ad0ec9785e6337671148325a0

Files: 51c13efa0a9e4c1ad0ec9785e6337671148325a0 / views.js

6013 bytesRaw
1var pull = require('pull-stream')
2var sbot = require('./scuttlebot')
3var hyperscroll = require('hyperscroll')
4var More = require('pull-more')
5var stream = require('hyperloadmore/stream')
6var h = require('hyperscript')
7var render = require('./render')
8var ref = require('ssb-ref')
9
10var config = require('./config')()
11
12var id = require('./keys').id
13
14var fs = require('fs')
15
16var compose = require('./compose')
17
18var about = function () {
19 var screen = document.getElementById('screen')
20
21 var about = require('./about')
22
23 var content = h('div.content', about)
24
25 screen.appendChild(hyperscroll(content))
26}
27
28var edit = function() {
29 var content = h('div.content')
30
31 var screen = document.getElementById('screen')
32
33 screen.appendChild(hyperscroll(content))
34
35 var nameInput = h('input', {placeholder: 'New name'})
36
37 var locInput = h('input', {placeholder: 'New location'})
38
39 var descInput = h('textarea', {placeholder: 'New description'})
40
41 var editor = h('div.message',
42 h('h1', 'Edit profile'),
43 nameInput,
44 h('button.btn.btn-primary', 'Preview', {onclick: function () {
45 if(nameInput.value) {
46 api.message_confirm({
47 type: 'about',
48 about: id,
49 name: nameInput.value || undefined
50 })
51 }
52 }}),
53 h('hr'),
54 locInput,
55 h('button.btn.btn-primary', 'Preview', {onclick: function () {
56 if(locInput.value) {
57 api.message_confirm({
58 type: 'loc',
59 about: id,
60 loc: locInput.value || undefined
61 })
62 }
63 }}),
64 h('hr'),
65 descInput,
66 h('button.btn.btn-primary', 'Preview', {onclick: function (){
67 if(descInput.value) {
68 api.message_confirm({
69 type: 'description',
70 about: id,
71 description: descInput.value || undefined
72 })
73 }
74 }}),
75 h('hr')
76 )
77
78 content.appendChild(editor)
79}
80
81var mentionsStream = function () {
82 var content = h('div.content')
83
84 var screen = document.getElementById('screen')
85
86 screen.appendChild(hyperscroll(content))
87
88 function createStream (opts) {
89 return pull(
90 sbot.backlinks({query: [{$filter: {dest: id}}], reverse: true}),
91 pull.map(function (msg) {
92 console.log(msg)
93 if (msg.value.private == true)
94 return 'ignoring private message'
95 else
96 return render(msg)
97 })
98 )
99 }
100
101 pull(
102 createStream({reverse: true, limit: 10}),
103 stream.bottom(content)
104 )
105}
106
107var logStream = function () {
108 var content = h('div.content')
109 var screen = document.getElementById('screen')
110 screen.appendChild(hyperscroll(content))
111
112 function createStream (opts) {
113 return pull(
114 More(sbot.createLogStream, opts),
115 pull.map(function (msg) {
116 return render(msg)
117 })
118
119 )
120 }
121
122 pull(
123 createStream({old: false, limit: 10}),
124 stream.top(content)
125 )
126
127 pull(
128 createStream({reverse: true, live: false, limit: 10}),
129 stream.bottom(content)
130 )
131}
132
133var userStream = function (src) {
134 var content = h('div.content')
135 var screen = document.getElementById('screen')
136 screen.appendChild(hyperscroll(content))
137 function createStream (opts) {
138 return pull(
139 More(sbot.userStream, opts, ['value', 'sequence']),
140 pull.map(function (msg) {
141 return render(msg)
142 })
143 )
144 }
145
146 pull(
147 createStream({old: false, limit: 10, id: src}),
148 stream.top(content)
149 )
150
151 pull(
152 createStream({reverse: true, live: false, limit: 10, id: src}),
153 stream.bottom(content)
154 )
155
156}
157
158var msgThread = function(src) {
159 var content = h('div.content')
160 var screen = document.getElementById('screen')
161 screen.appendChild(hyperscroll(content))
162 sbot.get(src, function (err, data) {
163 if (err) {console.log('could not find message') }
164 data.value = data
165 console.log(data)
166 var root = src
167 if (data.value.content.root)
168 root = data.value.content.root
169 sbot.get(root, function (err, data) {
170 if (err) { console.log('could not find root')}
171 data.value = data
172 data.key = root
173 content.appendChild(render(data))
174 pull(
175 sbot.links({rel: 'root', dest: root, values: true, keys: true, live: true}),
176 pull.drain(function (msg) {
177 console.log(msg)
178 if (msg.value)
179 content.appendChild(render(msg))
180 })
181 )
182 })
183 })
184}
185
186var keyPage = function () {
187 var screen = document.getElementById('screen')
188
189 var importKey = h('textarea.import', {placeholder: 'Import a new public/private key', name: 'textarea', style: 'width: 97%; height: 100px;'})
190
191 var content = h('div.content',
192 h('div.message#key',
193 h('h1', 'Your Key'),
194 h('p', {innerHTML: 'Your public/private key is: <pre><code>' + localStorage[config.caps.shs + '/secret'] + '</code></pre>'},
195 h('button.btn', {onclick: function (e){
196 localStorage[config.caps.shs +'/secret'] = ''
197 alert('Your public/private key has been deleted')
198 e.preventDefault()
199 location.hash = ""
200 location.reload()
201 }}, 'Delete Key')
202 ),
203 h('hr'),
204 h('form',
205 importKey,
206 h('button.btn', {onclick: function (e){
207 if(importKey.value) {
208 localStorage[config.caps.shs + '/secret'] = importKey.value.replace(/\s+/g, ' ')
209 e.preventDefault()
210 alert('Your public/private key has been updated')
211 }
212 location.hash = ""
213 location.reload()
214 }}, 'Import key'),
215 )
216 )
217 )
218
219 screen.appendChild(hyperscroll(content))
220}
221
222
223function hash () {
224 return window.location.hash.substring(1)
225}
226
227module.exports = function () {
228 var src = hash()
229
230 if (ref.isFeed(src)) {
231 userStream(src)
232 } else if (ref.isMsg(src)) {
233 msgThread(src)
234 } else if (src == 'mentions') {
235 console.log('mentions')
236 mentionsStream()
237 } else if (src == 'about') {
238 about()
239 } else if (src == 'edit') {
240 edit()
241 } else if (src == 'key') {
242 keyPage()
243 } else {
244 logStream()
245 }
246}
247

Built with git-ssb-web